OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/message_loop.h" | 8 #include "base/message_loop.h" |
8 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
9 #include "content/renderer/render_view_impl.h" | 10 #include "content/renderer/render_view_impl.h" |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 const int kMinimumDelayBetweenUpdatesMS = 100; | 14 const int kMinimumDelayBetweenUpdatesMS = 100; |
14 | 15 |
15 } | 16 } |
16 | 17 |
17 LoadProgressTracker::LoadProgressTracker(RenderViewImpl* render_view) | 18 LoadProgressTracker::LoadProgressTracker(RenderViewImpl* render_view) |
18 : render_view_(render_view), | 19 : render_view_(render_view), |
19 tracked_frame_(NULL), | 20 tracked_frame_(NULL), |
20 progress_(0.0), | 21 progress_(0.0), |
21 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 22 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
22 } | 23 } |
23 | 24 |
24 LoadProgressTracker::~LoadProgressTracker() { | 25 LoadProgressTracker::~LoadProgressTracker() { |
25 } | 26 } |
26 | 27 |
27 void LoadProgressTracker::DidStopLoading() { | 28 void LoadProgressTracker::DidStopLoading() { |
28 if (!tracked_frame_) | 29 if (!tracked_frame_) |
29 return; | 30 return; |
30 | 31 |
31 // Load stoped while we were still tracking load. Make sure we notify the | 32 // Load stoped while we were still tracking load. Make sure we notify the |
(...skipping 15 matching lines...) Expand all Loading... |
47 progress_ = progress; | 48 progress_ = progress; |
48 | 49 |
49 // 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 |
50 // 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 |
51 // 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 |
52 // sure to immediately send progress report if enough time has passed. | 53 // sure to immediately send progress report if enough time has passed. |
53 if (progress == 1.0 || last_time_progress_sent_.is_null() || | 54 if (progress == 1.0 || last_time_progress_sent_.is_null() || |
54 (base::TimeTicks::Now() - last_time_progress_sent_).InMilliseconds() > | 55 (base::TimeTicks::Now() - last_time_progress_sent_).InMilliseconds() > |
55 kMinimumDelayBetweenUpdatesMS) { | 56 kMinimumDelayBetweenUpdatesMS) { |
56 // 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. |
57 method_factory_.RevokeAll(); | 58 weak_factory_.InvalidateWeakPtrs(); |
58 SendChangeLoadProgress(); | 59 SendChangeLoadProgress(); |
59 if (progress == 1.0) | 60 if (progress == 1.0) |
60 ResetStates(); | 61 ResetStates(); |
61 return; | 62 return; |
62 } | 63 } |
63 | 64 |
64 if (!method_factory_.empty()) | 65 if (weak_factory_.HasWeakPtrs()) |
65 return; | 66 return; |
66 | 67 |
67 MessageLoop::current()->PostDelayedTask( | 68 MessageLoop::current()->PostDelayedTask( |
68 FROM_HERE, | 69 FROM_HERE, |
69 method_factory_.NewRunnableMethod( | 70 base::Bind(&LoadProgressTracker::SendChangeLoadProgress, |
70 &LoadProgressTracker::SendChangeLoadProgress), | 71 weak_factory_.GetWeakPtr()), |
71 kMinimumDelayBetweenUpdatesMS); | 72 kMinimumDelayBetweenUpdatesMS); |
72 } | 73 } |
73 | 74 |
74 void LoadProgressTracker::SendChangeLoadProgress() { | 75 void LoadProgressTracker::SendChangeLoadProgress() { |
75 last_time_progress_sent_ = base::TimeTicks::Now(); | 76 last_time_progress_sent_ = base::TimeTicks::Now(); |
76 render_view_->Send( | 77 render_view_->Send( |
77 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), | 78 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), |
78 progress_)); | 79 progress_)); |
79 } | 80 } |
80 | 81 |
81 void LoadProgressTracker::ResetStates() { | 82 void LoadProgressTracker::ResetStates() { |
82 tracked_frame_ = NULL; | 83 tracked_frame_ = NULL; |
83 progress_ = 0.0; | 84 progress_ = 0.0; |
84 method_factory_.RevokeAll(); | 85 weak_factory_.InvalidateWeakPtrs(); |
85 last_time_progress_sent_ = base::TimeTicks(); | 86 last_time_progress_sent_ = base::TimeTicks(); |
86 } | 87 } |
OLD | NEW |