blob: d4fd88b269bf3dba213ac21b013459e907568c52 [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"
16#include "chrome/browser/ui/tabs/tab_strip_model.h"
17#include "chrome/browser/ui/tab_contents/tab_contents.h"
18#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
19#include "chrome/common/chrome_notification_types.h"
20#include "chrome/common/pref_names.h"
21#include "content/public/browser/notification_service.h"
[email protected]7acfaf92012-07-11 15:51:5922#include "content/public/browser/web_contents.h"
23
[email protected]7acfaf92012-07-11 15:51:5924namespace chrome {
25
26////////////////////////////////////////////////////////////////////////////////
27// BrowserInstantController, public:
28
29BrowserInstantController::BrowserInstantController(Browser* browser)
[email protected]8a236702012-09-28 13:30:5730 : browser_(browser),
[email protected]c72226c82012-10-01 21:02:3231 instant_unload_handler_(browser) {
[email protected]7acfaf92012-07-11 15:51:5932 profile_pref_registrar_.Init(browser_->profile()->GetPrefs());
33 profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
[email protected]c55e3b82012-08-09 15:27:0534 ResetInstant();
[email protected]7acfaf92012-07-11 15:51:5935 browser_->tab_strip_model()->AddObserver(this);
36}
37
38BrowserInstantController::~BrowserInstantController() {
39 browser_->tab_strip_model()->RemoveObserver(this);
40}
41
42bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) {
[email protected]c55e3b82012-08-09 15:27:0543 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt
44 // to use the Instant preview.
45 if (!instant() || !instant_->IsCurrent() || disposition == NEW_BACKGROUND_TAB)
[email protected]7acfaf92012-07-11 15:51:5946 return false;
[email protected]7acfaf92012-07-11 15:51:5947
[email protected]7acfaf92012-07-11 15:51:5948 // The omnibox currently doesn't use other dispositions, so we don't attempt
[email protected]c72226c82012-10-01 21:02:3249 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
[email protected]7acfaf92012-07-11 15:51:5950 // support for the new disposition.
[email protected]c72226c82012-10-01 21:02:3251 DCHECK(disposition == CURRENT_TAB ||
52 disposition == NEW_FOREGROUND_TAB) << disposition;
53
54 instant_->CommitCurrentPreview(disposition == CURRENT_TAB ?
55 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER);
56 return true;
[email protected]7acfaf92012-07-11 15:51:5957}
58
59////////////////////////////////////////////////////////////////////////////////
60// BrowserInstantController, InstantControllerDelegate implementation:
61
[email protected]b0628ad2012-09-20 00:48:4762void BrowserInstantController::ShowInstant(int height, InstantSizeUnits units) {
[email protected]93cee482012-09-25 18:21:2063 browser_->window()->ShowInstant(instant_->GetPreviewContents(),
64 height, units);
[email protected]7acfaf92012-07-11 15:51:5965}
66
67void BrowserInstantController::HideInstant() {
[email protected]afeecab02012-09-12 22:54:1668 browser_->window()->HideInstant();
[email protected]7acfaf92012-07-11 15:51:5969}
70
[email protected]c72226c82012-10-01 21:02:3271void BrowserInstantController::CommitInstant(TabContents* preview,
72 bool in_new_tab) {
73 if (in_new_tab) {
74 // TabStripModel takes ownership of |preview|.
75 browser_->tab_strip_model()->AddTabContents(preview, -1,
76 instant_->last_transition_type(), TabStripModel::ADD_ACTIVE);
77 } else {
78 TabContents* active_tab = chrome::GetActiveTabContents(browser_);
79 int index = browser_->tab_strip_model()->GetIndexOfTabContents(active_tab);
80 DCHECK_NE(TabStripModel::kNoTab, index);
81 // TabStripModel takes ownership of |preview|.
82 browser_->tab_strip_model()->ReplaceTabContentsAt(index, preview);
83 // InstantUnloadHandler takes ownership of |active_tab|.
84 instant_unload_handler_.RunUnloadListenersOrDestroy(active_tab, index);
[email protected]7acfaf92012-07-11 15:51:5985
[email protected]c72226c82012-10-01 21:02:3286 GURL url = preview->web_contents()->GetURL();
87 DCHECK(browser_->profile()->GetExtensionService());
88 if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) {
89 AppLauncherHandler::RecordAppLaunchType(
90 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT);
91 }
[email protected]7acfaf92012-07-11 15:51:5992 }
93}
94
95void BrowserInstantController::SetSuggestedText(
96 const string16& text,
97 InstantCompleteBehavior behavior) {
98 if (browser_->window()->GetLocationBar())
99 browser_->window()->GetLocationBar()->SetSuggestedText(text, behavior);
100}
101
102gfx::Rect BrowserInstantController::GetInstantBounds() {
103 return browser_->window()->GetInstantBounds();
104}
105
106void BrowserInstantController::InstantPreviewFocused() {
107 // NOTE: This is only invoked on aura.
108 browser_->window()->WebContentsFocused(
109 instant_->GetPreviewContents()->web_contents());
110}
111
[email protected]c55e3b82012-08-09 15:27:05112TabContents* BrowserInstantController::GetActiveTabContents() const {
[email protected]7acfaf92012-07-11 15:51:59113 return chrome::GetActiveTabContents(browser_);
114}
115
116////////////////////////////////////////////////////////////////////////////////
117// BrowserInstantController, content::NotificationObserver implementation:
118
119void BrowserInstantController::Observe(
120 int type,
121 const content::NotificationSource& source,
122 const content::NotificationDetails& details) {
[email protected]c55e3b82012-08-09 15:27:05123 DCHECK_EQ(chrome::NOTIFICATION_PREF_CHANGED, type);
124 DCHECK_EQ(std::string(prefs::kInstantEnabled),
125 *content::Details<std::string>(details).ptr());
126 ResetInstant();
[email protected]7acfaf92012-07-11 15:51:59127}
128
129////////////////////////////////////////////////////////////////////////////////
130// BrowserInstantController, TabStripModelObserver implementation:
131
132void BrowserInstantController::TabDeactivated(TabContents* contents) {
133 if (instant())
[email protected]c55e3b82012-08-09 15:27:05134 instant_->Hide();
[email protected]7acfaf92012-07-11 15:51:59135}
136
137////////////////////////////////////////////////////////////////////////////////
138// BrowserInstantController, private:
139
[email protected]c55e3b82012-08-09 15:27:05140void BrowserInstantController::ResetInstant() {
[email protected]b67d0a42012-09-04 20:57:35141 instant_.reset(
142 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() &&
143 browser_->is_type_tabbed() ?
144 InstantController::CreateInstant(browser_->profile(), this) : NULL);
[email protected]7acfaf92012-07-11 15:51:59145}
146
147} // namespace chrome