OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 8 #include "base/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 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 | 44 |
45 if (!tracked_frame_) | 45 if (!tracked_frame_) |
46 tracked_frame_ = frame; | 46 tracked_frame_ = frame; |
47 | 47 |
48 progress_ = progress; | 48 progress_ = progress; |
49 | 49 |
50 // We send the progress change to the browser immediately for the first and | 50 // We send the progress change to the browser immediately for the first and |
51 // last updates. Also, since the message loop may be pretty busy when a page | 51 // last updates. Also, since the message loop may be pretty busy when a page |
52 // is loaded, it might not execute a posted task in a timely manner so we make | 52 // is loaded, it might not execute a posted task in a timely manner so we make |
53 // sure to immediately send progress report if enough time has passed. | 53 // sure to immediately send progress report if enough time has passed. |
54 base::TimeDelta min_delay = | |
55 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenUpdatesMS); | |
56 if (progress == 1.0 || last_time_progress_sent_.is_null() || | 54 if (progress == 1.0 || last_time_progress_sent_.is_null() || |
57 base::TimeTicks::Now() - last_time_progress_sent_ > | 55 (base::TimeTicks::Now() - last_time_progress_sent_).InMilliseconds() > |
58 min_delay) { | 56 kMinimumDelayBetweenUpdatesMS) { |
59 // If there is a pending task to send progress, it is now obsolete. | 57 // If there is a pending task to send progress, it is now obsolete. |
60 weak_factory_.InvalidateWeakPtrs(); | 58 weak_factory_.InvalidateWeakPtrs(); |
61 SendChangeLoadProgress(); | 59 SendChangeLoadProgress(); |
62 if (progress == 1.0) | 60 if (progress == 1.0) |
63 ResetStates(); | 61 ResetStates(); |
64 return; | 62 return; |
65 } | 63 } |
66 | 64 |
67 if (weak_factory_.HasWeakPtrs()) | 65 if (weak_factory_.HasWeakPtrs()) |
68 return; | 66 return; |
69 | 67 |
70 MessageLoop::current()->PostDelayedTask( | 68 MessageLoop::current()->PostDelayedTask( |
71 FROM_HERE, | 69 FROM_HERE, |
72 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, | 70 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, |
73 weak_factory_.GetWeakPtr()), | 71 weak_factory_.GetWeakPtr()), |
74 min_delay); | 72 kMinimumDelayBetweenUpdatesMS); |
75 } | 73 } |
76 | 74 |
77 void LoadProgressTracker::SendChangeLoadProgress() { | 75 void LoadProgressTracker::SendChangeLoadProgress() { |
78 last_time_progress_sent_ = base::TimeTicks::Now(); | 76 last_time_progress_sent_ = base::TimeTicks::Now(); |
79 render_view_->Send( | 77 render_view_->Send( |
80 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), | 78 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), |
81 progress_)); | 79 progress_)); |
82 } | 80 } |
83 | 81 |
84 void LoadProgressTracker::ResetStates() { | 82 void LoadProgressTracker::ResetStates() { |
85 tracked_frame_ = NULL; | 83 tracked_frame_ = NULL; |
86 progress_ = 0.0; | 84 progress_ = 0.0; |
87 weak_factory_.InvalidateWeakPtrs(); | 85 weak_factory_.InvalidateWeakPtrs(); |
88 last_time_progress_sent_ = base::TimeTicks(); | 86 last_time_progress_sent_ = base::TimeTicks(); |
89 } | 87 } |
OLD | NEW |