Index: chrome/renderer/extensions/user_script_idle_scheduler.cc |
diff --git a/chrome/renderer/extensions/user_script_idle_scheduler.cc b/chrome/renderer/extensions/user_script_idle_scheduler.cc |
index 13853d630d4081d1618b733c67c68972c5ae9732..6633c3c8a720e8680c6f21373c14f58f516982a1 100644 |
--- a/chrome/renderer/extensions/user_script_idle_scheduler.cc |
+++ b/chrome/renderer/extensions/user_script_idle_scheduler.cc |
@@ -8,6 +8,7 @@ |
#include "base/message_loop.h" |
#include "chrome/common/extensions/extension_error_utils.h" |
#include "chrome/common/extensions/extension_messages.h" |
+#include "chrome/common/extensions/user_script.h" |
#include "chrome/renderer/extensions/extension_dispatcher.h" |
#include "chrome/renderer/extensions/extension_groups.h" |
#include "chrome/renderer/extensions/extension_helper.h" |
@@ -34,7 +35,11 @@ UserScriptIdleScheduler::UserScriptIdleScheduler( |
: ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
frame_(frame), |
has_run_(false), |
+ current_location_(-1), |
extension_dispatcher_(extension_dispatcher) { |
+ for (int i = 0; i < UserScript::RUN_LOCATION_LAST; ++i) |
+ pending_code_execution_queue_.push_back( |
Aaron Boodman
2012/03/03 00:59:10
You're essentially using this like a map. So why n
eaugusti
2012/03/27 00:43:33
Done.
|
+ std::queue<linked_ptr<ExtensionMsg_ExecuteCode_Params> >()); |
} |
UserScriptIdleScheduler::~UserScriptIdleScheduler() { |
@@ -42,24 +47,39 @@ UserScriptIdleScheduler::~UserScriptIdleScheduler() { |
void UserScriptIdleScheduler::ExecuteCode( |
const ExtensionMsg_ExecuteCode_Params& params) { |
- if (!has_run_) { |
- pending_code_execution_queue_.push( |
+ if (!has_run_ && |
+ params.run_at != UserScript::DOCUMENT_NOW && |
Mihai Parparita -not on Chrome
2012/03/01 00:40:08
Is it OK to always immediately execute script when
Aaron Boodman
2012/03/03 00:59:10
Agree with Mihai that we should always wait until
eaugusti
2012/03/27 00:43:33
Done.
eaugusti
2012/03/27 00:43:34
Done.
|
+ current_location_ < params.run_at) { |
+ pending_code_execution_queue_[params.run_at].push( |
linked_ptr<ExtensionMsg_ExecuteCode_Params>( |
new ExtensionMsg_ExecuteCode_Params(params))); |
return; |
} |
- |
ExecuteCodeImpl(params); |
} |
+void UserScriptIdleScheduler::DidCreateDocumentElement() { |
+ current_location_ = UserScript::DOCUMENT_START; |
+ MessageLoop::current()->PostTask( |
Aaron Boodman
2012/03/03 00:59:10
Why PostTask as opposed to running immediately? Th
eaugusti
2012/03/27 00:43:34
Done.
|
+ FROM_HERE, base::Bind(&UserScriptIdleScheduler::MaybeRun, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
void UserScriptIdleScheduler::DidFinishDocumentLoad() { |
- MessageLoop::current()->PostDelayedTask( |
+ current_location_ = UserScript::DOCUMENT_END; |
+ // Schedule a run for DOCUMENT_END |
+ MessageLoop::current()->PostTask( |
Aaron Boodman
2012/03/03 00:59:10
Same here. Don't think PostTask is necessary.
eaugusti
2012/03/27 00:43:34
Done.
|
FROM_HERE, base::Bind(&UserScriptIdleScheduler::MaybeRun, |
+ weak_factory_.GetWeakPtr())); |
+ // Schedule a run for DOCUMENT_IDLE |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, base::Bind(&UserScriptIdleScheduler::IdleTimeout, |
weak_factory_.GetWeakPtr()), |
base::TimeDelta::FromMilliseconds(kUserScriptIdleTimeoutMs)); |
} |
void UserScriptIdleScheduler::DidFinishLoad() { |
+ current_location_ = UserScript::DOCUMENT_IDLE; |
// Ensure that running scripts does not keep any progress UI running. |
MessageLoop::current()->PostTask( |
FROM_HERE, base::Bind(&UserScriptIdleScheduler::MaybeRun, |
@@ -69,14 +89,26 @@ void UserScriptIdleScheduler::DidFinishLoad() { |
void UserScriptIdleScheduler::DidStartProvisionalLoad() { |
// The frame is navigating, so reset the state since we'll want to inject |
// scripts once the load finishes. |
+ current_location_ = -1; |
has_run_ = false; |
weak_factory_.InvalidateWeakPtrs(); |
- while (!pending_code_execution_queue_.empty()) |
- pending_code_execution_queue_.pop(); |
+ Schedule::iterator itr = pending_code_execution_queue_.begin(); |
+ for (itr = pending_code_execution_queue_.begin(); |
+ itr != pending_code_execution_queue_.end(); ++itr) { |
+ while (!itr->empty()) |
+ itr->pop(); |
+ } |
+} |
+ |
+void UserScriptIdleScheduler::IdleTimeout() { |
+ current_location_ = UserScript::DOCUMENT_IDLE; |
+ MessageLoop::current()->PostTask( |
+ FROM_HERE, base::Bind(&UserScriptIdleScheduler::MaybeRun, |
+ weak_factory_.GetWeakPtr())); |
} |
void UserScriptIdleScheduler::MaybeRun() { |
- if (has_run_) |
+ if (has_run_ || current_location_ == -1) |
return; |
// Note: we must set this before calling ExecuteCodeImpl, because that may |
Mihai Parparita -not on Chrome
2012/03/01 00:40:08
This comment doesn't seem to be true anymore (Exec
eaugusti
2012/03/27 00:43:34
Done.
|
@@ -86,13 +118,16 @@ void UserScriptIdleScheduler::MaybeRun() { |
has_run_ = true; |
extension_dispatcher_->user_script_slave()->InjectScripts( |
- frame_, UserScript::DOCUMENT_IDLE); |
- |
- while (!pending_code_execution_queue_.empty()) { |
- linked_ptr<ExtensionMsg_ExecuteCode_Params>& params = |
- pending_code_execution_queue_.front(); |
- ExecuteCodeImpl(*params); |
- pending_code_execution_queue_.pop(); |
+ frame_, (UserScript::RunLocation)current_location_); |
Mihai Parparita -not on Chrome
2012/03/01 00:40:08
Use C++-style casts: http://google-styleguide.goog
eaugusti
2012/03/27 00:43:34
Done.
|
+ |
+ // Run all tasks from the current time and earlier. |
+ for (int runTime = 0; runTime <= current_location_; ++runTime) { |
+ while (!pending_code_execution_queue_[runTime].empty()) { |
+ linked_ptr<ExtensionMsg_ExecuteCode_Params>& params = |
+ pending_code_execution_queue_[runTime].front(); |
+ ExecuteCodeImpl(*params); |
+ pending_code_execution_queue_[runTime].pop(); |
+ } |
} |
} |