| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_SLAVE_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_SLAVE_H_ |
| 6 #define CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_SLAVE_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_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/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/shared_memory.h" | 14 #include "base/shared_memory.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
| 17 #include "chrome/common/extensions/user_script.h" | 17 #include "chrome/common/extensions/user_script.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
| 19 | 19 |
| 20 class ExtensionSet; | 20 class ExtensionSet; |
| 21 class GURL; | 21 class GURL; |
| 22 | 22 |
| 23 namespace extensions { | |
| 24 class Extension; | |
| 25 } | |
| 26 | |
| 27 namespace WebKit { | 23 namespace WebKit { |
| 28 class WebFrame; | 24 class WebFrame; |
| 29 } | 25 } |
| 30 | 26 |
| 31 using WebKit::WebScriptSource; | 27 using WebKit::WebScriptSource; |
| 32 | 28 |
| 29 namespace extensions { |
| 30 class Extension; |
| 31 |
| 33 // Manages installed UserScripts for a render process. | 32 // Manages installed UserScripts for a render process. |
| 34 class UserScriptSlave { | 33 class UserScriptSlave { |
| 35 public: | 34 public: |
| 36 // Utility to get the URL we will match against for a frame. If the frame has | 35 // Utility to get the URL we will match against for a frame. If the frame has |
| 37 // committed, this is the commited URL. Otherwise it is the provisional URL. | 36 // committed, this is the commited URL. Otherwise it is the provisional URL. |
| 38 static GURL GetDataSourceURLForFrame(const WebKit::WebFrame* frame); | 37 static GURL GetDataSourceURLForFrame(const WebKit::WebFrame* frame); |
| 39 | 38 |
| 40 explicit UserScriptSlave(const ExtensionSet* extensions); | 39 explicit UserScriptSlave(const ExtensionSet* extensions); |
| 41 ~UserScriptSlave(); | 40 ~UserScriptSlave(); |
| 42 | 41 |
| 43 // Returns the unique set of extension IDs this UserScriptSlave knows about. | 42 // Returns the unique set of extension IDs this UserScriptSlave knows about. |
| 44 void GetActiveExtensions(std::set<std::string>* extension_ids); | 43 void GetActiveExtensions(std::set<std::string>* extension_ids); |
| 45 | 44 |
| 46 // Update the parsed scripts from shared memory. | 45 // Update the parsed scripts from shared memory. |
| 47 bool UpdateScripts(base::SharedMemoryHandle shared_memory); | 46 bool UpdateScripts(base::SharedMemoryHandle shared_memory); |
| 48 | 47 |
| 49 // Inject the appropriate scripts into a frame based on its URL. | 48 // Inject the appropriate scripts into a frame based on its URL. |
| 50 // TODO(aa): Extract a UserScriptFrame interface out of this to improve | 49 // TODO(aa): Extract a UserScriptFrame interface out of this to improve |
| 51 // testability. | 50 // testability. |
| 52 void InjectScripts(WebKit::WebFrame* frame, UserScript::RunLocation location); | 51 void InjectScripts(WebKit::WebFrame* frame, UserScript::RunLocation location); |
| 53 | 52 |
| 54 // Gets the isolated world ID to use for the given |extension| in the given | 53 // Gets the isolated world ID to use for the given |extension| in the given |
| 55 // |frame|. If no isolated world has been created for that extension, | 54 // |frame|. If no isolated world has been created for that extension, |
| 56 // one will be created and initialized. | 55 // one will be created and initialized. |
| 57 int GetIsolatedWorldIdForExtension(const extensions::Extension* extension, | 56 int GetIsolatedWorldIdForExtension(const Extension* extension, |
| 58 WebKit::WebFrame* frame); | 57 WebKit::WebFrame* frame); |
| 59 | 58 |
| 60 // Gets the id of the extension running in a given isolated world. If no such | 59 // Gets the id of the extension running in a given isolated world. If no such |
| 61 // isolated world exists, or no extension is running in it, returns empty | 60 // isolated world exists, or no extension is running in it, returns empty |
| 62 // string. | 61 // string. |
| 63 std::string GetExtensionIdForIsolatedWorld(int isolated_world_id); | 62 std::string GetExtensionIdForIsolatedWorld(int isolated_world_id); |
| 64 | 63 |
| 65 void RemoveIsolatedWorld(const std::string& extension_id); | 64 void RemoveIsolatedWorld(const std::string& extension_id); |
| 66 | 65 |
| 67 private: | 66 private: |
| 68 static void InitializeIsolatedWorld(int isolated_world_id, | 67 static void InitializeIsolatedWorld(int isolated_world_id, |
| 69 const extensions::Extension* extension); | 68 const Extension* extension); |
| 70 | 69 |
| 71 // Shared memory containing raw script data. | 70 // Shared memory containing raw script data. |
| 72 scoped_ptr<base::SharedMemory> shared_memory_; | 71 scoped_ptr<base::SharedMemory> shared_memory_; |
| 73 | 72 |
| 74 // Parsed script data. | 73 // Parsed script data. |
| 75 std::vector<UserScript*> scripts_; | 74 std::vector<UserScript*> scripts_; |
| 76 STLElementDeleter<std::vector<UserScript*> > script_deleter_; | 75 STLElementDeleter<std::vector<UserScript*> > script_deleter_; |
| 77 | 76 |
| 78 // Greasemonkey API source that is injected with the scripts. | 77 // Greasemonkey API source that is injected with the scripts. |
| 79 base::StringPiece api_js_; | 78 base::StringPiece api_js_; |
| 80 | 79 |
| 81 // Extension metadata. | 80 // Extension metadata. |
| 82 const ExtensionSet* extensions_; | 81 const ExtensionSet* extensions_; |
| 83 | 82 |
| 84 typedef std::map<std::string, int> IsolatedWorldMap; | 83 typedef std::map<std::string, int> IsolatedWorldMap; |
| 85 IsolatedWorldMap isolated_world_ids_; | 84 IsolatedWorldMap isolated_world_ids_; |
| 86 | 85 |
| 87 DISALLOW_COPY_AND_ASSIGN(UserScriptSlave); | 86 DISALLOW_COPY_AND_ASSIGN(UserScriptSlave); |
| 88 }; | 87 }; |
| 89 | 88 |
| 89 } // namespace extensions |
| 90 |
| 90 #endif // CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_SLAVE_H_ | 91 #endif // CHROME_RENDERER_EXTENSIONS_USER_SCRIPT_SLAVE_H_ |
| OLD | NEW |