OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "content/renderer/load_progress_tracker.h" | 5 #include "content/renderer/load_progress_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
10 #include "content/renderer/render_view_impl.h" | 10 #include "content/renderer/render_view_impl.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 namespace { | 13 namespace { |
14 | 14 |
15 const int kMinimumDelayBetweenUpdatesMS = 100; | 15 const int kMinimumDelayBetweenUpdatesMS = 100; |
16 | 16 |
17 // This matches what blink's ProgrssTracker has traditionally used for a | |
18 // minmum progress value. | |
Charlie Reis
2014/03/11 21:51:12
nit: minimum
| |
19 const double kMinimumProgress = 0.1; | |
20 | |
17 } | 21 } |
18 | 22 |
19 LoadProgressTracker::LoadProgressTracker(RenderViewImpl* render_view) | 23 LoadProgressTracker::LoadProgressTracker(RenderViewImpl* render_view) |
20 : render_view_(render_view), | 24 : render_view_(render_view), |
21 tracked_frame_(NULL), | 25 total_progress_(0.0), |
22 progress_(0.0), | |
23 weak_factory_(this) { | 26 weak_factory_(this) { |
24 } | 27 } |
25 | 28 |
26 LoadProgressTracker::~LoadProgressTracker() { | 29 LoadProgressTracker::~LoadProgressTracker() { |
27 } | 30 } |
28 | 31 |
29 void LoadProgressTracker::DidStopLoading() { | 32 void LoadProgressTracker::DidStartLoading(int frame_routing_id) { |
30 if (!tracked_frame_) | 33 progresses_[frame_routing_id] = kMinimumProgress; |
34 SendChangeLoadProgress(); | |
35 } | |
36 | |
37 void LoadProgressTracker::DidStopLoading(int frame_routing_id) { | |
38 if (progresses_.find(frame_routing_id) == progresses_.end()) | |
31 return; | 39 return; |
32 | 40 |
33 // Load stopped while we were still tracking load. Make sure we notify the | 41 // Load stopped while we were still tracking load. Make sure we update |
34 // browser that load is complete. | 42 // progress based on this frame's completion. |
35 progress_ = 1.0; | 43 progresses_[frame_routing_id] = 1.0; |
36 SendChangeLoadProgress(); | 44 SendChangeLoadProgress(); |
37 // Then we clean-up our states. | 45 // Then we clean-up our states. |
38 ResetStates(); | 46 if (total_progress_ == 1.0) |
47 ResetStates(); | |
39 } | 48 } |
40 | 49 |
41 void LoadProgressTracker::DidChangeLoadProgress(blink::WebFrame* frame, | 50 void LoadProgressTracker::DidChangeLoadProgress(int frame_routing_id, |
42 double progress) { | 51 double progress) { |
43 if (tracked_frame_ && frame != tracked_frame_) | 52 progresses_[frame_routing_id] = progress; |
44 return; | |
45 | |
46 if (!tracked_frame_) | |
47 tracked_frame_ = frame; | |
48 | |
49 progress_ = progress; | |
50 | 53 |
51 // We send the progress change to the browser immediately for the first and | 54 // 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 | 55 // 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 | 56 // 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. | 57 // sure to immediately send progress report if enough time has passed. |
55 base::TimeDelta min_delay = | 58 base::TimeDelta min_delay = |
56 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenUpdatesMS); | 59 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenUpdatesMS); |
57 if (progress == 1.0 || last_time_progress_sent_.is_null() || | 60 if (progress == 1.0 || last_time_progress_sent_.is_null() || |
58 base::TimeTicks::Now() - last_time_progress_sent_ > | 61 base::TimeTicks::Now() - last_time_progress_sent_ > |
59 min_delay) { | 62 min_delay) { |
Charlie Reis
2014/03/11 21:51:12
nit: I know this isn't yours, but can you add 4 sp
Nate Chapin
2014/03/11 22:09:59
I don't see why there's an extra line there, min_d
| |
60 // If there is a pending task to send progress, it is now obsolete. | 63 // If there is a pending task to send progress, it is now obsolete. |
61 weak_factory_.InvalidateWeakPtrs(); | 64 weak_factory_.InvalidateWeakPtrs(); |
62 SendChangeLoadProgress(); | 65 SendChangeLoadProgress(); |
63 if (progress == 1.0) | 66 if (total_progress_ == 1.0) |
64 ResetStates(); | 67 ResetStates(); |
65 return; | 68 return; |
66 } | 69 } |
67 | 70 |
68 if (weak_factory_.HasWeakPtrs()) | 71 if (weak_factory_.HasWeakPtrs()) |
69 return; | 72 return; |
70 | 73 |
71 base::MessageLoop::current()->PostDelayedTask( | 74 base::MessageLoop::current()->PostDelayedTask( |
72 FROM_HERE, | 75 FROM_HERE, |
73 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, | 76 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, |
74 weak_factory_.GetWeakPtr()), | 77 weak_factory_.GetWeakPtr()), |
75 min_delay); | 78 min_delay); |
76 } | 79 } |
77 | 80 |
78 void LoadProgressTracker::SendChangeLoadProgress() { | 81 void LoadProgressTracker::SendChangeLoadProgress() { |
79 last_time_progress_sent_ = base::TimeTicks::Now(); | 82 last_time_progress_sent_ = base::TimeTicks::Now(); |
83 double progress = 0.0; | |
84 unsigned frameCount = 0; | |
85 ProgressMap::iterator end = progresses_.end(); | |
86 for (ProgressMap::iterator it = progresses_.begin(); it != end; ++it) { | |
87 progress += it->second; | |
88 frameCount++; | |
89 } | |
90 progress /= frameCount; | |
Charlie Reis
2014/03/11 21:51:12
This is crying out to divide by zero if progresses
Nate Chapin
2014/03/11 22:09:59
That shouldn't be possible, but...yeah, let's just
| |
91 DCHECK(progress <= 1.0); | |
92 | |
93 if (progress <= total_progress_) | |
94 return; | |
95 total_progress_ = progress; | |
96 | |
80 render_view_->Send( | 97 render_view_->Send( |
81 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), | 98 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), |
82 progress_)); | 99 progress)); |
83 } | 100 } |
84 | 101 |
85 void LoadProgressTracker::ResetStates() { | 102 void LoadProgressTracker::ResetStates() { |
86 tracked_frame_ = NULL; | 103 progresses_.clear(); |
87 progress_ = 0.0; | 104 total_progress_ = 0.0; |
88 weak_factory_.InvalidateWeakPtrs(); | 105 weak_factory_.InvalidateWeakPtrs(); |
89 last_time_progress_sent_ = base::TimeTicks(); | 106 last_time_progress_sent_ = base::TimeTicks(); |
90 } | 107 } |
91 | 108 |
92 } // namespace content | 109 } // namespace content |
OLD | NEW |