Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Unified Diff: chrome/renderer/user_script_idle_scheduler.cc

Issue 339064: Add new user script injection point: document_idle. (Closed)
Patch Set: smaller, cleaner, better Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/renderer/user_script_idle_scheduler.cc
diff --git a/chrome/renderer/user_script_idle_scheduler.cc b/chrome/renderer/user_script_idle_scheduler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..27c989c77cd07fb6e9d54c7a71647557b6578e7e
--- /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) {
abarth-chromium 2009/10/31 07:07:39 These should each be on a different line according
+}
+
+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_->OnUserScriptIdleLocation(frame_);
+ Cancel();
+ has_run_ = true;
+}

Powered by Google App Engine
This is Rietveld 408576698