| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/dom_ui/new_tab_ui.h" | 7 #include "chrome/browser/dom_ui/new_tab_ui.h" |
| 8 | 8 |
| 9 #include "app/animation.h" | 9 #include "app/animation.h" |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 196 |
| 197 virtual std::string GetMimeType(const std::string&) const { | 197 virtual std::string GetMimeType(const std::string&) const { |
| 198 return "text/html"; | 198 return "text/html"; |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Setters and getters for first_view. | 201 // Setters and getters for first_view. |
| 202 static void set_first_view(bool first_view) { first_view_ = first_view; } | 202 static void set_first_view(bool first_view) { first_view_ = first_view; } |
| 203 static bool first_view() { return first_view_; } | 203 static bool first_view() { return first_view_; } |
| 204 | 204 |
| 205 private: | 205 private: |
| 206 // In case a file path to the new new tab page was provided this tries to load | 206 // In case a file path to the new tab page was provided this tries to load |
| 207 // the file and returns the file content if successful. This returns an empty | 207 // the file and returns the file content if successful. This returns an empty |
| 208 // string in case of failure. | 208 // string in case of failure. |
| 209 static std::string GetNewNewTabFromCommandLine(); | 209 static std::string GetCustomNewTabPageFromCommandLine(); |
| 210 | 210 |
| 211 // Whether this is the is the first viewing of the new tab page and | 211 // Whether this is the is the first viewing of the new tab page and |
| 212 // we think it is the user's startup page. | 212 // we think it is the user's startup page. |
| 213 static bool first_view_; | 213 static bool first_view_; |
| 214 | 214 |
| 215 // The user's profile. | 215 // The user's profile. |
| 216 Profile* profile_; | 216 Profile* profile_; |
| 217 | 217 |
| 218 DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource); | 218 DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource); |
| 219 }; | 219 }; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 | 347 |
| 348 #ifdef CHROME_PERSONALIZATION | 348 #ifdef CHROME_PERSONALIZATION |
| 349 localized_strings.SetString(L"p13nsrc", Personalization::GetNewTabSource()); | 349 localized_strings.SetString(L"p13nsrc", Personalization::GetNewTabSource()); |
| 350 #endif | 350 #endif |
| 351 | 351 |
| 352 // In case we have the new new tab page enabled we first try to read the file | 352 // In case we have the new new tab page enabled we first try to read the file |
| 353 // provided on the command line. If that fails we just get the resource from | 353 // provided on the command line. If that fails we just get the resource from |
| 354 // the resource bundle. | 354 // the resource bundle. |
| 355 StringPiece new_tab_html; | 355 StringPiece new_tab_html; |
| 356 std::string new_tab_html_str; | 356 std::string new_tab_html_str; |
| 357 if (NewTabUI::EnableNewNewTabPage()) { | 357 new_tab_html_str = GetCustomNewTabPageFromCommandLine(); |
| 358 new_tab_html_str = GetNewNewTabFromCommandLine(); | |
| 359 | 358 |
| 360 if (!new_tab_html_str.empty()) { | 359 if (!new_tab_html_str.empty()) { |
| 361 new_tab_html = StringPiece(new_tab_html_str); | 360 new_tab_html = StringPiece(new_tab_html_str); |
| 362 } else { | 361 } |
| 363 // Use the new new tab page from the resource bundle. | 362 |
| 364 new_tab_html = ResourceBundle::GetSharedInstance().GetRawDataResource( | 363 if (new_tab_html.empty()) { |
| 365 IDR_NEW_NEW_TAB_HTML); | |
| 366 } | |
| 367 } else { | |
| 368 // Use the default new tab page resource. | |
| 369 new_tab_html = ResourceBundle::GetSharedInstance().GetRawDataResource( | 364 new_tab_html = ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 370 IDR_NEW_TAB_HTML); | 365 NewTabUI::UseOldNewTabPage() ? |
| 366 IDR_NEW_TAB_HTML : |
| 367 IDR_NEW_NEW_TAB_HTML); |
| 371 } | 368 } |
| 372 | 369 |
| 373 const std::string full_html = jstemplate_builder::GetTemplateHtml( | 370 const std::string full_html = jstemplate_builder::GetTemplateHtml( |
| 374 new_tab_html, &localized_strings, "t" /* template root node id */); | 371 new_tab_html, &localized_strings, "t" /* template root node id */); |
| 375 | 372 |
| 376 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 373 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 377 html_bytes->data.resize(full_html.size()); | 374 html_bytes->data.resize(full_html.size()); |
| 378 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | 375 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 379 | 376 |
| 380 SendResponse(request_id, html_bytes); | 377 SendResponse(request_id, html_bytes); |
| 381 } | 378 } |
| 382 | 379 |
| 383 // static | 380 // static |
| 384 std::string NewTabHTMLSource::GetNewNewTabFromCommandLine() { | 381 std::string NewTabHTMLSource::GetCustomNewTabPageFromCommandLine() { |
| 385 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 382 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 386 const std::wstring file_path_wstring = command_line->GetSwitchValue( | 383 const std::wstring file_path_wstring = command_line->GetSwitchValue( |
| 387 switches::kNewNewTabPage); | 384 switches::kNewTabPage); |
| 388 | 385 |
| 389 #if defined(OS_WIN) | 386 #if defined(OS_WIN) |
| 390 const FilePath::StringType file_path = file_path_wstring; | 387 const FilePath::StringType file_path = file_path_wstring; |
| 391 #else | 388 #else |
| 392 const FilePath::StringType file_path = WideToASCII(file_path_wstring); | 389 const FilePath::StringType file_path = WideToASCII(file_path_wstring); |
| 393 #endif | 390 #endif |
| 394 | 391 |
| 395 if (!file_path.empty()) { | 392 if (!file_path.empty()) { |
| 396 std::string file_contents; | 393 std::string file_contents; |
| 397 if (file_util::ReadFileToString(FilePath(file_path), &file_contents)) | 394 if (file_util::ReadFileToString(FilePath(file_path), &file_contents)) |
| (...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 /////////////////////////////////////////////////////////////////////////////// | 984 /////////////////////////////////////////////////////////////////////////////// |
| 988 // RecentlyBookmarkedHandler | 985 // RecentlyBookmarkedHandler |
| 989 | 986 |
| 990 class RecentlyBookmarkedHandler : public DOMMessageHandler, | 987 class RecentlyBookmarkedHandler : public DOMMessageHandler, |
| 991 public BookmarkModelObserver { | 988 public BookmarkModelObserver { |
| 992 public: | 989 public: |
| 993 RecentlyBookmarkedHandler() : model_(NULL) {} | 990 RecentlyBookmarkedHandler() : model_(NULL) {} |
| 994 virtual ~RecentlyBookmarkedHandler(); | 991 virtual ~RecentlyBookmarkedHandler(); |
| 995 | 992 |
| 996 // DOMMessageHandler implementation. | 993 // DOMMessageHandler implementation. |
| 997 virtual void RegisterMessages(); | 994 virtual void RegisterMessages(); |
| 998 | 995 |
| 999 // Callback which navigates to the bookmarks page. | 996 // Callback which navigates to the bookmarks page. |
| 1000 void HandleShowBookmarkPage(const Value*); | 997 void HandleShowBookmarkPage(const Value*); |
| 1001 | 998 |
| 1002 // Callback for the "getRecentlyBookmarked" message. | 999 // Callback for the "getRecentlyBookmarked" message. |
| 1003 // It takes no arguments. | 1000 // It takes no arguments. |
| 1004 void HandleGetRecentlyBookmarked(const Value*); | 1001 void HandleGetRecentlyBookmarked(const Value*); |
| 1005 | 1002 |
| 1006 private: | 1003 private: |
| 1007 void SendBookmarksToPage(); | 1004 void SendBookmarksToPage(); |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1330 // Let the page contents record UMA actions. Only use when you can't do it from | 1327 // Let the page contents record UMA actions. Only use when you can't do it from |
| 1331 // C++. For example, we currently use it to let the NTP log the postion of the | 1328 // C++. For example, we currently use it to let the NTP log the postion of the |
| 1332 // Most Visited or Bookmark the user clicked on, as we don't get that | 1329 // Most Visited or Bookmark the user clicked on, as we don't get that |
| 1333 // information through RequestOpenURL. You will need to update the metrics | 1330 // information through RequestOpenURL. You will need to update the metrics |
| 1334 // dashboard with the action names you use, as our processor won't catch that | 1331 // dashboard with the action names you use, as our processor won't catch that |
| 1335 // information (treat it as RecordComputedMetrics) | 1332 // information (treat it as RecordComputedMetrics) |
| 1336 class MetricsHandler : public DOMMessageHandler { | 1333 class MetricsHandler : public DOMMessageHandler { |
| 1337 public: | 1334 public: |
| 1338 MetricsHandler() {} | 1335 MetricsHandler() {} |
| 1339 virtual ~MetricsHandler() {} | 1336 virtual ~MetricsHandler() {} |
| 1340 | 1337 |
| 1341 // DOMMessageHandler implementation. | 1338 // DOMMessageHandler implementation. |
| 1342 virtual void RegisterMessages(); | 1339 virtual void RegisterMessages(); |
| 1343 | 1340 |
| 1344 // Callback which records a user action. | 1341 // Callback which records a user action. |
| 1345 void HandleMetrics(const Value* content); | 1342 void HandleMetrics(const Value* content); |
| 1346 | 1343 |
| 1347 private: | 1344 private: |
| 1348 | 1345 |
| 1349 DISALLOW_COPY_AND_ASSIGN(MetricsHandler); | 1346 DISALLOW_COPY_AND_ASSIGN(MetricsHandler); |
| 1350 }; | 1347 }; |
| 1351 | 1348 |
| 1352 void MetricsHandler::RegisterMessages() { | 1349 void MetricsHandler::RegisterMessages() { |
| 1353 dom_ui_->RegisterMessageCallback("metrics", | 1350 dom_ui_->RegisterMessageCallback("metrics", |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1402 if (GetProfile()->IsOffTheRecord()) { | 1399 if (GetProfile()->IsOffTheRecord()) { |
| 1403 incognito_ = true; | 1400 incognito_ = true; |
| 1404 | 1401 |
| 1405 IncognitoTabHTMLSource* html_source = new IncognitoTabHTMLSource(); | 1402 IncognitoTabHTMLSource* html_source = new IncognitoTabHTMLSource(); |
| 1406 | 1403 |
| 1407 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, | 1404 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, |
| 1408 NewRunnableMethod(&chrome_url_data_manager, | 1405 NewRunnableMethod(&chrome_url_data_manager, |
| 1409 &ChromeURLDataManager::AddDataSource, | 1406 &ChromeURLDataManager::AddDataSource, |
| 1410 html_source)); | 1407 html_source)); |
| 1411 } else { | 1408 } else { |
| 1412 if (EnableNewNewTabPage()) { | 1409 DownloadManager* dlm = GetProfile()->GetDownloadManager(); |
| 1410 // This might be null in the case of running inside a unit test. |
| 1411 // TODO(arv): Fix unit tests to provide a working mock download manager. |
| 1412 if (dlm) { |
| 1413 DownloadManager* dlm = GetProfile()->GetDownloadManager(); | 1413 DownloadManager* dlm = GetProfile()->GetDownloadManager(); |
| 1414 DownloadsDOMHandler* downloads_handler = | 1414 DownloadsDOMHandler* downloads_handler = |
| 1415 new DownloadsDOMHandler(dlm); | 1415 new DownloadsDOMHandler(dlm); |
| 1416 downloads_handler->Attach(this); | 1416 downloads_handler->Attach(this); |
| 1417 AddMessageHandler(downloads_handler); | 1417 AddMessageHandler(downloads_handler); |
| 1418 downloads_handler->Init(); | 1418 downloads_handler->Init(); |
| 1419 | |
| 1420 AddMessageHandler((new ShownSectionsHandler())->Attach(this)); | |
| 1421 } | 1419 } |
| 1422 | 1420 |
| 1423 AddMessageHandler((new TemplateURLHandler())->Attach(this)); | 1421 AddMessageHandler((new ShownSectionsHandler())->Attach(this)); |
| 1424 AddMessageHandler((new MostVisitedHandler())->Attach(this)); | 1422 AddMessageHandler((new MostVisitedHandler())->Attach(this)); |
| 1425 AddMessageHandler((new RecentlyBookmarkedHandler())->Attach(this)); | |
| 1426 AddMessageHandler((new RecentlyClosedTabsHandler())->Attach(this)); | 1423 AddMessageHandler((new RecentlyClosedTabsHandler())->Attach(this)); |
| 1427 AddMessageHandler((new HistoryHandler())->Attach(this)); | |
| 1428 AddMessageHandler((new MetricsHandler())->Attach(this)); | 1424 AddMessageHandler((new MetricsHandler())->Attach(this)); |
| 1429 if (EnableWebResources()) | 1425 if (EnableWebResources()) |
| 1430 AddMessageHandler((new TipsHandler())->Attach(this)); | 1426 AddMessageHandler((new TipsHandler())->Attach(this)); |
| 1431 | 1427 |
| 1428 if (UseOldNewTabPage()) { |
| 1429 AddMessageHandler((new TemplateURLHandler())->Attach(this)); |
| 1430 AddMessageHandler((new RecentlyBookmarkedHandler())->Attach(this)); |
| 1431 AddMessageHandler((new HistoryHandler())->Attach(this)); |
| 1432 } |
| 1433 |
| 1432 #ifdef CHROME_PERSONALIZATION | 1434 #ifdef CHROME_PERSONALIZATION |
| 1433 if (!Personalization::IsP13NDisabled(GetProfile())) { | 1435 if (!Personalization::IsP13NDisabled(GetProfile())) { |
| 1434 AddMessageHandler(Personalization::CreateNewTabPageHandler(this)); | 1436 AddMessageHandler(Personalization::CreateNewTabPageHandler(this)); |
| 1435 } | 1437 } |
| 1436 #endif | 1438 #endif |
| 1437 | 1439 |
| 1438 // In testing mode there may not be an I/O thread. | 1440 // In testing mode there may not be an I/O thread. |
| 1439 if (g_browser_process->io_thread()) { | 1441 if (g_browser_process->io_thread()) { |
| 1440 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, | 1442 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, |
| 1441 NewRunnableMethod(&chrome_url_data_manager, | 1443 NewRunnableMethod(&chrome_url_data_manager, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1467 if (NotificationType::THEME_INSTALLED == type) { | 1469 if (NotificationType::THEME_INSTALLED == type) { |
| 1468 CallJavascriptFunction(L"themeChanged"); | 1470 CallJavascriptFunction(L"themeChanged"); |
| 1469 } else if (NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED) { | 1471 } else if (NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED) { |
| 1470 if (GetProfile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar)) | 1472 if (GetProfile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar)) |
| 1471 CallJavascriptFunction(L"bookmarkBarAttached"); | 1473 CallJavascriptFunction(L"bookmarkBarAttached"); |
| 1472 else | 1474 else |
| 1473 CallJavascriptFunction(L"bookmarkBarDetached"); | 1475 CallJavascriptFunction(L"bookmarkBarDetached"); |
| 1474 } | 1476 } |
| 1475 } | 1477 } |
| 1476 | 1478 |
| 1477 | |
| 1478 // static | 1479 // static |
| 1479 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { | 1480 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { |
| 1480 MostVisitedHandler::RegisterUserPrefs(prefs); | 1481 MostVisitedHandler::RegisterUserPrefs(prefs); |
| 1482 ShownSectionsHandler::RegisterUserPrefs(prefs); |
| 1481 if (NewTabUI::EnableWebResources()) | 1483 if (NewTabUI::EnableWebResources()) |
| 1482 TipsHandler::RegisterUserPrefs(prefs); | 1484 TipsHandler::RegisterUserPrefs(prefs); |
| 1483 if (NewTabUI::EnableNewNewTabPage()) | |
| 1484 ShownSectionsHandler::RegisterUserPrefs(prefs); | |
| 1485 } | 1485 } |
| 1486 | 1486 |
| 1487 // static | 1487 // static |
| 1488 bool NewTabUI::EnableNewNewTabPage() { | 1488 bool NewTabUI::UseOldNewTabPage() { |
| 1489 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 1489 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1490 return command_line->HasSwitch(switches::kNewNewTabPage); | 1490 return command_line->HasSwitch(switches::kOldNewTabPage); |
| 1491 } | 1491 } |
| 1492 | 1492 |
| 1493 bool NewTabUI::EnableWebResources() { | 1493 bool NewTabUI::EnableWebResources() { |
| 1494 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 1494 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1495 return command_line->HasSwitch(switches::kWebResources); | 1495 return command_line->HasSwitch(switches::kWebResources); |
| 1496 } | 1496 } |
| 1497 | |
| OLD | NEW |