[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 | |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 7 | #include "base/prefs/pref_service.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 26015ac | 2013-02-27 02:50:13 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_web_ui.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 11 | #include "chrome/browser/search/search.h" |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 12 | #include "chrome/browser/themes/theme_properties.h" |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 13 | #include "chrome/browser/themes/theme_service.h" |
| 14 | #include "chrome/browser/themes/theme_service_factory.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 16 | #include "chrome/browser/ui/browser_window.h" |
| 17 | #include "chrome/browser/ui/omnibox/location_bar.h" |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 18 | #include "chrome/browser/ui/omnibox/omnibox_view.h" |
[email protected] | 9d3d1170 | 2012-11-08 01:01:12 | [diff] [blame] | 19 | #include "chrome/browser/ui/search/search_tab_helper.h" |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 20 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 21 | #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
| 22 | #include "chrome/common/chrome_notification_types.h" |
| 23 | #include "chrome/common/pref_names.h" |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 24 | #include "chrome/common/url_constants.h" |
[email protected] | 75fee37 | 2013-03-06 00:42:44 | [diff] [blame] | 25 | #include "components/user_prefs/pref_registry_syncable.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 26 | #include "content/public/browser/notification_service.h" |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 27 | #include "content/public/browser/user_metrics.h" |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 28 | #include "grit/theme_resources.h" |
| 29 | #include "ui/gfx/color_utils.h" |
| 30 | #include "ui/gfx/sys_color_change_listener.h" |
| 31 | |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 32 | using content::UserMetricsAction; |
| 33 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 34 | //////////////////////////////////////////////////////////////////////////////// |
| 35 | // BrowserInstantController, public: |
| 36 | |
| 37 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 38 | : browser_(browser), |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 39 | instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame^] | 40 | chrome::IsInstantExtendedAPIEnabled()), |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 41 | instant_unload_handler_(browser), |
[email protected] | e4a379c | 2013-02-15 19:27:19 | [diff] [blame] | 42 | initialized_theme_info_(false) { |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 43 | |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 44 | // In one mode of the InstantExtended experiments, the kInstantExtendedEnabled |
| 45 | // preference's default value is set to the existing value of kInstantEnabled. |
| 46 | // Because this requires reading the value of the kInstantEnabled value, we |
| 47 | // reset the default for kInstantExtendedEnabled here. |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame^] | 48 | chrome::SetInstantExtendedPrefDefault(profile()); |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 49 | |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 50 | profile_pref_registrar_.Init(profile()->GetPrefs()); |
[email protected] | 0b3fa50 | 2012-11-21 13:57:46 | [diff] [blame] | 51 | profile_pref_registrar_.Add( |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 52 | prefs::kInstantEnabled, |
| 53 | base::Bind(&BrowserInstantController::ResetInstant, |
| 54 | base::Unretained(this))); |
| 55 | profile_pref_registrar_.Add( |
| 56 | prefs::kInstantExtendedEnabled, |
[email protected] | 0b3fa50 | 2012-11-21 13:57:46 | [diff] [blame] | 57 | base::Bind(&BrowserInstantController::ResetInstant, |
| 58 | base::Unretained(this))); |
[email protected] | 411c9c0 | 2013-02-07 04:55:59 | [diff] [blame] | 59 | profile_pref_registrar_.Add( |
| 60 | prefs::kSearchSuggestEnabled, |
| 61 | base::Bind(&BrowserInstantController::ResetInstant, |
| 62 | base::Unretained(this))); |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 63 | ResetInstant(std::string()); |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 64 | browser_->search_model()->AddObserver(this); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 65 | |
| 66 | #if defined(ENABLE_THEMES) |
| 67 | // Listen for theme installation. |
| 68 | registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 69 | content::Source<ThemeService>( |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 70 | ThemeServiceFactory::GetForProfile(profile()))); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 71 | #endif // defined(ENABLE_THEMES) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | BrowserInstantController::~BrowserInstantController() { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 75 | browser_->search_model()->RemoveObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 78 | bool BrowserInstantController::MaybeSwapInInstantNTPContents( |
| 79 | const GURL& url, |
| 80 | content::WebContents* source_contents, |
| 81 | content::WebContents** target_contents) { |
| 82 | if (url != GURL(chrome::kChromeUINewTabURL)) |
| 83 | return false; |
| 84 | |
[email protected] | 26015ac | 2013-02-27 02:50:13 | [diff] [blame] | 85 | GURL extension_url(url); |
| 86 | if (ExtensionWebUI::HandleChromeURLOverride(&extension_url, profile())) { |
| 87 | // If there is an extension overriding the NTP do not use the Instant NTP. |
| 88 | return false; |
| 89 | } |
| 90 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 91 | scoped_ptr<content::WebContents> instant_ntp = instant_.ReleaseNTPContents(); |
| 92 | if (!instant_ntp) |
| 93 | return false; |
| 94 | |
| 95 | *target_contents = instant_ntp.get(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 96 | if (source_contents) { |
| 97 | instant_ntp->GetController().CopyStateFromAndPrune( |
| 98 | &source_contents->GetController()); |
| 99 | ReplaceWebContentsAt( |
| 100 | browser_->tab_strip_model()->GetIndexOfWebContents(source_contents), |
| 101 | instant_ntp.Pass()); |
| 102 | } else { |
[email protected] | 57b58ca1 | 2013-02-26 23:41:25 | [diff] [blame] | 103 | instant_ntp->GetController().PruneAllButActive(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 104 | // If |source_contents| is NULL, then the caller is responsible for |
| 105 | // inserting instant_ntp into the tabstrip and will take ownership. |
| 106 | ignore_result(instant_ntp.release()); |
| 107 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 111 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 112 | // Unsupported dispositions. |
| 113 | if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 114 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 115 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 116 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 117 | // 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] | 118 | // support for the new disposition. |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 119 | DCHECK(disposition == CURRENT_TAB || |
| 120 | disposition == NEW_FOREGROUND_TAB) << disposition; |
| 121 | |
[email protected] | 221e920 | 2012-12-08 00:59:09 | [diff] [blame] | 122 | return instant_.CommitIfPossible(disposition == CURRENT_TAB ? |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 123 | INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 126 | Profile* BrowserInstantController::profile() const { |
| 127 | return browser_->profile(); |
| 128 | } |
| 129 | |
| 130 | void BrowserInstantController::CommitInstant( |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 131 | scoped_ptr<content::WebContents> overlay, |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 132 | bool in_new_tab) { |
[email protected] | 79f17ef46 | 2013-03-24 00:28:18 | [diff] [blame] | 133 | const extensions::Extension* extension = |
| 134 | profile()->GetExtensionService()->GetInstalledApp(overlay->GetURL()); |
| 135 | if (extension) { |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 136 | AppLauncherHandler::RecordAppLaunchType( |
[email protected] | 79f17ef46 | 2013-03-24 00:28:18 | [diff] [blame] | 137 | extension_misc::APP_LAUNCH_OMNIBOX_INSTANT, |
| 138 | extension->GetType()); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 139 | } |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 140 | if (in_new_tab) { |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 141 | // TabStripModel takes ownership of |overlay|. |
| 142 | browser_->tab_strip_model()->AddWebContents(overlay.release(), -1, |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 143 | instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 144 | } else { |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 145 | ReplaceWebContentsAt( |
| 146 | browser_->tab_strip_model()->active_index(), |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 147 | overlay.Pass()); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 151 | void BrowserInstantController::ReplaceWebContentsAt( |
| 152 | int index, |
| 153 | scoped_ptr<content::WebContents> new_contents) { |
| 154 | DCHECK_NE(TabStripModel::kNoTab, index); |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 155 | scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()-> |
| 156 | ReplaceWebContentsAt(index, new_contents.release())); |
| 157 | instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(), |
| 158 | index); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 159 | } |
| 160 | |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 161 | void BrowserInstantController::SetInstantSuggestion( |
| 162 | const InstantSuggestion& suggestion) { |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 163 | browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 727a6c4 | 2013-02-27 19:47:26 | [diff] [blame] | 166 | void BrowserInstantController::CommitSuggestedText( |
| 167 | bool skip_inline_autocomplete) { |
| 168 | browser_->window()->GetLocationBar()->GetLocationEntry()->model()-> |
| 169 | CommitSuggestedText(skip_inline_autocomplete); |
| 170 | } |
| 171 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 172 | gfx::Rect BrowserInstantController::GetInstantBounds() { |
| 173 | return browser_->window()->GetInstantBounds(); |
| 174 | } |
| 175 | |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 176 | void BrowserInstantController::InstantOverlayFocused() { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 177 | // NOTE: This is only invoked on aura. |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 178 | browser_->window()->WebContentsFocused(instant_.GetOverlayContents()); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | 10aec59 | 2013-03-06 20:19:00 | [diff] [blame] | 181 | void BrowserInstantController::FocusOmnibox(bool caret_visibility) { |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 182 | OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> |
| 183 | GetLocationEntry(); |
| 184 | omnibox_view->SetFocus(); |
[email protected] | 10aec59 | 2013-03-06 20:19:00 | [diff] [blame] | 185 | omnibox_view->model()->SetCaretVisibility(caret_visibility); |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 188 | content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 189 | return browser_->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 192 | void BrowserInstantController::ActiveTabChanged() { |
| 193 | instant_.ActiveTabChanged(); |
| 194 | } |
| 195 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 196 | void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 197 | instant_.TabDeactivated(contents); |
| 198 | } |
| 199 | |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 200 | void BrowserInstantController::UpdateThemeInfo() { |
[email protected] | e4a379c | 2013-02-15 19:27:19 | [diff] [blame] | 201 | // Update theme background info. |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 202 | // Initialize |theme_info| if necessary. |
| 203 | if (!initialized_theme_info_) |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 204 | OnThemeChanged(ThemeServiceFactory::GetForProfile(profile())); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 205 | else |
| 206 | OnThemeChanged(NULL); |
| 207 | } |
| 208 | |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 209 | void BrowserInstantController::OpenURL( |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 210 | const GURL& url, |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 211 | content::PageTransition transition, |
| 212 | WindowOpenDisposition disposition) { |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 213 | browser_->OpenURL(content::OpenURLParams(url, |
| 214 | content::Referrer(), |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 215 | disposition, |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 216 | transition, |
| 217 | false)); |
| 218 | } |
| 219 | |
[email protected] | fcde79a | 2013-02-28 02:25:09 | [diff] [blame] | 220 | void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) { |
| 221 | instant_.SetOmniboxBounds(bounds); |
[email protected] | ec4aad54 | 2012-12-14 01:11:04 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 224 | void BrowserInstantController::ResetInstant(const std::string& pref_name) { |
| 225 | // Update the default value of the kInstantExtendedEnabled pref to match the |
| 226 | // value of the kInstantEnabled pref, if necessary. |
| 227 | if (pref_name == prefs::kInstantEnabled) |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame^] | 228 | chrome::SetInstantExtendedPrefDefault(profile()); |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 229 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame^] | 230 | bool instant_pref_enabled = chrome::IsInstantPrefEnabled(profile()); |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 231 | bool use_local_overlay_only = profile()->IsOffTheRecord() || |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 232 | (!instant_pref_enabled && |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 233 | !profile()->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)); |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 234 | instant_.SetInstantEnabled(instant_pref_enabled, use_local_overlay_only); |
[email protected] | 749ce88 | 2012-11-16 04:18:01 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 237 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame^] | 238 | // BrowserInstantController, SearchModelObserver implementation: |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 239 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 240 | void BrowserInstantController::ModelChanged( |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame^] | 241 | const SearchModel::State& old_state, |
| 242 | const SearchModel::State& new_state) { |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 243 | if (old_state.mode == new_state.mode) |
| 244 | return; |
| 245 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame^] | 246 | const SearchMode& new_mode = new_state.mode; |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 247 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame^] | 248 | if (chrome::IsInstantExtendedAPIEnabled()) { |
[email protected] | c19ba104 | 2013-03-11 17:17:13 | [diff] [blame] | 249 | // Record some actions corresponding to the mode change. Note that to get |
| 250 | // the full story, it's necessary to look at other UMA actions as well, |
| 251 | // such as tab switches. |
| 252 | if (new_mode.is_search_results()) |
| 253 | content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP")); |
| 254 | else if (new_mode.is_ntp()) |
| 255 | content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); |
| 256 | } |
| 257 | |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 258 | // If mode is now |NTP|, send theme-related information to Instant. |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 259 | if (new_mode.is_ntp()) |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 260 | UpdateThemeInfo(); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 261 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 262 | instant_.SearchModeChanged(old_state.mode, new_mode); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 263 | } |
| 264 | |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 265 | //////////////////////////////////////////////////////////////////////////////// |
| 266 | // BrowserInstantController, content::NotificationObserver implementation: |
| 267 | |
| 268 | void BrowserInstantController::Observe( |
| 269 | int type, |
| 270 | const content::NotificationSource& source, |
| 271 | const content::NotificationDetails& details) { |
| 272 | #if defined(ENABLE_THEMES) |
| 273 | DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
| 274 | OnThemeChanged(content::Source<ThemeService>(source).ptr()); |
| 275 | #endif // defined(ENABLE_THEMES) |
| 276 | } |
| 277 | |
| 278 | void BrowserInstantController::OnThemeChanged(ThemeService* theme_service) { |
| 279 | if (theme_service) { // Get theme information from theme service. |
| 280 | theme_info_ = ThemeBackgroundInfo(); |
| 281 | |
| 282 | // Set theme background color. |
| 283 | SkColor background_color = |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 284 | theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 285 | if (gfx::IsInvertedColorScheme()) |
| 286 | background_color = color_utils::InvertColor(background_color); |
| 287 | theme_info_.color_r = SkColorGetR(background_color); |
| 288 | theme_info_.color_g = SkColorGetG(background_color); |
| 289 | theme_info_.color_b = SkColorGetB(background_color); |
| 290 | theme_info_.color_a = SkColorGetA(background_color); |
| 291 | |
| 292 | if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { |
| 293 | // Set theme id for theme background image url. |
| 294 | theme_info_.theme_id = theme_service->GetThemeID(); |
| 295 | |
| 296 | // Set theme background image horizontal alignment. |
| 297 | int alignment = 0; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 298 | theme_service->GetDisplayProperty( |
| 299 | ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &alignment); |
| 300 | if (alignment & ThemeProperties::ALIGN_LEFT) { |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 301 | theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 302 | } else if (alignment & ThemeProperties::ALIGN_RIGHT) { |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 303 | theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT; |
| 304 | } else { // ALIGN_CENTER |
| 305 | theme_info_.image_horizontal_alignment = |
| 306 | THEME_BKGRND_IMAGE_ALIGN_CENTER; |
| 307 | } |
| 308 | |
| 309 | // Set theme background image vertical alignment. |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 310 | if (alignment & ThemeProperties::ALIGN_TOP) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 311 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP; |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 312 | else if (alignment & ThemeProperties::ALIGN_BOTTOM) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 313 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM; |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 314 | else // ALIGN_CENTER |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 315 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER; |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 316 | |
| 317 | // Set theme background image tiling. |
| 318 | int tiling = 0; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 319 | theme_service->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING, |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 320 | &tiling); |
| 321 | switch (tiling) { |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 322 | case ThemeProperties::NO_REPEAT: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 323 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT; |
| 324 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 325 | case ThemeProperties::REPEAT_X: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 326 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X; |
| 327 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 328 | case ThemeProperties::REPEAT_Y: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 329 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y; |
| 330 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 331 | case ThemeProperties::REPEAT: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 332 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT; |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | // Set theme background image height. |
| 337 | gfx::ImageSkia* image = theme_service->GetImageSkiaNamed( |
| 338 | IDR_THEME_NTP_BACKGROUND); |
| 339 | DCHECK(image); |
| 340 | theme_info_.image_height = image->height(); |
[email protected] | 5189fa1 | 2013-03-20 23:00:05 | [diff] [blame] | 341 | |
| 342 | theme_info_.has_attribution = |
| 343 | theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | initialized_theme_info_ = true; |
| 347 | } |
| 348 | |
| 349 | DCHECK(initialized_theme_info_); |
| 350 | |
[email protected] | e4a379c | 2013-02-15 19:27:19 | [diff] [blame] | 351 | if (browser_->search_model()->mode().is_ntp()) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 352 | instant_.ThemeChanged(theme_info_); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 353 | } |