| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_ | 5 #ifndef EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_ |
| 6 #define EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_ | 6 #define EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
| 15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 16 #include "base/strings/string_piece.h" | 17 #include "base/strings/string_piece.h" |
| 17 #include "extensions/common/user_script.h" | 18 #include "extensions/common/user_script.h" |
| 19 #include "third_party/WebKit/public/platform/WebString.h" |
| 18 #include "third_party/WebKit/public/web/WebScriptSource.h" | 20 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 19 | 21 |
| 20 class GURL; | 22 class GURL; |
| 21 | 23 |
| 22 namespace blink { | 24 namespace blink { |
| 23 class WebFrame; | 25 class WebFrame; |
| 24 } | 26 } |
| 25 | 27 |
| 28 namespace content { |
| 29 class RenderView; |
| 30 } |
| 31 |
| 26 using blink::WebScriptSource; | 32 using blink::WebScriptSource; |
| 27 | 33 |
| 28 namespace extensions { | 34 namespace extensions { |
| 29 class Extension; | 35 class Extension; |
| 30 class ExtensionSet; | 36 class ExtensionSet; |
| 31 | |
| 32 // Manages installed UserScripts for a render process. | 37 // Manages installed UserScripts for a render process. |
| 33 class UserScriptSlave { | 38 class UserScriptSlave { |
| 34 public: | 39 public: |
| 35 explicit UserScriptSlave(const ExtensionSet* extensions); | 40 explicit UserScriptSlave(const ExtensionSet* extensions); |
| 36 ~UserScriptSlave(); | 41 ~UserScriptSlave(); |
| 37 | 42 |
| 38 // Returns the unique set of extension IDs this UserScriptSlave knows about. | 43 // Returns the unique set of extension IDs this UserScriptSlave knows about. |
| 39 void GetActiveExtensions(std::set<std::string>* extension_ids); | 44 void GetActiveExtensions(std::set<std::string>* extension_ids); |
| 40 | 45 |
| 41 // Update the parsed scripts from shared memory. | 46 // Update the parsed scripts from shared memory. |
| 42 bool UpdateScripts(base::SharedMemoryHandle shared_memory); | 47 bool UpdateScripts(base::SharedMemoryHandle shared_memory); |
| 43 | 48 |
| 44 // Inject the appropriate scripts into a frame based on its URL. | |
| 45 // TODO(aa): Extract a UserScriptFrame interface out of this to improve | |
| 46 // testability. | |
| 47 void InjectScripts(blink::WebFrame* frame, UserScript::RunLocation location); | |
| 48 | |
| 49 // Gets the isolated world ID to use for the given |extension| in the given | 49 // Gets the isolated world ID to use for the given |extension| in the given |
| 50 // |frame|. If no isolated world has been created for that extension, | 50 // |frame|. If no isolated world has been created for that extension, |
| 51 // one will be created and initialized. | 51 // one will be created and initialized. |
| 52 int GetIsolatedWorldIdForExtension(const Extension* extension, | 52 int GetIsolatedWorldIdForExtension(const Extension* extension, |
| 53 blink::WebFrame* frame); | 53 blink::WebFrame* frame); |
| 54 | 54 |
| 55 // Gets the id of the extension running in a given isolated world. If no such | 55 // Gets the id of the extension running in a given isolated world. If no such |
| 56 // isolated world exists, or no extension is running in it, returns empty | 56 // isolated world exists, or no extension is running in it, returns empty |
| 57 // string. | 57 // string. |
| 58 std::string GetExtensionIdForIsolatedWorld(int isolated_world_id); | 58 std::string GetExtensionIdForIsolatedWorld(int isolated_world_id); |
| 59 | 59 |
| 60 void RemoveIsolatedWorld(const std::string& extension_id); | 60 void RemoveIsolatedWorld(const std::string& extension_id); |
| 61 | 61 |
| 62 // Inject the appropriate scripts into a frame based on its URL. |
| 63 // TODO(aa): Extract a UserScriptFrame interface out of this to improve |
| 64 // testability. |
| 65 void InjectScripts(blink::WebFrame* frame, UserScript::RunLocation location); |
| 66 |
| 67 // Allow an extension to inject scripts that were previously delayed for user |
| 68 // approval. |
| 69 void OnContentScriptGrantedPermission( |
| 70 content::RenderView* render_view, int request_id); |
| 71 |
| 62 private: | 72 private: |
| 73 // A PendingInjection that needs user approval before it can be executed. |
| 74 struct PendingInjection; |
| 75 typedef std::vector<linked_ptr<PendingInjection> > PendingInjectionList; |
| 76 |
| 77 // The information about a given set of scripts being run. Primarily for |
| 78 // metrics. |
| 79 struct ScriptsRunInfo; |
| 80 |
| 81 // Adds a pending injection to |pending_injections_|. Returns the id of the |
| 82 // pending injection. |
| 83 int AddPendingInjection(UserScript* script, |
| 84 UserScript::RunLocation location, |
| 85 const blink::WebString& frame_name); |
| 86 |
| 87 // Inject the css scripts from the given |script| into the |frame|, and record |
| 88 // metrics into |scripts_run_info|. |
| 89 void InjectCSSScripts(blink::WebFrame* frame, |
| 90 UserScript* script, |
| 91 ScriptsRunInfo* scripts_run_info); |
| 92 |
| 93 // Inject the js scripts from the given |script| into the |frame|, and record |
| 94 // metrics into |scripts_run_info|. |
| 95 void InjectJSScripts(blink::WebFrame* frame, |
| 96 UserScript* script, |
| 97 const Extension* extension, |
| 98 ScriptsRunInfo* scripts_run_info); |
| 99 |
| 100 // Log the data from scripts being run, including doing UMA and notifying the |
| 101 // browser. |
| 102 void LogScriptsRun(blink::WebFrame* frame, |
| 103 UserScript::RunLocation location, |
| 104 const ScriptsRunInfo& info); |
| 105 |
| 63 // Shared memory containing raw script data. | 106 // Shared memory containing raw script data. |
| 64 scoped_ptr<base::SharedMemory> shared_memory_; | 107 scoped_ptr<base::SharedMemory> shared_memory_; |
| 65 | 108 |
| 66 // Parsed script data. | 109 // Parsed script data. |
| 67 std::vector<UserScript*> scripts_; | 110 std::vector<UserScript*> scripts_; |
| 68 STLElementDeleter<std::vector<UserScript*> > script_deleter_; | 111 STLElementDeleter<std::vector<UserScript*> > script_deleter_; |
| 69 | 112 |
| 113 // Map of extension ids to the injections pending for those extensions. |
| 114 PendingInjectionList pending_injections_; |
| 115 |
| 70 // Greasemonkey API source that is injected with the scripts. | 116 // Greasemonkey API source that is injected with the scripts. |
| 71 base::StringPiece api_js_; | 117 base::StringPiece api_js_; |
| 72 | 118 |
| 73 // Extension metadata. | 119 // Extension metadata. |
| 74 const ExtensionSet* extensions_; | 120 const ExtensionSet* extensions_; |
| 75 | 121 |
| 76 typedef std::map<std::string, int> IsolatedWorldMap; | 122 typedef std::map<std::string, int> IsolatedWorldMap; |
| 77 IsolatedWorldMap isolated_world_ids_; | 123 IsolatedWorldMap isolated_world_ids_; |
| 78 | 124 |
| 79 DISALLOW_COPY_AND_ASSIGN(UserScriptSlave); | 125 DISALLOW_COPY_AND_ASSIGN(UserScriptSlave); |
| 80 }; | 126 }; |
| 81 | 127 |
| 82 } // namespace extensions | 128 } // namespace extensions |
| 83 | 129 |
| 84 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_ | 130 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_ |
| OLD | NEW |