diff options
author | sreeram@chromium.org <sreeram@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-20 07:16:51 +0000 |
---|---|---|
committer | sreeram@chromium.org <sreeram@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-20 07:16:51 +0000 |
commit | e41982a7fd5dfdc59803abdde459b0065ad57f30 (patch) | |
tree | 46d3b0ba8a2400a342692a2547e6a67d778ad9a2 /chrome/browser/ui | |
parent | 9599e96765be3adda8573939d7143dce771e37a4 (diff) | |
download | chromium_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')
26 files changed, 237 insertions, 437 deletions
diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index ffd3bdf..78b4d3b8 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.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. @@ -414,7 +414,8 @@ Browser::Browser(const CreateParams& params) encoding_auto_detect_.Init(prefs::kWebKitUsesUniversalDetector, profile_->GetPrefs(), NULL); - instant_controller_.reset(new chrome::BrowserInstantController(this)); + if (is_type_tabbed()) + instant_controller_.reset(new chrome::BrowserInstantController(this)); #if 0 // Disabled for M22. See http://crbug.com/144326. @@ -1150,6 +1151,10 @@ void Browser::ActiveTabChanged(WebContents* old_contents, } UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH); + + // This needs to be called after UpdateSearchState(). + if (instant_controller_) + instant_controller_->ActiveTabChanged(); } void Browser::TabMoved(WebContents* contents, @@ -1211,6 +1216,10 @@ void Browser::TabStripEmpty() { // still present. MessageLoop::current()->PostTask( FROM_HERE, base::Bind(&Browser::CloseFrame, weak_factory_.GetWeakPtr())); + + // Instant may have visible WebContents that need to be detached before the + // window system closes. + instant_controller_.reset(); } bool Browser::PreHandleKeyboardEvent(content::WebContents* source, diff --git a/chrome/browser/ui/browser_commands.cc b/chrome/browser/ui/browser_commands.cc index a718b33..e0acc52 100644 --- a/chrome/browser/ui/browser_commands.cc +++ b/chrome/browser/ui/browser_commands.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. @@ -426,7 +426,8 @@ void OpenCurrentURL(Browser* browser) { WindowOpenDisposition open_disposition = location_bar->GetWindowOpenDisposition(); - if (browser->instant_controller()->OpenInstant(open_disposition)) + if (browser->instant_controller() && + browser->instant_controller()->OpenInstant(open_disposition)) return; GURL url(location_bar->GetInputString()); 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 diff --git a/chrome/browser/ui/browser_instant_controller.h b/chrome/browser/ui/browser_instant_controller.h index 90fe981..f00d8e31 100644 --- a/chrome/browser/ui/browser_instant_controller.h +++ b/chrome/browser/ui/browser_instant_controller.h @@ -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. @@ -7,19 +7,17 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" -#include "base/memory/scoped_ptr.h" #include "base/prefs/public/pref_change_registrar.h" #include "base/prefs/public/pref_observer.h" -#include "base/string16.h" +#include "chrome/browser/instant/instant_controller.h" #include "chrome/browser/instant/instant_unload_handler.h" #include "chrome/browser/ui/search/search_model_observer.h" -#include "chrome/browser/ui/tabs/tab_strip_model_observer.h" -#include "chrome/common/instant_types.h" #include "webkit/glue/window_open_disposition.h" class Browser; -class InstantController; -class InstantTest; +struct InstantSuggestion; +class PrefService; +class Profile; class TabContents; namespace gfx { @@ -28,20 +26,25 @@ class Rect; namespace chrome { -class BrowserInstantController : public TabStripModelObserver, - public search::SearchModelObserver, - public PrefObserver { +class BrowserInstantController : public PrefObserver, + public search::SearchModelObserver { public: explicit BrowserInstantController(Browser* browser); virtual ~BrowserInstantController(); + // Returns true if Instant is enabled in a visible, preview-showing mode. + static bool IsInstantEnabled(Profile* profile); + + // Registers Instant related preferences. + static void RegisterUserPrefs(PrefService* prefs); + // Commits the current Instant, returning true on success. This is intended // for use from OpenCurrentURL. bool OpenInstant(WindowOpenDisposition disposition); // Returns the InstantController or NULL if there is no InstantController for // this BrowserInstantController. - InstantController* instant() const { return instant_.get(); } + InstantController* instant() { return &instant_; } // Invoked by |instant_| to commit the |preview| by merging it into the active // tab or adding it as a new tab. We take ownership of |preview|. @@ -62,29 +65,21 @@ class BrowserInstantController : public TabStripModelObserver, // preview would be shown. TabContents* GetActiveTabContents() const; + // Invoked by |browser_| when the active tab changes. + void ActiveTabChanged(); + + private: // Overridden from PrefObserver: virtual void OnPreferenceChanged(PrefServiceBase* service, const std::string& pref_name) OVERRIDE; - // Overridden from TabStripModelObserver: - virtual void ActiveTabChanged(content::WebContents* old_contents, - content::WebContents* new_contents, - int index, - bool user_gesture) OVERRIDE; - virtual void TabStripEmpty() OVERRIDE; - // Overridden from search::SearchModelObserver: virtual void ModeChanged(const search::Mode& old_mode, const search::Mode& new_mode) OVERRIDE; - private: - // If this browser should have Instant, a new InstantController created; - // otherwise any existing InstantController is destroyed. - void ResetInstant(); - - Browser* browser_; + Browser* const browser_; - scoped_ptr<InstantController> instant_; + InstantController instant_; InstantUnloadHandler instant_unload_handler_; PrefChangeRegistrar profile_pref_registrar_; diff --git a/chrome/browser/ui/browser_window.h b/chrome/browser/ui/browser_window.h index c97ee42..15717f2 100644 --- a/chrome/browser/ui/browser_window.h +++ b/chrome/browser/ui/browser_window.h @@ -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. @@ -12,7 +12,6 @@ #include "chrome/browser/ui/fullscreen/fullscreen_exit_bubble_type.h" #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" #include "chrome/common/content_settings_types.h" -#include "chrome/common/instant_types.h" #include "ui/gfx/native_widget_types.h" #include "webkit/glue/window_open_disposition.h" @@ -314,12 +313,12 @@ class BrowserWindow : public BaseWindow { virtual bool InPresentationMode() = 0; #endif - // Returns the desired bounds for instant in screen coordinates. Note that if - // instant isn't currently visible this returns the bounds instant would be + // Returns the desired bounds for Instant in screen coordinates. Note that if + // Instant isn't currently visible this returns the bounds Instant would be // placed at. virtual gfx::Rect GetInstantBounds() = 0; - // Checks if an instant's tab contents is being shown. + // Checks if an Instant's tab contents is being shown. virtual bool IsInstantTabShowing() = 0; // Return the correct disposition for a popup window based on |bounds|. diff --git a/chrome/browser/ui/cocoa/browser_window_controller.mm b/chrome/browser/ui/cocoa/browser_window_controller.mm index fbf74ddb..92bb2f4 100644 --- a/chrome/browser/ui/cocoa/browser_window_controller.mm +++ b/chrome/browser/ui/cocoa/browser_window_controller.mm @@ -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. @@ -16,7 +16,6 @@ #include "chrome/browser/bookmarks/bookmark_editor.h" #include "chrome/browser/bookmarks/bookmark_model_factory.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/instant/instant_controller.h" #include "chrome/browser/managed_mode/managed_mode.h" #include "chrome/browser/profiles/avatar_menu_model.h" #include "chrome/browser/profiles/profile.h" @@ -1886,9 +1885,9 @@ willAnimateFromState:(bookmarks::VisualState)oldState } - (void)commitInstant { - InstantController* instant = browser_->instant_controller()->instant(); - if (instant && instant->IsCurrent()) - instant->CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST); + if (chrome::BrowserInstantController* controller = + browser_->instant_controller()) + controller->instant()->CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST); } - (BOOL)isInstantTabShowing { diff --git a/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.mm b/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.mm index 4030025..3f88673 100644 --- a/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.mm +++ b/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.mm @@ -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. @@ -335,7 +335,8 @@ string16 LocationBarViewMac::GetTitle() const { } InstantController* LocationBarViewMac::GetInstant() { - return browser_->instant_controller()->instant(); + return browser_->instant_controller() ? + browser_->instant_controller()->instant() : NULL; } void LocationBarViewMac::Revert() { diff --git a/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h b/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h index c1f14a4..69a0bb4 100644 --- a/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h +++ b/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h @@ -5,8 +5,8 @@ #ifndef CHROME_BROWSER_UI_COCOA_TAB_CONTENTS_INSTANT_PREVIEW_CONTROLLER_MAC_H_ #define CHROME_BROWSER_UI_COCOA_TAB_CONTENTS_INSTANT_PREVIEW_CONTROLLER_MAC_H_ +#include "base/basictypes.h" #include "base/compiler_specific.h" -#include "chrome/browser/instant/instant_model_observer.h" #include "chrome/browser/instant/instant_preview_controller.h" class Browser; @@ -15,25 +15,19 @@ class Browser; class InstantPreviewControllerMac : public InstantPreviewController { public: - InstantPreviewControllerMac( - Browser* browser, - BrowserWindowController* window_controller, - PreviewableContentsController* previewable_contents_controller); + InstantPreviewControllerMac(Browser* browser, + BrowserWindowController* window, + PreviewableContentsController* preview); virtual ~InstantPreviewControllerMac(); - // InstantModelObserver overrides: - virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE; - private: + // Overridden from InstantPreviewController: + virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE; - // Weak. - BrowserWindowController* window_controller_; - - // Weak. Owns us. - PreviewableContentsController* previewable_contents_controller_; + BrowserWindowController* const window_; + PreviewableContentsController* const preview_; DISALLOW_COPY_AND_ASSIGN(InstantPreviewControllerMac); }; - #endif // CHROME_BROWSER_UI_COCOA_TAB_CONTENTS_INSTANT_PREVIEW_CONTROLLER_MAC_H_ diff --git a/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.mm b/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.mm index ec7fa87..eb8d1b3 100644 --- a/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.mm +++ b/chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.mm @@ -11,11 +11,11 @@ InstantPreviewControllerMac::InstantPreviewControllerMac( Browser* browser, - BrowserWindowController* window_controller, - PreviewableContentsController* previewable_contents_controller) + BrowserWindowController* window, + PreviewableContentsController* preview) : InstantPreviewController(browser), - window_controller_(window_controller), - previewable_contents_controller_(previewable_contents_controller) { + window_(window), + preview_(preview) { } InstantPreviewControllerMac::~InstantPreviewControllerMac() { @@ -23,14 +23,11 @@ InstantPreviewControllerMac::~InstantPreviewControllerMac() { void InstantPreviewControllerMac::PreviewStateChanged( const InstantModel& model) { - if (model.preview_state() == InstantModel::QUERY_RESULTS) { + if (model.mode().is_search_suggestions()) { // TODO(dhollowa): Needs height and units implementation on Mac. - [previewable_contents_controller_ - showPreview:model.GetPreviewContents()->web_contents()]; + [preview_ showPreview:model.GetPreviewContents()->web_contents()]; } else { - if (![previewable_contents_controller_ isShowingPreview]) - return; - [previewable_contents_controller_ hidePreview]; + [preview_ hidePreview]; } - [window_controller_ updateBookmarkBarVisibilityWithAnimation:NO]; + [window_ updateBookmarkBarVisibilityWithAnimation:NO]; } diff --git a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h index d1b868c..1d0da58 100644 --- a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h +++ b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h @@ -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. @@ -10,8 +10,8 @@ #include "base/memory/scoped_ptr.h" class Browser; -class InstantPreviewControllerMac; @class BrowserWindowController; +class InstantPreviewControllerMac; namespace content { class WebContents; diff --git a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm index dc77339..b80d159 100644 --- a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm +++ b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.mm @@ -1,39 +1,28 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright 2011 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. #import "chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h" -#include "base/logging.h" #include "base/mac/bundle_locations.h" -#include "base/mac/mac_util.h" #include "chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h" #include "content/public/browser/web_contents.h" -using content::WebContents; - @implementation PreviewableContentsController @synthesize activeContainer = activeContainer_; -// For testing. Use |-initWithBrowser:| for production. -- (id)init { - if ((self = [super initWithNibName:@"PreviewableContents" - bundle:base::mac::FrameworkBundle()])) { - } - return self; -} - - (id)initWithBrowser:(Browser*)browser windowController:(BrowserWindowController*)windowController { - if ((self = [self init])) { + if ((self = [super initWithNibName:@"PreviewableContents" + bundle:base::mac::FrameworkBundle()])) { instantPreviewController_.reset( new InstantPreviewControllerMac(browser, windowController, self)); } return self; } -- (void)showPreview:(WebContents*)preview { +- (void)showPreview:(content::WebContents*)preview { DCHECK(preview); // Remove any old preview contents before showing the new one. @@ -63,7 +52,7 @@ using content::WebContents; previewContents_ = nil; } -- (void)onActivateTabWithContents:(WebContents*)contents { +- (void)onActivateTabWithContents:(content::WebContents*)contents { if (previewContents_ == contents) { [previewContents_->GetNativeView() removeFromSuperview]; previewContents_ = nil; diff --git a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller_unittest.mm b/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller_unittest.mm deleted file mode 100644 index f7064db..0000000 --- a/chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller_unittest.mm +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 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. - -#import <Cocoa/Cocoa.h> - -#import "base/memory/scoped_nsobject.h" -#include "chrome/browser/ui/cocoa/cocoa_test_helper.h" -#import "chrome/browser/ui/cocoa/tab_contents/previewable_contents_controller.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/platform_test.h" - -namespace { - -class PreviewableContentsControllerTest : public CocoaTest { - public: - virtual void SetUp() { - CocoaTest::SetUp(); - controller_.reset([[PreviewableContentsController alloc] init]); - [[test_window() contentView] addSubview:[controller_ view]]; - } - - scoped_nsobject<PreviewableContentsController> controller_; -}; - -TEST_VIEW(PreviewableContentsControllerTest, [controller_ view]) - -// TODO(rohitrao): Test showing and hiding the preview. This may require -// changing the interface to take in a WebContentsView* instead of a -// WebContents*. - -} // namespace - diff --git a/chrome/browser/ui/gtk/browser_window_gtk.cc b/chrome/browser/ui/gtk/browser_window_gtk.cc index e10cb8f..b5b1148 100644 --- a/chrome/browser/ui/gtk/browser_window_gtk.cc +++ b/chrome/browser/ui/gtk/browser_window_gtk.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. @@ -1678,9 +1678,7 @@ void BrowserWindowGtk::InitWidgets() { TRUE, TRUE, 0); instant_preview_controller_.reset( - new InstantPreviewControllerGtk(browser_.get(), - this, - contents_container_.get())); + new InstantPreviewControllerGtk(this, contents_container_.get())); if (IsBookmarkBarSupported()) { bookmark_bar_.reset(new BookmarkBarGtk(this, diff --git a/chrome/browser/ui/gtk/instant_preview_controller_gtk.cc b/chrome/browser/ui/gtk/instant_preview_controller_gtk.cc index 05e017f..21e6a6d 100644 --- a/chrome/browser/ui/gtk/instant_preview_controller_gtk.cc +++ b/chrome/browser/ui/gtk/instant_preview_controller_gtk.cc @@ -4,15 +4,14 @@ #include "chrome/browser/ui/gtk/instant_preview_controller_gtk.h" +#include "chrome/browser/instant/instant_model.h" #include "chrome/browser/ui/gtk/browser_window_gtk.h" #include "chrome/browser/ui/gtk/tab_contents_container_gtk.h" -#include "chrome/browser/instant/instant_model.h" InstantPreviewControllerGtk::InstantPreviewControllerGtk( - Browser* browser, BrowserWindowGtk* window, TabContentsContainerGtk* contents) - : InstantPreviewController(browser), + : InstantPreviewController(window->browser()), window_(window), contents_(contents) { } @@ -22,24 +21,11 @@ InstantPreviewControllerGtk::~InstantPreviewControllerGtk() { void InstantPreviewControllerGtk::PreviewStateChanged( const InstantModel& model) { - if (model.preview_state() == InstantModel::QUERY_RESULTS) { - ShowInstant(model.GetPreviewContents(), - model.height(), model.height_units()); + if (model.mode().is_search_suggestions()) { + // TODO(jered): Support non-100% height. + contents_->SetPreview(model.GetPreviewContents()); } else { - HideInstant(); + contents_->SetPreview(NULL); } -} - -void InstantPreviewControllerGtk::ShowInstant(TabContents* preview, - int height, - InstantSizeUnits units) { - // TODO(jered): Support height < 100%. - DCHECK(height == 100 && units == INSTANT_SIZE_PERCENT); - contents_->SetPreview(preview); - window_->MaybeShowBookmarkBar(false); -} - -void InstantPreviewControllerGtk::HideInstant() { - contents_->SetPreview(NULL); window_->MaybeShowBookmarkBar(false); } diff --git a/chrome/browser/ui/gtk/instant_preview_controller_gtk.h b/chrome/browser/ui/gtk/instant_preview_controller_gtk.h index 22e64c0..f0fb71a 100644 --- a/chrome/browser/ui/gtk/instant_preview_controller_gtk.h +++ b/chrome/browser/ui/gtk/instant_preview_controller_gtk.h @@ -5,35 +5,25 @@ #ifndef CHROME_BROWSER_UI_GTK_INSTANT_PREVIEW_CONTROLLER_GTK_H_ #define CHROME_BROWSER_UI_GTK_INSTANT_PREVIEW_CONTROLLER_GTK_H_ +#include "base/basictypes.h" #include "base/compiler_specific.h" -#include "chrome/browser/instant/instant_model_observer.h" #include "chrome/browser/instant/instant_preview_controller.h" -class Browser; class BrowserWindowGtk; -class InstantModel; -class TabContents; class TabContentsContainerGtk; class InstantPreviewControllerGtk : public InstantPreviewController { public: - InstantPreviewControllerGtk(Browser* browser, - BrowserWindowGtk* window, + InstantPreviewControllerGtk(BrowserWindowGtk* window, TabContentsContainerGtk* contents); virtual ~InstantPreviewControllerGtk(); - // InstantModelObserver overrides: - virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE; - private: - void ShowInstant(TabContents* preview, int height, InstantSizeUnits units); - void HideInstant(); - - // Weak. - BrowserWindowGtk* window_; + // Overridden from InstantPreviewController: + virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE; - // Weak. - TabContentsContainerGtk* contents_; + BrowserWindowGtk* const window_; + TabContentsContainerGtk* const contents_; DISALLOW_COPY_AND_ASSIGN(InstantPreviewControllerGtk); }; diff --git a/chrome/browser/ui/gtk/location_bar_view_gtk.cc b/chrome/browser/ui/gtk/location_bar_view_gtk.cc index 5417999..255e3ce 100644 --- a/chrome/browser/ui/gtk/location_bar_view_gtk.cc +++ b/chrome/browser/ui/gtk/location_bar_view_gtk.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. @@ -887,7 +887,8 @@ string16 LocationBarViewGtk::GetTitle() const { } InstantController* LocationBarViewGtk::GetInstant() { - return browser_->instant_controller()->instant(); + return browser_->instant_controller() ? + browser_->instant_controller()->instant() : NULL; } void LocationBarViewGtk::ShowFirstRunBubble() { diff --git a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc index 442cc90..beedfb8 100644 --- a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc +++ b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.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. @@ -19,7 +19,6 @@ #include "chrome/browser/bookmarks/bookmark_node_data.h" #include "chrome/browser/command_updater.h" #include "chrome/browser/defaults.h" -#include "chrome/browser/instant/instant_controller.h" #include "chrome/browser/platform_util.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_tabstrip.h" @@ -371,7 +370,7 @@ void OmniboxViewGtk::Init() { GtkTextIter end_iter; gtk_text_buffer_get_end_iter(text_buffer_, &end_iter); - // Insert a Zero Width Space character just before the instant anchor. + // Insert a Zero Width Space character just before the Instant anchor. // It's a hack to workaround a bug of GtkTextView which can not align the // pre-edit string and a child anchor correctly when there is no other content // around the pre-edit string. @@ -743,7 +742,7 @@ int OmniboxViewGtk::TextWidth() const { GdkRectangle first_char_bounds, last_char_bounds; gtk_text_buffer_get_start_iter(text_buffer_, &start); - // Use the real end iterator here to take the width of instant suggestion + // Use the real end iterator here to take the width of Instant suggestion // text into account, so that location bar can layout its children correctly. gtk_text_buffer_get_end_iter(text_buffer_, &end); gtk_text_view_get_iter_location(GTK_TEXT_VIEW(text_view_), @@ -1446,7 +1445,7 @@ void OmniboxViewGtk::HandleInsertText(GtkTextBuffer* buffer, p = g_utf8_next_char(p)) { gunichar c = g_utf8_get_char(p); - // 0x200B is Zero Width Space, which is inserted just before the instant + // 0x200B is Zero Width Space, which is inserted just before the Instant // anchor for working around the GtkTextView's misalignment bug. // This character might be captured and inserted into the content by undo // manager, so we need to filter it out here. @@ -1459,7 +1458,7 @@ void OmniboxViewGtk::HandleInsertText(GtkTextBuffer* buffer, CollapseWhitespace(filtered_text, true)); if (!filtered_text.empty()) { - // Avoid inserting the text after the instant anchor. + // Avoid inserting the text after the Instant anchor. ValidateTextBufferIter(location); // Call the default handler to insert filtered text. @@ -1971,8 +1970,8 @@ void OmniboxViewGtk::HandleKeymapDirectionChanged(GdkKeymap* sender) { void OmniboxViewGtk::HandleDeleteRange(GtkTextBuffer* buffer, GtkTextIter* start, GtkTextIter* end) { - // Prevent the user from deleting the instant anchor. We can't simply set the - // instant anchor readonly by applying a tag with "editable" = FALSE, because + // Prevent the user from deleting the Instant anchor. We can't simply set the + // Instant anchor readonly by applying a tag with "editable" = FALSE, because // it'll prevent the insert caret from blinking. ValidateTextBufferIter(start); ValidateTextBufferIter(end); @@ -1995,7 +1994,7 @@ void OmniboxViewGtk::HandleMarkSetAlways(GtkTextBuffer* buffer, static guint signal_id = g_signal_lookup("mark-set", GTK_TYPE_TEXT_BUFFER); // "mark-set" signal is actually emitted after the mark's location is already - // set, so if the location is beyond the instant anchor, we need to move the + // set, so if the location is beyond the Instant anchor, we need to move the // mark again, which will emit the signal again. In order to prevent other // signal handlers from being called twice, we need to stop signal emission // before moving the mark again. diff --git a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h index c813ea6..6b1d60b 100644 --- a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h +++ b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h @@ -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. @@ -106,7 +106,7 @@ class OmniboxViewGtk : public OmniboxView, // Sets the colors of the text view according to the theme. void SetBaseColor(); - // Sets the colors of the instant suggestion view according to the theme. + // Sets the colors of the Instant suggestion view according to the theme. void UpdateInstantViewColors(); // Returns the text view gtk widget. May return NULL if the widget @@ -309,14 +309,14 @@ class OmniboxViewGtk : public OmniboxView, GtkTextTag* security_error_scheme_tag_; GtkTextTag* normal_text_tag_; - // Objects for the instant suggestion text view. + // Objects for the Instant suggestion text view. GtkTextTag* instant_anchor_tag_; - // A widget for displaying instant suggestion text. It'll be attached to a + // A widget for displaying Instant suggestion text. It'll be attached to a // child anchor in the |text_buffer_| object. GtkWidget* instant_view_; - // A mark to split the content and the instant anchor. Wherever the end + // A mark to split the content and the Instant anchor. Wherever the end // iterator of the text buffer is required, the iterator to this mark should // be used. GtkTextMark* instant_mark_; diff --git a/chrome/browser/ui/omnibox/omnibox_edit_model.cc b/chrome/browser/ui/omnibox/omnibox_edit_model.cc index d73ba5f..a643b74 100644 --- a/chrome/browser/ui/omnibox/omnibox_edit_model.cc +++ b/chrome/browser/ui/omnibox/omnibox_edit_model.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. @@ -234,12 +234,8 @@ bool OmniboxEditModel::CommitSuggestedText(bool skip_inline_autocomplete) { } bool OmniboxEditModel::AcceptCurrentInstantPreview() { - InstantController* instant = controller_->GetInstant(); - if (instant && instant->IsCurrent()) { - instant->CommitCurrentPreview(INSTANT_COMMIT_PRESSED_ENTER); - return true; - } - return false; + return controller_->GetInstant() && + controller_->GetInstant()->CommitIfCurrent(INSTANT_COMMIT_PRESSED_ENTER); } void OmniboxEditModel::OnChanged() { @@ -288,7 +284,7 @@ void OmniboxEditModel::OnChanged() { // Hide any suggestions we might be showing. view_->SetInstantSuggestion(string16()); - // No need to wait any longer for instant. + // No need to wait any longer for Instant. FinalizeInstantQuery(string16(), InstantSuggestion(), false); } @@ -311,7 +307,7 @@ void OmniboxEditModel::GetDataForURLExport(GURL* url, bool OmniboxEditModel::UseVerbatimInstant() { #if defined(OS_MACOSX) // TODO(suzhe): Fix Mac port to display Instant suggest in a separated NSView, - // so that we can display instant suggest along with composition text. + // so that we can display Instant suggest along with composition text. const AutocompleteInput& input = autocomplete_controller_->input(); if (input.prevent_inline_autocomplete()) return true; @@ -321,11 +317,11 @@ bool OmniboxEditModel::UseVerbatimInstant() { // following conditions: // 1. If the caret is at the end of the text (checked below). // 2. If it's in IME composition mode. - // As we use a separated widget for displaying the instant suggest, it won't + // As we use a separated widget for displaying the Instant suggest, it won't // interfere with IME composition, so we don't need to care about the value of // input.prevent_inline_autocomplete() here. if (view_->DeleteAtEndPressed() || (popup_->selected_line() != 0) || - just_deleted_text_) + just_deleted_text_ || !inline_autocomplete_text_.empty()) return true; size_t start, end; @@ -624,6 +620,7 @@ void OmniboxEditModel::OpenMatch(const AutocompleteMatch& match, if (disposition != NEW_BACKGROUND_TAB) { in_revert_ = true; view_->RevertAll(); // Revert the box to its unedited state + in_revert_ = false; } if (match.type == AutocompleteMatch::EXTENSION_APP) { @@ -651,11 +648,6 @@ void OmniboxEditModel::OpenMatch(const AutocompleteMatch& match, if (match.starred) bookmark_utils::RecordBookmarkLaunch(bookmark_utils::LAUNCH_OMNIBOX); - - InstantController* instant = controller_->GetInstant(); - if (instant && !popup_->IsOpen()) - instant->Hide(); - in_revert_ = false; } bool OmniboxEditModel::AcceptKeyword() { @@ -716,9 +708,8 @@ void OmniboxEditModel::OnSetFocus(bool control_down) { has_focus_ = true; control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; - InstantController* instant = controller_->GetInstant(); - if (instant) - instant->OnAutocompleteGotFocus(); + if (InstantController* instant = controller_->GetInstant()) + instant->OmniboxGotFocus(); content::WebContents* web_contents = controller_->GetWebContents(); if (web_contents) { @@ -738,7 +729,7 @@ void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { SetInstantSuggestion(InstantSuggestion()); if (InstantController* instant = controller_->GetInstant()) - instant->OnAutocompleteLostFocus(view_gaining_focus); + instant->OmniboxLostFocus(view_gaining_focus); // TODO(jered): Rip this out along with StartZeroSuggest. autocomplete_controller_->StopZeroSuggest(); @@ -769,10 +760,6 @@ bool OmniboxEditModel::OnEscapeKeyPressed() { view_->Update(NULL); } - // Let Instant decide whether to hide itself. - if (InstantController* instant = controller_->GetInstant()) - instant->OnEscapeKeyPressed(); - // If the user wasn't editing, but merely had focus in the edit, allow <esc> // to be processed as an accelerator, so it can still be used to stop a load. // When the permanent text isn't all selected we still fall through to the @@ -1005,8 +992,7 @@ bool OmniboxEditModel::OnAfterPossibleChange(const string16& old_text, } void OmniboxEditModel::PopupBoundsChangedTo(const gfx::Rect& bounds) { - InstantController* instant = controller_->GetInstant(); - if (instant) + if (InstantController* instant = controller_->GetInstant()) instant->SetOmniboxBounds(bounds); } @@ -1190,39 +1176,32 @@ void OmniboxEditModel::NotifySearchTabHelper() { } bool OmniboxEditModel::DoInstant(const AutocompleteMatch& match) { - if (in_revert_) + InstantController* instant = controller_->GetInstant(); + if (!instant || in_revert_) return false; - InstantController* instant = controller_->GetInstant(); + // The two pieces of text we want to send Instant, viz., what the user has + // typed, and the full omnibox text including any inline autocompletion. + string16 user_text = user_text_; + string16 full_text = user_text_ + inline_autocomplete_text_; - if (!instant) - return false; + // If there's temporary text, that overrides the user_text. In this case, we + // should ignore any inline_autocomplete_text_, because it won't be visible. + if (has_temporary_text_) + user_text = full_text = CurrentMatch().fill_into_edit; - if (user_input_in_progress_) { - // The two pieces of text we want to send Instant, viz., what the user has - // typed, and the full omnibox text including any inline autocompletion. - string16 user_text = user_text_; - string16 full_text = user_text_ + inline_autocomplete_text_; - - // If there's temporary text, that overrides the user_text. In this case, we - // should ignore any inline_autocomplete_text_, because it won't be visible. - if (has_temporary_text_) - user_text = full_text = CurrentMatch().fill_into_edit; - - // Remove keyword if we're in keyword mode. - user_text = DisplayTextFromUserText(user_text); - full_text = DisplayTextFromUserText(full_text); - - // Remove "?" if we're in forced query mode. - AutocompleteInput::RemoveForcedQueryStringIfNecessary( - autocomplete_controller_->input().type(), &user_text); - AutocompleteInput::RemoveForcedQueryStringIfNecessary( - autocomplete_controller_->input().type(), &full_text); - - return instant->Update(match, user_text, full_text, UseVerbatimInstant()); - } + // Remove keyword if we're in keyword mode. + user_text = DisplayTextFromUserText(user_text); + full_text = DisplayTextFromUserText(full_text); + + // Remove "?" if we're in forced query mode. + AutocompleteInput::RemoveForcedQueryStringIfNecessary( + autocomplete_controller_->input().type(), &user_text); + AutocompleteInput::RemoveForcedQueryStringIfNecessary( + autocomplete_controller_->input().type(), &full_text); - return false; + return instant->Update(match, user_text, full_text, UseVerbatimInstant(), + user_input_in_progress_, popup_->IsOpen()); } void OmniboxEditModel::DoPrerender(const AutocompleteMatch& match) { diff --git a/chrome/browser/ui/omnibox/omnibox_edit_model.h b/chrome/browser/ui/omnibox/omnibox_edit_model.h index c420ad4..6caf62f 100644 --- a/chrome/browser/ui/omnibox/omnibox_edit_model.h +++ b/chrome/browser/ui/omnibox/omnibox_edit_model.h @@ -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. @@ -12,7 +12,6 @@ #include "base/time.h" #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" #include "chrome/browser/autocomplete/autocomplete_match.h" -#include "chrome/common/instant_types.h" #include "chrome/common/metrics/proto/omnibox_event.pb.h" #include "content/public/common/page_transition_types.h" #include "googleurl/src/gurl.h" @@ -21,6 +20,7 @@ class AutocompleteController; class AutocompleteResult; +struct InstantSuggestion; class OmniboxEditController; class OmniboxPopupModel; class OmniboxView; diff --git a/chrome/browser/ui/views/frame/browser_view.cc b/chrome/browser/ui/views/frame/browser_view.cc index ed18c6f..13996a4 100644 --- a/chrome/browser/ui/views/frame/browser_view.cc +++ b/chrome/browser/ui/views/frame/browser_view.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. @@ -20,7 +20,6 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/extensions/tab_helper.h" #include "chrome/browser/infobars/infobar_tab_helper.h" -#include "chrome/browser/instant/instant_controller.h" #include "chrome/browser/managed_mode/managed_mode.h" #include "chrome/browser/native_window_notification_source.h" #include "chrome/browser/password_manager/password_manager.h" @@ -528,7 +527,7 @@ BrowserView::~BrowserView() { launcher_item_controller_.reset(); #endif - preview_controller_.reset(NULL); + preview_controller_.reset(); browser_->tab_strip_model()->RemoveObserver(this); @@ -1510,7 +1509,7 @@ gfx::Rect BrowserView::GetInstantBounds() { } bool BrowserView::IsInstantTabShowing() { - return preview_controller_->preview_container() != NULL; + return preview_controller_->preview() != NULL; } WindowOpenDisposition BrowserView::GetDispositionForPopupBounds( @@ -1589,7 +1588,7 @@ void BrowserView::ActiveTabChanged(content::WebContents* old_contents, if (contents_->preview_web_contents() == new_contents) { contents_->MakePreviewContentsActiveContents(); views::WebView* old_container = contents_container_; - contents_container_ = preview_controller_->release_preview_container(); + contents_container_ = preview_controller_->release_preview(); old_container->SetWebContents(NULL); delete old_container; } @@ -2141,7 +2140,7 @@ void BrowserView::Init() { toolbar_->Init(); preview_controller_.reset( - new InstantPreviewControllerViews(browser(), this, contents_)); + new InstantPreviewControllerViews(browser(), contents_)); SkColor bg_color = GetWidget()->GetThemeProvider()-> GetColor(ThemeService::COLOR_TOOLBAR); diff --git a/chrome/browser/ui/views/frame/contents_container.cc b/chrome/browser/ui/views/frame/contents_container.cc index 7f9b36e..26c6700 100644 --- a/chrome/browser/ui/views/frame/contents_container.cc +++ b/chrome/browser/ui/views/frame/contents_container.cc @@ -1,21 +1,17 @@ -// 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. #include "chrome/browser/ui/views/frame/contents_container.h" -#include "base/logging.h" #include "ui/views/controls/webview/webview.h" -using content::WebContents; - // static const char ContentsContainer::kViewClassName[] = "browser/ui/views/frame/ContentsContainer"; ContentsContainer::ContentsContainer(views::WebView* active) : active_(active), - overlay_(NULL), preview_(NULL), preview_web_contents_(NULL), active_top_margin_(0), @@ -28,25 +24,6 @@ ContentsContainer::ContentsContainer(views::WebView* active) ContentsContainer::~ContentsContainer() { } -void ContentsContainer::SetActive(views::WebView* active) { - if (active_) - RemoveChildView(active_); - active_ = active; - // Note the active view is always the first child. - if (active_) - AddChildViewAt(active_, 0); - Layout(); -} - -void ContentsContainer::SetOverlay(views::View* overlay) { - if (overlay_) - RemoveChildView(overlay_); - overlay_ = overlay; - if (overlay_) - AddChildView(overlay_); - Layout(); -} - void ContentsContainer::MakePreviewContentsActiveContents() { DCHECK(preview_); @@ -57,15 +34,13 @@ void ContentsContainer::MakePreviewContentsActiveContents() { } void ContentsContainer::SetPreview(views::WebView* preview, - WebContents* preview_web_contents, + content::WebContents* preview_web_contents, int height, InstantSizeUnits units) { - const int old_height = PreviewHeightInPixels(); - preview_height_ = height; - preview_height_units_ = units; - if (preview == preview_ && preview_web_contents_ == preview_web_contents && - old_height == PreviewHeightInPixels()) + if (preview_ == preview && preview_web_contents_ == preview_web_contents && + preview_height_ == height && preview_height_units_ == units) return; + if (preview_ != preview) { if (preview_) RemoveChildView(preview_); @@ -74,6 +49,8 @@ void ContentsContainer::SetPreview(views::WebView* preview, AddChildView(preview_); } preview_web_contents_ = preview_web_contents; + preview_height_ = height; + preview_height_units_ = units; Layout(); } @@ -87,22 +64,24 @@ void ContentsContainer::SetActiveTopMargin(int margin) { InvalidateLayout(); } -gfx::Rect ContentsContainer::GetPreviewBounds() { +gfx::Rect ContentsContainer::GetPreviewBounds() const { gfx::Point screen_loc; ConvertPointToScreen(this, &screen_loc); return gfx::Rect(screen_loc, size()); } +void ContentsContainer::SetExtraContentHeight(int height) { + if (height == extra_content_height_) + return; + extra_content_height_ = height; +} + void ContentsContainer::Layout() { int content_y = active_top_margin_; int content_height = std::max(0, height() - content_y + extra_content_height_); - if (active_) - active_->SetBounds(0, content_y, width(), content_height); - - if (overlay_) - overlay_->SetBounds(0, 0, width(), height()); + active_->SetBounds(0, content_y, width(), content_height); if (preview_) preview_->SetBounds(0, 0, width(), PreviewHeightInPixels()); @@ -112,10 +91,8 @@ void ContentsContainer::Layout() { views::View::Layout(); } -void ContentsContainer::SetExtraContentHeight(int height) { - if (height == extra_content_height_) - return; - extra_content_height_ = height; +std::string ContentsContainer::GetClassName() const { + return kViewClassName; } int ContentsContainer::PreviewHeightInPixels() const { @@ -129,7 +106,3 @@ int ContentsContainer::PreviewHeightInPixels() const { NOTREACHED() << "unknown units: " << preview_height_units_; return 0; } - -std::string ContentsContainer::GetClassName() const { - return kViewClassName; -} diff --git a/chrome/browser/ui/views/frame/contents_container.h b/chrome/browser/ui/views/frame/contents_container.h index 0216874..9f57a03 100644 --- a/chrome/browser/ui/views/frame/contents_container.h +++ b/chrome/browser/ui/views/frame/contents_container.h @@ -1,10 +1,14 @@ -// 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. #ifndef CHROME_BROWSER_UI_VIEWS_FRAME_CONTENTS_CONTAINER_H_ #define CHROME_BROWSER_UI_VIEWS_FRAME_CONTENTS_CONTAINER_H_ +#include <string> + +#include "base/basictypes.h" +#include "base/compiler_specific.h" #include "chrome/common/instant_types.h" #include "ui/views/view.h" @@ -12,13 +16,17 @@ namespace content { class WebContents; } +namespace gfx { +class Rect; +} + namespace views { class WebView; } // ContentsContainer is responsible for managing the WebContents views. // ContentsContainer has up to two children: one for the currently active -// WebContents and one for instant's WebContents. +// WebContents and one for Instant's WebContents. class ContentsContainer : public views::View { public: // Internal class name @@ -27,19 +35,8 @@ class ContentsContainer : public views::View { explicit ContentsContainer(views::WebView* active); virtual ~ContentsContainer(); - // Sets the active web view first in stacking order. This view is deactivated - // when the |SearchViewController| is displaying the NTP, and activated - // otherwise. Deactivation removes the active view from the view hierarchy. - void SetActive(views::WebView* active); - views::WebView* active() { return active_; } - - // Sets the overlay. The overlay is sized to the bounds of this view. - void SetOverlay(views::View* overlay); - views::View* overlay() { return overlay_; } - // Makes the preview view the active view and nulls out the old active view. - // It's assumed the caller will delete or remove the old active view - // separately. + // The caller must delete or remove the old active view separately. void MakePreviewContentsActiveContents(); // Sets the preview view. This does not delete the old. @@ -47,7 +44,6 @@ class ContentsContainer : public views::View { content::WebContents* preview_web_contents, int height, InstantSizeUnits units); - views::WebView* preview() { return preview_; } content::WebContents* preview_web_contents() const { return preview_web_contents_; @@ -56,13 +52,8 @@ class ContentsContainer : public views::View { // Sets the active top margin. void SetActiveTopMargin(int margin); - // Returns the bounds of the preview. If the preview isn't active this - // retuns the bounds the preview would be shown at. - gfx::Rect GetPreviewBounds(); - - // View overrides: - virtual void Layout() OVERRIDE; - virtual std::string GetClassName() const OVERRIDE; + // Returns the bounds the preview would be shown at. + gfx::Rect GetPreviewBounds() const; // Set/Get an extra content height, so that room is left at the bottom of the // contents view for other views to draw on top of the extended child web @@ -73,14 +64,17 @@ class ContentsContainer : public views::View { void SetExtraContentHeight(int height); private: - views::WebView* active_; - views::View* overlay_; - views::WebView* preview_; - content::WebContents* preview_web_contents_; + // Overridden from views::View: + virtual void Layout() OVERRIDE; + virtual std::string GetClassName() const OVERRIDE; // Returns |preview_height_| in pixels. int PreviewHeightInPixels() const; + views::WebView* active_; + views::WebView* preview_; + content::WebContents* preview_web_contents_; + // The margin between the top and the active view. This is used to make the // preview overlap the bookmark bar on the new tab page. int active_top_margin_; diff --git a/chrome/browser/ui/views/frame/instant_preview_controller_views.cc b/chrome/browser/ui/views/frame/instant_preview_controller_views.cc index 4a67e2b..fd6c76d 100644 --- a/chrome/browser/ui/views/frame/instant_preview_controller_views.cc +++ b/chrome/browser/ui/views/frame/instant_preview_controller_views.cc @@ -4,22 +4,19 @@ #include "chrome/browser/ui/views/frame/instant_preview_controller_views.h" +#include "chrome/browser/instant/instant_model.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/tab_contents/tab_contents.h" #include "chrome/browser/ui/view_ids.h" -#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/views/frame/contents_container.h" -#include "chrome/browser/instant/instant_model.h" #include "ui/views/controls/webview/webview.h" InstantPreviewControllerViews::InstantPreviewControllerViews( Browser* browser, - BrowserView* browser_view, ContentsContainer* contents) : InstantPreviewController(browser), - browser_view_(browser_view), - contents_(contents), - preview_container_(NULL) { + contents_(contents) { } InstantPreviewControllerViews::~InstantPreviewControllerViews() { @@ -27,35 +24,21 @@ InstantPreviewControllerViews::~InstantPreviewControllerViews() { void InstantPreviewControllerViews::PreviewStateChanged( const InstantModel& model) { - if (model.preview_state() == InstantModel::QUERY_RESULTS || - model.preview_state() == InstantModel::CUSTOM_NTP_CONTENT) { - ShowInstant(model.GetPreviewContents(), - model.height(), model.height_units()); - } else { - HideInstant(); - } -} - -void InstantPreviewControllerViews::ShowInstant(TabContents* preview, - int height, - InstantSizeUnits units) { - if (!preview_container_) { - preview_container_ = new views::WebView(browser_->profile()); - preview_container_->set_id(VIEW_ID_TAB_CONTAINER); + if (model.mode().is_ntp() || model.mode().is_search_suggestions()) { + // Show the preview. + if (!preview_) { + preview_.reset(new views::WebView(browser_->profile())); + preview_->set_id(VIEW_ID_TAB_CONTAINER); + } + content::WebContents* web_contents = + model.GetPreviewContents()->web_contents(); + contents_->SetPreview(preview_.get(), web_contents, + model.height(), model.height_units()); + preview_->SetWebContents(web_contents); + } else if (preview_) { + // Hide the preview. SetWebContents() must happen before SetPreview(). + preview_->SetWebContents(NULL); + contents_->SetPreview(NULL, NULL, 100, INSTANT_SIZE_PERCENT); + preview_.reset(); } - contents_->SetPreview(preview_container_, - preview->web_contents(), - height, units); - preview_container_->SetWebContents(preview->web_contents()); -} - -void InstantPreviewControllerViews::HideInstant() { - if (!preview_container_) - return; - - // The contents must be changed before SetPreview is invoked. - preview_container_->SetWebContents(NULL); - contents_->SetPreview(NULL, NULL, 100, INSTANT_SIZE_PERCENT); - delete preview_container_; - preview_container_ = NULL; } diff --git a/chrome/browser/ui/views/frame/instant_preview_controller_views.h b/chrome/browser/ui/views/frame/instant_preview_controller_views.h index b241612..d80e5f2 100644 --- a/chrome/browser/ui/views/frame/instant_preview_controller_views.h +++ b/chrome/browser/ui/views/frame/instant_preview_controller_views.h @@ -5,53 +5,40 @@ #ifndef CHROME_BROWSER_UI_VIEWS_FRAME_INSTANT_PREVIEW_CONTROLLER_VIEWS_H_ #define CHROME_BROWSER_UI_VIEWS_FRAME_INSTANT_PREVIEW_CONTROLLER_VIEWS_H_ +#include "base/basictypes.h" #include "base/compiler_specific.h" -#include "chrome/browser/instant/instant_model_observer.h" +#include "base/memory/scoped_ptr.h" #include "chrome/browser/instant/instant_preview_controller.h" class Browser; -class BrowserView; class ContentsContainer; -class InstantModel; -class TabContents; namespace views { class WebView; } -// A controller that manages the Views-specific Instant preview. Its primary +// A controller that manages the Views-specific Instant preview. Its primary // role is to respond to display-state changes from the Instant model and // reflect this in the visibility and layout of the preview. class InstantPreviewControllerViews : public InstantPreviewController { public: - InstantPreviewControllerViews(Browser* browser, - BrowserView* browser_view, - ContentsContainer* contents); + InstantPreviewControllerViews(Browser* browser, ContentsContainer* contents); virtual ~InstantPreviewControllerViews(); - // InstantModelObserver overrides: - virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE; + views::WebView* preview() { return preview_.get(); } - views::WebView* preview_container() { return preview_container_; } - views::WebView* release_preview_container() { - views::WebView* tmp = preview_container_; - preview_container_ = NULL; - return tmp; + views::WebView* release_preview() WARN_UNUSED_RESULT { + return preview_.release(); } private: - void ShowInstant(TabContents* preview, int height, InstantSizeUnits units); - void HideInstant(); - - // Weak. - BrowserView* browser_view_; + // Overridden from InstantPreviewController: + virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE; - // Weak. - ContentsContainer* contents_; + ContentsContainer* const contents_; // The view that contains the Instant preview web contents. - // Lazily created in ShowInstant() with ownership passed to |contents_|. - views::WebView* preview_container_; + scoped_ptr<views::WebView> preview_; DISALLOW_COPY_AND_ASSIGN(InstantPreviewControllerViews); }; diff --git a/chrome/browser/ui/views/toolbar_view.cc b/chrome/browser/ui/views/toolbar_view.cc index 9a794cf..2714606 100644 --- a/chrome/browser/ui/views/toolbar_view.cc +++ b/chrome/browser/ui/views/toolbar_view.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. @@ -454,7 +454,8 @@ WebContents* ToolbarView::GetWebContents() const { } InstantController* ToolbarView::GetInstant() { - return browser_->instant_controller()->instant(); + return browser_->instant_controller() ? + browser_->instant_controller()->instant() : NULL; } ContentSettingBubbleModelDelegate* |