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 #ifndef CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_IDLE_SCHEDULER_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_SCHEDULER_H_ |
6 #define CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_IDLE_SCHEDULER_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_SCHEDULER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include <map> |
9 #include <queue> | 10 #include <queue> |
10 #include <vector> | |
11 | 11 |
| 12 #include "chrome/common/extensions/user_script.h" |
12 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 | 15 |
15 class ExtensionDispatcher; | 16 class ExtensionDispatcher; |
16 class RenderView; | 17 class RenderView; |
17 struct ExtensionMsg_ExecuteCode_Params; | 18 struct ExtensionMsg_ExecuteCode_Params; |
18 | 19 |
19 namespace WebKit { | 20 namespace WebKit { |
20 class WebFrame; | 21 class WebFrame; |
21 } | 22 } |
22 | 23 |
23 // Implements support for injecting scripts at "document idle". Currently, | 24 // Implements support for injecting scripts at different times in the document |
24 // determining idleness is simple: it is whichever of the following happens | 25 // loading process. The different possible time are described in |
25 // first: | 26 // UserScript::RunLocation. |
| 27 // |
| 28 // Currently, determining idleness is simple: it is whichever of the following |
| 29 // happens first: |
26 // | 30 // |
27 // a) When the initial DOM for a page is complete + kUserScriptIdleTimeout, | 31 // a) When the initial DOM for a page is complete + kUserScriptIdleTimeout, |
28 // b) or when the page has completely loaded including all subresources. | 32 // b) or when the page has completely loaded including all subresources. |
29 // | 33 // |
30 // The intent of this mechanism is to prevent user scripts from slowing down | 34 // The intent of this mechanism is to prevent user scripts from slowing down |
31 // fast pages (run after load), while still allowing them to run relatively | 35 // fast pages (run after load), while still allowing them to run relatively |
32 // timely for pages with lots of slow subresources. | 36 // timely for pages with lots of slow subresources. |
33 // | 37 // |
34 // NOTE: this class does not inherit from RenderViewObserver on purpose. The | 38 // NOTE: this class does not inherit from RenderViewObserver on purpose. The |
35 // reason is that this object is per frame, and a frame can move across | 39 // reason is that this object is per frame, and a frame can move across |
36 // RenderViews thanks to adoptNode. So we have each RenderView's | 40 // RenderViews thanks to adoptNode. So we have each RenderView's |
37 // ExtensionHelper proxy these calls to the renderer process' | 41 // ExtensionHelper proxy these calls to the renderer process' |
38 // ExtensionDispatcher, which contains the mapping from WebFrame to us. | 42 // ExtensionDispatcher, which contains the mapping from WebFrame to us. |
39 class UserScriptIdleScheduler { | 43 class UserScriptScheduler { |
40 public: | 44 public: |
41 UserScriptIdleScheduler(WebKit::WebFrame* frame, | 45 UserScriptScheduler(WebKit::WebFrame* frame, |
42 ExtensionDispatcher* extension_dispatcher); | 46 ExtensionDispatcher* extension_dispatcher); |
43 ~UserScriptIdleScheduler(); | 47 ~UserScriptScheduler(); |
44 | 48 |
45 void ExecuteCode(const ExtensionMsg_ExecuteCode_Params& params); | 49 void ExecuteCode(const ExtensionMsg_ExecuteCode_Params& params); |
| 50 void DidCreateDocumentElement(); |
46 void DidFinishDocumentLoad(); | 51 void DidFinishDocumentLoad(); |
47 void DidFinishLoad(); | 52 void DidFinishLoad(); |
48 void DidStartProvisionalLoad(); | 53 void DidStartProvisionalLoad(); |
49 | 54 |
50 private: | 55 private: |
| 56 typedef |
| 57 std::queue<linked_ptr<ExtensionMsg_ExecuteCode_Params> > |
| 58 ExecutionQueue; |
| 59 |
51 // Run user scripts, except if they've already run for this frame, or the | 60 // Run user scripts, except if they've already run for this frame, or the |
52 // frame has been destroyed. | 61 // frame has been destroyed. |
53 void MaybeRun(); | 62 void MaybeRun(); |
54 | 63 |
55 // Backend for the IPC Message ExecuteCode in addition to being used | 64 // Backend for the IPC Message ExecuteCode in addition to being used |
56 // internally. | 65 // internally. |
57 void ExecuteCodeImpl(const ExtensionMsg_ExecuteCode_Params& params); | 66 void ExecuteCodeImpl(const ExtensionMsg_ExecuteCode_Params& params); |
58 | 67 |
59 // Get all child frames of parent_frame, returned by frames_vector. | 68 // Get all child frames of parent_frame, returned by frames_vector. |
60 bool GetAllChildFrames(WebKit::WebFrame* parent_frame, | 69 bool GetAllChildFrames(WebKit::WebFrame* parent_frame, |
61 std::vector<WebKit::WebFrame*>* frames_vector) const; | 70 std::vector<WebKit::WebFrame*>* frames_vector) const; |
62 | 71 |
63 base::WeakPtrFactory<UserScriptIdleScheduler> weak_factory_; | 72 // Call to signify thet the idle timeout has expired. |
| 73 void IdleTimeout(); |
| 74 |
| 75 base::WeakPtrFactory<UserScriptScheduler> weak_factory_; |
64 | 76 |
65 // The Frame we will run scripts in. | 77 // The Frame we will run scripts in. |
66 WebKit::WebFrame* frame_; | 78 WebKit::WebFrame* frame_; |
67 | 79 |
68 // Whether we have already run scripts. | 80 // The current location in the document loading process. |
69 bool has_run_; | 81 // Will be UserScript::UNDEFINED if it is before any scripts should be run. |
| 82 UserScript::RunLocation current_location_; |
| 83 |
| 84 // Whether we have already run the idle scripts. |
| 85 bool has_run_idle_; |
70 | 86 |
71 // This is only used if we're for the main frame. | 87 // This is only used if we're for the main frame. |
72 std::queue<linked_ptr<ExtensionMsg_ExecuteCode_Params> > | 88 std::map<UserScript::RunLocation, ExecutionQueue> pending_execution_map_; |
73 pending_code_execution_queue_; | |
74 | 89 |
75 ExtensionDispatcher* extension_dispatcher_; | 90 ExtensionDispatcher* extension_dispatcher_; |
76 }; | 91 }; |
77 | 92 |
78 #endif // CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_IDLE_SCHEDULER_H_ | 93 #endif // CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_SCHEDULER_H_ |
OLD | NEW |