9141449ece5035b32c6cf2ba44f3f17d85bcdb7a
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / browser / xwalk_extension_service.h
1 // Copyright (c) 2013 Intel Corporation. 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 XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_
6 #define XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_
7
8 #include <stdint.h>
9 #include <map>
10 #include <string>
11 #include <vector>
12 #include "base/callback_forward.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/thread.h"
17 #include "base/values.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "xwalk/extensions/browser/xwalk_extension_process_host.h"
21 #include "xwalk/extensions/common/xwalk_extension_vector.h"
22
23 namespace content {
24 class RenderProcessHost;
25 class WebContents;
26 }
27
28 namespace xwalk {
29 namespace extensions {
30
31 class XWalkExtension;
32 class XWalkExtensionData;
33
34 // This is the entry point for Crosswalk extensions. Its responsible for keeping
35 // track of the extensions, and enable them on WebContents once they are
36 // created. It's life time follows the Browser process itself.
37 class XWalkExtensionService : public content::NotificationObserver,
38     public XWalkExtensionProcessHost::Delegate {
39  public:
40   class Delegate {
41    public:
42     virtual void CheckAPIAccessControl(
43         int render_process_id,
44         const std::string& extension_name,
45         const std::string& api_name,
46         const PermissionCallback& callback) {}
47     virtual bool RegisterPermissions(
48         int render_process_id,
49         const std::string& extension_name,
50         const std::string& perm_table);
51     virtual void ExtensionProcessCreated(
52         int render_process_id,
53         const IPC::ChannelHandle& channel_handle) {}
54
55    protected:
56     ~Delegate() {}
57   };
58
59   explicit XWalkExtensionService(Delegate* delegate);
60   virtual ~XWalkExtensionService();
61
62   void RegisterExternalExtensionsForPath(const base::FilePath& path);
63
64   // To be called when a new RenderProcessHost is created, will plug the
65   // extension system to that render process. See
66   // XWalkContentBrowserClient::RenderProcessWillLaunch().
67   //
68   // The vectors contain the extensions to be used for this render process,
69   // ownership of these extensions is taken by the XWalkExtensionService. The
70   // vectors will be empty after the call.
71   void OnRenderProcessWillLaunch(
72       content::RenderProcessHost* host,
73       XWalkExtensionVector* ui_thread_extensions,
74       XWalkExtensionVector* extension_thread_extensions,
75       scoped_ptr<base::ValueMap> runtime_variables);
76
77   // To be called when a RenderProcess died, so we can gracefully shutdown the
78   // associated ExtensionProcess. See Runtime::RenderProcessGone() and
79   // XWalkContentBrowserClient::RenderProcessHostGone().
80   void OnRenderProcessDied(content::RenderProcessHost* host);
81
82   typedef base::Callback<void(XWalkExtensionVector* extensions)>
83       CreateExtensionsCallback;
84
85   static void SetCreateExtensionThreadExtensionsCallbackForTesting(
86       const CreateExtensionsCallback& callback);
87   static void SetCreateUIThreadExtensionsCallbackForTesting(
88       const CreateExtensionsCallback& callback);
89
90   static void SetExternalExtensionsPathForTesting(const base::FilePath& path);
91
92  private:
93   void OnRenderProcessHostCreatedInternal(
94       content::RenderProcessHost* host,
95       XWalkExtensionVector* ui_thread_extensions,
96       XWalkExtensionVector* extension_thread_extensions,
97       scoped_ptr<base::ValueMap> runtime_variables);
98
99   // XWalkExtensionProcessHost::Delegate implementation.
100   virtual void OnExtensionProcessDied(XWalkExtensionProcessHost* eph,
101       int render_process_id) OVERRIDE;
102
103   virtual void OnExtensionProcessCreated(
104       int render_process_id,
105       const IPC::ChannelHandle handle) OVERRIDE;
106
107   virtual void OnCheckAPIAccessControl(
108       int render_process_id,
109       const std::string& extension_name,
110       const std::string& api_name,
111       const PermissionCallback& callback) OVERRIDE;
112   virtual bool OnRegisterPermissions(int render_process_id,
113                                      const std::string& extension_name,
114                                      const std::string& perm_table) OVERRIDE;
115
116   // NotificationObserver implementation.
117   virtual void Observe(int type, const content::NotificationSource& source,
118                        const content::NotificationDetails& details) OVERRIDE;
119
120   void OnRenderProcessHostClosed(content::RenderProcessHost* host);
121
122   void CreateInProcessExtensionServers(
123       content::RenderProcessHost* host,
124       XWalkExtensionData* data,
125       XWalkExtensionVector* ui_thread_extensions,
126       XWalkExtensionVector* extension_thread_extensions);
127
128   void CreateExtensionProcessHost(content::RenderProcessHost* host,
129       XWalkExtensionData* data, scoped_ptr<base::ValueMap> runtime_variables);
130
131   // The server that handles in process extensions will live in the
132   // extension_thread_.
133   base::Thread extension_thread_;
134
135   content::NotificationRegistrar registrar_;
136
137   Delegate* delegate_;
138
139   base::FilePath external_extensions_path_;
140
141   typedef std::map<int, XWalkExtensionData*> RenderProcessToExtensionDataMap;
142   RenderProcessToExtensionDataMap extension_data_map_;
143
144   DISALLOW_COPY_AND_ASSIGN(XWalkExtensionService);
145 };
146
147 }  // namespace extensions
148 }  // namespace xwalk
149
150 #endif  // XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_