Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / extensions / renderer / dispatcher.h
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
3 // found in the LICENSE file.
4
5 #ifndef EXTENSIONS_RENDERER_DISPATCHER_H_
6 #define EXTENSIONS_RENDERER_DISPATCHER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
13 #include "base/memory/shared_memory.h"
14 #include "base/timer/timer.h"
15 #include "content/public/renderer/render_process_observer.h"
16 #include "extensions/common/event_filter.h"
17 #include "extensions/common/extension_set.h"
18 #include "extensions/common/extensions_client.h"
19 #include "extensions/common/features/feature.h"
20 #include "extensions/renderer/resource_bundle_source_map.h"
21 #include "extensions/renderer/script_context.h"
22 #include "extensions/renderer/script_context_set.h"
23 #include "extensions/renderer/v8_schema_registry.h"
24 #include "third_party/WebKit/public/platform/WebString.h"
25 #include "third_party/WebKit/public/platform/WebVector.h"
26 #include "v8/include/v8.h"
27
28 class ChromeRenderViewTest;
29 class GURL;
30 class ModuleSystem;
31 class URLPattern;
32 struct ExtensionMsg_ExternalConnectionInfo;
33 struct ExtensionMsg_Loaded_Params;
34 struct ExtensionMsg_UpdatePermissions_Params;
35
36 namespace blink {
37 class WebFrame;
38 class WebSecurityOrigin;
39 }
40
41 namespace base {
42 class DictionaryValue;
43 class ListValue;
44 }
45
46 namespace content {
47 class RenderThread;
48 }
49
50 namespace extensions {
51 class ContentWatcher;
52 class DispatcherDelegate;
53 class Extension;
54 class FilteredEventRouter;
55 class ManifestPermissionSet;
56 class RequestSender;
57 class ScriptContext;
58 class UserScriptSlave;
59 struct Message;
60
61 // Dispatches extension control messages sent to the renderer and stores
62 // renderer extension related state.
63 class Dispatcher : public content::RenderProcessObserver {
64  public:
65   explicit Dispatcher(DispatcherDelegate* delegate);
66   virtual ~Dispatcher();
67
68   const std::set<std::string>& function_names() const {
69     return function_names_;
70   }
71
72   bool is_extension_process() const { return is_extension_process_; }
73
74   const ExtensionSet* extensions() const { return &extensions_; }
75
76   const ScriptContextSet& script_context_set() const {
77     return script_context_set_;
78   }
79
80   V8SchemaRegistry* v8_schema_registry() { return v8_schema_registry_.get(); }
81
82   ContentWatcher* content_watcher() { return content_watcher_.get(); }
83
84   UserScriptSlave* user_script_slave() { return user_script_slave_.get(); }
85
86   RequestSender* request_sender() { return request_sender_.get(); }
87
88   bool IsExtensionActive(const std::string& extension_id) const;
89
90   // Finds the extension ID for the JavaScript context associated with the
91   // specified |frame| and isolated world. If |world_id| is zero, finds the
92   // extension ID associated with the main world's JavaScript context. If the
93   // JavaScript context isn't from an extension, returns empty string.
94   std::string GetExtensionID(const blink::WebFrame* frame, int world_id);
95
96   void DidCreateScriptContext(blink::WebFrame* frame,
97                               const v8::Handle<v8::Context>& context,
98                               int extension_group,
99                               int world_id);
100
101   void WillReleaseScriptContext(blink::WebFrame* frame,
102                                 const v8::Handle<v8::Context>& context,
103                                 int world_id);
104
105   void DidCreateDocumentElement(blink::WebFrame* frame);
106
107   void DidMatchCSS(
108       blink::WebFrame* frame,
109       const blink::WebVector<blink::WebString>& newly_matching_selectors,
110       const blink::WebVector<blink::WebString>& stopped_matching_selectors);
111
112   void OnExtensionResponse(int request_id,
113                            bool success,
114                            const base::ListValue& response,
115                            const std::string& error);
116
117   // Checks that the current context contains an extension that has permission
118   // to execute the specified function. If it does not, a v8 exception is thrown
119   // and the method returns false. Otherwise returns true.
120   bool CheckContextAccessToExtensionAPI(const std::string& function_name,
121                                         ScriptContext* context) const;
122
123   // Dispatches the event named |event_name| to all render views.
124   void DispatchEvent(const std::string& extension_id,
125                      const std::string& event_name) const;
126
127   // Shared implementation of the various MessageInvoke IPCs.
128   void InvokeModuleSystemMethod(content::RenderView* render_view,
129                                 const std::string& extension_id,
130                                 const std::string& module_name,
131                                 const std::string& function_name,
132                                 const base::ListValue& args,
133                                 bool user_gesture);
134
135   void ClearPortData(int port_id);
136
137  private:
138   friend class ::ChromeRenderViewTest;
139   FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest,
140                            CannotScriptWebstore);
141
142   // RenderProcessObserver implementation:
143   virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE;
144   virtual void WebKitInitialized() OVERRIDE;
145   virtual void IdleNotification() OVERRIDE;
146   virtual void OnRenderProcessShutdown() OVERRIDE;
147
148   void OnActivateExtension(const std::string& extension_id);
149   void OnCancelSuspend(const std::string& extension_id);
150   void OnClearTabSpecificPermissions(
151       int tab_id,
152       const std::vector<std::string>& extension_ids);
153   void OnDeliverMessage(int target_port_id, const Message& message);
154   void OnDispatchOnConnect(int target_port_id,
155                            const std::string& channel_name,
156                            const base::DictionaryValue& source_tab,
157                            const ExtensionMsg_ExternalConnectionInfo& info,
158                            const std::string& tls_channel_id);
159   void OnDispatchOnDisconnect(int port_id, const std::string& error_message);
160   void OnLoaded(
161       const std::vector<ExtensionMsg_Loaded_Params>& loaded_extensions);
162   void OnLoadedInternal(scoped_refptr<const Extension> extension);
163   void OnMessageInvoke(const std::string& extension_id,
164                        const std::string& module_name,
165                        const std::string& function_name,
166                        const base::ListValue& args,
167                        bool user_gesture);
168   void OnSetChannel(int channel);
169   void OnSetFunctionNames(const std::vector<std::string>& names);
170   void OnSetScriptingWhitelist(
171       const ExtensionsClient::ScriptingWhitelist& extension_ids);
172   void OnSetSystemFont(const std::string& font_family,
173                        const std::string& font_size);
174   void OnShouldSuspend(const std::string& extension_id, int sequence_id);
175   void OnSuspend(const std::string& extension_id);
176   void OnUnloaded(const std::string& id);
177   void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params& params);
178   void OnUpdateTabSpecificPermissions(int page_id,
179                                       int tab_id,
180                                       const std::string& extension_id,
181                                       const URLPatternSet& origin_set);
182   void OnUpdateUserScripts(base::SharedMemoryHandle scripts);
183   void OnUsingWebRequestAPI(bool adblock,
184                             bool adblock_plus,
185                             bool other_webrequest);
186
187   void UpdateActiveExtensions();
188
189   // Sets up the host permissions for |extension|.
190   void InitOriginPermissions(const Extension* extension,
191                              Feature::Context context_type);
192   void UpdateOriginPermissions(UpdatedExtensionPermissionsInfo::Reason reason,
193                                const Extension* extension,
194                                const URLPatternSet& origins);
195
196   // Enable custom element whitelist in Apps.
197   void EnableCustomElementWhiteList();
198
199   // Adds or removes bindings for every context belonging to |extension_id|, or
200   // or all contexts if |extension_id| is empty.
201   void UpdateBindings(const std::string& extension_id);
202
203   void UpdateBindingsForContext(ScriptContext* context);
204
205   void RegisterBinding(const std::string& api_name, ScriptContext* context);
206
207   void RegisterNativeHandlers(ModuleSystem* module_system,
208                               ScriptContext* context);
209
210   // Inserts static source code into |source_map_|.
211   void PopulateSourceMap();
212
213   // Returns whether the current renderer hosts a platform app.
214   bool IsWithinPlatformApp();
215
216   bool IsSandboxedPage(const GURL& url) const;
217
218   // Returns the Feature::Context type of context for a JavaScript context.
219   Feature::Context ClassifyJavaScriptContext(
220       const Extension* extension,
221       int extension_group,
222       const GURL& url,
223       const blink::WebSecurityOrigin& origin);
224
225   // Gets |field| from |object| or creates it as an empty object if it doesn't
226   // exist.
227   v8::Handle<v8::Object> GetOrCreateObject(const v8::Handle<v8::Object>& object,
228                                            const std::string& field,
229                                            v8::Isolate* isolate);
230
231   v8::Handle<v8::Object> GetOrCreateBindObjectIfAvailable(
232       const std::string& api_name,
233       std::string* bind_name,
234       ScriptContext* context);
235
236   // The delegate for this dispatcher. Not owned, but must extend beyond the
237   // Dispatcher's own lifetime.
238   DispatcherDelegate* delegate_;
239
240   // True if this renderer is running extensions.
241   bool is_extension_process_;
242
243   // Contains all loaded extensions.  This is essentially the renderer
244   // counterpart to ExtensionService in the browser. It contains information
245   // about all extensions currently loaded by the browser.
246   ExtensionSet extensions_;
247
248   // The IDs of extensions that failed to load, mapped to the error message
249   // generated on failure.
250   std::map<std::string, std::string> extension_load_errors_;
251
252   // All the bindings contexts that are currently loaded for this renderer.
253   // There is zero or one for each v8 context.
254   ScriptContextSet script_context_set_;
255
256   scoped_ptr<ContentWatcher> content_watcher_;
257
258   scoped_ptr<UserScriptSlave> user_script_slave_;
259
260   // Same as above, but on a longer timer and will run even if the process is
261   // not idle, to ensure that IdleHandle gets called eventually.
262   scoped_ptr<base::RepeatingTimer<content::RenderThread> > forced_idle_timer_;
263
264   // All declared function names.
265   std::set<std::string> function_names_;
266
267   // The extensions and apps that are active in this process.
268   std::set<std::string> active_extension_ids_;
269
270   ResourceBundleSourceMap source_map_;
271
272   // Cache for the v8 representation of extension API schemas.
273   scoped_ptr<V8SchemaRegistry> v8_schema_registry_;
274
275   // Sends API requests to the extension host.
276   scoped_ptr<RequestSender> request_sender_;
277
278   // The platforms system font family and size;
279   std::string system_font_family_;
280   std::string system_font_size_;
281
282   // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
283   std::map<int, int> port_to_tab_id_map_;
284
285   // True once WebKit has been initialized (and it is therefore safe to poke).
286   bool is_webkit_initialized_;
287
288   DISALLOW_COPY_AND_ASSIGN(Dispatcher);
289 };
290
291 }  // namespace extensions
292
293 #endif  // EXTENSIONS_RENDERER_DISPATCHER_H_