blob: f8fd4f5cc36a119230041402b6cfec1fa90d45d4 [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]7acfaf92012-07-11 15:51:592// 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"
[email protected]fb8fdf12012-08-21 16:28:209#include "chrome/browser/prefs/pref_service.h"
[email protected]7acfaf92012-07-11 15:51:5910#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/ui/browser.h"
12#include "chrome/browser/ui/browser_tabstrip.h"
13#include "chrome/browser/ui/browser_window.h"
14#include "chrome/browser/ui/omnibox/location_bar.h"
[email protected]e41982a72012-11-20 07:16:5115#include "chrome/browser/ui/search/search.h"
[email protected]0b10c9ff2012-10-09 17:31:5516#include "chrome/browser/ui/search/search_model.h"
[email protected]9d3d11702012-11-08 01:01:1217#include "chrome/browser/ui/search/search_tab_helper.h"
[email protected]7acfaf92012-07-11 15:51:5918#include "chrome/browser/ui/tab_contents/tab_contents.h"
[email protected]e41982a72012-11-20 07:16:5119#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]7acfaf92012-07-11 15:51:5920#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
21#include "chrome/common/chrome_notification_types.h"
22#include "chrome/common/pref_names.h"
23#include "content/public/browser/notification_service.h"
[email protected]7acfaf92012-07-11 15:51:5924#include "content/public/browser/web_contents.h"
25
[email protected]7acfaf92012-07-11 15:51:5926namespace chrome {
27
28////////////////////////////////////////////////////////////////////////////////
29// BrowserInstantController, public:
30
31BrowserInstantController::BrowserInstantController(Browser* browser)
[email protected]8a236702012-09-28 13:30:5732 : browser_(browser),
[email protected]e41982a72012-11-20 07:16:5133 instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this),
34 chrome::search::IsInstantExtendedAPIEnabled(browser->profile())),
[email protected]c72226c82012-10-01 21:02:3235 instant_unload_handler_(browser) {
[email protected]7acfaf92012-07-11 15:51:5936 profile_pref_registrar_.Init(browser_->profile()->GetPrefs());
37 profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
[email protected]e41982a72012-11-20 07:16:5138 instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile()));
[email protected]0b10c9ff2012-10-09 17:31:5539 browser_->search_model()->AddObserver(this);
[email protected]7acfaf92012-07-11 15:51:5940}
41
42BrowserInstantController::~BrowserInstantController() {
[email protected]0b10c9ff2012-10-09 17:31:5543 browser_->search_model()->RemoveObserver(this);
[email protected]7acfaf92012-07-11 15:51:5944}
45
[email protected]e41982a72012-11-20 07:16:5146bool BrowserInstantController::IsInstantEnabled(Profile* profile) {
47 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() &&
48 profile->GetPrefs()->GetBoolean(prefs::kInstantEnabled);
49}
50
51void BrowserInstantController::RegisterUserPrefs(PrefService* prefs) {
52 prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false,
53 PrefService::SYNCABLE_PREF);
54 prefs->RegisterBooleanPref(prefs::kInstantEnabled, false,
55 PrefService::SYNCABLE_PREF);
56}
57
[email protected]7acfaf92012-07-11 15:51:5958bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) {
[email protected]e41982a72012-11-20 07:16:5159 // Unsupported dispositions.
60 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW)
[email protected]7acfaf92012-07-11 15:51:5961 return false;
[email protected]7acfaf92012-07-11 15:51:5962
[email protected]7acfaf92012-07-11 15:51:5963 // The omnibox currently doesn't use other dispositions, so we don't attempt
[email protected]c72226c82012-10-01 21:02:3264 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
[email protected]7acfaf92012-07-11 15:51:5965 // support for the new disposition.
[email protected]c72226c82012-10-01 21:02:3266 DCHECK(disposition == CURRENT_TAB ||
67 disposition == NEW_FOREGROUND_TAB) << disposition;
68
[email protected]e41982a72012-11-20 07:16:5169 return instant_.CommitIfCurrent(disposition == CURRENT_TAB ?
[email protected]c72226c82012-10-01 21:02:3270 INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER);
[email protected]7acfaf92012-07-11 15:51:5971}
72
[email protected]c72226c82012-10-01 21:02:3273void BrowserInstantController::CommitInstant(TabContents* preview,
74 bool in_new_tab) {
75 if (in_new_tab) {
76 // TabStripModel takes ownership of |preview|.
77 browser_->tab_strip_model()->AddTabContents(preview, -1,
[email protected]e41982a72012-11-20 07:16:5178 instant_.last_transition_type(), TabStripModel::ADD_ACTIVE);
[email protected]c72226c82012-10-01 21:02:3279 } else {
[email protected]59253a652012-11-20 00:17:2680 TabContents* active_tab =
81 browser_->tab_strip_model()->GetActiveTabContents();
[email protected]c72226c82012-10-01 21:02:3282 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
[email protected]93b73832012-10-18 20:18:3898void BrowserInstantController::SetInstantSuggestion(
99 const InstantSuggestion& suggestion) {
[email protected]7acfaf92012-07-11 15:51:59100 if (browser_->window()->GetLocationBar())
[email protected]93b73832012-10-18 20:18:38101 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion);
[email protected]7acfaf92012-07-11 15:51:59102}
103
104gfx::Rect BrowserInstantController::GetInstantBounds() {
105 return browser_->window()->GetInstantBounds();
106}
107
108void BrowserInstantController::InstantPreviewFocused() {
109 // NOTE: This is only invoked on aura.
110 browser_->window()->WebContentsFocused(
[email protected]e41982a72012-11-20 07:16:51111 instant_.GetPreviewContents()->web_contents());
[email protected]7acfaf92012-07-11 15:51:59112}
113
[email protected]c55e3b82012-08-09 15:27:05114TabContents* BrowserInstantController::GetActiveTabContents() const {
[email protected]59253a652012-11-20 00:17:26115 return browser_->tab_strip_model()->GetActiveTabContents();
[email protected]7acfaf92012-07-11 15:51:59116}
117
[email protected]e41982a72012-11-20 07:16:51118void BrowserInstantController::ActiveTabChanged() {
119 instant_.ActiveTabChanged();
120}
121
[email protected]7acfaf92012-07-11 15:51:59122////////////////////////////////////////////////////////////////////////////////
[email protected]a6a7ced2012-11-01 17:24:18123// BrowserInstantController, PrefObserver implementation:
[email protected]7acfaf92012-07-11 15:51:59124
[email protected]a6a7ced2012-11-01 17:24:18125void BrowserInstantController::OnPreferenceChanged(
126 PrefServiceBase* service,
127 const std::string& pref_name) {
[email protected]e41982a72012-11-20 07:16:51128 instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile()));
[email protected]749ce882012-11-16 04:18:01129}
130
[email protected]7acfaf92012-07-11 15:51:59131////////////////////////////////////////////////////////////////////////////////
[email protected]0b10c9ff2012-10-09 17:31:55132// BrowserInstantController, search::SearchModelObserver implementation:
133
134void BrowserInstantController::ModeChanged(const search::Mode& old_mode,
135 const search::Mode& new_mode) {
[email protected]e41982a72012-11-20 07:16:51136 instant_.SearchModeChanged(old_mode, new_mode);
[email protected]7acfaf92012-07-11 15:51:59137}
138
139} // namespace chrome