blob: 6f3acc2bcadf9a7faed44c8e71c08e14754837d6 [file] [log] [blame]
[email protected]7acfaf92012-07-11 15:51:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/browser_instant_controller.h"
6
7#include "chrome/browser/browser_shutdown.h"
8#include "chrome/browser/extensions/extension_service.h"
9#include "chrome/browser/instant/instant_controller.h"
[email protected]fb8fdf12012-08-21 16:28:2010#include "chrome/browser/prefs/pref_service.h"
[email protected]7acfaf92012-07-11 15:51:5911#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/browser_tabstrip.h"
14#include "chrome/browser/ui/browser_window.h"
15#include "chrome/browser/ui/omnibox/location_bar.h"
[email protected]0b10c9ff2012-10-09 17:31:5516#include "chrome/browser/ui/search/search_model.h"
[email protected]7acfaf92012-07-11 15:51:5917#include "chrome/browser/ui/tabs/tab_strip_model.h"
18#include "chrome/browser/ui/tab_contents/tab_contents.h"
19#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
20#include "chrome/common/chrome_notification_types.h"
21#include "chrome/common/pref_names.h"
22#include "content/public/browser/notification_service.h"
[email protected]7acfaf92012-07-11 15:51:5923#include "content/public/browser/web_contents.h"
24
[email protected]7acfaf92012-07-11 15:51:5925namespace chrome {
26
27////////////////////////////////////////////////////////////////////////////////
28// BrowserInstantController, public:
29
30BrowserInstantController::BrowserInstantController(Browser* browser)
[email protected]8a236702012-09-28 13:30:5731 : browser_(browser),
[email protected]c72226c82012-10-01 21:02:3232 instant_unload_handler_(browser) {
[email protected]7acfaf92012-07-11 15:51:5933 profile_pref_registrar_.Init(browser_->profile()->GetPrefs());
34 profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
[email protected]c55e3b82012-08-09 15:27:0535 ResetInstant();
[email protected]7acfaf92012-07-11 15:51:5936 browser_->tab_strip_model()->AddObserver(this);
[email protected]0b10c9ff2012-10-09 17:31:5537 browser_->search_model()->AddObserver(this);
[email protected]7acfaf92012-07-11 15:51:5938}
39
40BrowserInstantController::~BrowserInstantController() {
41 browser_->tab_strip_model()->RemoveObserver(this);
[email protected]0b10c9ff2012-10-09 17:31:5542 browser_->search_model()->RemoveObserver(this);
[email protected]7acfaf92012-07-11 15:51:5943}
44
45bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) {
[email protected]c55e3b82012-08-09 15:27:0546 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt
47 // to use the Instant preview.
48 if (!instant() || !instant_->IsCurrent() || disposition == NEW_BACKGROUND_TAB)
[email protected]7acfaf92012-07-11 15:51:5949 return false;
[email protected]7acfaf92012-07-11 15:51:5950
[email protected]7acfaf92012-07-11 15:51:5951 // The omnibox currently doesn't use other dispositions, so we don't attempt
[email protected]c72226c82012-10-01 21:02:3252 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
[email protected]7acfaf92012-07-11 15:51:5953 // support for the new disposition.
[email protected]c72226c82012-10-01 21:02:3254 DCHECK(disposition == CURRENT_TAB ||
55 disposition == NEW_FOREGROUND_TAB) << disposition;
56
57 instant_->CommitCurrentPreview(disposition == CURRENT_TAB ?
58 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER);
59 return true;
[email protected]7acfaf92012-07-11 15:51:5960}
61
62////////////////////////////////////////////////////////////////////////////////
63// BrowserInstantController, InstantControllerDelegate implementation:
64
[email protected]c72226c82012-10-01 21:02:3265void BrowserInstantController::CommitInstant(TabContents* preview,
66 bool in_new_tab) {
67 if (in_new_tab) {
68 // TabStripModel takes ownership of |preview|.
69 browser_->tab_strip_model()->AddTabContents(preview, -1,
70 instant_->last_transition_type(), TabStripModel::ADD_ACTIVE);
71 } else {
72 TabContents* active_tab = chrome::GetActiveTabContents(browser_);
73 int index = browser_->tab_strip_model()->GetIndexOfTabContents(active_tab);
74 DCHECK_NE(TabStripModel::kNoTab, index);
75 // TabStripModel takes ownership of |preview|.
76 browser_->tab_strip_model()->ReplaceTabContentsAt(index, preview);
77 // InstantUnloadHandler takes ownership of |active_tab|.
78 instant_unload_handler_.RunUnloadListenersOrDestroy(active_tab, index);
[email protected]7acfaf92012-07-11 15:51:5979
[email protected]c72226c82012-10-01 21:02:3280 GURL url = preview->web_contents()->GetURL();
81 DCHECK(browser_->profile()->GetExtensionService());
82 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) {
83 AppLauncherHandler::RecordAppLaunchType(
84 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT);
85 }
[email protected]7acfaf92012-07-11 15:51:5986 }
87}
88
[email protected]93b73832012-10-18 20:18:3889void BrowserInstantController::SetInstantSuggestion(
90 const InstantSuggestion& suggestion) {
[email protected]7acfaf92012-07-11 15:51:5991 if (browser_->window()->GetLocationBar())
[email protected]93b73832012-10-18 20:18:3892 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion);
[email protected]7acfaf92012-07-11 15:51:5993}
94
95gfx::Rect BrowserInstantController::GetInstantBounds() {
96 return browser_->window()->GetInstantBounds();
97}
98
99void BrowserInstantController::InstantPreviewFocused() {
100 // NOTE: This is only invoked on aura.
101 browser_->window()->WebContentsFocused(
102 instant_->GetPreviewContents()->web_contents());
103}
104
[email protected]c55e3b82012-08-09 15:27:05105TabContents* BrowserInstantController::GetActiveTabContents() const {
[email protected]7acfaf92012-07-11 15:51:59106 return chrome::GetActiveTabContents(browser_);
107}
108
109////////////////////////////////////////////////////////////////////////////////
110// BrowserInstantController, content::NotificationObserver implementation:
111
112void BrowserInstantController::Observe(
113 int type,
114 const content::NotificationSource& source,
115 const content::NotificationDetails& details) {
[email protected]c55e3b82012-08-09 15:27:05116 DCHECK_EQ(chrome::NOTIFICATION_PREF_CHANGED, type);
117 DCHECK_EQ(std::string(prefs::kInstantEnabled),
118 *content::Details<std::string>(details).ptr());
119 ResetInstant();
[email protected]7acfaf92012-07-11 15:51:59120}
121
122////////////////////////////////////////////////////////////////////////////////
123// BrowserInstantController, TabStripModelObserver implementation:
124
125void BrowserInstantController::TabDeactivated(TabContents* contents) {
126 if (instant())
[email protected]c55e3b82012-08-09 15:27:05127 instant_->Hide();
[email protected]7acfaf92012-07-11 15:51:59128}
129
130////////////////////////////////////////////////////////////////////////////////
[email protected]0b10c9ff2012-10-09 17:31:55131// BrowserInstantController, search::SearchModelObserver implementation:
132
133void BrowserInstantController::ModeChanged(const search::Mode& old_mode,
134 const search::Mode& new_mode) {
135 if (instant() && old_mode.is_ntp() != new_mode.is_ntp())
136 instant_->OnActiveTabModeChanged(new_mode.is_ntp());
137}
138
139////////////////////////////////////////////////////////////////////////////////
[email protected]7acfaf92012-07-11 15:51:59140// BrowserInstantController, private:
141
[email protected]c55e3b82012-08-09 15:27:05142void BrowserInstantController::ResetInstant() {
[email protected]b67d0a42012-09-04 20:57:35143 instant_.reset(
144 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() &&
145 browser_->is_type_tabbed() ?
146 InstantController::CreateInstant(browser_->profile(), this) : NULL);
[email protected]e4e15dc2012-10-18 03:22:50147
148 // Notify any observers that they need to reset.
149 content::NotificationService::current()->Notify(
150 chrome::NOTIFICATION_BROWSER_INSTANT_RESET,
151 content::Source<BrowserInstantController>(this),
152 content::NotificationService::NoDetails());
[email protected]7acfaf92012-07-11 15:51:59153}
154
155} // namespace chrome