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 #include "xwalk/runtime/browser/xwalk_content_browser_client.h"
10 #include "base/command_line.h"
11 #include "base/path_service.h"
12 #include "base/platform_file.h"
13 #include "content/public/browser/browser_main_parts.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/resource_context.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/main_function_params.h"
18 #include "content/public/common/show_desktop_notification_params.h"
19 #include "net/url_request/url_request_context_getter.h"
20 #include "xwalk/extensions/common/xwalk_extension_switches.h"
21 #include "xwalk/runtime/browser/xwalk_browser_main_parts.h"
22 #include "xwalk/runtime/browser/geolocation/xwalk_access_token_store.h"
23 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
24 #include "xwalk/runtime/browser/runtime_context.h"
25 #include "xwalk/runtime/browser/runtime_quota_permission_context.h"
26 #include "xwalk/runtime/browser/speech/speech_recognition_manager_delegate.h"
27 #include "xwalk/runtime/browser/xwalk_runner.h"
29 #if defined(OS_ANDROID)
30 #include "base/android/path_utils.h"
31 #include "base/base_paths_android.h"
32 #include "xwalk/runtime/browser/android/xwalk_cookie_access_policy.h"
33 #include "xwalk/runtime/browser/android/xwalk_contents_client_bridge.h"
34 #include "xwalk/runtime/browser/android/xwalk_web_contents_view_delegate.h"
35 #include "xwalk/runtime/browser/runtime_resource_dispatcher_host_delegate_android.h"
36 #include "xwalk/runtime/browser/xwalk_browser_main_parts_android.h"
37 #include "xwalk/runtime/common/android/xwalk_globals_android.h"
40 #if defined(OS_MACOSX)
41 #include "xwalk/runtime/browser/xwalk_browser_main_parts_mac.h"
45 #include "xwalk/runtime/browser/xwalk_browser_main_parts_tizen.h"
52 // The application-wide singleton of ContentBrowserClient impl.
53 XWalkContentBrowserClient* g_browser_client = NULL;
58 XWalkContentBrowserClient* XWalkContentBrowserClient::Get() {
59 return g_browser_client;
63 XWalkContentBrowserClient::XWalkContentBrowserClient(XWalkRunner* xwalk_runner)
64 : xwalk_runner_(xwalk_runner),
65 url_request_context_getter_(NULL),
67 DCHECK(!g_browser_client);
68 g_browser_client = this;
71 XWalkContentBrowserClient::~XWalkContentBrowserClient() {
72 DCHECK(g_browser_client);
73 g_browser_client = NULL;
76 content::BrowserMainParts* XWalkContentBrowserClient::CreateBrowserMainParts(
77 const content::MainFunctionParams& parameters) {
78 #if defined(OS_MACOSX)
79 main_parts_ = new XWalkBrowserMainPartsMac(parameters);
80 #elif defined(OS_ANDROID)
81 main_parts_ = new XWalkBrowserMainPartsAndroid(parameters);
82 #elif defined(OS_TIZEN)
83 main_parts_ = new XWalkBrowserMainPartsTizen(parameters);
85 main_parts_ = new XWalkBrowserMainParts(parameters);
91 net::URLRequestContextGetter* XWalkContentBrowserClient::CreateRequestContext(
92 content::BrowserContext* browser_context,
93 content::ProtocolHandlerMap* protocol_handlers) {
94 url_request_context_getter_ = static_cast<RuntimeContext*>(browser_context)->
95 CreateRequestContext(protocol_handlers);
96 return url_request_context_getter_;
99 net::URLRequestContextGetter*
100 XWalkContentBrowserClient::CreateRequestContextForStoragePartition(
101 content::BrowserContext* browser_context,
102 const base::FilePath& partition_path,
104 content::ProtocolHandlerMap* protocol_handlers) {
105 return static_cast<RuntimeContext*>(browser_context)->
106 CreateRequestContextForStoragePartition(
107 partition_path, in_memory, protocol_handlers);
110 // This allow us to append extra command line switches to the child
111 // process we launch.
112 void XWalkContentBrowserClient::AppendExtraCommandLineSwitches(
113 CommandLine* command_line, int child_process_id) {
114 CommandLine* browser_process_cmd_line = CommandLine::ForCurrentProcess();
115 const int extra_switches_count = 1;
116 const char* extra_switches[extra_switches_count] = {
117 switches::kXWalkDisableExtensionProcess
120 for (int i = 0; i < extra_switches_count; i++) {
121 if (browser_process_cmd_line->HasSwitch(extra_switches[i]))
122 command_line->AppendSwitch(extra_switches[i]);
126 content::QuotaPermissionContext*
127 XWalkContentBrowserClient::CreateQuotaPermissionContext() {
128 return new RuntimeQuotaPermissionContext();
131 content::AccessTokenStore* XWalkContentBrowserClient::CreateAccessTokenStore() {
132 return new XWalkAccessTokenStore(url_request_context_getter_);
135 content::WebContentsViewDelegate*
136 XWalkContentBrowserClient::GetWebContentsViewDelegate(
137 content::WebContents* web_contents) {
138 #if defined(OS_ANDROID)
139 return new XWalkWebContentsViewDelegate(web_contents);
145 void XWalkContentBrowserClient::RenderProcessWillLaunch(
146 content::RenderProcessHost* host) {
147 xwalk_runner_->OnRenderProcessWillLaunch(host);
150 content::MediaObserver* XWalkContentBrowserClient::GetMediaObserver() {
151 return XWalkMediaCaptureDevicesDispatcher::GetInstance();
154 bool XWalkContentBrowserClient::AllowGetCookie(
156 const GURL& first_party,
157 const net::CookieList& cookie_list,
158 content::ResourceContext* context,
159 int render_process_id,
160 int render_view_id) {
161 #if defined(OS_ANDROID)
162 return XWalkCookieAccessPolicy::GetInstance()->AllowGetCookie(
174 bool XWalkContentBrowserClient::AllowSetCookie(
176 const GURL& first_party,
177 const std::string& cookie_line,
178 content::ResourceContext* context,
179 int render_process_id,
181 net::CookieOptions* options) {
182 #if defined(OS_ANDROID)
183 return XWalkCookieAccessPolicy::GetInstance()->AllowSetCookie(
196 void XWalkContentBrowserClient::RequestDesktopNotificationPermission(
197 const GURL& source_origin,
198 int callback_context,
199 int render_process_id,
200 int render_view_id) {
203 blink::WebNotificationPresenter::Permission
204 XWalkContentBrowserClient::CheckDesktopNotificationPermission(
205 const GURL& source_url,
206 content::ResourceContext* context,
207 int render_process_id) {
208 #if defined(OS_ANDROID)
209 return blink::WebNotificationPresenter::PermissionAllowed;
211 return blink::WebNotificationPresenter::PermissionNotAllowed;
215 void XWalkContentBrowserClient::ShowDesktopNotification(
216 const content::ShowDesktopNotificationHostMsgParams& params,
217 int render_process_id,
220 #if defined(OS_ANDROID)
221 XWalkContentsClientBridgeBase* bridge =
222 XWalkContentsClientBridgeBase::FromID(render_process_id, render_view_id);
223 bridge->ShowNotification(params, worker, render_process_id, render_view_id);
227 void XWalkContentBrowserClient::CancelDesktopNotification(
228 int render_process_id,
230 int notification_id) {
231 #if defined(OS_ANDROID)
232 XWalkContentsClientBridgeBase* bridge =
233 XWalkContentsClientBridgeBase::FromID(render_process_id, render_view_id);
234 bridge->CancelNotification(
235 notification_id, render_process_id, render_view_id);
239 #if defined(OS_ANDROID)
240 void XWalkContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
241 const CommandLine& command_line,
242 int child_process_id,
243 std::vector<content::FileDescriptorInfo>* mappings) {
244 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
245 base::FilePath pak_file;
246 bool r = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_file);
248 pak_file = pak_file.Append(FILE_PATH_LITERAL("paks"));
249 pak_file = pak_file.Append(FILE_PATH_LITERAL(kXWalkPakFilePath));
251 base::PlatformFile f =
252 base::CreatePlatformFile(pak_file, flags, NULL, NULL);
253 if (f == base::kInvalidPlatformFileValue) {
254 NOTREACHED() << "Failed to open file when creating renderer process: "
258 content::FileDescriptorInfo(kXWalkPakDescriptor,
259 base::FileDescriptor(f, true)));
262 void XWalkContentBrowserClient::ResourceDispatcherHostCreated() {
263 RuntimeResourceDispatcherHostDelegateAndroid::
264 ResourceDispatcherHostCreated();
268 content::SpeechRecognitionManagerDelegate*
269 XWalkContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
270 return new xwalk::XWalkSpeechRecognitionManagerDelegate();