| 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 // minimum progress value. |
| 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_ > min_delay) { |
| 59 min_delay) { | |
| 60 // If there is a pending task to send progress, it is now obsolete. | 62 // If there is a pending task to send progress, it is now obsolete. |
| 61 weak_factory_.InvalidateWeakPtrs(); | 63 weak_factory_.InvalidateWeakPtrs(); |
| 62 SendChangeLoadProgress(); | 64 SendChangeLoadProgress(); |
| 63 if (progress == 1.0) | 65 if (total_progress_ == 1.0) |
| 64 ResetStates(); | 66 ResetStates(); |
| 65 return; | 67 return; |
| 66 } | 68 } |
| 67 | 69 |
| 68 if (weak_factory_.HasWeakPtrs()) | 70 if (weak_factory_.HasWeakPtrs()) |
| 69 return; | 71 return; |
| 70 | 72 |
| 71 base::MessageLoop::current()->PostDelayedTask( | 73 base::MessageLoop::current()->PostDelayedTask( |
| 72 FROM_HERE, | 74 FROM_HERE, |
| 73 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, | 75 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, |
| 74 weak_factory_.GetWeakPtr()), | 76 weak_factory_.GetWeakPtr()), |
| 75 min_delay); | 77 min_delay); |
| 76 } | 78 } |
| 77 | 79 |
| 78 void LoadProgressTracker::SendChangeLoadProgress() { | 80 void LoadProgressTracker::SendChangeLoadProgress() { |
| 79 last_time_progress_sent_ = base::TimeTicks::Now(); | 81 last_time_progress_sent_ = base::TimeTicks::Now(); |
| 82 double progress = 0.0; |
| 83 unsigned frameCount = 0; |
| 84 ProgressMap::iterator end = progresses_.end(); |
| 85 for (ProgressMap::iterator it = progresses_.begin(); it != end; ++it) { |
| 86 progress += it->second; |
| 87 frameCount++; |
| 88 } |
| 89 if (frameCount == 0) |
| 90 return; |
| 91 progress /= frameCount; |
| 92 DCHECK(progress <= 1.0); |
| 93 |
| 94 if (progress <= total_progress_) |
| 95 return; |
| 96 total_progress_ = progress; |
| 97 |
| 80 render_view_->Send( | 98 render_view_->Send( |
| 81 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), | 99 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), |
| 82 progress_)); | 100 progress)); |
| 83 } | 101 } |
| 84 | 102 |
| 85 void LoadProgressTracker::ResetStates() { | 103 void LoadProgressTracker::ResetStates() { |
| 86 tracked_frame_ = NULL; | 104 progresses_.clear(); |
| 87 progress_ = 0.0; | 105 total_progress_ = 0.0; |
| 88 weak_factory_.InvalidateWeakPtrs(); | 106 weak_factory_.InvalidateWeakPtrs(); |
| 89 last_time_progress_sent_ = base::TimeTicks(); | 107 last_time_progress_sent_ = base::TimeTicks(); |
| 90 } | 108 } |
| 91 | 109 |
| 92 } // namespace content | 110 } // namespace content |
| OLD | NEW |