363b4127faccba9b3458404f187e5e359006b599
[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/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"
28
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"
38 #endif
39
40 #if defined(OS_MACOSX)
41 #include "xwalk/runtime/browser/xwalk_browser_main_parts_mac.h"
42 #endif
43
44 #if defined(OS_TIZEN)
45 #include "xwalk/runtime/browser/xwalk_browser_main_parts_tizen.h"
46 #endif
47
48 namespace xwalk {
49
50 namespace {
51
52 // The application-wide singleton of ContentBrowserClient impl.
53 XWalkContentBrowserClient* g_browser_client = NULL;
54
55 }  // namespace
56
57 // static
58 XWalkContentBrowserClient* XWalkContentBrowserClient::Get() {
59   return g_browser_client;
60 }
61
62
63 XWalkContentBrowserClient::XWalkContentBrowserClient(XWalkRunner* xwalk_runner)
64     : xwalk_runner_(xwalk_runner),
65       url_request_context_getter_(NULL),
66       main_parts_(NULL) {
67   DCHECK(!g_browser_client);
68   g_browser_client = this;
69 }
70
71 XWalkContentBrowserClient::~XWalkContentBrowserClient() {
72   DCHECK(g_browser_client);
73   g_browser_client = NULL;
74 }
75
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);
84 #else
85   main_parts_ = new XWalkBrowserMainParts(parameters);
86 #endif
87
88   return main_parts_;
89 }
90
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_;
97 }
98
99 net::URLRequestContextGetter*
100 XWalkContentBrowserClient::CreateRequestContextForStoragePartition(
101     content::BrowserContext* browser_context,
102     const base::FilePath& partition_path,
103     bool in_memory,
104     content::ProtocolHandlerMap* protocol_handlers) {
105   return static_cast<RuntimeContext*>(browser_context)->
106       CreateRequestContextForStoragePartition(
107           partition_path, in_memory, protocol_handlers);
108 }
109
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
118   };
119
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]);
123   }
124 }
125
126 content::QuotaPermissionContext*
127 XWalkContentBrowserClient::CreateQuotaPermissionContext() {
128   return new RuntimeQuotaPermissionContext();
129 }
130
131 content::AccessTokenStore* XWalkContentBrowserClient::CreateAccessTokenStore() {
132   return new XWalkAccessTokenStore(url_request_context_getter_);
133 }
134
135 content::WebContentsViewDelegate*
136 XWalkContentBrowserClient::GetWebContentsViewDelegate(
137     content::WebContents* web_contents) {
138 #if defined(OS_ANDROID)
139   return new XWalkWebContentsViewDelegate(web_contents);
140 #else
141   return NULL;
142 #endif
143 }
144
145 void XWalkContentBrowserClient::RenderProcessWillLaunch(
146     content::RenderProcessHost* host) {
147   xwalk_runner_->OnRenderProcessWillLaunch(host);
148 }
149
150 content::MediaObserver* XWalkContentBrowserClient::GetMediaObserver() {
151   return XWalkMediaCaptureDevicesDispatcher::GetInstance();
152 }
153
154 bool XWalkContentBrowserClient::AllowGetCookie(
155     const GURL& url,
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(
163       url,
164       first_party,
165       cookie_list,
166       context,
167       render_process_id,
168       render_view_id);
169 #else
170   return true;
171 #endif
172 }
173
174 bool XWalkContentBrowserClient::AllowSetCookie(
175     const GURL& url,
176     const GURL& first_party,
177     const std::string& cookie_line,
178     content::ResourceContext* context,
179     int render_process_id,
180     int render_view_id,
181     net::CookieOptions* options) {
182 #if defined(OS_ANDROID)
183   return XWalkCookieAccessPolicy::GetInstance()->AllowSetCookie(
184       url,
185       first_party,
186       cookie_line,
187       context,
188       render_process_id,
189       render_view_id,
190       options);
191 #else
192   return true;
193 #endif
194 }
195
196 void XWalkContentBrowserClient::RequestDesktopNotificationPermission(
197     const GURL& source_origin,
198     int callback_context,
199     int render_process_id,
200     int render_view_id) {
201 }
202
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;
210 #else
211   return blink::WebNotificationPresenter::PermissionNotAllowed;
212 #endif
213 }
214
215 void XWalkContentBrowserClient::ShowDesktopNotification(
216     const content::ShowDesktopNotificationHostMsgParams& params,
217     int render_process_id,
218     int render_view_id,
219     bool worker) {
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);
224 #endif
225 }
226
227 void XWalkContentBrowserClient::CancelDesktopNotification(
228     int render_process_id,
229     int render_view_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);
236 #endif
237 }
238
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);
247   CHECK(r);
248   pak_file = pak_file.Append(FILE_PATH_LITERAL("paks"));
249   pak_file = pak_file.Append(FILE_PATH_LITERAL(kXWalkPakFilePath));
250
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: "
255                  << "xwalk.pak";
256   }
257   mappings->push_back(
258       content::FileDescriptorInfo(kXWalkPakDescriptor,
259                                   base::FileDescriptor(f, true)));
260 }
261
262 void XWalkContentBrowserClient::ResourceDispatcherHostCreated() {
263   RuntimeResourceDispatcherHostDelegateAndroid::
264   ResourceDispatcherHostCreated();
265 }
266 #endif
267
268 content::SpeechRecognitionManagerDelegate*
269     XWalkContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
270   return new xwalk::XWalkSpeechRecognitionManagerDelegate();
271 }
272
273 }  // namespace xwalk