summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/browser_instant_controller.cc
diff options
context:
space:
mode:
authorsreeram@chromium.org <sreeram@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-20 07:16:51 +0000
committersreeram@chromium.org <sreeram@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-20 07:16:51 +0000
commite41982a7fd5dfdc59803abdde459b0065ad57f30 (patch)
tree46d3b0ba8a2400a342692a2547e6a67d778ad9a2 /chrome/browser/ui/browser_instant_controller.cc
parent9599e96765be3adda8573939d7143dce771e37a4 (diff)
downloadchromium_src-e41982a7fd5dfdc59803abdde459b0065ad57f30.zip
chromium_src-e41982a7fd5dfdc59803abdde459b0065ad57f30.tar.gz
chromium_src-e41982a7fd5dfdc59803abdde459b0065ad57f30.tar.bz2
Use search::Mode in InstantModel.
Major changes: + Use search::Mode in InstantModel, instead of a custom enum. + Better logic in Update(), SearchModeChanged(), ActiveTabChanged() and Show() in InstantController, allowing us to delete some unnecessary code. + Make InstantController be always present in BrowserInstantController, thus getting rid of ResetInstant() and related code and notifications. + Remove |mode_| from InstantController, opting for a pair of booleans instead, which will pave the way for suggestions-only Instant (in the near future). + Move prefs related code from InstantController to BrowserInstantController. + Correctly avoid deleting InstantLoader::preview_delegate_ prematurely. + Refactor OmniboxLostFocus(), with code mostly borrowed from @samarth's excellent work in http://codereview.chromium.org/10909109/. + Make Hide() private to InstantController. Yay! + Call ActiveTabChanged() from Browser, instead of from an independent observer (BrowserInstantController), to enforce ordering requirements. + Added DVLOGs for easier development. My choice of commandline: --vmodule=*instant*=1,*searchbox*=1 + Refresh loader only when the omnibox is unfocused and the preview is hidden. + Remove a cocoa unittest that didn't seem to be testing anything worthwhile. + In Views code, make the ownership of the preview WebView clearer. I've been unable to make it crash by closing the browser with the preview showing, so I guess it works. If I missed something, please let me know. Minor changes include inconsequential refactoring or renaming, making things more private than public, updating copyright header in every file that was touched, removing unnecessary includes, etc. BUG=159012,161367,161270 R=sky@chromium.org TEST=See bugs. Review URL: https://chromiumcodereview.appspot.com/11417056 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@168754 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui/browser_instant_controller.cc')
-rw-r--r--chrome/browser/ui/browser_instant_controller.cc99
1 files changed, 29 insertions, 70 deletions
diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc
index e58bcb2..f8fd4f5 100644
--- a/chrome/browser/ui/browser_instant_controller.cc
+++ b/chrome/browser/ui/browser_instant_controller.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -6,38 +6,23 @@
#include "chrome/browser/browser_shutdown.h"
#include "chrome/browser/extensions/extension_service.h"
-#include "chrome/browser/instant/instant_controller.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/omnibox/location_bar.h"
+#include "chrome/browser/ui/search/search.h"
#include "chrome/browser/ui/search/search_model.h"
#include "chrome/browser/ui/search/search_tab_helper.h"
-#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
-namespace {
-
-// Returns true iff the search model for |tab| is in an NTP state, that is, a
-// state in which the Instant overlay may be showing custom NTP content in
-// EXTENDED mode.
-bool IsSearchModelNTP(content::WebContents* tab) {
- if (!tab)
- return false;
- chrome::search::SearchModel* model =
- chrome::search::SearchTabHelper::FromWebContents(tab)->model();
- return model && model->mode().is_ntp();
-}
-
-} // namespace
-
namespace chrome {
////////////////////////////////////////////////////////////////////////////////
@@ -45,23 +30,34 @@ namespace chrome {
BrowserInstantController::BrowserInstantController(Browser* browser)
: browser_(browser),
+ instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this),
+ chrome::search::IsInstantExtendedAPIEnabled(browser->profile())),
instant_unload_handler_(browser) {
profile_pref_registrar_.Init(browser_->profile()->GetPrefs());
profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
- ResetInstant();
- browser_->tab_strip_model()->AddObserver(this);
+ instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile()));
browser_->search_model()->AddObserver(this);
}
BrowserInstantController::~BrowserInstantController() {
- browser_->tab_strip_model()->RemoveObserver(this);
browser_->search_model()->RemoveObserver(this);
}
+bool BrowserInstantController::IsInstantEnabled(Profile* profile) {
+ return profile && !profile->IsOffTheRecord() && profile->GetPrefs() &&
+ profile->GetPrefs()->GetBoolean(prefs::kInstantEnabled);
+}
+
+void BrowserInstantController::RegisterUserPrefs(PrefService* prefs) {
+ prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false,
+ PrefService::SYNCABLE_PREF);
+ prefs->RegisterBooleanPref(prefs::kInstantEnabled, false,
+ PrefService::SYNCABLE_PREF);
+}
+
bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) {
- // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt
- // to use the Instant preview.
- if (!instant() || !instant_->IsCurrent() || disposition == NEW_BACKGROUND_TAB)
+ // Unsupported dispositions.
+ if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW)
return false;
// The omnibox currently doesn't use other dispositions, so we don't attempt
@@ -70,9 +66,8 @@ bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) {
DCHECK(disposition == CURRENT_TAB ||
disposition == NEW_FOREGROUND_TAB) << disposition;
- instant_->CommitCurrentPreview(disposition == CURRENT_TAB ?
+ return instant_.CommitIfCurrent(disposition == CURRENT_TAB ?
INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER);
- return true;
}
void BrowserInstantController::CommitInstant(TabContents* preview,
@@ -80,7 +75,7 @@ void BrowserInstantController::CommitInstant(TabContents* preview,
if (in_new_tab) {
// TabStripModel takes ownership of |preview|.
browser_->tab_strip_model()->AddTabContents(preview, -1,
- instant_->last_transition_type(), TabStripModel::ADD_ACTIVE);
+ instant_.last_transition_type(), TabStripModel::ADD_ACTIVE);
} else {
TabContents* active_tab =
browser_->tab_strip_model()->GetActiveTabContents();
@@ -113,43 +108,24 @@ gfx::Rect BrowserInstantController::GetInstantBounds() {
void BrowserInstantController::InstantPreviewFocused() {
// NOTE: This is only invoked on aura.
browser_->window()->WebContentsFocused(
- instant_->GetPreviewContents()->web_contents());
+ instant_.GetPreviewContents()->web_contents());
}
TabContents* BrowserInstantController::GetActiveTabContents() const {
return browser_->tab_strip_model()->GetActiveTabContents();
}
+void BrowserInstantController::ActiveTabChanged() {
+ instant_.ActiveTabChanged();
+}
+
////////////////////////////////////////////////////////////////////////////////
// BrowserInstantController, PrefObserver implementation:
void BrowserInstantController::OnPreferenceChanged(
PrefServiceBase* service,
const std::string& pref_name) {
- DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name);
- ResetInstant();
-}
-
-////////////////////////////////////////////////////////////////////////////////
-// BrowserInstantController, TabStripModelObserver implementation:
-
-void BrowserInstantController::ActiveTabChanged(
- content::WebContents* old_contents,
- content::WebContents* new_contents,
- int index,
- bool user_gesture) {
- if (instant()) {
- const bool old_is_ntp = IsSearchModelNTP(old_contents);
- const bool new_is_ntp = IsSearchModelNTP(new_contents);
- // Do not hide Instant if switching from an NTP to another NTP since that
- // would cause custom NTP content to flicker.
- if (!(old_is_ntp && new_is_ntp))
- instant()->Hide();
- }
-}
-
-void BrowserInstantController::TabStripEmpty() {
- instant_.reset();
+ instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile()));
}
////////////////////////////////////////////////////////////////////////////////
@@ -157,24 +133,7 @@ void BrowserInstantController::TabStripEmpty() {
void BrowserInstantController::ModeChanged(const search::Mode& old_mode,
const search::Mode& new_mode) {
- if (instant())
- instant_->OnActiveTabModeChanged(new_mode);
-}
-
-////////////////////////////////////////////////////////////////////////////////
-// BrowserInstantController, private:
-
-void BrowserInstantController::ResetInstant() {
- instant_.reset(
- !browser_shutdown::ShuttingDownWithoutClosingBrowsers() &&
- browser_->is_type_tabbed() ?
- InstantController::CreateInstant(browser_->profile(), this) : NULL);
-
- // Notify any observers that they need to reset.
- content::NotificationService::current()->Notify(
- chrome::NOTIFICATION_BROWSER_INSTANT_RESET,
- content::Source<BrowserInstantController>(this),
- content::NotificationService::NoDetails());
+ instant_.SearchModeChanged(old_mode, new_mode);
}
} // namespace chrome