3b7fb942da3b9c6039ae34fe3322846056b54209
[platform/framework/web/chromium-efl.git] / tizen_src / impl / content_browser_client_efl.cc
1 /*
2    Copyright (C) 2014 Samsung Electronics
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "content_browser_client_efl.h"
21
22 #include "command_line_efl.h"
23 #include "browser_main_parts_efl.h"
24 #include "browser_context_efl.h"
25 #include "web_contents_delegate_efl.h"
26 #include "browser/web_contents/web_contents_view_efl.h"
27 #include "browser/geolocation/access_token_store_efl.h"
28 #include "browser/renderer_host/render_message_filter_efl.h"
29 #include "browser/resource_dispatcher_host_delegate_efl.h"
30 #include "browser/vibration/vibration_message_filter.h"
31 #include "content/public/browser/resource_dispatcher_host.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/common/show_desktop_notification_params.h"
34 #include "common/web_contents_utils.h"
35 #include "components/editing/content/browser/editor_client_observer.h"
36
37 #if defined(OS_TIZEN)
38 #include "browser/geolocation/location_provider_efl.h"
39 #endif
40
41 #include "tizen_webview/public/tw_notification.h"
42 #include "tizen_webview/public/tw_security_origin.h"
43 #include "tizen_webview/public/tw_url.h"
44 #include "tizen_webview/tw_misc_utility.h"
45
46 using web_contents_utils::WebContentsFromFrameID;
47 using web_contents_utils::WebContentsFromViewID;
48 using tizen_webview::NotificationPermissionRequest;
49 using tizen_webview::URL;
50 using tizen_webview::Security_Origin;
51
52 #warning "[M38] tempoary disable notification. remove below line"
53 #undef ENABLE_NOTIFICATIONS
54
55 namespace content {
56
57 ContentBrowserClientEfl::ContentBrowserClientEfl()
58   : web_context_(NULL), browser_main_parts_efl_(NULL) {
59 }
60
61 BrowserMainParts* ContentBrowserClientEfl::CreateBrowserMainParts(
62     const MainFunctionParams& parameters) {
63   browser_main_parts_efl_ = new BrowserMainPartsEfl();
64   return browser_main_parts_efl_;
65 }
66
67 net::URLRequestContextGetter* ContentBrowserClientEfl::CreateRequestContext(
68       BrowserContext* browser_context,
69       ProtocolHandlerMap* protocol_handlers,
70       URLRequestInterceptorScopedVector request_interceptors) {
71   if (browser_context->IsOffTheRecord()) {
72     LOG(ERROR) << "off the record browser context not implemented";
73     return NULL;
74   }
75
76   return static_cast<BrowserContextEfl*>(browser_context)->
77       CreateRequestContext(protocol_handlers, request_interceptors.Pass());
78 }
79
80 AccessTokenStore* ContentBrowserClientEfl::CreateAccessTokenStore() {
81   return new AccessTokenStoreEfl();
82 }
83
84 #if defined(OS_TIZEN)
85 LocationProvider* ContentBrowserClientEfl::OverrideSystemLocationProvider() {
86   return LocationProviderEfl::Create();
87 }
88 #endif
89
90 void ContentBrowserClientEfl::AppendExtraCommandLineSwitches(CommandLine* command_line,
91     int child_process_id) {
92   CommandLineEfl::AppendProcessSpecificArgs(*command_line);
93 }
94
95 void ContentBrowserClientEfl::ResourceDispatcherHostCreated() {
96   ResourceDispatcherHost* host = ResourceDispatcherHost::Get();
97   resource_disp_host_del_efl_.reset(new ResourceDispatcherHostDelegateEfl());
98   host->SetDelegate(resource_disp_host_del_efl_.get());
99 }
100
101 void ContentBrowserClientEfl::AllowCertificateError(
102     int render_process_id, int render_frame_id, int cert_error,
103     const net::SSLInfo& ssl_info, const GURL& request_url,
104     ResourceType::Type resource_type, bool overridable,
105     bool strict_enforcement, const base::Callback<void(bool)>& callback,
106     CertificateRequestResultType* result) {
107
108   WebContents* web_contents = WebContentsFromFrameID(render_process_id, render_frame_id);
109   if (!web_contents) {
110     NOTREACHED();
111     return;
112   }
113   WebContentsDelegate * delegate = web_contents->GetDelegate();
114   if (!delegate) {
115     callback.Run(NULL);
116     return;
117   }
118   static_cast<content::WebContentsDelegateEfl*>(delegate)->
119      RequestCertificateConfirm(web_contents, cert_error, ssl_info, request_url,
120          resource_type, overridable, strict_enforcement, callback, result);
121 }
122
123 void ContentBrowserClientEfl::RequestDesktopNotificationPermission(
124     const GURL& source_origin,
125     content::RenderFrameHost* render_frame_host,
126     const base::Closure& callback) {
127 #if defined(ENABLE_NOTIFICATIONS) && !defined(EWK_BRINGUP)
128   WebContents* web_contents = WebContentsFromViewID(render_process_id,
129                                                     render_view_id);
130   if (!web_contents)
131     return;
132
133   WebContentsDelegateEfl* delegate =
134        static_cast<WebContentsDelegateEfl*>(web_contents->GetDelegate());
135   if (!delegate)
136     return;
137
138   BrowserContextEfl* browser_context =
139       static_cast<BrowserContextEfl*>(web_contents->GetBrowserContext());
140   NotificationPermissionRequest* notification_permission
141       = new NotificationPermissionRequest(delegate->web_view()->evas_object(),
142                                           callback_context,
143                                           tizen_webview::GetURL(source_origin));
144
145   delegate->web_view()->
146       SmartCallback<EWebViewCallbacks::NotificationPermissionRequest>()
147         .call(notification_permission);
148   // A smart callback cannot have ownership for data because the callback may
149   // not ever exist. Therefore new resource should be deleted in the call site.
150   // [sns.park] TODO: uncomment below if no side effect.
151   //delete notification_permission;
152 #else
153   NOTIMPLEMENTED();
154 #endif
155 }
156
157 void ContentBrowserClientEfl::ShowDesktopNotification(
158       const content::ShowDesktopNotificationHostMsgParams& params,
159       content::RenderFrameHost* render_frame_host,
160       content::DesktopNotificationDelegate* delegate,
161       base::Closure* cancel_callback) {
162 #if defined(ENABLE_NOTIFICATIONS) && !defined(EWK_BRINGUP)
163   WebContents* web_contents = WebContentsFromViewID(render_process_id, render_view_id);
164   if (!web_contents)
165     return;
166
167   WebContentsDelegateEfl* delegate =
168       static_cast<WebContentsDelegateEfl*>(web_contents->GetDelegate());
169   if (!delegate)
170     return;
171
172   BrowserContextEfl* browser_context =
173       static_cast<BrowserContextEfl*>(web_contents->GetBrowserContext());
174   uint64_t old_notification_id = 0;
175
176   if (!params.replace_id.empty() && browser_context->GetNotificationController()->
177       IsNotificationPresent(params.replace_id, old_notification_id))
178     CancelDesktopNotification(render_process_id, render_view_id, old_notification_id);
179
180   browser_context->GetNotificationController()->
181       AddNotification(params.notification_id, render_process_id,
182                       render_view_id, params.replace_id);
183   tizen_webview::Notification* notification =
184       new tizen_webview::Notification(base::UTF16ToUTF8(params.body),
185                                       params.icon_url.spec(),
186                                       base::UTF16ToUTF8(params.replace_id),
187                                       base::UTF16ToUTF8(params.title),
188                                       params.notification_id,
189                                       URL(params.origin.host(),
190                                           params.origin.scheme(),
191                                           atoi(params.origin.port().c_str())));
192
193   delegate->web_view()->
194       SmartCallback<EWebViewCallbacks::NotificationShow>().call(notification);
195   // A smart callback cannot have ownership for data because the callback may
196   // not ever exist. Therefore new resource should be deleted in the call site.
197   // [sns.park] TODO: uncomment below if no side effect.
198   //delete notification;
199 #else
200   NOTIMPLEMENTED();
201 #endif
202 }
203
204 bool ContentBrowserClientEfl::AllowGetCookie(const GURL& url,
205                                              const GURL& first_party,
206                                              const net::CookieList& cookie_list,
207                                              content::ResourceContext* context,
208                                              int render_process_id,
209                                              int render_frame_id) {
210   if (!web_context_)
211       return false;
212
213   CookieManager* cookie_manager = web_context_->cookieManager();
214   if (!cookie_manager)
215     return false;
216
217   return cookie_manager->AllowGetCookie(url,
218                                         first_party,
219                                         cookie_list,
220                                         context,
221                                         render_process_id,
222                                         render_frame_id);
223 }
224
225 bool ContentBrowserClientEfl::AllowSetCookie(const GURL& url,
226                                              const GURL& first_party,
227                                              const std::string& cookie_line,
228                                              content::ResourceContext* context,
229                                              int render_process_id,
230                                              int render_frame_id,
231                                              net::CookieOptions* options) {
232   if (!web_context_)
233     return false;
234
235   CookieManager* cookie_manager = web_context_->cookieManager();
236   if (!cookie_manager)
237     return false;
238
239   return cookie_manager->AllowSetCookie(url,
240                                         first_party,
241                                         cookie_line,
242                                         context,
243                                         render_process_id,
244                                         render_frame_id,
245                                         options);
246 }
247
248 void ContentBrowserClientEfl::RenderProcessWillLaunch(
249     content::RenderProcessHost* host) {
250
251   BrowserContextEfl* browser_context = static_cast<BrowserContextEfl*>(
252                                           host->GetBrowserContext());
253
254   // TODO: is this situation possible? Should it ever happen?
255   DCHECK(browser_context);
256
257   if (browser_context)
258     web_context_ = browser_context->WebContext();
259
260   host->AddFilter(new RenderMessageFilterEfl(host->GetID()));
261   host->AddFilter(new VibrationMessageFilter());
262   host->AddFilter(new editing::EditorClientObserver(host->GetID()));
263 }
264
265 }