| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/renderer/load_progress_tracker.h" | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "chrome/common/render_messages.h" | |
| 9 #include "chrome/renderer/render_view.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 const int kMinimumDelayBetweenUpdatesMS = 100; | |
| 14 | |
| 15 } | |
| 16 | |
| 17 LoadProgressTracker::LoadProgressTracker(RenderView* render_view) | |
| 18 : render_view_(render_view), | |
| 19 tracked_frame_(NULL), | |
| 20 progress_(0.0), | |
| 21 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
| 22 } | |
| 23 | |
| 24 LoadProgressTracker::~LoadProgressTracker() { | |
| 25 } | |
| 26 | |
| 27 void LoadProgressTracker::DidStopLoading() { | |
| 28 if (!tracked_frame_) | |
| 29 return; | |
| 30 | |
| 31 // Load stoped while we were still tracking load. Make sure we notify the | |
| 32 // browser that load is complete. | |
| 33 progress_ = 1.0; | |
| 34 SendChangeLoadProgress(); | |
| 35 // Then we clean-up our states. | |
| 36 ResetStates(); | |
| 37 } | |
| 38 | |
| 39 void LoadProgressTracker::DidChangeLoadProgress(WebKit::WebFrame* frame, | |
| 40 double progress) { | |
| 41 if (tracked_frame_ && frame != tracked_frame_) | |
| 42 return; | |
| 43 | |
| 44 if (!tracked_frame_) | |
| 45 tracked_frame_ = frame; | |
| 46 | |
| 47 progress_ = progress; | |
| 48 | |
| 49 // 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 // 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 if (progress == 1.0 || last_time_progress_sent_.is_null() || | |
| 54 (base::TimeTicks::Now() - last_time_progress_sent_).InMilliseconds() > | |
| 55 kMinimumDelayBetweenUpdatesMS) { | |
| 56 // If there is a pending task to send progress, it is now obsolete. | |
| 57 method_factory_.RevokeAll(); | |
| 58 SendChangeLoadProgress(); | |
| 59 if (progress == 1.0) | |
| 60 ResetStates(); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 if (!method_factory_.empty()) | |
| 65 return; | |
| 66 | |
| 67 MessageLoop::current()->PostDelayedTask( | |
| 68 FROM_HERE, | |
| 69 method_factory_.NewRunnableMethod( | |
| 70 &LoadProgressTracker::SendChangeLoadProgress), | |
| 71 kMinimumDelayBetweenUpdatesMS); | |
| 72 } | |
| 73 | |
| 74 void LoadProgressTracker::SendChangeLoadProgress() { | |
| 75 last_time_progress_sent_ = base::TimeTicks::Now(); | |
| 76 render_view_->Send( | |
| 77 new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), | |
| 78 progress_)); | |
| 79 } | |
| 80 | |
| 81 void LoadProgressTracker::ResetStates() { | |
| 82 tracked_frame_ = NULL; | |
| 83 progress_ = 0.0; | |
| 84 method_factory_.RevokeAll(); | |
| 85 last_time_progress_sent_ = base::TimeTicks(); | |
| 86 } | |
| OLD | NEW |