[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" |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 16 | #include "chrome/browser/ui/search/search_model.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 17 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 | #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 19 | #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
| 20 | #include "chrome/common/chrome_notification_types.h" |
| 21 | #include "chrome/common/pref_names.h" |
| 22 | #include "content/public/browser/notification_service.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 23 | #include "content/public/browser/web_contents.h" |
| 24 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 25 | namespace chrome { |
| 26 | |
| 27 | //////////////////////////////////////////////////////////////////////////////// |
| 28 | // BrowserInstantController, public: |
| 29 | |
| 30 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 31 | : browser_(browser), |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 32 | instant_unload_handler_(browser) { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 33 | profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); |
| 34 | profile_pref_registrar_.Add(prefs::kInstantEnabled, this); |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 35 | ResetInstant(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 36 | browser_->tab_strip_model()->AddObserver(this); |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 37 | browser_->search_model()->AddObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | BrowserInstantController::~BrowserInstantController() { |
| 41 | browser_->tab_strip_model()->RemoveObserver(this); |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 42 | browser_->search_model()->RemoveObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 46 | // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt |
| 47 | // to use the Instant preview. |
| 48 | if (!instant() || !instant_->IsCurrent() || disposition == NEW_BACKGROUND_TAB) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 49 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 50 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 51 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 52 | // 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] | 53 | // support for the new disposition. |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 54 | DCHECK(disposition == CURRENT_TAB || |
| 55 | disposition == NEW_FOREGROUND_TAB) << disposition; |
| 56 | |
| 57 | instant_->CommitCurrentPreview(disposition == CURRENT_TAB ? |
| 58 | INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); |
| 59 | return true; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | //////////////////////////////////////////////////////////////////////////////// |
| 63 | // BrowserInstantController, InstantControllerDelegate implementation: |
| 64 | |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 65 | void BrowserInstantController::CommitInstant(TabContents* preview, |
| 66 | bool in_new_tab) { |
| 67 | if (in_new_tab) { |
| 68 | // TabStripModel takes ownership of |preview|. |
| 69 | browser_->tab_strip_model()->AddTabContents(preview, -1, |
| 70 | instant_->last_transition_type(), TabStripModel::ADD_ACTIVE); |
| 71 | } else { |
| 72 | TabContents* active_tab = chrome::GetActiveTabContents(browser_); |
| 73 | int index = browser_->tab_strip_model()->GetIndexOfTabContents(active_tab); |
| 74 | DCHECK_NE(TabStripModel::kNoTab, index); |
| 75 | // TabStripModel takes ownership of |preview|. |
| 76 | browser_->tab_strip_model()->ReplaceTabContentsAt(index, preview); |
| 77 | // InstantUnloadHandler takes ownership of |active_tab|. |
| 78 | instant_unload_handler_.RunUnloadListenersOrDestroy(active_tab, index); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 79 | |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 80 | GURL url = preview->web_contents()->GetURL(); |
| 81 | DCHECK(browser_->profile()->GetExtensionService()); |
| 82 | if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { |
| 83 | AppLauncherHandler::RecordAppLaunchType( |
| 84 | extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); |
| 85 | } |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame^] | 89 | void BrowserInstantController::SetInstantSuggestion( |
| 90 | const InstantSuggestion& suggestion) { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 91 | if (browser_->window()->GetLocationBar()) |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame^] | 92 | browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | gfx::Rect BrowserInstantController::GetInstantBounds() { |
| 96 | return browser_->window()->GetInstantBounds(); |
| 97 | } |
| 98 | |
| 99 | void BrowserInstantController::InstantPreviewFocused() { |
| 100 | // NOTE: This is only invoked on aura. |
| 101 | browser_->window()->WebContentsFocused( |
| 102 | instant_->GetPreviewContents()->web_contents()); |
| 103 | } |
| 104 | |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 105 | TabContents* BrowserInstantController::GetActiveTabContents() const { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 106 | return chrome::GetActiveTabContents(browser_); |
| 107 | } |
| 108 | |
| 109 | //////////////////////////////////////////////////////////////////////////////// |
| 110 | // BrowserInstantController, content::NotificationObserver implementation: |
| 111 | |
| 112 | void BrowserInstantController::Observe( |
| 113 | int type, |
| 114 | const content::NotificationSource& source, |
| 115 | const content::NotificationDetails& details) { |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 116 | DCHECK_EQ(chrome::NOTIFICATION_PREF_CHANGED, type); |
| 117 | DCHECK_EQ(std::string(prefs::kInstantEnabled), |
| 118 | *content::Details<std::string>(details).ptr()); |
| 119 | ResetInstant(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | //////////////////////////////////////////////////////////////////////////////// |
| 123 | // BrowserInstantController, TabStripModelObserver implementation: |
| 124 | |
| 125 | void BrowserInstantController::TabDeactivated(TabContents* contents) { |
| 126 | if (instant()) |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 127 | instant_->Hide(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 131 | // BrowserInstantController, search::SearchModelObserver implementation: |
| 132 | |
| 133 | void BrowserInstantController::ModeChanged(const search::Mode& old_mode, |
| 134 | const search::Mode& new_mode) { |
| 135 | if (instant() && old_mode.is_ntp() != new_mode.is_ntp()) |
| 136 | instant_->OnActiveTabModeChanged(new_mode.is_ntp()); |
| 137 | } |
| 138 | |
| 139 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 140 | // BrowserInstantController, private: |
| 141 | |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 142 | void BrowserInstantController::ResetInstant() { |
[email protected] | b67d0a4 | 2012-09-04 20:57:35 | [diff] [blame] | 143 | instant_.reset( |
| 144 | !browser_shutdown::ShuttingDownWithoutClosingBrowsers() && |
| 145 | browser_->is_type_tabbed() ? |
| 146 | InstantController::CreateInstant(browser_->profile(), this) : NULL); |
[email protected] | e4e15dc | 2012-10-18 03:22:50 | [diff] [blame] | 147 | |
| 148 | // Notify any observers that they need to reset. |
| 149 | content::NotificationService::current()->Notify( |
| 150 | chrome::NOTIFICATION_BROWSER_INSTANT_RESET, |
| 151 | content::Source<BrowserInstantController>(this), |
| 152 | content::NotificationService::NoDetails()); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace chrome |