OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 7 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 | 10 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 } // namespace | 186 } // namespace |
187 | 187 |
188 /////////////////////////////////////////////////////////////////////////////// | 188 /////////////////////////////////////////////////////////////////////////////// |
189 // NewTabUI | 189 // NewTabUI |
190 | 190 |
191 NewTabUI::NewTabUI(TabContents* contents) | 191 NewTabUI::NewTabUI(TabContents* contents) |
192 : ChromeWebUI(contents) { | 192 : ChromeWebUI(contents) { |
193 // Override some options on the Web UI. | 193 // Override some options on the Web UI. |
194 hide_favicon_ = true; | 194 hide_favicon_ = true; |
195 | 195 |
196 if (!NTP4Enabled() && | |
197 GetProfile()->GetPrefs()->GetBoolean(prefs::kEnableBookmarkBar) && | |
198 browser_defaults::bookmarks_enabled) { | |
199 set_force_bookmark_bar_visible(true); | |
200 } | |
201 | |
202 focus_location_bar_by_default_ = true; | 196 focus_location_bar_by_default_ = true; |
203 should_hide_url_ = true; | 197 should_hide_url_ = true; |
204 overridden_title_ = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE); | 198 overridden_title_ = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE); |
205 | 199 |
206 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more | 200 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more |
207 // highly. Note this means we're including clicks on not only most visited | 201 // highly. Note this means we're including clicks on not only most visited |
208 // thumbnails, but also clicks on recently bookmarked. | 202 // thumbnails, but also clicks on recently bookmarked. |
209 link_transition_type_ = PageTransition::AUTO_BOOKMARK; | 203 link_transition_type_ = PageTransition::AUTO_BOOKMARK; |
210 | 204 |
211 if (!GetProfile()->IsOffTheRecord()) { | 205 if (!GetProfile()->IsOffTheRecord()) { |
212 PrefService* pref_service = GetProfile()->GetPrefs(); | 206 PrefService* pref_service = GetProfile()->GetPrefs(); |
213 if (!NewTabSyncSetupHandler::ShouldShowSyncPromo()) | 207 if (!NewTabSyncSetupHandler::ShouldShowSyncPromo()) |
214 AddMessageHandler((new NTPLoginHandler())->Attach(this)); | 208 AddMessageHandler((new NTPLoginHandler())->Attach(this)); |
215 AddMessageHandler((new ShownSectionsHandler(pref_service))->Attach(this)); | 209 AddMessageHandler((new ShownSectionsHandler(pref_service))->Attach(this)); |
216 AddMessageHandler((new browser_sync::ForeignSessionHandler())-> | 210 AddMessageHandler((new browser_sync::ForeignSessionHandler())-> |
217 Attach(this)); | 211 Attach(this)); |
218 AddMessageHandler((new MostVisitedHandler())->Attach(this)); | 212 AddMessageHandler((new MostVisitedHandler())->Attach(this)); |
219 AddMessageHandler((new RecentlyClosedTabsHandler())->Attach(this)); | 213 AddMessageHandler((new RecentlyClosedTabsHandler())->Attach(this)); |
220 AddMessageHandler((new MetricsHandler())->Attach(this)); | 214 AddMessageHandler((new MetricsHandler())->Attach(this)); |
221 if (GetProfile()->IsSyncAccessible()) | 215 if (GetProfile()->IsSyncAccessible()) |
222 AddMessageHandler((new NewTabPageSyncHandler())->Attach(this)); | 216 AddMessageHandler((new NewTabPageSyncHandler())->Attach(this)); |
223 ExtensionService* service = GetProfile()->GetExtensionService(); | 217 ExtensionService* service = GetProfile()->GetExtensionService(); |
224 // We might not have an ExtensionService (on ChromeOS when not logged in | 218 // We might not have an ExtensionService (on ChromeOS when not logged in |
225 // for example). | 219 // for example). |
226 if (service) | 220 if (service) |
227 AddMessageHandler((new AppLauncherHandler(service))->Attach(this)); | 221 AddMessageHandler((new AppLauncherHandler(service))->Attach(this)); |
228 | 222 |
229 AddMessageHandler((new NewTabPageHandler())->Attach(this)); | 223 AddMessageHandler((new NewTabPageHandler())->Attach(this)); |
230 if (NTP4Enabled()) { | 224 AddMessageHandler((new BookmarksHandler())->Attach(this)); |
231 AddMessageHandler((new BookmarksHandler())->Attach(this)); | 225 AddMessageHandler((new FaviconWebUIHandler())->Attach(this)); |
232 AddMessageHandler((new FaviconWebUIHandler())->Attach(this)); | |
233 } | |
234 } | 226 } |
235 | 227 |
236 // Add the sync setup handler for the sync promo UI. | 228 // Add the sync setup handler for the sync promo UI. |
237 scoped_ptr<SyncSetupHandler> handler(new NewTabSyncSetupHandler()); | 229 scoped_ptr<SyncSetupHandler> handler(new NewTabSyncSetupHandler()); |
238 AddMessageHandler(handler.release()->Attach(this)); | 230 AddMessageHandler(handler.release()->Attach(this)); |
239 | 231 |
240 // Initializing the CSS and HTML can require some CPU, so do it after | 232 // Initializing the CSS and HTML can require some CPU, so do it after |
241 // we've hooked up the most visited handler. This allows the DB query | 233 // we've hooked up the most visited handler. This allows the DB query |
242 // for the new tab thumbs to happen earlier. | 234 // for the new tab thumbs to happen earlier. |
243 InitializeCSSCaches(); | 235 InitializeCSSCaches(); |
244 NewTabHTMLSource* html_source = | 236 NewTabHTMLSource* html_source = |
245 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); | 237 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); |
246 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 238 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); |
247 profile->GetChromeURLDataManager()->AddDataSource(html_source); | 239 profile->GetChromeURLDataManager()->AddDataSource(html_source); |
248 | 240 |
249 // Listen for theme installation. | 241 // Listen for theme installation. |
250 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 242 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
251 Source<ThemeService>( | 243 Source<ThemeService>( |
252 ThemeServiceFactory::GetForProfile(GetProfile()))); | 244 ThemeServiceFactory::GetForProfile(GetProfile()))); |
253 // Listen for bookmark bar visibility changes. | |
254 registrar_.Add(this, | |
255 chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, | |
256 NotificationService::AllSources()); | |
257 } | 245 } |
258 | 246 |
259 NewTabUI::~NewTabUI() { | 247 NewTabUI::~NewTabUI() { |
260 } | 248 } |
261 | 249 |
262 // The timer callback. If enough time has elapsed since the last paint | 250 // The timer callback. If enough time has elapsed since the last paint |
263 // message, we say we're done painting; otherwise, we keep waiting. | 251 // message, we say we're done painting; otherwise, we keep waiting. |
264 void NewTabUI::PaintTimeout() { | 252 void NewTabUI::PaintTimeout() { |
265 // The amount of time there must be no painting for us to consider painting | 253 // The amount of time there must be no painting for us to consider painting |
266 // finished. Observed times are in the ~1200ms range on Windows. | 254 // finished. Observed times are in the ~1200ms range on Windows. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { | 295 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { |
308 InitializeCSSCaches(); | 296 InitializeCSSCaches(); |
309 ListValue args; | 297 ListValue args; |
310 args.Append(Value::CreateStringValue( | 298 args.Append(Value::CreateStringValue( |
311 ThemeServiceFactory::GetForProfile(GetProfile())->HasCustomImage( | 299 ThemeServiceFactory::GetForProfile(GetProfile())->HasCustomImage( |
312 IDR_THEME_NTP_ATTRIBUTION) ? | 300 IDR_THEME_NTP_ATTRIBUTION) ? |
313 "true" : "false")); | 301 "true" : "false")); |
314 CallJavascriptFunction("themeChanged", args); | 302 CallJavascriptFunction("themeChanged", args); |
315 break; | 303 break; |
316 } | 304 } |
317 case chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED: { | |
318 if (GetProfile()->GetPrefs()->IsManagedPreference( | |
319 prefs::kEnableBookmarkBar)) { | |
320 break; | |
321 } | |
322 if (!NTP4Enabled()) { | |
323 if (GetProfile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar)) | |
324 CallJavascriptFunction("bookmarkBarAttached"); | |
325 else | |
326 CallJavascriptFunction("bookmarkBarDetached"); | |
327 } | |
328 break; | |
329 } | |
330 case content::NOTIFICATION_RENDER_WIDGET_HOST_DID_PAINT: { | 305 case content::NOTIFICATION_RENDER_WIDGET_HOST_DID_PAINT: { |
331 last_paint_ = base::TimeTicks::Now(); | 306 last_paint_ = base::TimeTicks::Now(); |
332 break; | 307 break; |
333 } | 308 } |
334 default: | 309 default: |
335 CHECK(false) << "Unexpected notification: " << type; | 310 CHECK(false) << "Unexpected notification: " << type; |
336 } | 311 } |
337 } | 312 } |
338 | 313 |
339 void NewTabUI::InitializeCSSCaches() { | 314 void NewTabUI::InitializeCSSCaches() { |
340 Profile* profile = GetProfile(); | 315 Profile* profile = GetProfile(); |
341 ThemeSource* theme = new ThemeSource(profile); | 316 ThemeSource* theme = new ThemeSource(profile); |
342 profile->GetChromeURLDataManager()->AddDataSource(theme); | 317 profile->GetChromeURLDataManager()->AddDataSource(theme); |
343 } | 318 } |
344 | 319 |
345 // static | 320 // static |
346 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { | 321 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { |
347 prefs->RegisterIntegerPref(prefs::kNTPPrefVersion, | 322 prefs->RegisterIntegerPref(prefs::kNTPPrefVersion, |
348 0, | 323 0, |
349 PrefService::UNSYNCABLE_PREF); | 324 PrefService::UNSYNCABLE_PREF); |
350 | 325 |
351 NewTabPageHandler::RegisterUserPrefs(prefs); | 326 NewTabPageHandler::RegisterUserPrefs(prefs); |
352 AppLauncherHandler::RegisterUserPrefs(prefs); | 327 AppLauncherHandler::RegisterUserPrefs(prefs); |
353 MostVisitedHandler::RegisterUserPrefs(prefs); | 328 MostVisitedHandler::RegisterUserPrefs(prefs); |
354 ShownSectionsHandler::RegisterUserPrefs(prefs); | 329 ShownSectionsHandler::RegisterUserPrefs(prefs); |
355 if (NTP4Enabled()) | 330 BookmarksHandler::RegisterUserPrefs(prefs); |
356 BookmarksHandler::RegisterUserPrefs(prefs); | |
357 | 331 |
358 UpdateUserPrefsVersion(prefs); | 332 UpdateUserPrefsVersion(prefs); |
359 } | 333 } |
360 | 334 |
361 // static | 335 // static |
362 bool NewTabUI::UpdateUserPrefsVersion(PrefService* prefs) { | 336 bool NewTabUI::UpdateUserPrefsVersion(PrefService* prefs) { |
363 const int old_pref_version = prefs->GetInteger(prefs::kNTPPrefVersion); | 337 const int old_pref_version = prefs->GetInteger(prefs::kNTPPrefVersion); |
364 if (old_pref_version != current_pref_version()) { | 338 if (old_pref_version != current_pref_version()) { |
365 MigrateUserPrefs(prefs, old_pref_version, current_pref_version()); | 339 MigrateUserPrefs(prefs, old_pref_version, current_pref_version()); |
366 prefs->SetInteger(prefs::kNTPPrefVersion, current_pref_version()); | 340 prefs->SetInteger(prefs::kNTPPrefVersion, current_pref_version()); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 direction = kRTLHtmlTextDirection; | 393 direction = kRTLHtmlTextDirection; |
420 } else { | 394 } else { |
421 base::i18n::WrapStringWithLTRFormatting(&title_to_set); | 395 base::i18n::WrapStringWithLTRFormatting(&title_to_set); |
422 } | 396 } |
423 } | 397 } |
424 } | 398 } |
425 dictionary->SetString("title", title_to_set); | 399 dictionary->SetString("title", title_to_set); |
426 dictionary->SetString("direction", direction); | 400 dictionary->SetString("direction", direction); |
427 } | 401 } |
428 | 402 |
429 // static | |
430 bool NewTabUI::NTP4Enabled() { | |
431 #if defined(TOUCH_UI) | |
432 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage); | |
433 #else | |
434 return !CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage); | |
435 #endif | |
436 } | |
437 | |
438 /////////////////////////////////////////////////////////////////////////////// | 403 /////////////////////////////////////////////////////////////////////////////// |
439 // NewTabHTMLSource | 404 // NewTabHTMLSource |
440 | 405 |
441 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) | 406 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) |
442 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), | 407 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), |
443 profile_(profile) { | 408 profile_(profile) { |
444 } | 409 } |
445 | 410 |
446 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, | 411 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, |
447 bool is_incognito, | 412 bool is_incognito, |
(...skipping 16 matching lines...) Expand all Loading... |
464 SendResponse(request_id, html_bytes); | 429 SendResponse(request_id, html_bytes); |
465 } | 430 } |
466 | 431 |
467 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { | 432 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { |
468 return "text/html"; | 433 return "text/html"; |
469 } | 434 } |
470 | 435 |
471 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { | 436 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { |
472 return false; | 437 return false; |
473 } | 438 } |
OLD | NEW |