Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/browser/ui/browser_instant_controller.cc

Issue 11413018: alternate ntp: implement searchbox api for instant overlay to adopt themes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed to only send ht change when there's theme image Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/browser_instant_controller.h" 5 #include "chrome/browser/ui/browser_instant_controller.h"
6 6
7 #include "base/string_number_conversions.h"
8 #include "base/stringprintf.h"
9 #include "base/utf_string_conversions.h"
7 #include "chrome/browser/browser_shutdown.h" 10 #include "chrome/browser/browser_shutdown.h"
8 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/instant/instant_controller.h" 12 #include "chrome/browser/instant/instant_controller.h"
10 #include "chrome/browser/prefs/pref_service.h" 13 #include "chrome/browser/prefs/pref_service.h"
11 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/themes/theme_service.h"
16 #include "chrome/browser/themes/theme_service_factory.h"
12 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_tabstrip.h" 18 #include "chrome/browser/ui/browser_tabstrip.h"
14 #include "chrome/browser/ui/browser_window.h" 19 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/omnibox/location_bar.h" 20 #include "chrome/browser/ui/omnibox/location_bar.h"
16 #include "chrome/browser/ui/search/search_model.h" 21 #include "chrome/browser/ui/search/search_model.h"
17 #include "chrome/browser/ui/search/search_tab_helper.h" 22 #include "chrome/browser/ui/search/search_tab_helper.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/browser/ui/tab_contents/tab_contents.h" 24 #include "chrome/browser/ui/tab_contents/tab_contents.h"
20 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" 25 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
21 #include "chrome/common/chrome_notification_types.h" 26 #include "chrome/common/chrome_notification_types.h"
22 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
23 #include "content/public/browser/notification_service.h" 28 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/web_contents.h" 29 #include "content/public/browser/web_contents.h"
30 #include "grit/theme_resources.h"
31 #include "ui/base/theme_provider.h"
32 #include "ui/gfx/color_utils.h"
33 #include "ui/gfx/sys_color_change_listener.h"
34
25 35
26 namespace { 36 namespace {
27 37
38 const char kCSSBackgroundImageFormat[] =
39 "-webkit-image-set(url(chrome://theme/IDR_THEME_BACKGROUND?%s) 1x)";
40
41 const char kCSSBackgroundColorFormat[] = "rgba(%d,%d,%d,%s)";
42
28 // Returns true iff the search model for |tab| is in an NTP state, that is, a 43 // Returns true iff the search model for |tab| is in an NTP state, that is, a
29 // state in which the Instant overlay may be showing custom NTP content in 44 // state in which the Instant overlay may be showing custom NTP content in
30 // EXTENDED mode. 45 // EXTENDED mode.
31 bool IsSearchModelNTP(content::WebContents* tab) { 46 bool IsSearchModelNTP(content::WebContents* tab) {
32 if (!tab) 47 if (!tab)
33 return false; 48 return false;
34 chrome::search::SearchModel* model = 49 chrome::search::SearchModel* model =
35 chrome::search::SearchTabHelper::FromWebContents(tab)->model(); 50 chrome::search::SearchTabHelper::FromWebContents(tab)->model();
36 return model && model->mode().is_ntp(); 51 return model && model->mode().is_ntp();
37 } 52 }
38 53
39 } // namespace 54 } // namespace
40 55
41 namespace chrome { 56 namespace chrome {
42 57
43 //////////////////////////////////////////////////////////////////////////////// 58 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserInstantController, public: 59 // BrowserInstantController, public:
45 60
46 BrowserInstantController::BrowserInstantController(Browser* browser) 61 BrowserInstantController::BrowserInstantController(Browser* browser)
47 : browser_(browser), 62 : browser_(browser),
48 instant_unload_handler_(browser) { 63 instant_unload_handler_(browser),
64 theme_image_alignment_(0),
65 theme_area_height_(0) {
49 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); 66 profile_pref_registrar_.Init(browser_->profile()->GetPrefs());
50 profile_pref_registrar_.Add(prefs::kInstantEnabled, this); 67 profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
51 ResetInstant(); 68 ResetInstant();
52 browser_->tab_strip_model()->AddObserver(this); 69 browser_->tab_strip_model()->AddObserver(this);
53 browser_->search_model()->AddObserver(this); 70 browser_->search_model()->AddObserver(this);
71
72 #if defined(ENABLE_THEMES)
73 // Listen for theme installation.
74 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
75 content::Source<ThemeService>(
76 ThemeServiceFactory::GetForProfile(browser_->profile())));
77 #endif // defined(ENABLE_THEMES)
54 } 78 }
55 79
56 BrowserInstantController::~BrowserInstantController() { 80 BrowserInstantController::~BrowserInstantController() {
57 browser_->tab_strip_model()->RemoveObserver(this); 81 browser_->tab_strip_model()->RemoveObserver(this);
58 browser_->search_model()->RemoveObserver(this); 82 browser_->search_model()->RemoveObserver(this);
59 } 83 }
60 84
61 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { 85 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) {
62 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt 86 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt
63 // to use the Instant preview. 87 // to use the Instant preview.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 void BrowserInstantController::InstantPreviewFocused() { 136 void BrowserInstantController::InstantPreviewFocused() {
113 // NOTE: This is only invoked on aura. 137 // NOTE: This is only invoked on aura.
114 browser_->window()->WebContentsFocused( 138 browser_->window()->WebContentsFocused(
115 instant_->GetPreviewContents()->web_contents()); 139 instant_->GetPreviewContents()->web_contents());
116 } 140 }
117 141
118 TabContents* BrowserInstantController::GetActiveTabContents() const { 142 TabContents* BrowserInstantController::GetActiveTabContents() const {
119 return chrome::GetActiveTabContents(browser_); 143 return chrome::GetActiveTabContents(browser_);
120 } 144 }
121 145
146 void BrowserInstantController::SetContentHeight(int height) {
147 OnThemeAreaHeightChanged(height);
148 }
149
150 void BrowserInstantController::UpdateThemeRelatedInfoForPreview() {
151 // Update theme info.
152 // |theme_info_.color_rgba| must always be filled, so if it's empty,
153 // |theme_info_| hasn't been initialized.
154 if (theme_info_.color_rgba.length() == 0)
155 OnThemeChanged(ThemeServiceFactory::GetForProfile(browser_->profile()));
156 else
157 instant_->OnThemeChanged(theme_info_);
158
159 // Update theme area height.
160 OnThemeAreaHeightChanged(theme_area_height_);
161 }
162
122 //////////////////////////////////////////////////////////////////////////////// 163 ////////////////////////////////////////////////////////////////////////////////
123 // BrowserInstantController, PrefObserver implementation: 164 // BrowserInstantController, PrefObserver implementation:
124 165
125 void BrowserInstantController::OnPreferenceChanged( 166 void BrowserInstantController::OnPreferenceChanged(
126 PrefServiceBase* service, 167 PrefServiceBase* service,
127 const std::string& pref_name) { 168 const std::string& pref_name) {
128 DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name); 169 DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name);
129 ResetInstant(); 170 ResetInstant();
130 } 171 }
131 172
(...skipping 13 matching lines...) Expand all
145 if (!(old_is_ntp && new_is_ntp)) 186 if (!(old_is_ntp && new_is_ntp))
146 instant()->Hide(); 187 instant()->Hide();
147 } 188 }
148 } 189 }
149 190
150 //////////////////////////////////////////////////////////////////////////////// 191 ////////////////////////////////////////////////////////////////////////////////
151 // BrowserInstantController, search::SearchModelObserver implementation: 192 // BrowserInstantController, search::SearchModelObserver implementation:
152 193
153 void BrowserInstantController::ModeChanged(const search::Mode& old_mode, 194 void BrowserInstantController::ModeChanged(const search::Mode& old_mode,
154 const search::Mode& new_mode) { 195 const search::Mode& new_mode) {
155 if (instant() && old_mode.is_ntp() != new_mode.is_ntp()) 196 if (instant() && old_mode.is_ntp() != new_mode.is_ntp()) {
156 instant_->OnActiveTabModeChanged(new_mode.is_ntp()); 197 instant_->OnActiveTabModeChanged(new_mode.is_ntp());
198
199 // If mode is now |NTP|, send theme-related information to instant.
200 if (new_mode.is_ntp()) {
201 instant_->OnThemeChanged(theme_info_);
202 OnThemeAreaHeightChanged(theme_area_height_);
203 }
204 }
157 } 205 }
158 206
159 //////////////////////////////////////////////////////////////////////////////// 207 ////////////////////////////////////////////////////////////////////////////////
160 // BrowserInstantController, private: 208 // BrowserInstantController, private:
161 209
210 void BrowserInstantController::Observe(
211 int type,
212 const content::NotificationSource& source,
213 const content::NotificationDetails& details) {
214 #if defined(ENABLE_THEMES)
215 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
216 OnThemeChanged(content::Source<ThemeService>(source).ptr());
217 #endif // defined(ENABLE_THEMES)
218 }
219
162 void BrowserInstantController::ResetInstant() { 220 void BrowserInstantController::ResetInstant() {
163 instant_.reset( 221 instant_.reset(
164 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() && 222 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() &&
165 browser_->is_type_tabbed() ? 223 browser_->is_type_tabbed() ?
166 InstantController::CreateInstant(browser_->profile(), this) : NULL); 224 InstantController::CreateInstant(browser_->profile(), this) : NULL);
167 225
168 // Notify any observers that they need to reset. 226 // Notify any observers that they need to reset.
169 content::NotificationService::current()->Notify( 227 content::NotificationService::current()->Notify(
170 chrome::NOTIFICATION_BROWSER_INSTANT_RESET, 228 chrome::NOTIFICATION_BROWSER_INSTANT_RESET,
171 content::Source<BrowserInstantController>(this), 229 content::Source<BrowserInstantController>(this),
172 content::NotificationService::NoDetails()); 230 content::NotificationService::NoDetails());
173 } 231 }
174 232
233 void BrowserInstantController::OnThemeChanged(
234 ui::ThemeProvider* theme_provider) {
235 theme_info_ = ThemeBackgroundInfo();
236 theme_image_alignment_ = 0;
237
238 // Set theme background color.
239 SkColor background_color =
240 theme_provider->GetColor(ThemeService::COLOR_NTP_BACKGROUND);
241 if (gfx::IsInvertedColorScheme())
242 background_color = color_utils::InvertColor(background_color);
243 theme_info_.color_rgba = UTF8ToUTF16(
244 // Convert the alpha using DoubleToString because StringPrintf will use
245 // locale specific formatters (e.g., use , instead of . in German).
246 base::StringPrintf(
247 kCSSBackgroundColorFormat,
248 SkColorGetR(background_color),
249 SkColorGetG(background_color),
250 SkColorGetB(background_color),
251 base::DoubleToString(SkColorGetA(background_color) / 255.0).c_str()));
252
253 if (theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
254 // Set theme background image url.
255 theme_info_.image_url = UTF8ToUTF16(
256 base::StringPrintf(kCSSBackgroundImageFormat,
257 browser_->profile()->GetPrefs()->GetString(
258 prefs::kCurrentThemeID).c_str()));
259
260 // Set theme background image horizontal alignment.
261 theme_provider->GetDisplayProperty(
262 ThemeService::NTP_BACKGROUND_ALIGNMENT, &theme_image_alignment_);
263 std::string alignment;
264 if (theme_image_alignment_ & ThemeService::ALIGN_LEFT)
265 alignment = ThemeService::kAlignmentLeft;
266 else if (theme_image_alignment_& ThemeService::ALIGN_RIGHT)
267 alignment = ThemeService::kAlignmentRight;
268 else // ALIGN_CENTER
269 alignment = ThemeService::kAlignmentCenter;
270 theme_info_.image_horizontal_alignment = UTF8ToUTF16(alignment);
271
272 // Set theme background image vertical alignment.
273 if (theme_image_alignment_& ThemeService::ALIGN_TOP)
274 alignment = ThemeService::kAlignmentTop;
275 else if (theme_image_alignment_& ThemeService::ALIGN_BOTTOM)
276 alignment = ThemeService::kAlignmentBottom;
277 else // ALIGN_CENTER
278 alignment = ThemeService::kAlignmentCenter;
279 theme_info_.image_vertical_alignment = UTF8ToUTF16(alignment);
280
281 // Set tiling of theme background image.
282 int repeat_mode = 0;
283 theme_provider->GetDisplayProperty(
284 ThemeService::NTP_BACKGROUND_TILING, &repeat_mode);
285 theme_info_.image_tiling = UTF8ToUTF16(
286 ThemeService::TilingToString(repeat_mode));
287 }
288
289 if (instant() && browser_->search_model()->mode().is_ntp()) {
290 instant()->OnThemeChanged(theme_info_);
291
292 // Theme area height is only sent to preview for non-top-aligned images;
293 // new theme may have a different alignment that requires preview to know
294 // theme area height.
295 OnThemeAreaHeightChanged(theme_area_height_);
296 }
297 }
298
299 void BrowserInstantController::OnThemeAreaHeightChanged(int height) {
300 theme_area_height_ = height;
301
302 // Notify preview if mode is |NTP| and theme background image is not top-
303 // aligned; top-aligned images don't need theme area height to determine which
304 // part of the image overlay should draw, 'cos the origin is top-left.
305
306 if (!(instant() && browser_->search_model()->mode().is_ntp() &&
307 theme_info_.image_url.length() > 0)) {
308 return;
309 }
310
311 bool notify = false;
312 if (theme_image_alignment_ & ThemeService::ALIGN_TOP)
313 notify = false;
314 else if (theme_image_alignment_ & ThemeService::ALIGN_BOTTOM)
315 notify = true;
316 else // ALIGN_CENTER
317 notify = true;
318 if (notify)
319 instant()->OnThemeAreaHeightChanged(theme_area_height_);
320 }
321
175 } // namespace chrome 322 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698