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