1 // Copyright 2014 Samsung Electronics. 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 "web_contents_delegate_efl.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "browser/favicon/favicon_service.h"
9 #include "browser/inputpicker/color_chooser_efl.h"
10 #include "browser/javascript_dialog_manager_efl.h"
11 #include "browser/policy_response_delegate_efl.h"
12 #include "common/render_messages_ewk.h"
13 #include "content/browser/renderer_host/render_widget_host_view_efl.h"
14 #include "content/common/content_switches_internal.h"
15 #include "content/common/date_time_suggestion.h"
16 #include "content/common/render_messages_efl.h"
17 #include "content/common/view_messages.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/favicon_status.h"
20 #include "content/public/browser/invalidate_type.h"
21 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/common/favicon_url.h"
23 #include "eweb_view.h"
24 #include "eweb_view_callbacks.h"
25 #include "net/base/load_states.h"
26 #include "net/http/http_response_headers.h"
27 #include "printing/pdf_metafile_skia.h"
28 #include "private/ewk_certificate_private.h"
29 #include "private/ewk_console_message_private.h"
30 #include "private/ewk_custom_handlers_private.h"
31 #include "private/ewk_error_private.h"
32 #include "private/ewk_policy_decision_private.h"
33 #include "private/ewk_user_media_private.h"
34 #include "private/webview_delegate_ewk.h"
35 #include "public/web/WebInputEvent.h"
39 #include "content/public/browser/media_capture_devices.h"
40 #include "media/video/capture/tizen/video_capture_device_tizen.h"
43 #if defined(TIZEN_AUTOFILL_SUPPORT)
44 #include "browser/autofill/autofill_manager_delegate_efl.h"
45 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
46 #include "browser/password_manager/password_manager_client_efl.h"
47 #include "components/autofill/core/browser/autofill_client.h"
48 #include "components/autofill/core/browser/autofill_manager.h"
49 #include "components/web_modal/web_contents_modal_dialog_manager.h"
51 using autofill::AutofillManager;
52 using autofill::AutofillManagerDelegateEfl;
53 using autofill::ContentAutofillDriverFactory;
54 using password_manager::PasswordManagerClientEfl;
61 void WritePdfDataToFile(printing::PdfMetafileSkia* metafile, const base::FilePath& filename) {
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
64 base::File file(filename, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
65 metafile->SaveTo(&file);
71 static const content::MediaStreamDevice*
72 GetRequestedVideoDevice(const std::string& device_id) {
73 const content::MediaStreamDevices& video_devices =
74 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
75 if (video_devices.empty())
77 if (device_id.length() == 0)
78 return &(*video_devices.begin());
79 return video_devices.FindById(device_id);
83 WebContentsDelegateEfl::WebContentsDelegateEfl(EWebView* view)
84 : WebContentsObserver(&view->web_contents())
86 , is_fullscreen_(false)
87 , web_contents_(view->web_contents())
88 , document_created_(false)
89 , dialog_manager_(NULL)
90 , weak_ptr_factory_(this) {
91 #if defined(TIZEN_AUTOFILL_SUPPORT)
92 AutofillManagerDelegateEfl::CreateForWebContents(&web_contents_);
93 AutofillManagerDelegateEfl* autofill_manager =
94 AutofillManagerDelegateEfl::FromWebContents(&web_contents_);
95 autofill_manager->SetEWebView(view);
96 PasswordManagerClientEfl::CreateForWebContents(&web_contents_);
97 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(&web_contents_,
99 EWebView::GetPlatformLocale(),
100 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
104 WebContentsDelegateEfl::~WebContentsDelegateEfl() {
105 // It's important to delete web_contents_ before dialog_manager_
106 // destructor of web contents uses dialog_manager_
108 delete dialog_manager_;
111 WebContents* WebContentsDelegateEfl::OpenURLFromTab(
113 const content::OpenURLParams& params) {
115 const GURL& url = params.url;
116 WindowOpenDisposition disposition = params.disposition;
118 if (!source || (disposition != CURRENT_TAB &&
119 disposition != NEW_FOREGROUND_TAB &&
120 disposition != NEW_BACKGROUND_TAB &&
121 disposition != OFF_THE_RECORD)) {
126 if (disposition == NEW_FOREGROUND_TAB ||
127 disposition == NEW_BACKGROUND_TAB ||
128 disposition == OFF_THE_RECORD) {
129 Evas_Object* new_object = NULL;
130 web_view_->SmartCallback<EWebViewCallbacks::CreateNewWindow>().call(&new_object);
135 EWebView* wv = WebViewDelegateEwk::GetInstance().
136 GetWebViewFromEvasObject(new_object);
138 wv->SetURL(url.spec().c_str());
142 ui::PageTransition transition(ui::PageTransitionFromInt(params.transition));
143 source->GetController().LoadURL(url, params.referrer, transition,
148 void WebContentsDelegateEfl::NavigationStateChanged(
149 WebContents* source, InvalidateTypes changed_flags) {
150 if (changed_flags & content::INVALIDATE_TYPE_URL) {
151 const char* url = source->GetVisibleURL().spec().c_str();
152 web_view_->SmartCallback<EWebViewCallbacks::URLChanged>().call(url);
153 web_view_->SmartCallback<EWebViewCallbacks::URIChanged>().call(url);
157 void WebContentsDelegateEfl::LoadingStateChanged(WebContents* source,
158 bool to_different_document) {
159 if (source->IsLoading())
160 web_view_->SmartCallback<EWebViewCallbacks::LoadProgressStarted>().call();
162 web_view_->SmartCallback<EWebViewCallbacks::LoadProgressFinished>().call();
165 void WebContentsDelegateEfl::LoadProgressChanged(WebContents* source, double progress) {
166 web_view_->SetProgressValue(progress);
167 web_view_->SmartCallback<EWebViewCallbacks::LoadProgress>().call(&progress);
170 bool WebContentsDelegateEfl::ShouldCreateWebContents(
171 WebContents* web_contents,
173 int main_frame_route_id,
174 WindowContainerType window_container_type,
175 const string16& /*frame_name*/,
176 const GURL& target_url,
177 const std::string& partition_id,
178 SessionStorageNamespace* session_storage_namespace) {
179 // We implement the asynchronous version of the function, this
180 // one should never be invoked.
185 void WebContentsDelegateEfl::CloseContents(WebContents* source) {
186 web_view_->SmartCallback<EWebViewCallbacks::WindowClosed>().call();
189 void WebContentsDelegateEfl::EnterFullscreenModeForTab(content::WebContents* web_contents,
190 const GURL& origin, int display_id) {
191 is_fullscreen_ = true;
192 web_view_->SmartCallback<EWebViewCallbacks::EnterFullscreen>().call();
195 void WebContentsDelegateEfl::ExitFullscreenModeForTab(content::WebContents* web_contents) {
196 is_fullscreen_ = false;
197 web_view_->SmartCallback<EWebViewCallbacks::ExitFullscreen>().call();
200 bool WebContentsDelegateEfl::IsFullscreenForTabOrPending(
201 const WebContents* web_contents) const {
202 return is_fullscreen_;
205 void WebContentsDelegateEfl::RegisterProtocolHandler(WebContents* web_contents,
206 const std::string& protocol, const GURL& url, bool user_gesture) {
207 Ewk_Custom_Handlers_Data protocol_data(protocol.c_str(),
208 url.host().c_str(), url.spec().c_str());
209 web_view_->SmartCallback<EWebViewCallbacks::RegisterProtocolHandler>().call(&protocol_data);
212 void WebContentsDelegateEfl::UnregisterProtocolHandler(WebContents* web_contents,
213 const std::string& protocol,
216 Ewk_Custom_Handlers_Data protocol_data(protocol.c_str(),
217 url.host().c_str(), url.spec().c_str());
218 web_view_->SmartCallback<EWebViewCallbacks::UnregisterProtocolHandler>()
219 .call(&protocol_data);
222 #if defined(TIZEN_MULTIMEDIA_SUPPORT)
223 void WebContentsDelegateEfl::RequestMediaAccessAllow(
224 const MediaStreamRequest& request,
225 const MediaResponseCallback& callback) {
227 MediaStreamDevices devices;
229 if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) {
230 devices.push_back(MediaStreamDevice(request.audio_type,
231 "default", "Default"));
234 if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) {
235 #if defined(OS_TIZEN)
236 const content::MediaStreamDevice* video_device =
237 GetRequestedVideoDevice(request.requested_video_device_id);
239 devices.push_back(*video_device);
242 callback.Run(MediaStreamDevices(), MEDIA_DEVICE_NOT_SUPPORTED,
243 scoped_ptr<MediaStreamUI>());
246 devices.push_back(MediaStreamDevice(request.video_type, "/dev/video0", "1"));
250 callback.Run(devices, MEDIA_DEVICE_OK,scoped_ptr<MediaStreamUI>());
253 void WebContentsDelegateEfl::RequestMediaAccessDeny(
254 const MediaStreamRequest& request,
255 const MediaResponseCallback& callback) {
256 LOG(ERROR) << __FUNCTION__ << " Decline request with empty list";
257 callback.Run(MediaStreamDevices(), MEDIA_DEVICE_NOT_SUPPORTED,
258 scoped_ptr<MediaStreamUI>());
261 bool WebContentsDelegateEfl::CheckMediaAccessPermission(
262 WebContents* web_contents,
263 const GURL& security_origin,
264 MediaStreamType type) {
268 void WebContentsDelegateEfl::RequestMediaAccessPermission(
269 WebContents* web_contents,
270 const MediaStreamRequest& request,
271 const MediaResponseCallback& callback) {
272 scoped_ptr<_Ewk_User_Media_Permission_Request> media_permission_request(
273 new _Ewk_User_Media_Permission_Request(this, request, callback));
275 Eina_Bool callback_result = EINA_FALSE;
276 if (!web_view_->InvokeViewUserMediaPermissionCallback(
277 media_permission_request.get(), &callback_result)) {
278 web_view_->SmartCallback<EWebViewCallbacks::UserMediaPermission>()
279 .call(media_permission_request.get());
282 if (media_permission_request->IsSuspended())
283 media_permission_request.release();
284 else if (!media_permission_request->IsDecided()) {
285 callback.Run(MediaStreamDevices(), MEDIA_DEVICE_NOT_SUPPORTED,
286 scoped_ptr<MediaStreamUI>());
291 void WebContentsDelegateEfl::OnAuthRequired(net::URLRequest* request,
292 const std::string& realm,
293 LoginDelegateEfl* login_delegate) {
294 web_view_->InvokeAuthCallback(login_delegate, request->url(), realm);
297 void WebContentsDelegateEfl::DidStartProvisionalLoadForFrame(RenderFrameHost* render_frame_host,
298 const GURL& validated_url,
300 bool is_iframe_srcdoc) {
301 web_view_->SmartCallback<EWebViewCallbacks::ProvisionalLoadStarted>().call();
304 void WebContentsDelegateEfl::DidCommitProvisionalLoadForFrame(RenderFrameHost* render_frame_host,
306 ui::PageTransition transition_type) {
307 web_view_->SmartCallback<EWebViewCallbacks::LoadCommitted>().call();
311 void WebContentsDelegateEfl::DidNavigateMainFrame(const LoadCommittedDetails& details,
312 const FrameNavigateParams& params) {
313 // The condition checks whether the main frame navigated to a different page,
314 // not scrolled to a fragment inside the current page.
315 if (details.is_navigation_to_different_page())
316 web_view_->SmartCallback<EWebViewCallbacks::LoadStarted>().call();
319 void WebContentsDelegateEfl::DocumentOnLoadCompletedInMainFrame() {
320 web_view_->SmartCallback<EWebViewCallbacks::LoadFinished>().call();
323 void WebContentsDelegateEfl::DidNavigateAnyFrame(RenderFrameHost* render_frame_host, const LoadCommittedDetails& details, const FrameNavigateParams& params) {
324 web_view_->SmartCallback<EWebViewCallbacks::ProvisionalLoadRedirect>().call();
325 static_cast<BrowserContextEfl*>(web_contents_.GetBrowserContext())->AddVisitedURLs(params.redirects);
328 void WebContentsDelegateEfl::DidFinishLoad(RenderFrameHost* render_frame_host,
329 const GURL& validated_url) {
330 if (render_frame_host->GetParent())
333 NavigationEntry *entry = web_contents().GetController().GetVisibleEntry();
337 FaviconStatus &favicon = entry->GetFavicon();
340 // check/update the url and favicon url in favicon database
342 fs.SetFaviconURLForPageURL(favicon.url, validated_url);
344 // download favicon if there is no such in database
345 if (!fs.ExistsForFaviconURL(favicon.url)) {
346 LOG(ERROR) << "[DidFinishLoad] :: no favicon in database for URL: "
347 << favicon.url.spec();
348 favicon_downloader_.reset(
349 new FaviconDownloader(&web_contents_,
352 &WebContentsDelegateEfl::DidDownloadFavicon,
353 weak_ptr_factory_.GetWeakPtr())));
354 favicon_downloader_->Start();
356 web_view_->SmartCallback<EWebViewCallbacks::IconReceived>().call();
360 web_contents_.Focus();
364 void WebContentsDelegateEfl::DidUpdateFaviconURL(const std::vector<FaviconURL>& candidates) {
365 // select and set proper favicon
366 for (unsigned int i = 0; i < candidates.size(); ++i) {
367 FaviconURL favicon = candidates[i];
368 if (favicon.icon_type == FaviconURL::FAVICON && !favicon.icon_url.is_empty()) {
369 NavigationEntry *entry = web_contents_.GetController().GetVisibleEntry();
372 entry->GetFavicon().url = favicon.icon_url;
373 entry->GetFavicon().valid = true;
380 void WebContentsDelegateEfl::DidDownloadFavicon(bool success, const GURL& icon_url, const SkBitmap& bitmap) {
381 favicon_downloader_.reset();
384 fs.SetBitmapForFaviconURL(bitmap, icon_url);
385 // emit "icon,received"
386 web_view_->SmartCallback<EWebViewCallbacks::IconReceived>().call();
390 void WebContentsDelegateEfl::RequestCertificateConfirm(WebContents* /*web_contents*/,
392 const net::SSLInfo& ssl_info,
394 ResourceType /*resource_type*/,
395 bool /*overridable*/,
396 bool /*strict_enforcement*/,
397 const base::Callback<void(bool)>& callback,
398 CertificateRequestResultType* result) {
400 std::string pem_certificate;
401 if (!net::X509Certificate::GetPEMEncoded(ssl_info.cert->os_cert_handle(), &pem_certificate)) {
402 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL;
405 certificate_policy_decision_.reset(new _Ewk_Certificate_Policy_Decision(url,
409 web_view_->SmartCallback<EWebViewCallbacks::RequestCertificateConfirm>().call(
410 certificate_policy_decision_.get());
412 // if policy is suspended, the API takes over the policy object lifetime
413 // and policy will be deleted after decision is made
414 if (certificate_policy_decision_->isSuspended()) {
415 certificate_policy_decision_.release();
417 certificate_policy_decision_->setDecision(true);
421 void WebContentsDelegateEfl::SetContentSecurityPolicy(const std::string& policy, Ewk_CSP_Header_Type header_type) {
422 if (document_created_) {
423 RenderViewHost* rvh = web_contents_.GetRenderViewHost();
424 rvh->Send(new EwkViewMsg_SetCSP(rvh->GetRoutingID(), policy, header_type));
426 DCHECK(!pending_content_security_policy_.get());
427 pending_content_security_policy_.reset(new ContentSecurityPolicy(policy, header_type));
431 void WebContentsDelegateEfl::FindReply(WebContents* web_contents,
433 int number_of_matches,
434 const gfx::Rect& selection_rect,
435 int active_match_ordinal,
437 if (final_update && request_id == web_view_->current_find_request_id()) {
438 unsigned int uint_number_of_matches = static_cast<unsigned int>(number_of_matches);
439 web_view_->SmartCallback<EWebViewCallbacks::TextFound>().call(&uint_number_of_matches);
443 JavaScriptDialogManager* WebContentsDelegateEfl::GetJavaScriptDialogManager(
444 WebContents* source) {
445 if (!dialog_manager_)
446 dialog_manager_ = new JavaScriptDialogManagerEfl();
447 return dialog_manager_;
450 void WebContentsDelegateEfl::OnUpdateSettings(const Ewk_Settings *settings) {
451 #if defined(TIZEN_AUTOFILL_SUPPORT)
452 PasswordManagerClientEfl *client =
453 PasswordManagerClientEfl::FromWebContents(&web_contents_);
455 client->SetPasswordManagerSavingEnabled(settings->autofillPasswordForm());
456 client->SetPasswordManagerFillingEnabled(settings->autofillPasswordForm());
461 bool WebContentsDelegateEfl::OnMessageReceived(const IPC::Message& message) {
463 IPC_BEGIN_MESSAGE_MAP(WebContentsDelegateEfl, message)
464 IPC_MESSAGE_HANDLER_DELAY_REPLY(EwkHostMsg_GetContentSecurityPolicy, OnGetContentSecurityPolicy)
465 IPC_MESSAGE_HANDLER(EwkHostMsg_DidPrintPagesToPdf, OnPrintedMetafileReceived)
466 IPC_MESSAGE_HANDLER(EwkHostMsg_WrtMessage, OnWrtPluginMessage)
467 IPC_MESSAGE_HANDLER_DELAY_REPLY(EwkHostMsg_WrtSyncMessage, OnWrtPluginSyncMessage)
468 IPC_MESSAGE_UNHANDLED(handled = false)
469 IPC_END_MESSAGE_MAP()
474 void WebContentsDelegateEfl::OnWrtPluginMessage(const Ewk_Wrt_Message_Data& data) {
475 scoped_ptr<Ewk_Wrt_Message_Data> p(new Ewk_Wrt_Message_Data);
477 p->value = data.value;
479 p->reference_id = data.reference_id;
481 web_view_->SmartCallback<EWebViewCallbacks::WrtPluginsMessage>().call(p.get());
484 void WebContentsDelegateEfl::OnWrtPluginSyncMessage(const Ewk_Wrt_Message_Data& data,
485 IPC::Message* reply) {
486 Ewk_Wrt_Message_Data tmp = data;
487 web_view_->SmartCallback<EWebViewCallbacks::WrtPluginsMessage>().call(&tmp);
488 EwkHostMsg_WrtSyncMessage::WriteReplyParams(reply, tmp.value);
492 void WebContentsDelegateEfl::DidFirstVisuallyNonEmptyPaint() {
493 web_view_->SmartCallback<EWebViewCallbacks::LoadNonEmptyLayoutFinished>().call();
494 web_view_->SmartCallback<EWebViewCallbacks::FrameRendered>().call(0);
497 void WebContentsDelegateEfl::OnGetContentSecurityPolicy(IPC::Message* reply_msg) {
498 document_created_ = true;
499 if (!pending_content_security_policy_.get()) {
500 EwkHostMsg_GetContentSecurityPolicy::WriteReplyParams(reply_msg, std::string(), EWK_DEFAULT_POLICY);
502 EwkHostMsg_GetContentSecurityPolicy::WriteReplyParams(reply_msg,
503 pending_content_security_policy_->policy, pending_content_security_policy_->header_type);
504 pending_content_security_policy_.reset();
509 void WebContentsDelegateEfl::OnPrintedMetafileReceived(const DidPrintPagesParams& params) {
510 base::SharedMemory shared_buf(params.metafile_data_handle, true);
511 if (!shared_buf.Map(params.data_size)) {
512 NOTREACHED() << "couldn't map";
515 scoped_ptr<printing::PdfMetafileSkia> metafile(new printing::PdfMetafileSkia);
516 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) {
517 NOTREACHED() << "Invalid metafile header";
520 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
521 base::Bind(&WritePdfDataToFile, metafile.release(), params.filename));
524 void WebContentsDelegateEfl::NavigationEntryCommitted(const LoadCommittedDetails& load_details) {
525 web_view_->InvokeBackForwardListChangedCallback();
528 void WebContentsDelegateEfl::RenderViewCreated(RenderViewHost* render_view_host) {
529 web_view_->RenderViewCreated(render_view_host);
532 void WebContentsDelegateEfl::RenderProcessGone(base::TerminationStatus status) {
533 // See RenderWidgetHostViewEfl::RenderProcessGone.
534 if (status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION
535 || status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED
536 || status == base::TERMINATION_STATUS_PROCESS_CRASHED) {
537 web_view_->HandleRendererProcessCrash();
541 bool WebContentsDelegateEfl::AddMessageToConsole(WebContents* source,
543 const string16& message,
545 const string16& source_id) {
546 scoped_ptr<_Ewk_Console_Message> console_message(new _Ewk_Console_Message(level,
547 UTF16ToUTF8(message).c_str(),
549 source->GetVisibleURL().spec().c_str()));
550 web_view_->SmartCallback<EWebViewCallbacks::ConsoleMessage>().call(console_message.get());
554 void WebContentsDelegateEfl::RunFileChooser(WebContents* web_contents, const FileChooserParams& params) {
555 web_view_->ShowFileChooser(params);
558 content::ColorChooser* WebContentsDelegateEfl::OpenColorChooser(
559 WebContents* web_contents,
561 const std::vector<ColorSuggestion>& suggestions) {
562 ColorChooserEfl* color_chooser_efl = new ColorChooserEfl(*web_contents);
563 web_view_->RequestColorPicker(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), SkColorGetA(color));
565 return color_chooser_efl;
568 void WebContentsDelegateEfl::OpenDateTimeDialog(
569 ui::TextInputType dialog_type,
574 const std::vector<DateTimeSuggestion>& suggestions) {
575 web_view_->InputPickerShow(dialog_type, dialog_value);
578 bool WebContentsDelegateEfl::PreHandleGestureEvent(
580 const blink::WebGestureEvent& event) {
581 switch (event.type) {
582 case blink::WebInputEvent::GestureDoubleTap:
586 case blink::WebInputEvent::GesturePinchBegin:
587 case blink::WebInputEvent::GesturePinchUpdate:
588 case blink::WebInputEvent::GesturePinchEnd:
589 if (!IsPinchToZoomEnabled() ||
590 IsFullscreenForTabOrPending(&web_contents()))
599 } //namespace content