| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/extensions/user_script_idle_scheduler.h" | 5 #include "chrome/renderer/extensions/user_script_idle_scheduler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 8 #include "chrome/common/extensions/extension_error_utils.h" | 9 #include "chrome/common/extensions/extension_error_utils.h" |
| 9 #include "chrome/common/extensions/extension_messages.h" | 10 #include "chrome/common/extensions/extension_messages.h" |
| 10 #include "chrome/renderer/extensions/extension_dispatcher.h" | 11 #include "chrome/renderer/extensions/extension_dispatcher.h" |
| 11 #include "chrome/renderer/extensions/extension_groups.h" | 12 #include "chrome/renderer/extensions/extension_groups.h" |
| 12 #include "chrome/renderer/extensions/extension_helper.h" | 13 #include "chrome/renderer/extensions/extension_helper.h" |
| 13 #include "chrome/renderer/extensions/user_script_slave.h" | 14 #include "chrome/renderer/extensions/user_script_slave.h" |
| 14 #include "content/public/renderer/render_view.h" | 15 #include "content/public/renderer/render_view.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 // The length of time to wait after the DOM is complete to try and run user | 22 // The length of time to wait after the DOM is complete to try and run user |
| 22 // scripts. | 23 // scripts. |
| 23 const int kUserScriptIdleTimeoutMs = 200; | 24 const int kUserScriptIdleTimeoutMs = 200; |
| 24 } | 25 } |
| 25 | 26 |
| 26 using WebKit::WebDocument; | 27 using WebKit::WebDocument; |
| 27 using WebKit::WebFrame; | 28 using WebKit::WebFrame; |
| 28 using WebKit::WebString; | 29 using WebKit::WebString; |
| 29 using WebKit::WebView; | 30 using WebKit::WebView; |
| 30 | 31 |
| 31 UserScriptIdleScheduler::UserScriptIdleScheduler( | 32 UserScriptIdleScheduler::UserScriptIdleScheduler( |
| 32 WebFrame* frame, ExtensionDispatcher* extension_dispatcher) | 33 WebFrame* frame, ExtensionDispatcher* extension_dispatcher) |
| 33 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 34 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 34 frame_(frame), | 35 frame_(frame), |
| 35 has_run_(false), | 36 has_run_(false), |
| 36 extension_dispatcher_(extension_dispatcher) { | 37 extension_dispatcher_(extension_dispatcher) { |
| 37 } | 38 } |
| 38 | 39 |
| 39 UserScriptIdleScheduler::~UserScriptIdleScheduler() { | 40 UserScriptIdleScheduler::~UserScriptIdleScheduler() { |
| 40 } | 41 } |
| 41 | 42 |
| 42 void UserScriptIdleScheduler::ExecuteCode( | 43 void UserScriptIdleScheduler::ExecuteCode( |
| 43 const ExtensionMsg_ExecuteCode_Params& params) { | 44 const ExtensionMsg_ExecuteCode_Params& params) { |
| 44 if (!has_run_) { | 45 if (!has_run_) { |
| 45 pending_code_execution_queue_.push( | 46 pending_code_execution_queue_.push( |
| 46 linked_ptr<ExtensionMsg_ExecuteCode_Params>( | 47 linked_ptr<ExtensionMsg_ExecuteCode_Params>( |
| 47 new ExtensionMsg_ExecuteCode_Params(params))); | 48 new ExtensionMsg_ExecuteCode_Params(params))); |
| 48 return; | 49 return; |
| 49 } | 50 } |
| 50 | 51 |
| 51 ExecuteCodeImpl(params); | 52 ExecuteCodeImpl(params); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void UserScriptIdleScheduler::DidFinishDocumentLoad() { | 55 void UserScriptIdleScheduler::DidFinishDocumentLoad() { |
| 55 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 56 MessageLoop::current()->PostDelayedTask( |
| 56 method_factory_.NewRunnableMethod(&UserScriptIdleScheduler::MaybeRun), | 57 FROM_HERE, base::Bind(&UserScriptIdleScheduler::MaybeRun, |
| 58 weak_factory_.GetWeakPtr()), |
| 57 kUserScriptIdleTimeoutMs); | 59 kUserScriptIdleTimeoutMs); |
| 58 } | 60 } |
| 59 | 61 |
| 60 void UserScriptIdleScheduler::DidFinishLoad() { | 62 void UserScriptIdleScheduler::DidFinishLoad() { |
| 61 // Ensure that running scripts does not keep any progress UI running. | 63 // Ensure that running scripts does not keep any progress UI running. |
| 62 MessageLoop::current()->PostTask(FROM_HERE, | 64 MessageLoop::current()->PostTask( |
| 63 method_factory_.NewRunnableMethod(&UserScriptIdleScheduler::MaybeRun)); | 65 FROM_HERE, base::Bind(&UserScriptIdleScheduler::MaybeRun, |
| 66 weak_factory_.GetWeakPtr())); |
| 64 } | 67 } |
| 65 | 68 |
| 66 void UserScriptIdleScheduler::DidStartProvisionalLoad() { | 69 void UserScriptIdleScheduler::DidStartProvisionalLoad() { |
| 67 // The frame is navigating, so reset the state since we'll want to inject | 70 // The frame is navigating, so reset the state since we'll want to inject |
| 68 // scripts once the load finishes. | 71 // scripts once the load finishes. |
| 69 has_run_ = false; | 72 has_run_ = false; |
| 70 method_factory_.RevokeAll(); | 73 weak_factory_.InvalidateWeakPtrs(); |
| 71 while (!pending_code_execution_queue_.empty()) | 74 while (!pending_code_execution_queue_.empty()) |
| 72 pending_code_execution_queue_.pop(); | 75 pending_code_execution_queue_.pop(); |
| 73 } | 76 } |
| 74 | 77 |
| 75 void UserScriptIdleScheduler::MaybeRun() { | 78 void UserScriptIdleScheduler::MaybeRun() { |
| 76 if (has_run_) | 79 if (has_run_) |
| 77 return; | 80 return; |
| 78 | 81 |
| 79 // Note: we must set this before calling ExecuteCodeImpl, because that may | 82 // Note: we must set this before calling ExecuteCodeImpl, because that may |
| 80 // result in a synchronous call back into MaybeRun if there is a pending task | 83 // result in a synchronous call back into MaybeRun if there is a pending task |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 if (!parent_frame) | 173 if (!parent_frame) |
| 171 return false; | 174 return false; |
| 172 | 175 |
| 173 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; | 176 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; |
| 174 child_frame = child_frame->nextSibling()) { | 177 child_frame = child_frame->nextSibling()) { |
| 175 frames_vector->push_back(child_frame); | 178 frames_vector->push_back(child_frame); |
| 176 GetAllChildFrames(child_frame, frames_vector); | 179 GetAllChildFrames(child_frame, frames_vector); |
| 177 } | 180 } |
| 178 return true; | 181 return true; |
| 179 } | 182 } |
| OLD | NEW |