diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-03 01:29:39 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-03 01:29:39 +0000 |
commit | 5442dafd85700e323fe6c332eeec0f17de6f7891 (patch) | |
tree | 9e065d301222177c4371e0522424deb8f07ba488 /chrome/renderer/user_script_idle_scheduler.cc | |
parent | 40fc8b766191d37dbcbcb67894d233da2899821c (diff) | |
download | chromium_src-5442dafd85700e323fe6c332eeec0f17de6f7891.zip chromium_src-5442dafd85700e323fe6c332eeec0f17de6f7891.tar.gz chromium_src-5442dafd85700e323fe6c332eeec0f17de6f7891.tar.bz2 |
Add new user script injection point "document_idle" and make it the default.
Semantically, document-idle means "when the DOM is ready and layout has been idle for awhile", or more loosely, "as soon as we get around to it".
Right now this uses a simple heuristic. It injects scripts 200ms after DOMContentLoaded, or immediately after onload, whichever happens first.
BUG=26126
TEST=Manual. Extensions with content scripts should work.
Review URL: http://codereview.chromium.org/339064
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30784 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/user_script_idle_scheduler.cc')
-rw-r--r-- | chrome/renderer/user_script_idle_scheduler.cc | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/chrome/renderer/user_script_idle_scheduler.cc b/chrome/renderer/user_script_idle_scheduler.cc new file mode 100644 index 0000000..2468bf1 --- /dev/null +++ b/chrome/renderer/user_script_idle_scheduler.cc @@ -0,0 +1,47 @@ +// Copyright (c) 2009 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/user_script_idle_scheduler.h" + +#include "base/message_loop.h" +#include "chrome/renderer/render_view.h" + +namespace { +// The length of time to wait after the DOM is complete to try and run user +// scripts. +const int kUserScriptIdleTimeoutMs = 200; +} + +UserScriptIdleScheduler::UserScriptIdleScheduler(RenderView* view, + WebKit::WebFrame* frame) + : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), view_(view), + frame_(frame), has_run_(false) { +} + +void UserScriptIdleScheduler::DidFinishDocumentLoad() { + MessageLoop::current()->PostDelayedTask(FROM_HERE, + method_factory_.NewRunnableMethod(&UserScriptIdleScheduler::MaybeRun), + kUserScriptIdleTimeoutMs); +} + +void UserScriptIdleScheduler::DidFinishLoad() { + // Ensure that running scripts does not keep any progress UI running. + MessageLoop::current()->PostTask(FROM_HERE, + method_factory_.NewRunnableMethod(&UserScriptIdleScheduler::MaybeRun)); +} + +void UserScriptIdleScheduler::Cancel() { + view_ = NULL; + frame_ = NULL; +} + +void UserScriptIdleScheduler::MaybeRun() { + if (!view_) + return; + + DCHECK(frame_); + view_->OnUserScriptIdleTriggered(frame_); + Cancel(); + has_run_ = true; +} |