diff options
Diffstat (limited to 'chrome/renderer')
-rw-r--r-- | chrome/renderer/load_progress_tracker.cc | 85 | ||||
-rw-r--r-- | chrome/renderer/load_progress_tracker.h | 46 | ||||
-rw-r--r-- | chrome/renderer/render_view.cc | 9 | ||||
-rw-r--r-- | chrome/renderer/render_view.h | 9 |
4 files changed, 149 insertions, 0 deletions
diff --git a/chrome/renderer/load_progress_tracker.cc b/chrome/renderer/load_progress_tracker.cc new file mode 100644 index 0000000..0ebba4b --- /dev/null +++ b/chrome/renderer/load_progress_tracker.cc @@ -0,0 +1,85 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/renderer/load_progress_tracker.h" + +#include "base/message_loop.h" +#include "chrome/renderer/render_view.h" + +namespace { + +const int kMinimumDelayBetweenUpdatesMS = 100; + +} + +LoadProgressTracker::LoadProgressTracker(RenderView* render_view) + : render_view_(render_view), + tracked_frame_(NULL), + progress_(0.0), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { +} + +LoadProgressTracker::~LoadProgressTracker() { +} + +void LoadProgressTracker::DidStopLoading() { + if (!tracked_frame_) + return; + + // Load stoped while we were still tracking load. Make sure we notify the + // browser that load is complete. + progress_ = 1.0; + SendChangeLoadProgress(); + // Then we clean-up our states. + ResetStates(); +} + +void LoadProgressTracker::DidChangeLoadProgress(WebKit::WebFrame* frame, + double progress) { + if (tracked_frame_ && frame != tracked_frame_) + return; + + if (!tracked_frame_) + tracked_frame_ = frame; + + progress_ = progress; + + // We send the progress change to the browser immediately for the first and + // last updates. Also, since the message loop may be pretty busy when a page + // is loaded, it might not execute a posted task in a timely manner so we make + // sure to immediately send progress report if enough time has passed. + if (progress == 1.0 || last_time_progress_sent_.is_null() || + (base::TimeTicks::Now() - last_time_progress_sent_).InMilliseconds() > + kMinimumDelayBetweenUpdatesMS) { + // If there is a pending task to send progress, it is now obsolete. + method_factory_.RevokeAll(); + SendChangeLoadProgress(); + if (progress == 1.0) + ResetStates(); + return; + } + + if (!method_factory_.empty()) + return; + + MessageLoop::current()->PostDelayedTask( + FROM_HERE, + method_factory_.NewRunnableMethod( + &LoadProgressTracker::SendChangeLoadProgress), + kMinimumDelayBetweenUpdatesMS); +} + +void LoadProgressTracker::SendChangeLoadProgress() { + last_time_progress_sent_ = base::TimeTicks::Now(); + render_view_->Send( + new ViewHostMsg_DidChangeLoadProgress(render_view_->routing_id(), + progress_)); +} + +void LoadProgressTracker::ResetStates() { + tracked_frame_ = NULL; + progress_ = 0.0; + method_factory_.RevokeAll(); + last_time_progress_sent_ = base::TimeTicks(); +} diff --git a/chrome/renderer/load_progress_tracker.h b/chrome/renderer/load_progress_tracker.h new file mode 100644 index 0000000..a9455c3 --- /dev/null +++ b/chrome/renderer/load_progress_tracker.h @@ -0,0 +1,46 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_RENDERER_LOAD_PROGRESS_TRACKER_H_ +#define CHROME_RENDERER_LOAD_PROGRESS_TRACKER_H_ + +#include "base/logging.h" +#include "base/task.h" +#include "base/time.h" + +class RenderView; + +namespace WebKit { +class WebFrame; +} + +class LoadProgressTracker { + public: + explicit LoadProgressTracker(RenderView* render_view); + ~LoadProgressTracker(); + + void DidStopLoading(); + + void DidChangeLoadProgress(WebKit::WebFrame* frame, double progress); + + private: + void ResetStates(); + + void SendChangeLoadProgress(); + + RenderView* render_view_; + + WebKit::WebFrame* tracked_frame_; + + double progress_; + + base::TimeTicks last_time_progress_sent_; + + ScopedRunnableMethodFactory<LoadProgressTracker> method_factory_; + + DISALLOW_COPY_AND_ASSIGN(LoadProgressTracker); +}; + +#endif // CHROME_RENDERER_LOAD_PROGRESS_TRACKER_H_ + diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc index ca0c0e5..b7240f5 100644 --- a/chrome/renderer/render_view.cc +++ b/chrome/renderer/render_view.cc @@ -69,6 +69,7 @@ #include "chrome/renderer/geolocation_dispatcher_old.h" #endif #include "chrome/renderer/ggl/ggl.h" +#include "chrome/renderer/load_progress_tracker.h" #include "chrome/renderer/localized_error.h" #include "chrome/renderer/media/audio_renderer_impl.h" #include "chrome/renderer/media/ipc_video_decoder.h" @@ -2200,6 +2201,9 @@ void RenderView::didStopLoading() { Send(new ViewHostMsg_DidStopLoading(routing_id_)); + if (load_progress_tracker_ != NULL) + load_progress_tracker_->DidStopLoading(); + MessageLoop::current()->PostDelayedTask( FROM_HERE, page_info_method_factory_.NewRunnableMethod( @@ -2207,6 +2211,11 @@ void RenderView::didStopLoading() { send_content_state_immediately_ ? 0 : kDelayForCaptureMs); } +void RenderView::didChangeLoadProgress(WebFrame* frame, double load_progress) { + if (load_progress_tracker_ != NULL) + load_progress_tracker_->DidChangeLoadProgress(frame, load_progress); +} + bool RenderView::isSmartInsertDeleteEnabled() { #if defined(OS_MACOSX) return true; diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h index 8b0fb5b..3d73c7c 100644 --- a/chrome/renderer/render_view.h +++ b/chrome/renderer/render_view.h @@ -68,6 +68,7 @@ class GeolocationDispatcher; class GeolocationDispatcherOld; class GURL; class ListValue; +class LoadProgressTracker; class NavigationState; class NotificationProvider; class PageClickTracker; @@ -296,6 +297,9 @@ class RenderView : public RenderWidget, bool InstallWebApplicationUsingDefinitionFile(WebKit::WebFrame* frame, string16* error); + // Sets whether the renderer should report load progress to the browser. + void SetReportLoadProgressEnabled(bool enabled); + // Extensions ---------------------------------------------------------------- void SendExtensionRequest(const ViewHostMsg_DomMessage_Params& params); @@ -412,6 +416,8 @@ class RenderView : public RenderWidget, virtual WebKit::WebNotificationPresenter* notificationPresenter(); virtual void didStartLoading(); virtual void didStopLoading(); + virtual void didChangeLoadProgress(WebKit::WebFrame* frame, + double load_progress); virtual bool isSmartInsertDeleteEnabled(); virtual bool isSelectTrailingWhitespaceEnabled(); virtual void didChangeSelection(bool is_selection_empty); @@ -1486,6 +1492,9 @@ class RenderView : public RenderWidget, // The custom menu event listener, if any. CustomMenuListener* custom_menu_listener_; + // Reports load progress to the browser. + scoped_ptr<LoadProgressTracker> load_progress_tracker_; + // --------------------------------------------------------------------------- // ADDING NEW DATA? Please see if it fits appropriately in one of the above // sections rather than throwing it randomly at the end. If you're adding a |