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

Side by Side Diff: chrome/browser/dom_ui/new_tab_ui.cc

Issue 179069: Improve New Tab Page load performance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 3 months 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) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 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 <set> 9 #include <set>
10 10
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 explicit NewTabHTMLSource(Profile* profile); 193 explicit NewTabHTMLSource(Profile* profile);
194 194
195 // Called when the network layer has requested a resource underneath 195 // Called when the network layer has requested a resource underneath
196 // the path we registered. 196 // the path we registered.
197 virtual void StartDataRequest(const std::string& path, int request_id); 197 virtual void StartDataRequest(const std::string& path, int request_id);
198 198
199 virtual std::string GetMimeType(const std::string&) const { 199 virtual std::string GetMimeType(const std::string&) const {
200 return "text/html"; 200 return "text/html";
201 } 201 }
202 202
203 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path)
204 const {
205 // NewTabHTMLSource does all of the operations that need to be on the
206 // UI thread from InitFullHTML, called by the constructor. It is safe
207 // to call StartDataRequest from any thread, so return NULL.
208 return NULL;
209 }
210
203 // Setters and getters for first_view. 211 // Setters and getters for first_view.
204 static void set_first_view(bool first_view) { first_view_ = first_view; } 212 static void set_first_view(bool first_view) { first_view_ = first_view; }
205 static bool first_view() { return first_view_; } 213 static bool first_view() { return first_view_; }
206 214
207 // Setters and getters for first_run. 215 // Setters and getters for first_run.
208 static void set_first_run(bool first_run) { first_run_ = first_run; } 216 static void set_first_run(bool first_run) { first_run_ = first_run; }
209 static bool first_run() { return first_run_; } 217 static bool first_run() { return first_run_; }
210 218
211 private: 219 private:
212 // In case a file path to the new tab page was provided this tries to load 220 // In case a file path to the new tab page was provided this tries to load
213 // the file and returns the file content if successful. This returns an empty 221 // the file and returns the file content if successful. This returns an empty
214 // string in case of failure. 222 // string in case of failure.
215 static std::string GetCustomNewTabPageFromCommandLine(); 223 static std::string GetCustomNewTabPageFromCommandLine();
216 224
225 // Populate full_html_. This must be called from the UI thread because it
226 // involves profile access.
227 //
228 // A new NewTabHTMLSource object is used for each new tab page instance
229 // and each reload of an existing new tab page, so there is no concern
230 // about cached data becoming stale.
231 void InitFullHTML();
232
233 // The content to be served by StartDataRequest, stored by InitFullHTML.
234 std::string full_html_;
235
217 // Whether this is the first viewing of the new tab page and 236 // Whether this is the first viewing of the new tab page and
218 // we think it is the user's startup page. 237 // we think it is the user's startup page.
219 static bool first_view_; 238 static bool first_view_;
220 239
221 // Whether this is the first run. 240 // Whether this is the first run.
222 static bool first_run_; 241 static bool first_run_;
223 242
224 // The user's profile. 243 // The user's profile.
225 Profile* profile_; 244 Profile* profile_;
226 245
227 DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource); 246 DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource);
228 }; 247 };
229 248
230 bool NewTabHTMLSource::first_view_ = true; 249 bool NewTabHTMLSource::first_view_ = true;
231 250
232 bool NewTabHTMLSource::first_run_ = true; 251 bool NewTabHTMLSource::first_run_ = true;
233 252
234 NewTabHTMLSource::NewTabHTMLSource(Profile* profile) 253 NewTabHTMLSource::NewTabHTMLSource(Profile* profile)
235 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), 254 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()),
236 profile_(profile) { 255 profile_(profile) {
256 InitFullHTML();
237 } 257 }
238 258
239 void NewTabHTMLSource::StartDataRequest(const std::string& path, 259 void NewTabHTMLSource::StartDataRequest(const std::string& path,
240 int request_id) { 260 int request_id) {
241 if (!path.empty()) { 261 if (!path.empty()) {
242 // A path under new-tab was requested; it's likely a bad relative 262 // A path under new-tab was requested; it's likely a bad relative
243 // URL from the new tab page, but in any case it's an error. 263 // URL from the new tab page, but in any case it's an error.
244 NOTREACHED(); 264 NOTREACHED();
245 return; 265 return;
246 } 266 }
247 267
268 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
269 html_bytes->data.resize(full_html_.size());
270 std::copy(full_html_.begin(), full_html_.end(), html_bytes->data.begin());
271
272 SendResponse(request_id, html_bytes);
273 }
274
275 // static
276 std::string NewTabHTMLSource::GetCustomNewTabPageFromCommandLine() {
277 const CommandLine* command_line = CommandLine::ForCurrentProcess();
278 const std::wstring file_path_wstring = command_line->GetSwitchValue(
279 switches::kNewTabPage);
280
281 #if defined(OS_WIN)
282 const FilePath::StringType file_path = file_path_wstring;
283 #else
284 const FilePath::StringType file_path = WideToASCII(file_path_wstring);
285 #endif
286
287 if (!file_path.empty()) {
288 std::string file_contents;
289 if (file_util::ReadFileToString(FilePath(file_path), &file_contents))
290 return file_contents;
291 }
292
293 return std::string();
294 }
295
296 void NewTabHTMLSource::InitFullHTML() {
248 // Show the profile name in the title and most visited labels if the current 297 // Show the profile name in the title and most visited labels if the current
249 // profile is not the default. 298 // profile is not the default.
250 std::wstring title; 299 std::wstring title;
251 std::wstring most_visited; 300 std::wstring most_visited;
252 if (UserDataManager::Get()->is_current_profile_default()) { 301 if (UserDataManager::Get()->is_current_profile_default()) {
253 title = l10n_util::GetString(IDS_NEW_TAB_TITLE); 302 title = l10n_util::GetString(IDS_NEW_TAB_TITLE);
254 most_visited = l10n_util::GetString(IDS_NEW_TAB_MOST_VISITED); 303 most_visited = l10n_util::GetString(IDS_NEW_TAB_MOST_VISITED);
255 } else { 304 } else {
256 // Get the current profile name. 305 // Get the current profile name.
257 std::wstring profile_name = 306 std::wstring profile_name =
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 new_tab_html = StringPiece(new_tab_html_str); 425 new_tab_html = StringPiece(new_tab_html_str);
377 } 426 }
378 427
379 if (new_tab_html.empty()) { 428 if (new_tab_html.empty()) {
380 new_tab_html = ResourceBundle::GetSharedInstance().GetRawDataResource( 429 new_tab_html = ResourceBundle::GetSharedInstance().GetRawDataResource(
381 NewTabUI::UseOldNewTabPage() ? 430 NewTabUI::UseOldNewTabPage() ?
382 IDR_NEW_TAB_HTML : 431 IDR_NEW_TAB_HTML :
383 IDR_NEW_NEW_TAB_HTML); 432 IDR_NEW_NEW_TAB_HTML);
384 } 433 }
385 434
386 std::string full_html(new_tab_html.data(), new_tab_html.size()); 435 full_html_.assign(new_tab_html.data(), new_tab_html.size());
387 jstemplate_builder::AppendJsonHtml(&localized_strings, &full_html); 436 jstemplate_builder::AppendJsonHtml(&localized_strings, &full_html_);
388 jstemplate_builder::AppendI18nTemplateSourceHtml(&full_html); 437 jstemplate_builder::AppendI18nTemplateSourceHtml(&full_html_);
389 jstemplate_builder::AppendI18nTemplateProcessHtml(&full_html); 438 jstemplate_builder::AppendI18nTemplateProcessHtml(&full_html_);
390
391 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
392 html_bytes->data.resize(full_html.size());
393 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
394
395 SendResponse(request_id, html_bytes);
396 }
397
398 // static
399 std::string NewTabHTMLSource::GetCustomNewTabPageFromCommandLine() {
400 const CommandLine* command_line = CommandLine::ForCurrentProcess();
401 const std::wstring file_path_wstring = command_line->GetSwitchValue(
402 switches::kNewTabPage);
403
404 #if defined(OS_WIN)
405 const FilePath::StringType file_path = file_path_wstring;
406 #else
407 const FilePath::StringType file_path = WideToASCII(file_path_wstring);
408 #endif
409
410 if (!file_path.empty()) {
411 std::string file_contents;
412 if (file_util::ReadFileToString(FilePath(file_path), &file_contents))
413 return file_contents;
414 }
415
416 return std::string();
417 } 439 }
418 440
419 /////////////////////////////////////////////////////////////////////////////// 441 ///////////////////////////////////////////////////////////////////////////////
420 // IncognitoTabHTMLSource 442 // IncognitoTabHTMLSource
421 443
422 class IncognitoTabHTMLSource : public ChromeURLDataManager::DataSource { 444 class IncognitoTabHTMLSource : public ChromeURLDataManager::DataSource {
423 public: 445 public:
424 // Creates our datasource and registers initial state of the bookmark bar. 446 // Creates our datasource and registers initial state of the bookmark bar.
425 explicit IncognitoTabHTMLSource(bool bookmark_bar_attached); 447 explicit IncognitoTabHTMLSource(bool bookmark_bar_attached);
426 448
427 // Called when the network layer has requested a resource underneath 449 // Called when the network layer has requested a resource underneath
428 // the path we registered. 450 // the path we registered.
429 virtual void StartDataRequest(const std::string& path, int request_id); 451 virtual void StartDataRequest(const std::string& path, int request_id);
430 452
431 virtual std::string GetMimeType(const std::string&) const { 453 virtual std::string GetMimeType(const std::string&) const {
432 return "text/html"; 454 return "text/html";
433 } 455 }
434 456
457 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path)
458 const {
459 // IncognitoTabHTMLSource does all of the operations that need to be on
460 // the UI thread from InitFullHTML, called by the constructor. It is safe
461 // to call StartDataRequest from any thread, so return NULL.
462 return NULL;
463 }
464
435 private: 465 private:
466 // Populate full_html_. This must be called from the UI thread because it
467 // involves profile access.
468 //
469 // A new IncognitoTabHTMLSource object is used for each incognito tab page
470 // instance and each reload of an existing incognito tab page, so there is
471 // no concern about cached data becoming stale.
472 void InitFullHTML();
473
474 // The content to be served by StartDataRequest, stored by InitFullHTML.
475 std::string full_html_;
476
436 bool bookmark_bar_attached_; 477 bool bookmark_bar_attached_;
437 478
438 DISALLOW_COPY_AND_ASSIGN(IncognitoTabHTMLSource); 479 DISALLOW_COPY_AND_ASSIGN(IncognitoTabHTMLSource);
439 }; 480 };
440 481
441 IncognitoTabHTMLSource::IncognitoTabHTMLSource(bool bookmark_bar_attached) 482 IncognitoTabHTMLSource::IncognitoTabHTMLSource(bool bookmark_bar_attached)
442 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), 483 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()),
443 bookmark_bar_attached_(bookmark_bar_attached) { 484 bookmark_bar_attached_(bookmark_bar_attached) {
485 InitFullHTML();
444 } 486 }
445 487
446 void IncognitoTabHTMLSource::StartDataRequest(const std::string& path, 488 void IncognitoTabHTMLSource::StartDataRequest(const std::string& path,
447 int request_id) { 489 int request_id) {
490 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
491 html_bytes->data.resize(full_html_.size());
492 std::copy(full_html_.begin(), full_html_.end(), html_bytes->data.begin());
493
494 SendResponse(request_id, html_bytes);
495 }
496
497 void IncognitoTabHTMLSource::InitFullHTML() {
448 DictionaryValue localized_strings; 498 DictionaryValue localized_strings;
449 localized_strings.SetString(L"title", 499 localized_strings.SetString(L"title",
450 l10n_util::GetString(IDS_NEW_TAB_TITLE)); 500 l10n_util::GetString(IDS_NEW_TAB_TITLE));
451 localized_strings.SetString(L"content", 501 localized_strings.SetString(L"content",
452 l10n_util::GetStringF(IDS_NEW_TAB_OTR_MESSAGE, 502 l10n_util::GetStringF(IDS_NEW_TAB_OTR_MESSAGE,
453 l10n_util::GetString(IDS_LEARN_MORE_INCOGNITO_URL))); 503 l10n_util::GetString(IDS_LEARN_MORE_INCOGNITO_URL)));
454 localized_strings.SetString(L"bookmarkbarattached", 504 localized_strings.SetString(L"bookmarkbarattached",
455 bookmark_bar_attached_ ? "true" : "false"); 505 bookmark_bar_attached_ ? "true" : "false");
456 506
457 SetFontAndTextDirection(&localized_strings); 507 SetFontAndTextDirection(&localized_strings);
458 508
459 static const StringPiece incognito_tab_html( 509 static const StringPiece incognito_tab_html(
460 ResourceBundle::GetSharedInstance().GetRawDataResource( 510 ResourceBundle::GetSharedInstance().GetRawDataResource(
461 IDR_INCOGNITO_TAB_HTML)); 511 IDR_INCOGNITO_TAB_HTML));
462 512
463 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( 513 full_html_ = jstemplate_builder::GetI18nTemplateHtml(incognito_tab_html,
464 incognito_tab_html, &localized_strings); 514 &localized_strings);
465
466 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
467 html_bytes->data.resize(full_html.size());
468 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
469
470 SendResponse(request_id, html_bytes);
471 } 515 }
472 516
473 /////////////////////////////////////////////////////////////////////////////// 517 ///////////////////////////////////////////////////////////////////////////////
474 // MostVisitedPage 518 // MostVisitedPage
475 519
476 // This struct is used when getting the prepopulated pages in case the user 520 // This struct is used when getting the prepopulated pages in case the user
477 // hasn't filled up his most visited pages. 521 // hasn't filled up his most visited pages.
478 struct MostVisitedPage { 522 struct MostVisitedPage {
479 std::wstring title; 523 std::wstring title;
480 GURL url; 524 GURL url;
(...skipping 1167 matching lines...) Expand 10 before | Expand all | Expand 10 after
1648 bool NewTabUI::WebResourcesEnabled() { 1692 bool NewTabUI::WebResourcesEnabled() {
1649 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 1693 const CommandLine* command_line = CommandLine::ForCurrentProcess();
1650 return !command_line->HasSwitch(switches::kDisableWebResources); 1694 return !command_line->HasSwitch(switches::kDisableWebResources);
1651 } 1695 }
1652 1696
1653 // static 1697 // static
1654 bool NewTabUI::FirstRunDisabled() { 1698 bool NewTabUI::FirstRunDisabled() {
1655 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 1699 const CommandLine* command_line = CommandLine::ForCurrentProcess();
1656 return command_line->HasSwitch(switches::kDisableNewTabFirstRun); 1700 return command_line->HasSwitch(switches::kDisableNewTabFirstRun);
1657 } 1701 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698