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
3 // found in the LICENSE file.
5 #include "content/renderer/load_progress_tracker.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/common/view_messages.h"
10 #include "content/renderer/render_view_impl.h"
15 const int kMinimumDelayBetweenUpdatesMS
= 100;
19 LoadProgressTracker::LoadProgressTracker(RenderViewImpl
* render_view
)
20 : render_view_(render_view
),
26 LoadProgressTracker::~LoadProgressTracker() {
29 void LoadProgressTracker::DidStopLoading() {
33 // Load stopped while we were still tracking load. Make sure we notify the
34 // browser that load is complete.
36 SendChangeLoadProgress();
37 // Then we clean-up our states.
41 void LoadProgressTracker::DidChangeLoadProgress(blink::WebFrame
* frame
,
43 if (tracked_frame_
&& frame
!= tracked_frame_
)
47 tracked_frame_
= frame
;
51 // We send the progress change to the browser immediately for the first and
52 // last updates. Also, since the message loop may be pretty busy when a page
53 // is loaded, it might not execute a posted task in a timely manner so we make
54 // sure to immediately send progress report if enough time has passed.
55 base::TimeDelta min_delay
=
56 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenUpdatesMS
);
57 if (progress
== 1.0 || last_time_progress_sent_
.is_null() ||
58 base::TimeTicks::Now() - last_time_progress_sent_
>
60 // If there is a pending task to send progress, it is now obsolete.
61 weak_factory_
.InvalidateWeakPtrs();
62 SendChangeLoadProgress();
68 if (weak_factory_
.HasWeakPtrs())
71 base::MessageLoop::current()->PostDelayedTask(
73 base::Bind(&LoadProgressTracker::SendChangeLoadProgress
,
74 weak_factory_
.GetWeakPtr()),
78 void LoadProgressTracker::SendChangeLoadProgress() {
79 last_time_progress_sent_
= base::TimeTicks::Now();
81 new ViewHostMsg_DidChangeLoadProgress(render_view_
->routing_id(),
85 void LoadProgressTracker::ResetStates() {
86 tracked_frame_
= NULL
;
88 weak_factory_
.InvalidateWeakPtrs();
89 last_time_progress_sent_
= base::TimeTicks();
92 } // namespace content