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

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: removed extra dispatch of theme area height on init 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)";
sky 2012/11/19 15:07:31 nit: indent 2 more.
kuan 2012/11/19 16:59:41 Done.
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 background info and theme area height.
152 // |theme_info_.color_rgba| must always be filled, so if it's empty,
153 // |theme_info_| hasn't been initialized.
154 // |OnThemeChanged| also updates theme area height.
155 if (theme_info_.color_rgba.length() == 0)
156 OnThemeChanged(ThemeServiceFactory::GetForProfile(browser_->profile()));
157 else
158 OnThemeChanged(NULL);
159 }
160
122 //////////////////////////////////////////////////////////////////////////////// 161 ////////////////////////////////////////////////////////////////////////////////
123 // BrowserInstantController, PrefObserver implementation: 162 // BrowserInstantController, PrefObserver implementation:
124 163
125 void BrowserInstantController::OnPreferenceChanged( 164 void BrowserInstantController::OnPreferenceChanged(
126 PrefServiceBase* service, 165 PrefServiceBase* service,
127 const std::string& pref_name) { 166 const std::string& pref_name) {
128 DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name); 167 DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name);
129 ResetInstant(); 168 ResetInstant();
130 } 169 }
131 170
(...skipping 13 matching lines...) Expand all
145 if (!(old_is_ntp && new_is_ntp)) 184 if (!(old_is_ntp && new_is_ntp))
146 instant()->Hide(); 185 instant()->Hide();
147 } 186 }
148 } 187 }
149 188
150 //////////////////////////////////////////////////////////////////////////////// 189 ////////////////////////////////////////////////////////////////////////////////
151 // BrowserInstantController, search::SearchModelObserver implementation: 190 // BrowserInstantController, search::SearchModelObserver implementation:
152 191
153 void BrowserInstantController::ModeChanged(const search::Mode& old_mode, 192 void BrowserInstantController::ModeChanged(const search::Mode& old_mode,
154 const search::Mode& new_mode) { 193 const search::Mode& new_mode) {
155 if (instant()) 194 if (instant()) {
156 instant_->OnActiveTabModeChanged(new_mode); 195 instant_->OnActiveTabModeChanged(new_mode);
196
197 // If mode is now |NTP|, send theme-related information to instant.
198 if (new_mode.is_ntp())
199 OnThemeChanged(NULL);
200 }
157 } 201 }
158 202
159 //////////////////////////////////////////////////////////////////////////////// 203 ////////////////////////////////////////////////////////////////////////////////
160 // BrowserInstantController, private: 204 // BrowserInstantController, private:
161 205
206 void BrowserInstantController::Observe(
207 int type,
208 const content::NotificationSource& source,
209 const content::NotificationDetails& details) {
210 #if defined(ENABLE_THEMES)
211 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
212 OnThemeChanged(content::Source<ThemeService>(source).ptr());
213 #endif // defined(ENABLE_THEMES)
214 }
215
162 void BrowserInstantController::ResetInstant() { 216 void BrowserInstantController::ResetInstant() {
163 instant_.reset( 217 instant_.reset(
164 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() && 218 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() &&
165 browser_->is_type_tabbed() ? 219 browser_->is_type_tabbed() ?
166 InstantController::CreateInstant(browser_->profile(), this) : NULL); 220 InstantController::CreateInstant(browser_->profile(), this) : NULL);
167 221
168 // Notify any observers that they need to reset. 222 // Notify any observers that they need to reset.
169 content::NotificationService::current()->Notify( 223 content::NotificationService::current()->Notify(
170 chrome::NOTIFICATION_BROWSER_INSTANT_RESET, 224 chrome::NOTIFICATION_BROWSER_INSTANT_RESET,
171 content::Source<BrowserInstantController>(this), 225 content::Source<BrowserInstantController>(this),
172 content::NotificationService::NoDetails()); 226 content::NotificationService::NoDetails());
173 } 227 }
174 228
229 void BrowserInstantController::OnThemeChanged(
230 ui::ThemeProvider* theme_provider) {
231 if (theme_provider) { // Get theme information from theme provider.
232 theme_info_ = ThemeBackgroundInfo();
233 theme_image_alignment_ = 0;
234
235 // Set theme background color.
236 SkColor background_color =
237 theme_provider->GetColor(ThemeService::COLOR_NTP_BACKGROUND);
238 if (gfx::IsInvertedColorScheme())
239 background_color = color_utils::InvertColor(background_color);
240 theme_info_.color_rgba = UTF8ToUTF16(
241 // Convert the alpha using DoubleToString because StringPrintf will use
242 // locale specific formatters (e.g., use , instead of . in German).
243 base::StringPrintf(
244 kCSSBackgroundColorFormat,
245 SkColorGetR(background_color),
246 SkColorGetG(background_color),
247 SkColorGetB(background_color),
248 base::DoubleToString(SkColorGetA(background_color) / 255.0).
249 c_str()));
250
251 if (theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
252 // Set theme background image url.
253 theme_info_.image_url = UTF8ToUTF16(
254 base::StringPrintf(kCSSBackgroundImageFormat,
255 browser_->profile()->GetPrefs()->GetString(
256 prefs::kCurrentThemeID).c_str()));
257
258 // Set theme background image horizontal alignment.
259 theme_provider->GetDisplayProperty(
260 ThemeService::NTP_BACKGROUND_ALIGNMENT, &theme_image_alignment_);
261 std::string alignment;
262 if (theme_image_alignment_ & ThemeService::ALIGN_LEFT)
263 alignment = ThemeService::kAlignmentLeft;
264 else if (theme_image_alignment_& ThemeService::ALIGN_RIGHT)
265 alignment = ThemeService::kAlignmentRight;
266 else // ALIGN_CENTER
267 alignment = ThemeService::kAlignmentCenter;
268 theme_info_.image_horizontal_alignment = UTF8ToUTF16(alignment);
269
270 // Set theme background image vertical alignment.
271 if (theme_image_alignment_& ThemeService::ALIGN_TOP)
272 alignment = ThemeService::kAlignmentTop;
273 else if (theme_image_alignment_& ThemeService::ALIGN_BOTTOM)
274 alignment = ThemeService::kAlignmentBottom;
275 else // ALIGN_CENTER
276 alignment = ThemeService::kAlignmentCenter;
277 theme_info_.image_vertical_alignment = UTF8ToUTF16(alignment);
278
279 // Set tiling of theme background image.
280 int repeat_mode = 0;
281 theme_provider->GetDisplayProperty(
282 ThemeService::NTP_BACKGROUND_TILING, &repeat_mode);
283 theme_info_.image_tiling = UTF8ToUTF16(
284 ThemeService::TilingToString(repeat_mode));
285
286 // Set theme background image height.
287 gfx::ImageSkia* image = theme_provider->GetImageSkiaNamed(
288 IDR_THEME_NTP_BACKGROUND);
289 DCHECK(image);
290 theme_info_.image_height = image->height();
291 }
292 }
293
294 if (instant() && browser_->search_model()->mode().is_ntp()) {
295 instant()->OnThemeChanged(theme_info_);
296
297 // Theme area height is only sent to preview for non-top-aligned images;
298 // new theme may have a different alignment that requires preview to know
299 // theme area height.
300 OnThemeAreaHeightChanged(theme_area_height_);
301 }
302 }
303
304 void BrowserInstantController::OnThemeAreaHeightChanged(int height) {
305 theme_area_height_ = height;
306
307 // Notify preview if mode is |NTP| and theme background image is not top-
308 // aligned; top-aligned images don't need theme area height to determine which
309 // part of the image overlay should draw, 'cos the origin is top-left.
310 bool notify = instant() && browser_->search_model()->mode().is_ntp() &&
311 theme_info_.image_url.length() > 0;
312 if (!notify)
313 return;
314 if (theme_image_alignment_ & ThemeService::ALIGN_TOP)
315 notify = false;
316 else if (theme_image_alignment_ & ThemeService::ALIGN_BOTTOM)
317 notify = true;
318 else // ALIGN_CENTER
319 notify = true;
320 if (notify)
321 instant()->OnThemeAreaHeightChanged(theme_area_height_);
322 }
323
175 } // namespace chrome 324 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698