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.
5 #ifndef XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_
6 #define XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_
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"
24 class RenderProcessHost;
29 namespace extensions {
32 class XWalkExtensionData;
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 {
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 virtual void RenderChannelCreated(int render_process_id) {}
60 explicit XWalkExtensionService(Delegate* delegate);
61 virtual ~XWalkExtensionService();
63 void RegisterExternalExtensionsForPath(const base::FilePath& path);
65 // To be called when a new RenderProcessHost is created, will plug the
66 // extension system to that render process. See
67 // XWalkContentBrowserClient::RenderProcessWillLaunch().
69 // The vectors contain the extensions to be used for this render process,
70 // ownership of these extensions is taken by the XWalkExtensionService. The
71 // vectors will be empty after the call.
72 void OnRenderProcessWillLaunch(
73 content::RenderProcessHost* host,
74 XWalkExtensionVector* ui_thread_extensions,
75 XWalkExtensionVector* extension_thread_extensions,
76 scoped_ptr<base::ValueMap> runtime_variables);
78 // To be called when a RenderProcess died, so we can gracefully shutdown the
79 // associated ExtensionProcess. See Runtime::RenderProcessGone() and
80 // XWalkContentBrowserClient::RenderProcessHostGone().
81 void OnRenderProcessDied(content::RenderProcessHost* host);
83 typedef base::Callback<void(XWalkExtensionVector* extensions)>
84 CreateExtensionsCallback;
86 static void SetCreateExtensionThreadExtensionsCallbackForTesting(
87 const CreateExtensionsCallback& callback);
88 static void SetCreateUIThreadExtensionsCallbackForTesting(
89 const CreateExtensionsCallback& callback);
91 static void SetExternalExtensionsPathForTesting(const base::FilePath& path);
94 void OnRenderProcessHostCreatedInternal(
95 content::RenderProcessHost* host,
96 XWalkExtensionVector* ui_thread_extensions,
97 XWalkExtensionVector* extension_thread_extensions,
98 scoped_ptr<base::ValueMap> runtime_variables);
100 // XWalkExtensionProcessHost::Delegate implementation.
101 void OnExtensionProcessDied(XWalkExtensionProcessHost* eph,
102 int render_process_id) override;
104 void OnExtensionProcessCreated(
105 int render_process_id,
106 const IPC::ChannelHandle handle) override;
107 void OnRenderChannelCreated(int render_process_id) override;
109 void OnCheckAPIAccessControl(
110 int render_process_id,
111 const std::string& extension_name,
112 const std::string& api_name,
113 const PermissionCallback& callback) override;
114 bool OnRegisterPermissions(int render_process_id,
115 const std::string& extension_name,
116 const std::string& perm_table) override;
118 // NotificationObserver implementation.
119 void Observe(int type, const content::NotificationSource& source,
120 const content::NotificationDetails& details) override;
122 void OnRenderProcessHostClosed(content::RenderProcessHost* host);
124 void CreateInProcessExtensionServers(
125 content::RenderProcessHost* host,
126 XWalkExtensionData* data,
127 XWalkExtensionVector* ui_thread_extensions,
128 XWalkExtensionVector* extension_thread_extensions);
130 void CreateExtensionProcessHost(content::RenderProcessHost* host,
131 XWalkExtensionData* data, scoped_ptr<base::ValueMap> runtime_variables);
133 // The server that handles in process extensions will live in the
134 // extension_thread_.
135 base::Thread extension_thread_;
137 content::NotificationRegistrar registrar_;
141 base::FilePath external_extensions_path_;
143 typedef std::map<int, XWalkExtensionData*> RenderProcessToExtensionDataMap;
144 RenderProcessToExtensionDataMap extension_data_map_;
146 DISALLOW_COPY_AND_ASSIGN(XWalkExtensionService);
149 } // namespace extensions
152 #endif // XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_