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/runtime_resource_dispatcher_host_delegate_android.h"
35 #include "xwalk/runtime/browser/xwalk_browser_main_parts_android.h"
36 #include "xwalk/runtime/common/android/xwalk_globals_android.h"
39 #if defined(OS_MACOSX)
40 #include "xwalk/runtime/browser/xwalk_browser_main_parts_mac.h"
44 #include "xwalk/runtime/browser/xwalk_browser_main_parts_tizen.h"
51 // The application-wide singleton of ContentBrowserClient impl.
52 XWalkContentBrowserClient* g_browser_client = NULL;
57 XWalkContentBrowserClient* XWalkContentBrowserClient::Get() {
58 return g_browser_client;
62 XWalkContentBrowserClient::XWalkContentBrowserClient(XWalkRunner* xwalk_runner)
63 : xwalk_runner_(xwalk_runner),
64 url_request_context_getter_(NULL),
66 DCHECK(!g_browser_client);
67 g_browser_client = this;
70 XWalkContentBrowserClient::~XWalkContentBrowserClient() {
71 DCHECK(g_browser_client);
72 g_browser_client = NULL;
75 content::BrowserMainParts* XWalkContentBrowserClient::CreateBrowserMainParts(
76 const content::MainFunctionParams& parameters) {
77 #if defined(OS_MACOSX)
78 main_parts_ = new XWalkBrowserMainPartsMac(parameters);
79 #elif defined(OS_ANDROID)
80 main_parts_ = new XWalkBrowserMainPartsAndroid(parameters);
81 #elif defined(OS_TIZEN)
82 main_parts_ = new XWalkBrowserMainPartsTizen(parameters);
84 main_parts_ = new XWalkBrowserMainParts(parameters);
90 net::URLRequestContextGetter* XWalkContentBrowserClient::CreateRequestContext(
91 content::BrowserContext* browser_context,
92 content::ProtocolHandlerMap* protocol_handlers) {
93 url_request_context_getter_ = static_cast<RuntimeContext*>(browser_context)->
94 CreateRequestContext(protocol_handlers);
95 return url_request_context_getter_;
98 net::URLRequestContextGetter*
99 XWalkContentBrowserClient::CreateRequestContextForStoragePartition(
100 content::BrowserContext* browser_context,
101 const base::FilePath& partition_path,
103 content::ProtocolHandlerMap* protocol_handlers) {
104 return static_cast<RuntimeContext*>(browser_context)->
105 CreateRequestContextForStoragePartition(
106 partition_path, in_memory, protocol_handlers);
109 // This allow us to append extra command line switches to the child
110 // process we launch.
111 void XWalkContentBrowserClient::AppendExtraCommandLineSwitches(
112 CommandLine* command_line, int child_process_id) {
113 CommandLine* browser_process_cmd_line = CommandLine::ForCurrentProcess();
114 const int extra_switches_count = 1;
115 const char* extra_switches[extra_switches_count] = {
116 switches::kXWalkDisableExtensionProcess
119 for (int i = 0; i < extra_switches_count; i++) {
120 if (browser_process_cmd_line->HasSwitch(extra_switches[i]))
121 command_line->AppendSwitch(extra_switches[i]);
125 content::QuotaPermissionContext*
126 XWalkContentBrowserClient::CreateQuotaPermissionContext() {
127 return new RuntimeQuotaPermissionContext();
130 content::AccessTokenStore* XWalkContentBrowserClient::CreateAccessTokenStore() {
131 return new XWalkAccessTokenStore(url_request_context_getter_);
134 content::WebContentsViewDelegate*
135 XWalkContentBrowserClient::GetWebContentsViewDelegate(
136 content::WebContents* web_contents) {
140 void XWalkContentBrowserClient::RenderProcessWillLaunch(
141 content::RenderProcessHost* host) {
142 xwalk_runner_->OnRenderProcessWillLaunch(host);
145 content::MediaObserver* XWalkContentBrowserClient::GetMediaObserver() {
146 return XWalkMediaCaptureDevicesDispatcher::GetInstance();
149 bool XWalkContentBrowserClient::AllowGetCookie(
151 const GURL& first_party,
152 const net::CookieList& cookie_list,
153 content::ResourceContext* context,
154 int render_process_id,
155 int render_view_id) {
156 #if defined(OS_ANDROID)
157 return XWalkCookieAccessPolicy::GetInstance()->AllowGetCookie(
169 bool XWalkContentBrowserClient::AllowSetCookie(
171 const GURL& first_party,
172 const std::string& cookie_line,
173 content::ResourceContext* context,
174 int render_process_id,
176 net::CookieOptions* options) {
177 #if defined(OS_ANDROID)
178 return XWalkCookieAccessPolicy::GetInstance()->AllowSetCookie(
191 void XWalkContentBrowserClient::RequestDesktopNotificationPermission(
192 const GURL& source_origin,
193 int callback_context,
194 int render_process_id,
195 int render_view_id) {
198 blink::WebNotificationPresenter::Permission
199 XWalkContentBrowserClient::CheckDesktopNotificationPermission(
200 const GURL& source_url,
201 content::ResourceContext* context,
202 int render_process_id) {
203 #if defined(OS_ANDROID)
204 return blink::WebNotificationPresenter::PermissionAllowed;
206 return blink::WebNotificationPresenter::PermissionNotAllowed;
210 void XWalkContentBrowserClient::ShowDesktopNotification(
211 const content::ShowDesktopNotificationHostMsgParams& params,
212 int render_process_id,
215 #if defined(OS_ANDROID)
216 XWalkContentsClientBridgeBase* bridge =
217 XWalkContentsClientBridgeBase::FromID(render_process_id, render_view_id);
218 bridge->ShowNotification(params, worker, render_process_id, render_view_id);
222 void XWalkContentBrowserClient::CancelDesktopNotification(
223 int render_process_id,
225 int notification_id) {
226 #if defined(OS_ANDROID)
227 XWalkContentsClientBridgeBase* bridge =
228 XWalkContentsClientBridgeBase::FromID(render_process_id, render_view_id);
229 bridge->CancelNotification(
230 notification_id, render_process_id, render_view_id);
234 #if defined(OS_ANDROID)
235 void XWalkContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
236 const CommandLine& command_line,
237 int child_process_id,
238 std::vector<content::FileDescriptorInfo>* mappings) {
239 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
240 base::FilePath pak_file;
241 bool r = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_file);
243 pak_file = pak_file.Append(FILE_PATH_LITERAL("paks"));
244 pak_file = pak_file.Append(FILE_PATH_LITERAL(kXWalkPakFilePath));
246 base::PlatformFile f =
247 base::CreatePlatformFile(pak_file, flags, NULL, NULL);
248 if (f == base::kInvalidPlatformFileValue) {
249 NOTREACHED() << "Failed to open file when creating renderer process: "
253 content::FileDescriptorInfo(kXWalkPakDescriptor,
254 base::FileDescriptor(f, true)));
257 void XWalkContentBrowserClient::ResourceDispatcherHostCreated() {
258 RuntimeResourceDispatcherHostDelegateAndroid::
259 ResourceDispatcherHostCreated();
263 content::SpeechRecognitionManagerDelegate*
264 XWalkContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
265 return new xwalk::XWalkSpeechRecognitionManagerDelegate();