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

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

Issue 6010004: Refactor RenderWidgetHost::set_paint_observer() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added comments Created 10 years 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "grit/theme_resources.h" 48 #include "grit/theme_resources.h"
49 49
50 namespace { 50 namespace {
51 51
52 // The number of recent bookmarks we show. 52 // The number of recent bookmarks we show.
53 const int kRecentBookmarks = 9; 53 const int kRecentBookmarks = 9;
54 54
55 // The number of search URLs to show. 55 // The number of search URLs to show.
56 const int kSearchURLs = 3; 56 const int kSearchURLs = 3;
57 57
58 // The amount of time there must be no painting for us to consider painting
59 // finished. Observed times are in the ~1200ms range on Windows.
60 const int kTimeoutMs = 2000;
61
58 // Strings sent to the page via jstemplates used to set the direction of the 62 // Strings sent to the page via jstemplates used to set the direction of the
59 // HTML document based on locale. 63 // HTML document based on locale.
60 const char kRTLHtmlTextDirection[] = "rtl"; 64 const char kRTLHtmlTextDirection[] = "rtl";
61 const char kDefaultHtmlTextDirection[] = "ltr"; 65 const char kDefaultHtmlTextDirection[] = "ltr";
62 66
63 ////////////////////////////////////////////////////////////////////////////////
64 // PaintTimer
65
66 // To measure end-to-end performance of the new tab page, we observe paint
67 // messages and wait for the page to stop repainting.
68 class PaintTimer : public RenderWidgetHost::PaintObserver {
69 public:
70 PaintTimer() {
71 Start();
72 }
73
74 // Start the benchmarking and the timer.
75 void Start() {
76 start_ = base::TimeTicks::Now();
77 last_paint_ = start_;
78
79 timer_.Start(base::TimeDelta::FromMilliseconds(kTimeoutMs), this,
80 &PaintTimer::Timeout);
81 }
82
83 // A callback that is invoked whenever our RenderWidgetHost paints.
84 virtual void RenderWidgetHostWillPaint(RenderWidgetHost* rhw) {}
85
86 virtual void RenderWidgetHostDidPaint(RenderWidgetHost* rwh) {
87 last_paint_ = base::TimeTicks::Now();
88 }
89
90 // The timer callback. If enough time has elapsed since the last paint
91 // message, we say we're done painting; otherwise, we keep waiting.
92 void Timeout() {
93 base::TimeTicks now = base::TimeTicks::Now();
94 if ((now - last_paint_) >= base::TimeDelta::FromMilliseconds(kTimeoutMs)) {
95 // Painting has quieted down. Log this as the full time to run.
96 base::TimeDelta load_time = last_paint_ - start_;
97 int load_time_ms = static_cast<int>(load_time.InMilliseconds());
98 NotificationService::current()->Notify(
99 NotificationType::INITIAL_NEW_TAB_UI_LOAD,
100 NotificationService::AllSources(),
101 Details<int>(&load_time_ms));
102 UMA_HISTOGRAM_TIMES("NewTabUI load", load_time);
103 } else {
104 // Not enough quiet time has elapsed.
105 // Some more paints must've occurred since we set the timeout.
106 // Wait some more.
107 timer_.Start(base::TimeDelta::FromMilliseconds(kTimeoutMs), this,
108 &PaintTimer::Timeout);
109 }
110 }
111
112 private:
113 // The amount of time there must be no painting for us to consider painting
114 // finished. Observed times are in the ~1200ms range on Windows.
115 static const int kTimeoutMs = 2000;
116 // The time when we started benchmarking.
117 base::TimeTicks start_;
118 // The last time we got a paint notification.
119 base::TimeTicks last_paint_;
120 // Scoping so we can be sure our timeouts don't outlive us.
121 base::OneShotTimer<PaintTimer> timer_;
122
123 DISALLOW_COPY_AND_ASSIGN(PaintTimer);
124 };
125
126 /////////////////////////////////////////////////////////////////////////////// 67 ///////////////////////////////////////////////////////////////////////////////
127 // RecentlyClosedTabsHandler 68 // RecentlyClosedTabsHandler
128 69
129 class RecentlyClosedTabsHandler : public DOMMessageHandler, 70 class RecentlyClosedTabsHandler : public DOMMessageHandler,
130 public TabRestoreServiceObserver { 71 public TabRestoreServiceObserver {
131 public: 72 public:
132 RecentlyClosedTabsHandler() : tab_restore_service_(NULL) {} 73 RecentlyClosedTabsHandler() : tab_restore_service_(NULL) {}
133 virtual ~RecentlyClosedTabsHandler(); 74 virtual ~RecentlyClosedTabsHandler();
134 75
135 // DOMMessageHandler implementation. 76 // DOMMessageHandler implementation.
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 NotificationService::AllSources()); 345 NotificationService::AllSources());
405 // Listen for bookmark bar visibility changes. 346 // Listen for bookmark bar visibility changes.
406 registrar_.Add(this, NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, 347 registrar_.Add(this, NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED,
407 NotificationService::AllSources()); 348 NotificationService::AllSources());
408 } 349 }
409 350
410 NewTabUI::~NewTabUI() { 351 NewTabUI::~NewTabUI() {
411 } 352 }
412 353
413 void NewTabUI::RenderViewCreated(RenderViewHost* render_view_host) { 354 void NewTabUI::RenderViewCreated(RenderViewHost* render_view_host) {
414 render_view_host->set_paint_observer(new PaintTimer); 355 paint_timer_.reset(new PaintTimer(render_view_host));
415 } 356 }
416 357
417 void NewTabUI::RenderViewReused(RenderViewHost* render_view_host) { 358 void NewTabUI::RenderViewReused(RenderViewHost* render_view_host) {
418 render_view_host->set_paint_observer(new PaintTimer); 359 paint_timer_.reset(new PaintTimer(render_view_host));
419 } 360 }
420 361
421 void NewTabUI::Observe(NotificationType type, 362 void NewTabUI::Observe(NotificationType type,
422 const NotificationSource& source, 363 const NotificationSource& source,
423 const NotificationDetails& details) { 364 const NotificationDetails& details) {
424 if (NotificationType::BROWSER_THEME_CHANGED == type) { 365 if (NotificationType::BROWSER_THEME_CHANGED == type) {
425 InitializeCSSCaches(); 366 InitializeCSSCaches();
426 ListValue args; 367 ListValue args;
427 args.Append(Value::CreateStringValue( 368 args.Append(Value::CreateStringValue(
428 GetProfile()->GetThemeProvider()->HasCustomImage( 369 GetProfile()->GetThemeProvider()->HasCustomImage(
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 557
617 scoped_refptr<RefCountedBytes> html_bytes( 558 scoped_refptr<RefCountedBytes> html_bytes(
618 profile_->GetNTPResourceCache()->GetNewTabHTML(is_off_the_record)); 559 profile_->GetNTPResourceCache()->GetNewTabHTML(is_off_the_record));
619 560
620 SendResponse(request_id, html_bytes); 561 SendResponse(request_id, html_bytes);
621 } 562 }
622 563
623 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { 564 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const {
624 return "text/html"; 565 return "text/html";
625 } 566 }
567
568 ////////////////////////////////////////////////////////////////////////////////
569 // PaintTimer
570
571 // To measure end-to-end performance of the new tab page, we observe paint
572 // messages and wait for the page to stop repainting.
573 NewTabUI::PaintTimer::PaintTimer(RenderViewHost* render_view_host)
574 : start_(base::TimeTicks::Now()),
brettw 2011/01/03 19:48:30 Indent these two lines 2 more spaces.
575 last_paint_(start_) {
576
577 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DID_PAINT,
578 Source<RenderWidgetHost>(render_view_host));
579 timer_.Start(base::TimeDelta::FromMilliseconds(kTimeoutMs), this,
580 &PaintTimer::Timeout);
581 }
582
583 void NewTabUI::PaintTimer::Observe(NotificationType type,
584 const NotificationSource& source,
brettw 2011/01/03 19:48:30 Be sure args are aligned (either indent these to t
585 const NotificationDetails& details) {
586 if (type == NotificationType::RENDER_WIDGET_HOST_DID_PAINT)
587 last_paint_ = base::TimeTicks::Now();
588 }
589
590 // The timer callback. If enough time has elapsed since the last paint
591 // message, we say we're done painting; otherwise, we keep waiting.
592 void NewTabUI::PaintTimer::Timeout() {
593 // The amount of time there must be no painting for us to consider painting
594 // finished. Observed times are in the ~1200ms range on Windows.
595 base::TimeTicks now = base::TimeTicks::Now();
596 if ((now - last_paint_) >= base::TimeDelta::FromMilliseconds(kTimeoutMs)) {
597 // Painting has quieted down. Log this as the full time to run.
598 base::TimeDelta load_time = last_paint_ - start_;
599 int load_time_ms = static_cast<int>(load_time.InMilliseconds());
600 NotificationService::current()->Notify(
601 NotificationType::INITIAL_NEW_TAB_UI_LOAD,
602 NotificationService::AllSources(),
603 Details<int>(&load_time_ms));
604 UMA_HISTOGRAM_TIMES("NewTabUI load", load_time);
605 } else {
606 // Not enough quiet time has elapsed.
607 // Some more paints must've occurred since we set the timeout.
608 // Wait some more.
609 timer_.Start(base::TimeDelta::FromMilliseconds(kTimeoutMs), this,
610 &PaintTimer::Timeout);
611 }
612 }
613
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698