blob: 6b3504243c9413dc8c5752ca41165a1c5fbc746c [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]b0628ad2012-09-20 00:48:4765void BrowserInstantController::ShowInstant(int height, InstantSizeUnits units) {
[email protected]93cee482012-09-25 18:21:2066 browser_->window()->ShowInstant(instant_->GetPreviewContents(),
67 height, units);
[email protected]7acfaf92012-07-11 15:51:5968}
69
70void BrowserInstantController::HideInstant() {
[email protected]afeecab02012-09-12 22:54:1671 browser_->window()->HideInstant();
[email protected]7acfaf92012-07-11 15:51:5972}
73
[email protected]c72226c82012-10-01 21:02:3274void BrowserInstantController::CommitInstant(TabContents* preview,
75 bool in_new_tab) {
76 if (in_new_tab) {
77 // TabStripModel takes ownership of |preview|.
78 browser_->tab_strip_model()->AddTabContents(preview, -1,
79 instant_->last_transition_type(), TabStripModel::ADD_ACTIVE);
80 } else {
81 TabContents* active_tab = chrome::GetActiveTabContents(browser_);
82 int index = browser_->tab_strip_model()->GetIndexOfTabContents(active_tab);
83 DCHECK_NE(TabStripModel::kNoTab, index);
84 // TabStripModel takes ownership of |preview|.
85 browser_->tab_strip_model()->ReplaceTabContentsAt(index, preview);
86 // InstantUnloadHandler takes ownership of |active_tab|.
87 instant_unload_handler_.RunUnloadListenersOrDestroy(active_tab, index);
[email protected]7acfaf92012-07-11 15:51:5988
[email protected]c72226c82012-10-01 21:02:3289 GURL url = preview->web_contents()->GetURL();
90 DCHECK(browser_->profile()->GetExtensionService());
91 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) {
92 AppLauncherHandler::RecordAppLaunchType(
93 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT);
94 }
[email protected]7acfaf92012-07-11 15:51:5995 }
96}
97
98void BrowserInstantController::SetSuggestedText(
99 const string16& text,
100 InstantCompleteBehavior behavior) {
101 if (browser_->window()->GetLocationBar())
102 browser_->window()->GetLocationBar()->SetSuggestedText(text, behavior);
103}
104
105gfx::Rect BrowserInstantController::GetInstantBounds() {
106 return browser_->window()->GetInstantBounds();
107}
108
109void BrowserInstantController::InstantPreviewFocused() {
110 // NOTE: This is only invoked on aura.
111 browser_->window()->WebContentsFocused(
112 instant_->GetPreviewContents()->web_contents());
113}
114
[email protected]c55e3b82012-08-09 15:27:05115TabContents* BrowserInstantController::GetActiveTabContents() const {
[email protected]7acfaf92012-07-11 15:51:59116 return chrome::GetActiveTabContents(browser_);
117}
118
119////////////////////////////////////////////////////////////////////////////////
120// BrowserInstantController, content::NotificationObserver implementation:
121
122void BrowserInstantController::Observe(
123 int type,
124 const content::NotificationSource& source,
125 const content::NotificationDetails& details) {
[email protected]c55e3b82012-08-09 15:27:05126 DCHECK_EQ(chrome::NOTIFICATION_PREF_CHANGED, type);
127 DCHECK_EQ(std::string(prefs::kInstantEnabled),
128 *content::Details<std::string>(details).ptr());
129 ResetInstant();
[email protected]7acfaf92012-07-11 15:51:59130}
131
132////////////////////////////////////////////////////////////////////////////////
133// BrowserInstantController, TabStripModelObserver implementation:
134
135void BrowserInstantController::TabDeactivated(TabContents* contents) {
136 if (instant())
[email protected]c55e3b82012-08-09 15:27:05137 instant_->Hide();
[email protected]7acfaf92012-07-11 15:51:59138}
139
140////////////////////////////////////////////////////////////////////////////////
[email protected]0b10c9ff2012-10-09 17:31:55141// BrowserInstantController, search::SearchModelObserver implementation:
142
143void BrowserInstantController::ModeChanged(const search::Mode& old_mode,
144 const search::Mode& new_mode) {
145 if (instant() && old_mode.is_ntp() != new_mode.is_ntp())
146 instant_->OnActiveTabModeChanged(new_mode.is_ntp());
147}
148
149////////////////////////////////////////////////////////////////////////////////
[email protected]7acfaf92012-07-11 15:51:59150// BrowserInstantController, private:
151
[email protected]c55e3b82012-08-09 15:27:05152void BrowserInstantController::ResetInstant() {
[email protected]b67d0a42012-09-04 20:57:35153 instant_.reset(
154 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() &&
155 browser_->is_type_tabbed() ?
156 InstantController::CreateInstant(browser_->profile(), this) : NULL);
[email protected]7acfaf92012-07-11 15:51:59157}
158
159} // namespace chrome