[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 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" |
[email protected] | fb8fdf1 | 2012-08-21 16:28:20 | [diff] [blame] | 9 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 10 | #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] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 15 | #include "chrome/browser/ui/search/search.h" |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 16 | #include "chrome/browser/ui/search/search_model.h" |
[email protected] | 9d3d1170 | 2012-11-08 01:01:12 | [diff] [blame] | 17 | #include "chrome/browser/ui/search/search_tab_helper.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 18 | #include "chrome/browser/ui/tab_contents/tab_contents.h" |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 19 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 20 | #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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 24 | #include "content/public/browser/web_contents.h" |
| 25 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 26 | namespace chrome { |
| 27 | |
| 28 | //////////////////////////////////////////////////////////////////////////////// |
| 29 | // BrowserInstantController, public: |
| 30 | |
| 31 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 32 | : browser_(browser), |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 33 | instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 34 | chrome::search::IsInstantExtendedAPIEnabled(browser->profile())), |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 35 | instant_unload_handler_(browser) { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 36 | profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); |
| 37 | profile_pref_registrar_.Add(prefs::kInstantEnabled, this); |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 38 | instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile())); |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 39 | browser_->search_model()->AddObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | BrowserInstantController::~BrowserInstantController() { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 43 | browser_->search_model()->RemoveObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 46 | bool BrowserInstantController::IsInstantEnabled(Profile* profile) { |
| 47 | return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && |
| 48 | profile->GetPrefs()->GetBoolean(prefs::kInstantEnabled); |
| 49 | } |
| 50 | |
| 51 | void 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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 58 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 59 | // Unsupported dispositions. |
| 60 | if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 61 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 62 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 63 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 64 | // 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] | 65 | // support for the new disposition. |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 66 | DCHECK(disposition == CURRENT_TAB || |
| 67 | disposition == NEW_FOREGROUND_TAB) << disposition; |
| 68 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 69 | return instant_.CommitIfCurrent(disposition == CURRENT_TAB ? |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 70 | INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 73 | void 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] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 78 | instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 79 | } else { |
[email protected] | 59253a65 | 2012-11-20 00:17:26 | [diff] [blame] | 80 | TabContents* active_tab = |
| 81 | browser_->tab_strip_model()->GetActiveTabContents(); |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 82 | 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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 88 | |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 89 | 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] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 98 | void BrowserInstantController::SetInstantSuggestion( |
| 99 | const InstantSuggestion& suggestion) { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 100 | if (browser_->window()->GetLocationBar()) |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 101 | browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | gfx::Rect BrowserInstantController::GetInstantBounds() { |
| 105 | return browser_->window()->GetInstantBounds(); |
| 106 | } |
| 107 | |
| 108 | void BrowserInstantController::InstantPreviewFocused() { |
| 109 | // NOTE: This is only invoked on aura. |
| 110 | browser_->window()->WebContentsFocused( |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 111 | instant_.GetPreviewContents()->web_contents()); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | c55e3b8 | 2012-08-09 15:27:05 | [diff] [blame] | 114 | TabContents* BrowserInstantController::GetActiveTabContents() const { |
[email protected] | 59253a65 | 2012-11-20 00:17:26 | [diff] [blame] | 115 | return browser_->tab_strip_model()->GetActiveTabContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 118 | void BrowserInstantController::ActiveTabChanged() { |
| 119 | instant_.ActiveTabChanged(); |
| 120 | } |
| 121 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 122 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 123 | // BrowserInstantController, PrefObserver implementation: |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 124 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 125 | void BrowserInstantController::OnPreferenceChanged( |
| 126 | PrefServiceBase* service, |
| 127 | const std::string& pref_name) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 128 | instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile())); |
[email protected] | 749ce88 | 2012-11-16 04:18:01 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 131 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 132 | // BrowserInstantController, search::SearchModelObserver implementation: |
| 133 | |
| 134 | void BrowserInstantController::ModeChanged(const search::Mode& old_mode, |
| 135 | const search::Mode& new_mode) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame^] | 136 | instant_.SearchModeChanged(old_mode, new_mode); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | } // namespace chrome |