Upstream version 6.35.131.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_content_browser_client.cc
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.
4
5 #include "xwalk/runtime/browser/xwalk_content_browser_client.h"
6
7 #include <string>
8 #include <vector>
9
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"
30
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"
40 #else
41 #include "xwalk/application/browser/application_system.h"
42 #include "xwalk/application/browser/application_service.h"
43 #include "xwalk/application/browser/application.h"
44 #endif
45
46 #if defined(OS_MACOSX)
47 #include "xwalk/runtime/browser/xwalk_browser_main_parts_mac.h"
48 #endif
49
50 #if defined(OS_TIZEN)
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"
56 #endif
57
58 namespace xwalk {
59
60 namespace {
61
62 // The application-wide singleton of ContentBrowserClient impl.
63 XWalkContentBrowserClient* g_browser_client = NULL;
64
65 }  // namespace
66
67 // static
68 XWalkContentBrowserClient* XWalkContentBrowserClient::Get() {
69   return g_browser_client;
70 }
71
72
73 XWalkContentBrowserClient::XWalkContentBrowserClient(XWalkRunner* xwalk_runner)
74     : xwalk_runner_(xwalk_runner),
75       url_request_context_getter_(NULL),
76       main_parts_(NULL) {
77   DCHECK(!g_browser_client);
78   g_browser_client = this;
79 }
80
81 XWalkContentBrowserClient::~XWalkContentBrowserClient() {
82   DCHECK(g_browser_client);
83   g_browser_client = NULL;
84 }
85
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);
94 #else
95   main_parts_ = new XWalkBrowserMainParts(parameters);
96 #endif
97
98   return main_parts_;
99 }
100
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_;
108 }
109
110 net::URLRequestContextGetter*
111 XWalkContentBrowserClient::CreateRequestContextForStoragePartition(
112     content::BrowserContext* browser_context,
113     const base::FilePath& partition_path,
114     bool in_memory,
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());
121 }
122
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
131   };
132
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]);
136   }
137 }
138
139 content::QuotaPermissionContext*
140 XWalkContentBrowserClient::CreateQuotaPermissionContext() {
141   return new RuntimeQuotaPermissionContext();
142 }
143
144 content::AccessTokenStore* XWalkContentBrowserClient::CreateAccessTokenStore() {
145   return new XWalkAccessTokenStore(url_request_context_getter_);
146 }
147
148 content::WebContentsViewDelegate*
149 XWalkContentBrowserClient::GetWebContentsViewDelegate(
150     content::WebContents* web_contents) {
151 #if defined(OS_ANDROID)
152   return new XWalkWebContentsViewDelegate(web_contents);
153 #else
154   return NULL;
155 #endif
156 }
157
158 void XWalkContentBrowserClient::RenderProcessWillLaunch(
159     content::RenderProcessHost* host) {
160   xwalk_runner_->OnRenderProcessWillLaunch(host);
161   host->AddFilter(new XWalkRenderMessageFilter);
162 }
163
164 content::MediaObserver* XWalkContentBrowserClient::GetMediaObserver() {
165   return XWalkMediaCaptureDevicesDispatcher::GetInstance();
166 }
167
168 bool XWalkContentBrowserClient::AllowGetCookie(
169     const GURL& url,
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(
177       url,
178       first_party,
179       cookie_list,
180       context,
181       render_process_id,
182       render_frame_id);
183 #else
184   return true;
185 #endif
186 }
187
188 bool XWalkContentBrowserClient::AllowSetCookie(
189     const GURL& url,
190     const GURL& first_party,
191     const std::string& cookie_line,
192     content::ResourceContext* context,
193     int render_process_id,
194     int render_frame_id,
195     net::CookieOptions* options) {
196 #if defined(OS_ANDROID)
197   return XWalkCookieAccessPolicy::GetInstance()->AllowSetCookie(
198       url,
199       first_party,
200       cookie_line,
201       context,
202       render_process_id,
203       render_frame_id,
204       options);
205 #else
206   return true;
207 #endif
208 }
209
210 void XWalkContentBrowserClient::AllowCertificateError(
211     int render_process_id,
212     int render_frame_id,
213     int cert_error,
214     const net::SSLInfo& ssl_info,
215     const GURL& request_url,
216     ResourceType::Type resource_type,
217     bool overridable,
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,
226           render_frame_id);
227   bool cancel_request = true;
228   if (client)
229     client->AllowCertificateError(cert_error,
230                                   ssl_info.cert.get(),
231                                   request_url,
232                                   callback,
233                                   &cancel_request);
234   if (cancel_request)
235     *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
236 #endif
237 }
238
239 void XWalkContentBrowserClient::RequestDesktopNotificationPermission(
240     const GURL& source_origin,
241     int callback_context,
242     int render_process_id,
243     int render_view_id) {
244 }
245
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;
253 #else
254   return blink::WebNotificationPresenter::PermissionNotAllowed;
255 #endif
256 }
257
258 void XWalkContentBrowserClient::ShowDesktopNotification(
259     const content::ShowDesktopNotificationHostMsgParams& params,
260     int render_process_id,
261     int render_view_id,
262     bool worker) {
263 #if defined(OS_ANDROID)
264   XWalkContentsClientBridgeBase* bridge =
265       XWalkContentsClientBridgeBase::FromRenderViewID(render_process_id,
266           render_view_id);
267   bridge->ShowNotification(params, worker, render_process_id, render_view_id);
268 #endif
269 }
270
271 void XWalkContentBrowserClient::CancelDesktopNotification(
272     int render_process_id,
273     int render_view_id,
274     int notification_id) {
275 #if defined(OS_ANDROID)
276   XWalkContentsClientBridgeBase* bridge =
277       XWalkContentsClientBridgeBase::FromRenderViewID(render_process_id,
278           render_view_id);
279   bridge->CancelNotification(
280       notification_id, render_process_id, render_view_id);
281 #endif
282 }
283
284 #if defined(OS_ANDROID)
285 void XWalkContentBrowserClient::ResourceDispatcherHostCreated() {
286   RuntimeResourceDispatcherHostDelegateAndroid::
287   ResourceDispatcherHostCreated();
288 }
289 #endif
290
291 content::SpeechRecognitionManagerDelegate*
292     XWalkContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
293   return new xwalk::XWalkSpeechRecognitionManagerDelegate();
294 }
295
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,
305                              bool user_gesture,
306                              bool opener_suppressed,
307                              content::ResourceContext* context,
308                              int render_process_id,
309                              bool is_guest,
310                              int opener_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);
315   if (!app)
316     // If it's not a request from an application, always enable this action.
317     return true;
318
319   if (app->CanRequestURL(target_url)) {
320     LOG(INFO) << "[ALLOW] CreateWindow: " << target_url.spec();
321     return true;
322   }
323
324   LOG(INFO) << "[BlOCK] CreateWindow: " << target_url.spec();
325 #if defined(OS_TIZEN)
326   platform_util::OpenExternal(target_url);
327 #endif
328   return false;
329 }
330 #endif
331
332 }  // namespace xwalk