Upstream version 5.34.97.0
[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
52    protected:
53     ~Delegate() {}
54   };
55
56   explicit XWalkExtensionService(Delegate* delegate);
57   virtual ~XWalkExtensionService();
58
59   void RegisterExternalExtensionsForPath(const base::FilePath& path);
60
61   // To be called when a new RenderProcessHost is created, will plug the
62   // extension system to that render process. See
63   // XWalkContentBrowserClient::RenderProcessWillLaunch().
64   //
65   // The vectors contain the extensions to be used for this render process,
66   // ownership of these extensions is taken by the XWalkExtensionService. The
67   // vectors will be empty after the call.
68   void OnRenderProcessWillLaunch(
69       content::RenderProcessHost* host,
70       XWalkExtensionVector* ui_thread_extensions,
71       XWalkExtensionVector* extension_thread_extensions,
72       const base::ValueMap& runtime_variables);
73
74   // To be called when a RenderProcess died, so we can gracefully shutdown the
75   // associated ExtensionProcess. See Runtime::RenderProcessGone() and
76   // XWalkContentBrowserClient::RenderProcessHostGone().
77   void OnRenderProcessDied(content::RenderProcessHost* host);
78
79   typedef base::Callback<void(XWalkExtensionVector* extensions)>
80       CreateExtensionsCallback;
81
82   static void SetCreateExtensionThreadExtensionsCallbackForTesting(
83       const CreateExtensionsCallback& callback);
84   static void SetCreateUIThreadExtensionsCallbackForTesting(
85       const CreateExtensionsCallback& callback);
86
87   static void SetExternalExtensionsPathForTesting(const base::FilePath& path);
88
89  private:
90   void OnRenderProcessHostCreatedInternal(
91       content::RenderProcessHost* host,
92       XWalkExtensionVector* ui_thread_extensions,
93       XWalkExtensionVector* extension_thread_extensions,
94       const base::ValueMap& runtime_variables);
95
96   // XWalkExtensionProcessHost::Delegate implementation.
97   virtual void OnExtensionProcessDied(XWalkExtensionProcessHost* eph,
98       int render_process_id) OVERRIDE;
99
100   virtual void OnCheckAPIAccessControl(
101       int render_process_id,
102       const std::string& extension_name,
103       const std::string& api_name,
104       const PermissionCallback& callback) OVERRIDE;
105   virtual bool OnRegisterPermissions(int render_process_id,
106                                      const std::string& extension_name,
107                                      const std::string& perm_table) OVERRIDE;
108
109   // NotificationObserver implementation.
110   virtual void Observe(int type, const content::NotificationSource& source,
111                        const content::NotificationDetails& details) OVERRIDE;
112
113   void OnRenderProcessHostClosed(content::RenderProcessHost* host);
114
115   void CreateInProcessExtensionServers(
116       content::RenderProcessHost* host,
117       XWalkExtensionData* data,
118       XWalkExtensionVector* ui_thread_extensions,
119       XWalkExtensionVector* extension_thread_extensions);
120
121   void CreateExtensionProcessHost(content::RenderProcessHost* host,
122       XWalkExtensionData* data, const base::ValueMap& runtime_variables);
123
124   // The server that handles in process extensions will live in the
125   // extension_thread_.
126   base::Thread extension_thread_;
127
128   content::NotificationRegistrar registrar_;
129
130   Delegate* delegate_;
131
132   base::FilePath external_extensions_path_;
133
134   typedef std::map<int, XWalkExtensionData*> RenderProcessToExtensionDataMap;
135   RenderProcessToExtensionDataMap extension_data_map_;
136
137   DISALLOW_COPY_AND_ASSIGN(XWalkExtensionService);
138 };
139
140 }  // namespace extensions
141 }  // namespace xwalk
142
143 #endif  // XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_