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) {}
59 explicit XWalkExtensionService(Delegate* delegate);
60 virtual ~XWalkExtensionService();
62 void RegisterExternalExtensionsForPath(const base::FilePath& path);
64 // To be called when a new RenderProcessHost is created, will plug the
65 // extension system to that render process. See
66 // XWalkContentBrowserClient::RenderProcessWillLaunch().
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);
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);
82 typedef base::Callback<void(XWalkExtensionVector* extensions)>
83 CreateExtensionsCallback;
85 static void SetCreateExtensionThreadExtensionsCallbackForTesting(
86 const CreateExtensionsCallback& callback);
87 static void SetCreateUIThreadExtensionsCallbackForTesting(
88 const CreateExtensionsCallback& callback);
90 static void SetExternalExtensionsPathForTesting(const base::FilePath& path);
93 void OnRenderProcessHostCreatedInternal(
94 content::RenderProcessHost* host,
95 XWalkExtensionVector* ui_thread_extensions,
96 XWalkExtensionVector* extension_thread_extensions,
97 scoped_ptr<base::ValueMap> runtime_variables);
99 // XWalkExtensionProcessHost::Delegate implementation.
100 virtual void OnExtensionProcessDied(XWalkExtensionProcessHost* eph,
101 int render_process_id) OVERRIDE;
103 virtual void OnExtensionProcessCreated(
104 int render_process_id,
105 const IPC::ChannelHandle handle) OVERRIDE;
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;
116 // NotificationObserver implementation.
117 virtual void Observe(int type, const content::NotificationSource& source,
118 const content::NotificationDetails& details) OVERRIDE;
120 void OnRenderProcessHostClosed(content::RenderProcessHost* host);
122 void CreateInProcessExtensionServers(
123 content::RenderProcessHost* host,
124 XWalkExtensionData* data,
125 XWalkExtensionVector* ui_thread_extensions,
126 XWalkExtensionVector* extension_thread_extensions);
128 void CreateExtensionProcessHost(content::RenderProcessHost* host,
129 XWalkExtensionData* data, scoped_ptr<base::ValueMap> runtime_variables);
131 // The server that handles in process extensions will live in the
132 // extension_thread_.
133 base::Thread extension_thread_;
135 content::NotificationRegistrar registrar_;
139 base::FilePath external_extensions_path_;
141 typedef std::map<int, XWalkExtensionData*> RenderProcessToExtensionDataMap;
142 RenderProcessToExtensionDataMap extension_data_map_;
144 DISALLOW_COPY_AND_ASSIGN(XWalkExtensionService);
147 } // namespace extensions
150 #endif // XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_