[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 1 | // 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] | fb8fdf1 | 2012-08-21 16:28:20 | [diff] [blame] | 10 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 11 | #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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 22 | #include "content/public/browser/web_contents.h" |
| 23 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 24 | namespace chrome { |
| 25 | |
| 26 | //////////////////////////////////////////////////////////////////////////////// |
| 27 | // BrowserInstantController, public: |
| 28 | |
| 29 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 30 | : browser_(browser), |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame^] | 31 | instant_unload_handler_(browser) { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 32 | profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); |
| 33 | profile_pref_registrar_.Add(prefs::kInstantEnabled, this); |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 34 | ResetInstant(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 35 | browser_->tab_strip_model()->AddObserver(this); |
| 36 | } |
| 37 | |
| 38 | BrowserInstantController::~BrowserInstantController() { |
| 39 | browser_->tab_strip_model()->RemoveObserver(this); |
| 40 | } |
| 41 | |
| 42 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 43 | // 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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 46 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 47 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 48 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame^] | 49 | // to handle them. If you hit this DCHECK file a bug and I'll (sky) add |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 50 | // support for the new disposition. |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame^] | 51 | 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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | //////////////////////////////////////////////////////////////////////////////// |
| 60 | // BrowserInstantController, InstantControllerDelegate implementation: |
| 61 | |
[email protected] | b0628ad | 2012-09-20 00:48:47 | [diff] [blame] | 62 | void BrowserInstantController::ShowInstant(int height, InstantSizeUnits units) { |
[email protected] | 93cee48 | 2012-09-25 18:21:20 | [diff] [blame] | 63 | browser_->window()->ShowInstant(instant_->GetPreviewContents(), |
| 64 | height, units); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void BrowserInstantController::HideInstant() { |
[email protected] | afeecab0 | 2012-09-12 22:54:16 | [diff] [blame] | 68 | browser_->window()->HideInstant(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame^] | 71 | void 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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 85 | |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame^] | 86 | 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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | void BrowserInstantController::SetSuggestedText( |
| 96 | const string16& text, |
| 97 | InstantCompleteBehavior behavior) { |
| 98 | if (browser_->window()->GetLocationBar()) |
| 99 | browser_->window()->GetLocationBar()->SetSuggestedText(text, behavior); |
| 100 | } |
| 101 | |
| 102 | gfx::Rect BrowserInstantController::GetInstantBounds() { |
| 103 | return browser_->window()->GetInstantBounds(); |
| 104 | } |
| 105 | |
| 106 | void BrowserInstantController::InstantPreviewFocused() { |
| 107 | // NOTE: This is only invoked on aura. |
| 108 | browser_->window()->WebContentsFocused( |
| 109 | instant_->GetPreviewContents()->web_contents()); |
| 110 | } |
| 111 | |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 112 | TabContents* BrowserInstantController::GetActiveTabContents() const { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 113 | return chrome::GetActiveTabContents(browser_); |
| 114 | } |
| 115 | |
| 116 | //////////////////////////////////////////////////////////////////////////////// |
| 117 | // BrowserInstantController, content::NotificationObserver implementation: |
| 118 | |
| 119 | void BrowserInstantController::Observe( |
| 120 | int type, |
| 121 | const content::NotificationSource& source, |
| 122 | const content::NotificationDetails& details) { |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 123 | 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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | //////////////////////////////////////////////////////////////////////////////// |
| 130 | // BrowserInstantController, TabStripModelObserver implementation: |
| 131 | |
| 132 | void BrowserInstantController::TabDeactivated(TabContents* contents) { |
| 133 | if (instant()) |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 134 | instant_->Hide(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | //////////////////////////////////////////////////////////////////////////////// |
| 138 | // BrowserInstantController, private: |
| 139 | |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 140 | void BrowserInstantController::ResetInstant() { |
[email protected] | b67d0a4 | 2012-09-04 20:57:35 | [diff] [blame] | 141 | instant_.reset( |
| 142 | !browser_shutdown::ShuttingDownWithoutClosingBrowsers() && |
| 143 | browser_->is_type_tabbed() ? |
| 144 | InstantController::CreateInstant(browser_->profile(), this) : NULL); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace chrome |