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);
56 explicit XWalkExtensionService(Delegate* delegate);
57 virtual ~XWalkExtensionService();
59 void RegisterExternalExtensionsForPath(const base::FilePath& path);
61 // To be called when a new RenderProcessHost is created, will plug the
62 // extension system to that render process. See
63 // XWalkContentBrowserClient::RenderProcessWillLaunch().
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);
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);
79 typedef base::Callback<void(XWalkExtensionVector* extensions)>
80 CreateExtensionsCallback;
82 static void SetCreateExtensionThreadExtensionsCallbackForTesting(
83 const CreateExtensionsCallback& callback);
84 static void SetCreateUIThreadExtensionsCallbackForTesting(
85 const CreateExtensionsCallback& callback);
87 static void SetExternalExtensionsPathForTesting(const base::FilePath& path);
90 void OnRenderProcessHostCreatedInternal(
91 content::RenderProcessHost* host,
92 XWalkExtensionVector* ui_thread_extensions,
93 XWalkExtensionVector* extension_thread_extensions,
94 const base::ValueMap& runtime_variables);
96 // XWalkExtensionProcessHost::Delegate implementation.
97 virtual void OnExtensionProcessDied(XWalkExtensionProcessHost* eph,
98 int render_process_id) OVERRIDE;
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;
109 // NotificationObserver implementation.
110 virtual void Observe(int type, const content::NotificationSource& source,
111 const content::NotificationDetails& details) OVERRIDE;
113 void OnRenderProcessHostClosed(content::RenderProcessHost* host);
115 void CreateInProcessExtensionServers(
116 content::RenderProcessHost* host,
117 XWalkExtensionData* data,
118 XWalkExtensionVector* ui_thread_extensions,
119 XWalkExtensionVector* extension_thread_extensions);
121 void CreateExtensionProcessHost(content::RenderProcessHost* host,
122 XWalkExtensionData* data, const base::ValueMap& runtime_variables);
124 // The server that handles in process extensions will live in the
125 // extension_thread_.
126 base::Thread extension_thread_;
128 content::NotificationRegistrar registrar_;
132 base::FilePath external_extensions_path_;
134 typedef std::map<int, XWalkExtensionData*> RenderProcessToExtensionDataMap;
135 RenderProcessToExtensionDataMap extension_data_map_;
137 DISALLOW_COPY_AND_ASSIGN(XWalkExtensionService);
140 } // namespace extensions
143 #endif // XWALK_EXTENSIONS_BROWSER_XWALK_EXTENSION_SERVICE_H_