Chromium Code Reviews| 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 } | 17 } |
| 18 | 18 |
| 19 LoadProgressTracker::LoadProgressTracker(RenderViewImpl* render_view) | 19 LoadProgressTracker::LoadProgressTracker(RenderViewImpl* render_view) |
| 20 : render_view_(render_view), | 20 : render_view_(render_view), |
| 21 tracked_frame_(NULL), | 21 total_progress_(0.0), |
| 22 progress_(0.0), | |
| 23 weak_factory_(this) { | 22 weak_factory_(this) { |
| 24 } | 23 } |
| 25 | 24 |
| 26 LoadProgressTracker::~LoadProgressTracker() { | 25 LoadProgressTracker::~LoadProgressTracker() { |
| 27 } | 26 } |
| 28 | 27 |
| 29 void LoadProgressTracker::DidStopLoading() { | 28 void LoadProgressTracker::DidStartLoading(RenderFrame* frame) { |
| 30 if (!tracked_frame_) | 29 progresses_[frame->GetRoutingID()] = 0.1; |
|
Charlie Reis
2014/02/27 21:52:07
Is 0.1 the standard starting point from somewhere
| |
| 30 SendChangeLoadProgress(); | |
| 31 } | |
| 32 | |
| 33 void LoadProgressTracker::DidStopLoading(RenderFrame* frame) { | |
| 34 if (progresses_.find(frame->GetRoutingID()) == progresses_.end()) | |
| 31 return; | 35 return; |
| 32 | 36 |
| 33 // Load stopped while we were still tracking load. Make sure we notify the | 37 // Load stopped while we were still tracking load. Make sure we notify the |
| 34 // browser that load is complete. | 38 // browser that load is complete. |
|
Charlie Reis
2014/02/27 21:52:07
nit: that load is complete -> that loading has pro
| |
| 35 progress_ = 1.0; | 39 progresses_[frame->GetRoutingID()] = 1.0; |
| 36 SendChangeLoadProgress(); | 40 SendChangeLoadProgress(); |
| 37 // Then we clean-up our states. | 41 // Then we clean-up our states. |
| 38 ResetStates(); | 42 progresses_.erase(frame->GetRoutingID()); |
| 43 if (total_progress_ == 1.0) | |
| 44 ResetStates(); | |
| 39 } | 45 } |
| 40 | 46 |
| 41 void LoadProgressTracker::DidChangeLoadProgress(blink::WebFrame* frame, | 47 void LoadProgressTracker::DidChangeLoadProgress(RenderFrame* frame, |
| 42 double progress) { | 48 double progress) { |
| 43 if (tracked_frame_ && frame != tracked_frame_) | 49 progresses_[frame->GetRoutingID()] = progress; |
| 44 return; | |
| 45 | |
| 46 if (!tracked_frame_) | |
| 47 tracked_frame_ = frame; | |
| 48 | |
| 49 progress_ = progress; | |
| 50 | 50 |
| 51 // We send the progress change to the browser immediately for the first and | 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 | 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 | 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. | 54 // sure to immediately send progress report if enough time has passed. |
| 55 base::TimeDelta min_delay = | 55 base::TimeDelta min_delay = |
| 56 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenUpdatesMS); | 56 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenUpdatesMS); |
| 57 if (progress == 1.0 || last_time_progress_sent_.is_null() || | 57 if (progress == 1.0 || last_time_progress_sent_.is_null() || |
| 58 base::TimeTicks::Now() - last_time_progress_sent_ > | 58 base::TimeTicks::Now() - last_time_progress_sent_ > |
| 59 min_delay) { | 59 min_delay) { |
| 60 // If there is a pending task to send progress, it is now obsolete. | 60 // If there is a pending task to send progress, it is now obsolete. |
| 61 weak_factory_.InvalidateWeakPtrs(); | 61 weak_factory_.InvalidateWeakPtrs(); |
| 62 SendChangeLoadProgress(); | 62 SendChangeLoadProgress(); |
| 63 if (progress == 1.0) | 63 if (total_progress_ == 1.0) |
| 64 ResetStates(); | 64 ResetStates(); |
|
Charlie Reis
2014/02/27 21:52:07
nit: 2 space indent.
| |
| 65 return; | 65 return; |
| 66 } | 66 } |
| 67 | 67 |
| 68 if (weak_factory_.HasWeakPtrs()) | 68 if (weak_factory_.HasWeakPtrs()) |
| 69 return; | 69 return; |
| 70 | 70 |
| 71 base::MessageLoop::current()->PostDelayedTask( | 71 base::MessageLoop::current()->PostDelayedTask( |
| 72 FROM_HERE, | 72 FROM_HERE, |
| 73 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, | 73 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, |
| 74 weak_factory_.GetWeakPtr()), | 74 weak_factory_.GetWeakPtr()), |
| 75 min_delay); | 75 min_delay); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void LoadProgressTracker::SendChangeLoadProgress() { | 78 void LoadProgressTracker::SendChangeLoadProgress() { |
| 79 last_time_progress_sent_ = base::TimeTicks::Now(); | 79 last_time_progress_sent_ = base::TimeTicks::Now(); |
| 80 double progress = 1.0; | |
| 81 ProgressMap::iterator end = progresses_.end(); | |
| 82 for (ProgressMap::iterator it = progresses_.begin(); it != end; ++it) { | |
| 83 progress *= it->second; | |
|
Charlie Reis
2014/02/27 21:52:07
This doesn't make sense to me. Consider a page wi
Nate Chapin
2014/02/27 21:58:39
You're right. There's a defense against decreasing
Charlie Reis
2014/02/28 02:02:38
Oh, I even missed that in my earlier analysis. Th
| |
| 84 } | |
| 85 if (progress < total_progress_) | |
| 86 return; | |
| 87 total_progress_ = progress; | |
| 88 | |
| 80 render_view_->Send( | 89 render_view_->Send( |
| 81 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), | 90 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), |
| 82 progress_)); | 91 progress)); |
| 83 } | 92 } |
| 84 | 93 |
| 85 void LoadProgressTracker::ResetStates() { | 94 void LoadProgressTracker::ResetStates() { |
| 86 tracked_frame_ = NULL; | 95 progresses_.clear(); |
| 87 progress_ = 0.0; | 96 total_progress_ = 0.0; |
| 88 weak_factory_.InvalidateWeakPtrs(); | 97 weak_factory_.InvalidateWeakPtrs(); |
| 89 last_time_progress_sent_ = base::TimeTicks(); | 98 last_time_progress_sent_ = base::TimeTicks(); |
| 90 } | 99 } |
| 91 | 100 |
| 92 } // namespace content | 101 } // namespace content |
| OLD | NEW |