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/ssl/ssl_info.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "xwalk/extensions/common/xwalk_extension_switches.h"
22 #include "xwalk/runtime/browser/xwalk_browser_main_parts.h"
23 #include "xwalk/runtime/browser/geolocation/xwalk_access_token_store.h"
24 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
25 #include "xwalk/runtime/browser/runtime_context.h"
26 #include "xwalk/runtime/browser/runtime_quota_permission_context.h"
27 #include "xwalk/runtime/browser/speech/speech_recognition_manager_delegate.h"
28 #include "xwalk/runtime/browser/xwalk_render_message_filter.h"
29 #include "xwalk/runtime/browser/xwalk_runner.h"
31 #if defined(OS_ANDROID)
32 #include "base/android/path_utils.h"
33 #include "base/base_paths_android.h"
34 #include "xwalk/runtime/browser/android/xwalk_cookie_access_policy.h"
35 #include "xwalk/runtime/browser/android/xwalk_contents_client_bridge.h"
36 #include "xwalk/runtime/browser/android/xwalk_web_contents_view_delegate.h"
37 #include "xwalk/runtime/browser/runtime_resource_dispatcher_host_delegate_android.h"
38 #include "xwalk/runtime/browser/xwalk_browser_main_parts_android.h"
39 #include "xwalk/runtime/common/android/xwalk_globals_android.h"
41 #include "xwalk/application/browser/application_system.h"
42 #include "xwalk/application/browser/application_service.h"
43 #include "xwalk/application/browser/application.h"
46 #if defined(OS_MACOSX)
47 #include "xwalk/runtime/browser/xwalk_browser_main_parts_mac.h"
51 #include "xwalk/application/common/application_manifest_constants.h"
52 #include "xwalk/application/common/manifest_handlers/navigation_handler.h"
53 #include "xwalk/application/common/constants.h"
54 #include "xwalk/runtime/browser/runtime_platform_util.h"
55 #include "xwalk/runtime/browser/xwalk_browser_main_parts_tizen.h"
62 // The application-wide singleton of ContentBrowserClient impl.
63 XWalkContentBrowserClient* g_browser_client = NULL;
68 XWalkContentBrowserClient* XWalkContentBrowserClient::Get() {
69 return g_browser_client;
73 XWalkContentBrowserClient::XWalkContentBrowserClient(XWalkRunner* xwalk_runner)
74 : xwalk_runner_(xwalk_runner),
75 url_request_context_getter_(NULL),
77 DCHECK(!g_browser_client);
78 g_browser_client = this;
81 XWalkContentBrowserClient::~XWalkContentBrowserClient() {
82 DCHECK(g_browser_client);
83 g_browser_client = NULL;
86 content::BrowserMainParts* XWalkContentBrowserClient::CreateBrowserMainParts(
87 const content::MainFunctionParams& parameters) {
88 #if defined(OS_MACOSX)
89 main_parts_ = new XWalkBrowserMainPartsMac(parameters);
90 #elif defined(OS_ANDROID)
91 main_parts_ = new XWalkBrowserMainPartsAndroid(parameters);
92 #elif defined(OS_TIZEN)
93 main_parts_ = new XWalkBrowserMainPartsTizen(parameters);
95 main_parts_ = new XWalkBrowserMainParts(parameters);
101 net::URLRequestContextGetter* XWalkContentBrowserClient::CreateRequestContext(
102 content::BrowserContext* browser_context,
103 content::ProtocolHandlerMap* protocol_handlers,
104 content::ProtocolHandlerScopedVector protocol_interceptors) {
105 url_request_context_getter_ = static_cast<RuntimeContext*>(browser_context)->
106 CreateRequestContext(protocol_handlers, protocol_interceptors.Pass());
107 return url_request_context_getter_;
110 net::URLRequestContextGetter*
111 XWalkContentBrowserClient::CreateRequestContextForStoragePartition(
112 content::BrowserContext* browser_context,
113 const base::FilePath& partition_path,
115 content::ProtocolHandlerMap* protocol_handlers,
116 content::ProtocolHandlerScopedVector protocol_interceptors) {
117 return static_cast<RuntimeContext*>(browser_context)->
118 CreateRequestContextForStoragePartition(
119 partition_path, in_memory, protocol_handlers,
120 protocol_interceptors.Pass());
123 // This allow us to append extra command line switches to the child
124 // process we launch.
125 void XWalkContentBrowserClient::AppendExtraCommandLineSwitches(
126 CommandLine* command_line, int child_process_id) {
127 CommandLine* browser_process_cmd_line = CommandLine::ForCurrentProcess();
128 const int extra_switches_count = 1;
129 const char* extra_switches[extra_switches_count] = {
130 switches::kXWalkDisableExtensionProcess
133 for (int i = 0; i < extra_switches_count; i++) {
134 if (browser_process_cmd_line->HasSwitch(extra_switches[i]))
135 command_line->AppendSwitch(extra_switches[i]);
139 content::QuotaPermissionContext*
140 XWalkContentBrowserClient::CreateQuotaPermissionContext() {
141 return new RuntimeQuotaPermissionContext();
144 content::AccessTokenStore* XWalkContentBrowserClient::CreateAccessTokenStore() {
145 return new XWalkAccessTokenStore(url_request_context_getter_);
148 content::WebContentsViewDelegate*
149 XWalkContentBrowserClient::GetWebContentsViewDelegate(
150 content::WebContents* web_contents) {
151 #if defined(OS_ANDROID)
152 return new XWalkWebContentsViewDelegate(web_contents);
158 void XWalkContentBrowserClient::RenderProcessWillLaunch(
159 content::RenderProcessHost* host) {
160 xwalk_runner_->OnRenderProcessWillLaunch(host);
161 host->AddFilter(new XWalkRenderMessageFilter);
164 content::MediaObserver* XWalkContentBrowserClient::GetMediaObserver() {
165 return XWalkMediaCaptureDevicesDispatcher::GetInstance();
168 bool XWalkContentBrowserClient::AllowGetCookie(
170 const GURL& first_party,
171 const net::CookieList& cookie_list,
172 content::ResourceContext* context,
173 int render_process_id,
174 int render_frame_id) {
175 #if defined(OS_ANDROID)
176 return XWalkCookieAccessPolicy::GetInstance()->AllowGetCookie(
188 bool XWalkContentBrowserClient::AllowSetCookie(
190 const GURL& first_party,
191 const std::string& cookie_line,
192 content::ResourceContext* context,
193 int render_process_id,
195 net::CookieOptions* options) {
196 #if defined(OS_ANDROID)
197 return XWalkCookieAccessPolicy::GetInstance()->AllowSetCookie(
210 void XWalkContentBrowserClient::AllowCertificateError(
211 int render_process_id,
214 const net::SSLInfo& ssl_info,
215 const GURL& request_url,
216 ResourceType::Type resource_type,
218 bool strict_enforcement,
219 const base::Callback<void(bool)>& callback, // NOLINT
220 content::CertificateRequestResultType* result) {
221 // Currently only Android handles it.
222 // TODO(yongsheng): applies it for other platforms?
223 #if defined(OS_ANDROID)
224 XWalkContentsClientBridgeBase* client =
225 XWalkContentsClientBridgeBase::FromRenderFrameID(render_process_id,
227 bool cancel_request = true;
229 client->AllowCertificateError(cert_error,
235 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
239 void XWalkContentBrowserClient::RequestDesktopNotificationPermission(
240 const GURL& source_origin,
241 int callback_context,
242 int render_process_id,
243 int render_view_id) {
246 blink::WebNotificationPresenter::Permission
247 XWalkContentBrowserClient::CheckDesktopNotificationPermission(
248 const GURL& source_url,
249 content::ResourceContext* context,
250 int render_process_id) {
251 #if defined(OS_ANDROID)
252 return blink::WebNotificationPresenter::PermissionAllowed;
254 return blink::WebNotificationPresenter::PermissionNotAllowed;
258 void XWalkContentBrowserClient::ShowDesktopNotification(
259 const content::ShowDesktopNotificationHostMsgParams& params,
260 int render_process_id,
263 #if defined(OS_ANDROID)
264 XWalkContentsClientBridgeBase* bridge =
265 XWalkContentsClientBridgeBase::FromRenderViewID(render_process_id,
267 bridge->ShowNotification(params, worker, render_process_id, render_view_id);
271 void XWalkContentBrowserClient::CancelDesktopNotification(
272 int render_process_id,
274 int notification_id) {
275 #if defined(OS_ANDROID)
276 XWalkContentsClientBridgeBase* bridge =
277 XWalkContentsClientBridgeBase::FromRenderViewID(render_process_id,
279 bridge->CancelNotification(
280 notification_id, render_process_id, render_view_id);
284 #if defined(OS_ANDROID)
285 void XWalkContentBrowserClient::ResourceDispatcherHostCreated() {
286 RuntimeResourceDispatcherHostDelegateAndroid::
287 ResourceDispatcherHostCreated();
291 content::SpeechRecognitionManagerDelegate*
292 XWalkContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
293 return new xwalk::XWalkSpeechRecognitionManagerDelegate();
296 #if !defined(OS_ANDROID)
297 bool XWalkContentBrowserClient::CanCreateWindow(const GURL& opener_url,
298 const GURL& opener_top_level_frame_url,
299 const GURL& source_origin,
300 WindowContainerType container_type,
301 const GURL& target_url,
302 const content::Referrer& referrer,
303 WindowOpenDisposition disposition,
304 const blink::WebWindowFeatures& features,
306 bool opener_suppressed,
307 content::ResourceContext* context,
308 int render_process_id,
311 bool* no_javascript_access) {
312 *no_javascript_access = false;
313 application::Application* app = xwalk_runner_->app_system()->
314 application_service()->GetApplicationByRenderHostID(render_process_id);
316 // If it's not a request from an application, always enable this action.
319 if (app->CanRequestURL(target_url)) {
320 LOG(INFO) << "[ALLOW] CreateWindow: " << target_url.spec();
324 LOG(INFO) << "[BlOCK] CreateWindow: " << target_url.spec();
325 #if defined(OS_TIZEN)
326 platform_util::OpenExternal(target_url);