1 // Copyright (c) 2014 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
5 #include "atom/browser/api/atom_api_web_contents.h"
11 #include "atom/browser/api/atom_api_debugger.h"
12 #include "atom/browser/api/atom_api_session.h"
13 #include "atom/browser/api/atom_api_window.h"
14 #include "atom/browser/atom_browser_client.h"
15 #include "atom/browser/atom_browser_context.h"
16 #include "atom/browser/atom_browser_main_parts.h"
17 #include "atom/browser/browser.h"
18 #include "atom/browser/lib/bluetooth_chooser.h"
19 #include "atom/browser/native_window.h"
20 #include "atom/browser/net/atom_network_delegate.h"
21 #include "atom/browser/osr/osr_output_device.h"
22 #include "atom/browser/osr/osr_render_widget_host_view.h"
23 #include "atom/browser/osr/osr_web_contents_view.h"
24 #include "atom/browser/ui/drag_util.h"
25 #include "atom/browser/web_contents_permission_helper.h"
26 #include "atom/browser/web_contents_preferences.h"
27 #include "atom/browser/web_contents_zoom_controller.h"
28 #include "atom/browser/web_view_guest_delegate.h"
29 #include "atom/common/api/api_messages.h"
30 #include "atom/common/api/event_emitter_caller.h"
31 #include "atom/common/color_util.h"
32 #include "atom/common/mouse_util.h"
33 #include "atom/common/native_mate_converters/blink_converter.h"
34 #include "atom/common/native_mate_converters/callback.h"
35 #include "atom/common/native_mate_converters/content_converter.h"
36 #include "atom/common/native_mate_converters/file_path_converter.h"
37 #include "atom/common/native_mate_converters/gfx_converter.h"
38 #include "atom/common/native_mate_converters/gurl_converter.h"
39 #include "atom/common/native_mate_converters/image_converter.h"
40 #include "atom/common/native_mate_converters/net_converter.h"
41 #include "atom/common/native_mate_converters/string16_converter.h"
42 #include "atom/common/native_mate_converters/value_converter.h"
43 #include "atom/common/options_switches.h"
44 #include "base/strings/utf_string_conversions.h"
45 #include "base/threading/thread_task_runner_handle.h"
46 #include "brightray/browser/inspectable_web_contents.h"
47 #include "brightray/browser/inspectable_web_contents_view.h"
48 #include "chrome/browser/printing/print_preview_message_handler.h"
49 #include "chrome/browser/printing/print_view_manager_basic.h"
50 #include "chrome/browser/ssl/security_state_tab_helper.h"
51 #include "content/browser/frame_host/navigation_entry_impl.h"
52 #include "content/browser/renderer_host/render_widget_host_impl.h"
53 #include "content/browser/web_contents/web_contents_impl.h"
54 #include "content/common/view_messages.h"
55 #include "content/public/browser/favicon_status.h"
56 #include "content/public/browser/native_web_keyboard_event.h"
57 #include "content/public/browser/navigation_details.h"
58 #include "content/public/browser/navigation_entry.h"
59 #include "content/public/browser/navigation_handle.h"
60 #include "content/public/browser/notification_details.h"
61 #include "content/public/browser/notification_source.h"
62 #include "content/public/browser/notification_types.h"
63 #include "content/public/browser/plugin_service.h"
64 #include "content/public/browser/render_frame_host.h"
65 #include "content/public/browser/render_process_host.h"
66 #include "content/public/browser/render_view_host.h"
67 #include "content/public/browser/render_widget_host.h"
68 #include "content/public/browser/render_widget_host_view.h"
69 #include "content/public/browser/resource_request_details.h"
70 #include "content/public/browser/service_worker_context.h"
71 #include "content/public/browser/site_instance.h"
72 #include "content/public/browser/storage_partition.h"
73 #include "content/public/browser/web_contents.h"
74 #include "content/public/common/context_menu_params.h"
75 #include "native_mate/dictionary.h"
76 #include "native_mate/object_template_builder.h"
77 #include "net/url_request/url_request_context.h"
78 #include "third_party/WebKit/public/platform/WebInputEvent.h"
79 #include "third_party/WebKit/public/web/WebFindOptions.h"
80 #include "ui/display/screen.h"
83 #include "tizen/extensions/common/xwalk_extension_server.h"
86 #if !defined(OS_MACOSX) && !defined(USE_EFL)
87 #include "ui/aura/window.h"
90 #include "atom/common/node_includes.h"
94 struct PrintSettings {
96 bool print_background;
104 struct Converter<atom::SetSizeParams> {
105 static bool FromV8(v8::Isolate* isolate,
106 v8::Local<v8::Value> val,
107 atom::SetSizeParams* out) {
108 mate::Dictionary params;
109 if (!ConvertFromV8(isolate, val, ¶ms))
112 if (params.Get("enableAutoSize", &autosize))
113 out->enable_auto_size.reset(new bool(true));
115 if (params.Get("min", &size))
116 out->min_size.reset(new gfx::Size(size));
117 if (params.Get("max", &size))
118 out->max_size.reset(new gfx::Size(size));
119 if (params.Get("normal", &size))
120 out->normal_size.reset(new gfx::Size(size));
126 struct Converter<PrintSettings> {
127 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
128 PrintSettings* out) {
129 mate::Dictionary dict;
130 if (!ConvertFromV8(isolate, val, &dict))
132 dict.Get("silent", &(out->silent));
133 dict.Get("printBackground", &(out->print_background));
139 struct Converter<WindowOpenDisposition> {
140 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
141 WindowOpenDisposition val) {
142 std::string disposition = "other";
144 case WindowOpenDisposition::CURRENT_TAB:
145 disposition = "default";
147 case WindowOpenDisposition::NEW_FOREGROUND_TAB:
148 disposition = "foreground-tab";
150 case WindowOpenDisposition::NEW_BACKGROUND_TAB:
151 disposition = "background-tab";
153 case WindowOpenDisposition::NEW_POPUP:
154 case WindowOpenDisposition::NEW_WINDOW:
155 disposition = "new-window";
157 case WindowOpenDisposition::SAVE_TO_DISK:
158 disposition = "save-to-disk";
163 return mate::ConvertToV8(isolate, disposition);
168 struct Converter<content::SavePageType> {
169 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
170 content::SavePageType* out) {
171 std::string save_type;
172 if (!ConvertFromV8(isolate, val, &save_type))
174 save_type = base::ToLowerASCII(save_type);
175 if (save_type == "htmlonly") {
176 *out = content::SAVE_PAGE_TYPE_AS_ONLY_HTML;
177 } else if (save_type == "htmlcomplete") {
178 *out = content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML;
179 } else if (save_type == "mhtml") {
180 *out = content::SAVE_PAGE_TYPE_AS_MHTML;
189 struct Converter<atom::api::WebContents::Type> {
190 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
191 atom::api::WebContents::Type val) {
192 using Type = atom::api::WebContents::Type;
193 std::string type = "";
195 case Type::BACKGROUND_PAGE: type = "backgroundPage"; break;
196 case Type::BROWSER_WINDOW: type = "window"; break;
197 case Type::BROWSER_VIEW: type = "browserView"; break;
198 case Type::REMOTE: type = "remote"; break;
199 case Type::WEB_VIEW: type = "webview"; break;
200 case Type::OFF_SCREEN: type = "offscreen"; break;
203 return mate::ConvertToV8(isolate, type);
206 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
207 atom::api::WebContents::Type* out) {
208 using Type = atom::api::WebContents::Type;
210 if (!ConvertFromV8(isolate, val, &type))
212 if (type == "backgroundPage") {
213 *out = Type::BACKGROUND_PAGE;
214 } else if (type == "browserView") {
215 *out = Type::BROWSER_VIEW;
216 } else if (type == "webview") {
217 *out = Type::WEB_VIEW;
218 } else if (type == "offscreen") {
219 *out = Type::OFF_SCREEN;
236 content::ServiceWorkerContext* GetServiceWorkerContext(
237 const content::WebContents* web_contents) {
238 auto context = web_contents->GetBrowserContext();
239 auto site_instance = web_contents->GetSiteInstance();
240 if (!context || !site_instance)
243 auto storage_partition =
244 content::BrowserContext::GetStoragePartition(context, site_instance);
245 if (!storage_partition)
248 return storage_partition->GetServiceWorkerContext();
251 // Called when CapturePage is done.
252 void OnCapturePageDone(const base::Callback<void(const gfx::Image&)>& callback,
253 const SkBitmap& bitmap,
254 content::ReadbackResponse response) {
255 callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap));
258 // Set the background color of RenderWidgetHostView.
259 void SetBackgroundColor(content::WebContents* web_contents) {
260 const auto view = web_contents->GetRenderWidgetHostView();
262 WebContentsPreferences* web_preferences =
263 WebContentsPreferences::FromWebContents(web_contents);
264 std::string color_name;
265 if (web_preferences->web_preferences()->GetString(options::kBackgroundColor,
267 view->SetBackgroundColor(ParseHexColor(color_name));
269 view->SetBackgroundColor(SK_ColorTRANSPARENT);
276 WebContents::WebContents(v8::Isolate* isolate,
277 content::WebContents* web_contents,
279 : content::WebContentsObserver(web_contents),
281 zoom_controller_(nullptr),
284 background_throttling_(true),
285 enable_devtools_(true) {
286 if (type == REMOTE) {
287 web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
289 AttachAsUserData(web_contents);
291 const mate::Dictionary options = mate::Dictionary::CreateEmpty(isolate);
292 auto session = Session::CreateFrom(isolate, GetBrowserContext());
293 session_.Reset(isolate, session.ToV8());
294 InitWithSessionAndOptions(isolate, web_contents, session, options);
298 WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options)
299 : embedder_(nullptr),
300 zoom_controller_(nullptr),
301 type_(BROWSER_WINDOW),
303 background_throttling_(true),
304 enable_devtools_(true) {
306 options.Get("backgroundThrottling", &background_throttling_);
308 // FIXME(zcbenz): We should read "type" parameter for better design, but
309 // on Windows we have encountered a compiler bug that if we read "type"
310 // from |options| and then set |type_|, a memory corruption will happen
311 // and Electron will soon crash.
312 // Remvoe this after we upgraded to use VS 2015 Update 3.
314 if (options.Get("isGuest", &b) && b)
316 else if (options.Get("isBackgroundPage", &b) && b)
317 type_ = BACKGROUND_PAGE;
318 else if (options.Get("isBrowserView", &b) && b)
319 type_ = BROWSER_VIEW;
320 else if (options.Get("offscreen", &b) && b)
323 // Whether to enable DevTools.
324 options.Get("devTools", &enable_devtools_);
326 // Obtain the session.
327 std::string partition;
328 mate::Handle<api::Session> session;
329 if (options.Get("session", &session)) {
330 } else if (options.Get("partition", &partition)) {
331 session = Session::FromPartition(isolate, partition);
333 // Use the default session if not specified.
334 session = Session::FromPartition(isolate, "");
336 session_.Reset(isolate, session.ToV8());
338 content::WebContents* web_contents;
340 scoped_refptr<content::SiteInstance> site_instance =
341 content::SiteInstance::CreateForURL(
342 session->browser_context(), GURL("chrome-guest://fake-host"));
343 content::WebContents::CreateParams params(
344 session->browser_context(), site_instance);
345 guest_delegate_.reset(new WebViewGuestDelegate);
346 params.guest_delegate = guest_delegate_.get();
347 web_contents = content::WebContents::Create(params);
348 } else if (IsOffScreen()) {
349 bool transparent = false;
350 options.Get("transparent", &transparent);
352 content::WebContents::CreateParams params(session->browser_context());
353 auto* view = new OffScreenWebContentsView(
354 transparent, base::Bind(&WebContents::OnPaint, base::Unretained(this)));
356 params.delegate_view = view;
358 web_contents = content::WebContents::Create(params);
359 view->SetWebContents(web_contents);
361 content::WebContents::CreateParams params(session->browser_context());
362 web_contents = content::WebContents::Create(params);
365 InitWithSessionAndOptions(isolate, web_contents, session, options);
368 void WebContents::InitWithSessionAndOptions(v8::Isolate* isolate,
369 content::WebContents *web_contents,
370 mate::Handle<api::Session> session,
371 const mate::Dictionary& options) {
372 content::WebContentsObserver::Observe(web_contents);
373 InitWithWebContents(web_contents, session->browser_context());
375 managed_web_contents()->GetView()->SetDelegate(this);
377 // Save the preferences in C++.
378 new WebContentsPreferences(web_contents, options);
380 // Initialize permission helper.
381 WebContentsPermissionHelper::CreateForWebContents(web_contents);
382 // Initialize security state client.
383 SecurityStateTabHelper::CreateForWebContents(web_contents);
384 // Initialize zoom controller.
385 WebContentsZoomController::CreateForWebContents(web_contents);
386 zoom_controller_ = WebContentsZoomController::FromWebContents(web_contents);
388 if (options.Get(options::kZoomFactor, &zoom_factor))
389 zoom_controller_->SetDefaultZoomFactor(zoom_factor);
391 web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
394 guest_delegate_->Initialize(this);
396 NativeWindow* owner_window = nullptr;
397 if (options.Get("embedder", &embedder_) && embedder_) {
398 // New WebContents's owner_window is the embedder's owner_window.
400 NativeWindowRelay::FromWebContents(embedder_->web_contents());
402 owner_window = relay->window.get();
405 SetOwnerWindow(owner_window);
408 const content::NavigationController* controller =
409 &web_contents->GetController();
410 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING,
411 content::Source<content::NavigationController>(controller));
414 AttachAsUserData(web_contents);
417 WebContents::~WebContents() {
418 // The destroy() is called.
419 if (managed_web_contents()) {
420 // For webview we need to tell content module to do some cleanup work before
422 if (type_ == WEB_VIEW)
423 guest_delegate_->Destroy();
425 RenderViewDeleted(web_contents()->GetRenderViewHost());
426 DestroyWebContents();
430 void WebContents::DestroyWebContents() {
431 // This event is only for internal use, which is emitted when WebContents is
433 Emit("will-destroy");
434 ResetManagedWebContents();
437 bool WebContents::DidAddMessageToConsole(content::WebContents* source,
439 const base::string16& message,
441 const base::string16& source_id) {
442 if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) {
443 LOG(ERROR) << "Console Message : " << message << ", source:" << source_id << " (" << line_no << ")";
446 Emit("console-message", level, message, line_no, source_id);
451 void WebContents::OnCreateWindow(
452 const GURL& target_url,
453 const std::string& frame_name,
454 WindowOpenDisposition disposition,
455 const std::vector<std::string>& features,
456 const scoped_refptr<content::ResourceRequestBodyImpl>& body) {
457 if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
458 Emit("-new-window", target_url, frame_name, disposition, features, body);
460 Emit("new-window", target_url, frame_name, disposition, features);
463 void WebContents::WebContentsCreated(content::WebContents* source_contents,
464 int opener_render_process_id,
465 int opener_render_frame_id,
466 const std::string& frame_name,
467 const GURL& target_url,
468 content::WebContents* new_contents) {
469 v8::Locker locker(isolate());
470 v8::HandleScope handle_scope(isolate());
471 auto api_web_contents = CreateFrom(isolate(), new_contents, BROWSER_WINDOW);
472 Emit("-web-contents-created", api_web_contents, target_url, frame_name);
475 void WebContents::AddNewContents(content::WebContents* source,
476 content::WebContents* new_contents,
477 WindowOpenDisposition disposition,
478 const gfx::Rect& initial_rect,
481 v8::Locker locker(isolate());
482 v8::HandleScope handle_scope(isolate());
483 auto api_web_contents = CreateFrom(isolate(), new_contents);
484 if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture,
485 initial_rect.x(), initial_rect.y(), initial_rect.width(),
486 initial_rect.height())) {
487 api_web_contents->DestroyWebContents();
491 content::WebContents* WebContents::OpenURLFromTab(
492 content::WebContents* source,
493 const content::OpenURLParams& params) {
494 if (params.disposition != WindowOpenDisposition::CURRENT_TAB) {
495 if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
496 Emit("-new-window", params.url, "", params.disposition);
498 Emit("new-window", params.url, "", params.disposition);
502 // Give user a chance to cancel navigation.
503 if (Emit("will-navigate", params.url))
506 // Don't load the URL if the web contents was marked as destroyed from a
507 // will-navigate event listener
511 return CommonWebContentsDelegate::OpenURLFromTab(source, params);
514 void WebContents::BeforeUnloadFired(content::WebContents* tab,
516 bool* proceed_to_fire_unload) {
517 if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
518 *proceed_to_fire_unload = proceed;
520 *proceed_to_fire_unload = true;
523 void WebContents::MoveContents(content::WebContents* source,
524 const gfx::Rect& pos) {
528 void WebContents::CloseContents(content::WebContents* source) {
530 LOG(ERROR) << __FUNCTION__;
531 if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) && owner_window())
532 owner_window()->CloseContents(source);
535 void WebContents::ActivateContents(content::WebContents* source) {
539 void WebContents::UpdateTargetURL(content::WebContents* source,
541 Emit("update-target-url", url);
544 bool WebContents::IsPopupOrPanel(const content::WebContents* source) const {
545 return type_ == BROWSER_WINDOW;
548 void WebContents::HandleKeyboardEvent(
549 content::WebContents* source,
550 const content::NativeWebKeyboardEvent& event) {
551 if (type_ == WEB_VIEW && embedder_) {
552 // Send the unhandled keyboard events back to the embedder.
553 embedder_->HandleKeyboardEvent(source, event);
555 // Go to the default keyboard handling.
556 CommonWebContentsDelegate::HandleKeyboardEvent(source, event);
560 bool WebContents::PreHandleKeyboardEvent(
561 content::WebContents* source,
562 const content::NativeWebKeyboardEvent& event,
563 bool* is_keyboard_shortcut) {
564 if (event.type == blink::WebInputEvent::Type::RawKeyDown
565 || event.type == blink::WebInputEvent::Type::KeyUp)
566 return Emit("before-input-event", event);
571 void WebContents::EnterFullscreenModeForTab(content::WebContents* source,
572 const GURL& origin) {
573 auto permission_helper =
574 WebContentsPermissionHelper::FromWebContents(source);
575 auto callback = base::Bind(&WebContents::OnEnterFullscreenModeForTab,
576 base::Unretained(this), source, origin);
577 permission_helper->RequestFullscreenPermission(callback);
580 void WebContents::OnEnterFullscreenModeForTab(content::WebContents* source,
585 CommonWebContentsDelegate::EnterFullscreenModeForTab(source, origin);
586 Emit("enter-html-full-screen");
589 void WebContents::ExitFullscreenModeForTab(content::WebContents* source) {
590 CommonWebContentsDelegate::ExitFullscreenModeForTab(source);
591 Emit("leave-html-full-screen");
594 void WebContents::RendererUnresponsive(
595 content::WebContents* source,
596 const content::WebContentsUnresponsiveState& unresponsive_state) {
597 Emit("unresponsive");
598 if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) && owner_window())
599 owner_window()->RendererUnresponsive(source);
602 void WebContents::RendererResponsive(content::WebContents* source) {
604 if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) && owner_window())
605 owner_window()->RendererResponsive(source);
608 bool WebContents::HandleContextMenu(const content::ContextMenuParams& params) {
609 if (params.custom_context.is_pepper_menu) {
610 Emit("pepper-context-menu", std::make_pair(params, web_contents()));
611 web_contents()->NotifyContextMenuClosed(params.custom_context);
613 Emit("context-menu", std::make_pair(params, web_contents()));
619 bool WebContents::OnGoToEntryOffset(int offset) {
624 void WebContents::FindReply(content::WebContents* web_contents,
626 int number_of_matches,
627 const gfx::Rect& selection_rect,
628 int active_match_ordinal,
633 v8::Locker locker(isolate());
634 v8::HandleScope handle_scope(isolate());
635 mate::Dictionary result = mate::Dictionary::CreateEmpty(isolate());
636 result.Set("requestId", request_id);
637 result.Set("matches", number_of_matches);
638 result.Set("selectionArea", selection_rect);
639 result.Set("activeMatchOrdinal", active_match_ordinal);
640 result.Set("finalUpdate", final_update); // Deprecate after 2.0
641 Emit("found-in-page", result);
644 bool WebContents::CheckMediaAccessPermission(
645 content::WebContents* web_contents,
646 const GURL& security_origin,
647 content::MediaStreamType type) {
651 void WebContents::RequestMediaAccessPermission(
652 content::WebContents* web_contents,
653 const content::MediaStreamRequest& request,
654 const content::MediaResponseCallback& callback) {
655 auto permission_helper =
656 WebContentsPermissionHelper::FromWebContents(web_contents);
657 permission_helper->RequestMediaAccessPermission(request, callback);
660 void WebContents::RequestToLockMouse(
661 content::WebContents* web_contents,
663 bool last_unlocked_by_target) {
664 auto permission_helper =
665 WebContentsPermissionHelper::FromWebContents(web_contents);
666 permission_helper->RequestPointerLockPermission(user_gesture);
669 std::unique_ptr<content::BluetoothChooser> WebContents::RunBluetoothChooser(
670 content::RenderFrameHost* frame,
671 const content::BluetoothChooser::EventHandler& event_handler) {
672 std::unique_ptr<BluetoothChooser> bluetooth_chooser(
673 new BluetoothChooser(this, event_handler));
674 return std::move(bluetooth_chooser);
677 void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
678 // Do nothing, we override this method just to avoid compilation error since
679 // there are two virtual functions named BeforeUnloadFired.
682 void WebContents::RenderViewCreated(content::RenderViewHost* render_view_host) {
683 const auto impl = content::RenderWidgetHostImpl::FromID(
684 render_view_host->GetProcess()->GetID(),
685 render_view_host->GetRoutingID());
687 impl->disable_hidden_ = !background_throttling_;
690 void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) {
691 Emit("render-view-deleted", render_view_host->GetProcess()->GetID());
694 void WebContents::RenderProcessGone(base::TerminationStatus status) {
695 Emit("crashed", status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED);
698 void WebContents::PluginCrashed(const base::FilePath& plugin_path,
699 base::ProcessId plugin_pid) {
700 content::WebPluginInfo info;
701 auto plugin_service = content::PluginService::GetInstance();
702 plugin_service->GetPluginInfoByPath(plugin_path, &info);
703 Emit("plugin-crashed", info.name, info.version);
706 void WebContents::MediaStartedPlaying(const MediaPlayerInfo& video_type,
707 const MediaPlayerId& id) {
708 Emit("media-started-playing");
711 void WebContents::MediaStoppedPlaying(const MediaPlayerInfo& video_type,
712 const MediaPlayerId& id) {
713 Emit("media-paused");
716 void WebContents::DidChangeThemeColor(SkColor theme_color) {
717 Emit("did-change-theme-color", atom::ToRGBHex(theme_color));
720 void WebContents::DocumentLoadedInFrame(
721 content::RenderFrameHost* render_frame_host) {
722 if (!render_frame_host->GetParent())
726 void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
727 const GURL& validated_url) {
728 bool is_main_frame = !render_frame_host->GetParent();
729 Emit("did-frame-finish-load", is_main_frame);
732 Emit("did-finish-load");
735 void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host,
738 const base::string16& error_description,
739 bool was_ignored_by_handler) {
740 bool is_main_frame = !render_frame_host->GetParent();
741 Emit("did-fail-load", error_code, error_description, url, is_main_frame);
744 void WebContents::DidStartLoading() {
745 #if defined(OS_TIZEN)
746 if (owner_window() && !owner_window()->IsVisible()) {
747 std::string scheme = web_contents()->GetURL().scheme();
748 if (std::string::npos != scheme.find("http")) {
749 owner_window()->Show();
753 Emit("did-start-loading");
756 void WebContents::DidStopLoading() {
757 Emit("did-stop-loading");
760 void WebContents::DidGetResourceResponseStart(
761 const content::ResourceRequestDetails& details) {
762 Emit("did-get-response-details",
763 details.socket_address.IsEmpty(),
765 details.original_url,
766 details.http_response_code,
769 details.headers.get(),
770 ResourceTypeToString(details.resource_type));
773 void WebContents::DidGetRedirectForResourceRequest(
774 const content::ResourceRedirectDetails& details) {
775 Emit("did-get-redirect-request",
778 (details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME),
779 details.http_response_code,
782 details.headers.get());
785 void WebContents::DidStartNavigation(
786 content::NavigationHandle* navigation_handle) {
787 if (!navigation_handle->IsInMainFrame() || navigation_handle->IsSamePage())
790 if (deferred_load_url_.id) {
791 auto web_contents = navigation_handle->GetWebContents();
792 auto& controller = web_contents->GetController();
793 int id = controller.GetPendingEntry()->GetUniqueID();
794 if (id == deferred_load_url_.id) {
795 if (!deferred_load_url_.params.url.is_empty()) {
796 auto params = deferred_load_url_.params;
797 deferred_load_url_.id = 0;
798 deferred_load_url_.params =
799 content::NavigationController::LoadURLParams(GURL());
800 controller.LoadURLWithParams(params);
801 SetBackgroundColor(web_contents);
803 deferred_load_url_.id = 0;
809 void WebContents::DidFinishNavigation(
810 content::NavigationHandle* navigation_handle) {
811 bool is_main_frame = navigation_handle->IsInMainFrame();
812 if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) {
813 auto url = navigation_handle->GetURL();
814 bool is_in_page = navigation_handle->IsSamePage();
815 if (is_main_frame && !is_in_page) {
816 Emit("did-navigate", url);
817 } else if (is_in_page) {
818 Emit("did-navigate-in-page", url, is_main_frame);
821 auto url = navigation_handle->GetURL();
822 int code = navigation_handle->GetNetErrorCode();
823 auto description = net::ErrorToShortString(code);
824 Emit("did-fail-provisional-load", code, description, url, is_main_frame);
826 // Do not emit "did-fail-load" for canceled requests.
827 if (code != net::ERR_ABORTED)
828 Emit("did-fail-load", code, description, url, is_main_frame);
832 void WebContents::TitleWasSet(content::NavigationEntry* entry,
835 Emit("-page-title-updated", entry->GetTitle(), explicit_set);
837 Emit("-page-title-updated", "", explicit_set);
840 void WebContents::DidUpdateFaviconURL(
841 const std::vector<content::FaviconURL>& urls) {
842 std::set<GURL> unique_urls;
843 for (const auto& iter : urls) {
844 if (iter.icon_type != content::FaviconURL::FAVICON)
846 const GURL& url = iter.icon_url;
848 unique_urls.insert(url);
850 Emit("page-favicon-updated", unique_urls);
853 void WebContents::Observe(int type,
854 const content::NotificationSource& source,
855 const content::NotificationDetails& details) {
857 case content::NOTIFICATION_NAV_ENTRY_PENDING: {
858 content::NavigationEntry* entry =
859 content::Details<content::NavigationEntry>(details).ptr();
860 content::NavigationEntryImpl* entry_impl =
861 static_cast<content::NavigationEntryImpl*>(entry);
862 // In NavigatorImpl::DidStartMainFrameNavigation when there is no
863 // browser side pending entry available it creates a new one based
864 // on existing pending entry, hence we track the unique id here
865 // instead in WebContents::LoadURL with controller.GetPendingEntry()
866 // TODO(deepak1556): Remove once we have
867 // https://codereview.chromium.org/2661743002.
868 if (entry_impl->frame_tree_node_id() == -1) {
869 deferred_load_url_.id = entry->GetUniqueID();
879 void WebContents::DevToolsReloadPage() {
880 Emit("devtools-reload-page");
883 void WebContents::DevToolsFocused() {
884 Emit("devtools-focused");
887 void WebContents::DevToolsOpened() {
888 v8::Locker locker(isolate());
889 v8::HandleScope handle_scope(isolate());
890 auto handle = WebContents::CreateFrom(
891 isolate(), managed_web_contents()->GetDevToolsWebContents());
892 devtools_web_contents_.Reset(isolate(), handle.ToV8());
894 // Set inspected tabID.
895 base::FundamentalValue tab_id(ID());
896 managed_web_contents()->CallClientFunction(
897 "DevToolsAPI.setInspectedTabId", &tab_id, nullptr, nullptr);
899 // Inherit owner window in devtools.
901 handle->SetOwnerWindow(managed_web_contents()->GetDevToolsWebContents(),
904 Emit("devtools-opened");
907 void WebContents::DevToolsClosed() {
908 v8::Locker locker(isolate());
909 v8::HandleScope handle_scope(isolate());
910 devtools_web_contents_.Reset();
912 Emit("devtools-closed");
915 void WebContents::OnWrtPluginMessage(const Ewk_Wrt_Message_Data& data) {
916 Ewk_Wrt_Message_Data tmp = data;
917 HandleWrtPluginMessage(&tmp);
920 void WebContents::OnWrtPluginSyncMessage(const Ewk_Wrt_Message_Data& data,
921 IPC::Message* reply) {
922 Ewk_Wrt_Message_Data tmp = data;
923 HandleWrtPluginMessage(&tmp);
924 AtomHostMsg_WrtSyncMessage::WriteReplyParams(reply, tmp.value);
928 void WebContents::HandleWrtPluginMessage(Ewk_Wrt_Message_Data* msg) {
929 Eina_Stringshare* msg_type = ewk_ipc_wrt_message_data_type_get(msg);
930 LOG(INFO) << msg_type;
931 #define TYPE_BEGIN(x) (!strncmp(msg_type, x, strlen(x)))
932 #define TYPE_IS(x) (!strcmp(msg_type, x))
933 if (TYPE_BEGIN("xwalk://")) {
934 auto extension_server = extensions::XWalkExtensionServer::GetInstance();
935 extension_server->HandleIPCMessage(msg);
937 Eina_Stringshare* msg_id = msg->GetId();
938 Eina_Stringshare* msg_ref_id = msg->GetReferenceId();
939 Eina_Stringshare* msg_value = msg->GetValue();
940 if (TYPE_IS("tizen://exit")) {
941 atom::Browser::Get()->Quit();
944 eina_stringshare_del(msg_ref_id);
945 eina_stringshare_del(msg_id);
946 eina_stringshare_del(msg_value);
951 eina_stringshare_del(msg_type);
954 bool WebContents::OnMessageReceived(const IPC::Message& message) {
956 IPC_BEGIN_MESSAGE_MAP(WebContents, message)
957 IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage)
958 IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync,
959 OnRendererMessageSync)
960 IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_SetTemporaryZoomLevel,
961 OnSetTemporaryZoomLevel)
962 IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_GetZoomLevel,
964 IPC_MESSAGE_HANDLER(AtomHostMsg_WrtMessage, OnWrtPluginMessage)
965 IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomHostMsg_WrtSyncMessage, OnWrtPluginSyncMessage)
966 // FIXME: Disable OnCursorChange due to stach_chk_fail crash.
967 // IPC_MESSAGE_HANDLER_CODE(ViewHostMsg_SetCursor, OnCursorChange,
969 IPC_MESSAGE_UNHANDLED(handled = false)
970 IPC_END_MESSAGE_MAP()
975 // There are three ways of destroying a webContents:
976 // 1. call webContents.destroy();
977 // 2. garbage collection;
978 // 3. user closes the window of webContents;
979 // For webview only #1 will happen, for BrowserWindow both #1 and #3 may
980 // happen. The #2 should never happen for webContents, because webview is
981 // managed by GuestViewManager, and BrowserWindow's webContents is managed
983 // For #1, the destructor will do the cleanup work and we only need to make
984 // sure "destroyed" event is emitted. For #3, the content::WebContents will
985 // be destroyed on close, and WebContentsDestroyed would be called for it, so
986 // we need to make sure the api::WebContents is also deleted.
987 void WebContents::WebContentsDestroyed() {
988 // Cleanup relationships with other parts.
991 // We can not call Destroy here because we need to call Emit first, but we
992 // also do not want any method to be used, so just mark as destroyed here.
997 // Destroy the native class in next tick.
998 base::ThreadTaskRunnerHandle::Get()->PostTask(
999 FROM_HERE, GetDestroyClosure());
1002 void WebContents::NavigationEntryCommitted(
1003 const content::LoadCommittedDetails& details) {
1004 Emit("navigation-entry-commited", details.entry->GetURL(),
1005 details.is_in_page, details.did_replace_entry);
1008 int64_t WebContents::GetID() const {
1009 int64_t process_id = web_contents()->GetRenderProcessHost()->GetID();
1010 int64_t routing_id = web_contents()->GetRoutingID();
1011 int64_t rv = (process_id << 32) + routing_id;
1015 int WebContents::GetProcessID() const {
1016 return web_contents()->GetRenderProcessHost()->GetID();
1019 WebContents::Type WebContents::GetType() const {
1023 bool WebContents::Equal(const WebContents* web_contents) const {
1024 return GetID() == web_contents->GetID();
1027 void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
1028 if (!url.is_valid() || url.spec().size() > url::kMaxURLChars) {
1029 Emit("did-fail-load",
1030 static_cast<int>(net::ERR_INVALID_URL),
1031 net::ErrorToShortString(net::ERR_INVALID_URL),
1032 url.possibly_invalid_spec(),
1037 content::NavigationController::LoadURLParams params(url);
1040 if (options.Get("httpReferrer", &http_referrer))
1041 params.referrer = content::Referrer(http_referrer.GetAsReferrer(),
1042 blink::WebReferrerPolicyDefault);
1044 std::string user_agent;
1045 if (options.Get("userAgent", &user_agent))
1046 web_contents()->SetUserAgentOverride(user_agent);
1048 std::string extra_headers;
1049 if (options.Get("extraHeaders", &extra_headers))
1050 params.extra_headers = extra_headers;
1052 scoped_refptr<content::ResourceRequestBodyImpl> body;
1053 if (options.Get("postData", &body)) {
1054 params.post_data = body;
1055 params.load_type = content::NavigationController::LOAD_TYPE_HTTP_POST;
1058 GURL base_url_for_data_url;
1059 if (options.Get("baseURLForDataURL", &base_url_for_data_url)) {
1060 params.base_url_for_data_url = base_url_for_data_url;
1061 params.load_type = content::NavigationController::LOAD_TYPE_DATA;
1064 params.transition_type = ui::PAGE_TRANSITION_TYPED;
1065 params.should_clear_history_list = true;
1066 params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
1068 if (deferred_load_url_.id) {
1069 deferred_load_url_.params = params;
1073 web_contents()->GetController().LoadURLWithParams(params);
1074 // We have to call it right after LoadURL because the RenderViewHost is only
1075 // created after loading a page.
1076 SetBackgroundColor(web_contents());
1079 void WebContents::DownloadURL(const GURL& url) {
1080 auto browser_context = web_contents()->GetBrowserContext();
1081 auto download_manager =
1082 content::BrowserContext::GetDownloadManager(browser_context);
1084 download_manager->DownloadUrl(
1085 content::DownloadUrlParameters::CreateForWebContentsMainFrame(
1086 web_contents(), url));
1089 GURL WebContents::GetURL() const {
1090 return web_contents()->GetURL();
1093 base::string16 WebContents::GetTitle() const {
1094 return web_contents()->GetTitle();
1097 bool WebContents::IsLoading() const {
1098 return web_contents()->IsLoading();
1101 bool WebContents::IsLoadingMainFrame() const {
1102 // Comparing site instances works because Electron always creates a new site
1103 // instance when navigating, regardless of origin. See AtomBrowserClient.
1104 return (web_contents()->GetLastCommittedURL().is_empty() ||
1105 web_contents()->GetSiteInstance() !=
1106 web_contents()->GetPendingSiteInstance()) && IsLoading();
1109 bool WebContents::IsWaitingForResponse() const {
1110 return web_contents()->IsWaitingForResponse();
1113 void WebContents::Stop() {
1114 LOG(ERROR) << __FUNCTION__;
1115 web_contents()->Stop();
1118 void WebContents::GoBack() {
1119 atom::AtomBrowserClient::SuppressRendererProcessRestartForOnce();
1120 web_contents()->GetController().GoBack();
1123 void WebContents::GoForward() {
1124 atom::AtomBrowserClient::SuppressRendererProcessRestartForOnce();
1125 web_contents()->GetController().GoForward();
1128 void WebContents::GoToOffset(int offset) {
1129 atom::AtomBrowserClient::SuppressRendererProcessRestartForOnce();
1130 web_contents()->GetController().GoToOffset(offset);
1133 const std::string WebContents::GetWebRTCIPHandlingPolicy() const {
1134 return web_contents()->
1135 GetMutableRendererPrefs()->webrtc_ip_handling_policy;
1138 void WebContents::SetWebRTCIPHandlingPolicy(
1139 const std::string& webrtc_ip_handling_policy) {
1140 if (GetWebRTCIPHandlingPolicy() == webrtc_ip_handling_policy)
1142 web_contents()->GetMutableRendererPrefs()->webrtc_ip_handling_policy =
1143 webrtc_ip_handling_policy;
1145 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
1147 host->SyncRendererPrefs();
1150 bool WebContents::IsCrashed() const {
1151 return web_contents()->IsCrashed();
1154 void WebContents::SetUserAgent(const std::string& user_agent,
1155 mate::Arguments* args) {
1156 web_contents()->SetUserAgentOverride(user_agent);
1159 std::string WebContents::GetUserAgent() {
1160 return web_contents()->GetUserAgentOverride();
1163 bool WebContents::SavePage(const base::FilePath& full_file_path,
1164 const content::SavePageType& save_type,
1165 const SavePageHandler::SavePageCallback& callback) {
1166 auto handler = new SavePageHandler(web_contents(), callback);
1167 return handler->Handle(full_file_path, save_type);
1170 void WebContents::OpenDevTools(mate::Arguments* args) {
1171 if (type_ == REMOTE)
1174 if (!enable_devtools_)
1178 if (type_ == WEB_VIEW || !owner_window()) {
1180 } else if (args && args->Length() == 1) {
1181 bool detach = false;
1182 mate::Dictionary options;
1183 if (args->GetNext(&options)) {
1184 options.Get("mode", &state);
1186 // TODO(kevinsawicki) Remove in 2.0
1187 options.Get("detach", &detach);
1188 if (state.empty() && detach)
1192 managed_web_contents()->SetDockState(state);
1193 managed_web_contents()->ShowDevTools();
1196 void WebContents::CloseDevTools() {
1197 if (type_ == REMOTE)
1200 managed_web_contents()->CloseDevTools();
1203 bool WebContents::IsDevToolsOpened() {
1204 if (type_ == REMOTE)
1207 return managed_web_contents()->IsDevToolsViewShowing();
1210 bool WebContents::IsDevToolsFocused() {
1211 if (type_ == REMOTE)
1214 return managed_web_contents()->GetView()->IsDevToolsViewFocused();
1217 void WebContents::EnableDeviceEmulation(
1218 const blink::WebDeviceEmulationParams& params) {
1219 if (type_ == REMOTE)
1222 Send(new ViewMsg_EnableDeviceEmulation(routing_id(), params));
1225 void WebContents::DisableDeviceEmulation() {
1226 if (type_ == REMOTE)
1229 Send(new ViewMsg_DisableDeviceEmulation(routing_id()));
1232 void WebContents::ToggleDevTools() {
1233 if (IsDevToolsOpened())
1236 OpenDevTools(nullptr);
1239 void WebContents::InspectElement(int x, int y) {
1240 if (type_ == REMOTE)
1243 if (!enable_devtools_)
1246 if (!managed_web_contents()->GetDevToolsWebContents())
1247 OpenDevTools(nullptr);
1248 managed_web_contents()->InspectElement(x, y);
1251 void WebContents::InspectServiceWorker() {
1252 if (type_ == REMOTE)
1255 if (!enable_devtools_)
1258 for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
1259 if (agent_host->GetType() ==
1260 content::DevToolsAgentHost::kTypeServiceWorker) {
1261 OpenDevTools(nullptr);
1262 managed_web_contents()->AttachTo(agent_host);
1268 void WebContents::HasServiceWorker(
1269 const base::Callback<void(bool)>& callback) {
1270 auto context = GetServiceWorkerContext(web_contents());
1274 context->CheckHasServiceWorker(web_contents()->GetLastCommittedURL(),
1279 void WebContents::UnregisterServiceWorker(
1280 const base::Callback<void(bool)>& callback) {
1281 auto context = GetServiceWorkerContext(web_contents());
1285 context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(),
1289 void WebContents::SetAudioMuted(bool muted) {
1290 web_contents()->SetAudioMuted(muted);
1293 bool WebContents::IsAudioMuted() {
1294 return web_contents()->IsAudioMuted();
1297 void WebContents::Print(mate::Arguments* args) {
1298 PrintSettings settings = { false, false };
1299 if (args->Length() == 1 && !args->GetNext(&settings)) {
1304 printing::PrintViewManagerBasic::FromWebContents(web_contents())->
1305 PrintNow(web_contents()->GetMainFrame(),
1307 settings.print_background);
1310 void WebContents::PrintToPDF(const base::DictionaryValue& setting,
1311 const PrintToPDFCallback& callback) {
1312 printing::PrintPreviewMessageHandler::FromWebContents(web_contents())->
1313 PrintToPDF(setting, callback);
1316 void WebContents::AddWorkSpace(mate::Arguments* args,
1317 const base::FilePath& path) {
1319 args->ThrowError("path cannot be empty");
1322 DevToolsAddFileSystem(path);
1325 void WebContents::RemoveWorkSpace(mate::Arguments* args,
1326 const base::FilePath& path) {
1328 args->ThrowError("path cannot be empty");
1331 DevToolsRemoveFileSystem(path);
1334 void WebContents::Undo() {
1335 web_contents()->Undo();
1338 void WebContents::Redo() {
1339 web_contents()->Redo();
1342 void WebContents::Cut() {
1343 web_contents()->Cut();
1346 void WebContents::Copy() {
1347 web_contents()->Copy();
1350 void WebContents::Paste() {
1351 web_contents()->Paste();
1354 void WebContents::PasteAndMatchStyle() {
1355 web_contents()->PasteAndMatchStyle();
1358 void WebContents::Delete() {
1359 web_contents()->Delete();
1362 void WebContents::SelectAll() {
1363 web_contents()->SelectAll();
1366 void WebContents::Unselect() {
1367 web_contents()->Unselect();
1370 void WebContents::Replace(const base::string16& word) {
1371 web_contents()->Replace(word);
1374 void WebContents::ReplaceMisspelling(const base::string16& word) {
1375 web_contents()->ReplaceMisspelling(word);
1378 uint32_t WebContents::FindInPage(mate::Arguments* args) {
1379 uint32_t request_id = GetNextRequestId();
1380 base::string16 search_text;
1381 blink::WebFindOptions options;
1382 if (!args->GetNext(&search_text) || search_text.empty()) {
1383 args->ThrowError("Must provide a non-empty search content");
1387 args->GetNext(&options);
1389 web_contents()->Find(request_id, search_text, options);
1393 void WebContents::StopFindInPage(content::StopFindAction action) {
1394 web_contents()->StopFinding(action);
1397 void WebContents::ShowDefinitionForSelection() {
1398 #if defined(OS_MACOSX)
1399 const auto view = web_contents()->GetRenderWidgetHostView();
1401 view->ShowDefinitionForSelection();
1405 void WebContents::CopyImageAt(int x, int y) {
1406 const auto host = web_contents()->GetMainFrame();
1408 host->CopyImageAt(x, y);
1411 void WebContents::Focus() {
1412 web_contents()->Focus();
1415 #if !defined(OS_MACOSX)
1416 bool WebContents::IsFocused() const {
1417 auto view = web_contents()->GetRenderWidgetHostView();
1418 if (!view) return false;
1420 #if defined(USE_EFL)
1423 if (GetType() != BACKGROUND_PAGE) {
1424 auto window = web_contents()->GetNativeView()->GetToplevelWindow();
1425 if (window && !window->IsVisible())
1430 return view->HasFocus();
1434 void WebContents::TabTraverse(bool reverse) {
1435 web_contents()->FocusThroughTabTraversal(reverse);
1438 bool WebContents::SendIPCMessage(bool all_frames,
1439 const base::string16& channel,
1440 const base::ListValue& args) {
1441 return Send(new AtomViewMsg_Message(routing_id(), all_frames, channel, args));
1444 void WebContents::SendInputEvent(v8::Isolate* isolate,
1445 v8::Local<v8::Value> input_event) {
1446 const auto view = web_contents()->GetRenderWidgetHostView();
1449 const auto host = view->GetRenderWidgetHost();
1453 int type = mate::GetWebInputEventType(isolate, input_event);
1454 if (blink::WebInputEvent::isMouseEventType(type)) {
1455 blink::WebMouseEvent mouse_event;
1456 if (mate::ConvertFromV8(isolate, input_event, &mouse_event)) {
1457 host->ForwardMouseEvent(mouse_event);
1460 } else if (blink::WebInputEvent::isKeyboardEventType(type)) {
1461 content::NativeWebKeyboardEvent keyboard_event;
1462 if (mate::ConvertFromV8(isolate, input_event, &keyboard_event)) {
1463 host->ForwardKeyboardEvent(keyboard_event);
1466 } else if (type == blink::WebInputEvent::MouseWheel) {
1467 blink::WebMouseWheelEvent mouse_wheel_event;
1468 if (mate::ConvertFromV8(isolate, input_event, &mouse_wheel_event)) {
1469 host->ForwardWheelEvent(mouse_wheel_event);
1474 isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
1475 isolate, "Invalid event object")));
1478 void WebContents::BeginFrameSubscription(mate::Arguments* args) {
1479 bool only_dirty = false;
1480 FrameSubscriber::FrameCaptureCallback callback;
1482 args->GetNext(&only_dirty);
1483 if (!args->GetNext(&callback)) {
1488 const auto view = web_contents()->GetRenderWidgetHostView();
1490 std::unique_ptr<FrameSubscriber> frame_subscriber(new FrameSubscriber(
1491 isolate(), view, callback, only_dirty));
1492 view->BeginFrameSubscription(std::move(frame_subscriber));
1496 void WebContents::EndFrameSubscription() {
1497 const auto view = web_contents()->GetRenderWidgetHostView();
1499 view->EndFrameSubscription();
1502 void WebContents::StartDrag(const mate::Dictionary& item,
1503 mate::Arguments* args) {
1504 base::FilePath file;
1505 std::vector<base::FilePath> files;
1506 if (!item.Get("files", &files) && item.Get("file", &file)) {
1507 files.push_back(file);
1510 mate::Handle<NativeImage> icon;
1511 if (!item.Get("icon", &icon) && !file.empty()) {
1512 // TODO(zcbenz): Set default icon from file.
1516 if (icon.IsEmpty()) {
1517 args->ThrowError("Must specify 'icon' option");
1521 #if defined(OS_MACOSX)
1522 // NSWindow.dragImage requires a non-empty NSImage
1523 if (icon->image().IsEmpty()) {
1524 args->ThrowError("Must specify non-empty 'icon' option");
1530 if (!files.empty()) {
1531 base::MessageLoop::ScopedNestableTaskAllower allow(
1532 base::MessageLoop::current());
1533 DragFileItems(files, icon->image(), web_contents()->GetNativeView());
1535 args->ThrowError("Must specify either 'file' or 'files' option");
1539 void WebContents::CapturePage(mate::Arguments* args) {
1541 base::Callback<void(const gfx::Image&)> callback;
1543 if (!(args->Length() == 1 && args->GetNext(&callback)) &&
1544 !(args->Length() == 2 && args->GetNext(&rect)
1545 && args->GetNext(&callback))) {
1550 const auto view = web_contents()->GetRenderWidgetHostView();
1551 const auto host = view ? view->GetRenderWidgetHost() : nullptr;
1552 if (!view || !host) {
1553 callback.Run(gfx::Image());
1557 // Capture full page if user doesn't specify a |rect|.
1558 const gfx::Size view_size = rect.IsEmpty() ? view->GetViewBounds().size() :
1561 // By default, the requested bitmap size is the view size in screen
1562 // coordinates. However, if there's more pixel detail available on the
1563 // current system, increase the requested bitmap size to capture it all.
1564 gfx::Size bitmap_size = view_size;
1565 const gfx::NativeView native_view = view->GetNativeView();
1567 display::Screen::GetScreen()->GetDisplayNearestWindow(native_view)
1568 .device_scale_factor();
1570 bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);
1572 host->CopyFromBackingStore(gfx::Rect(rect.origin(), view_size),
1574 base::Bind(&OnCapturePageDone, callback),
1575 kBGRA_8888_SkColorType);
1578 void WebContents::OnCursorChange(const content::WebCursor& cursor) {
1579 content::WebCursor::CursorInfo info;
1580 cursor.GetCursorInfo(&info);
1582 if (cursor.IsCustom()) {
1583 Emit("cursor-changed", CursorTypeToString(info),
1584 gfx::Image::CreateFrom1xBitmap(info.custom_image),
1585 info.image_scale_factor,
1586 gfx::Size(info.custom_image.width(), info.custom_image.height()),
1589 Emit("cursor-changed", CursorTypeToString(info));
1593 void WebContents::SetSize(const SetSizeParams& params) {
1594 if (guest_delegate_)
1595 guest_delegate_->SetSize(params);
1598 bool WebContents::IsGuest() const {
1599 return type_ == WEB_VIEW;
1602 bool WebContents::IsOffScreen() const {
1603 return type_ == OFF_SCREEN;
1606 void WebContents::OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap) {
1607 mate::Handle<NativeImage> image =
1608 NativeImage::Create(isolate(), gfx::Image::CreateFrom1xBitmap(bitmap));
1609 Emit("paint", dirty_rect, image);
1612 void WebContents::StartPainting() {
1616 auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1617 web_contents()->GetRenderWidgetHostView());
1619 osr_rwhv->SetPainting(true);
1622 void WebContents::StopPainting() {
1626 auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1627 web_contents()->GetRenderWidgetHostView());
1629 osr_rwhv->SetPainting(false);
1632 bool WebContents::IsPainting() const {
1636 const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1637 web_contents()->GetRenderWidgetHostView());
1638 return osr_rwhv && osr_rwhv->IsPainting();
1641 void WebContents::SetFrameRate(int frame_rate) {
1645 auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1646 web_contents()->GetRenderWidgetHostView());
1648 osr_rwhv->SetFrameRate(frame_rate);
1651 int WebContents::GetFrameRate() const {
1655 const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1656 web_contents()->GetRenderWidgetHostView());
1657 return osr_rwhv ? osr_rwhv->GetFrameRate() : 0;
1660 void WebContents::Invalidate() {
1661 if (IsOffScreen()) {
1662 auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1663 web_contents()->GetRenderWidgetHostView());
1665 osr_rwhv->Invalidate();
1667 const auto window = owner_window();
1669 window->Invalidate();
1673 void WebContents::SetZoomLevel(double level) {
1674 zoom_controller_->SetZoomLevel(level);
1677 double WebContents::GetZoomLevel() {
1678 return zoom_controller_->GetZoomLevel();
1681 void WebContents::SetZoomFactor(double factor) {
1682 auto level = content::ZoomFactorToZoomLevel(factor);
1683 SetZoomLevel(level);
1686 double WebContents::GetZoomFactor() {
1687 auto level = GetZoomLevel();
1688 return content::ZoomLevelToZoomFactor(level);
1691 void WebContents::OnSetTemporaryZoomLevel(double level,
1692 IPC::Message* reply_msg) {
1693 zoom_controller_->SetTemporaryZoomLevel(level);
1694 double new_level = zoom_controller_->GetZoomLevel();
1695 AtomViewHostMsg_SetTemporaryZoomLevel::WriteReplyParams(reply_msg, new_level);
1699 void WebContents::OnGetZoomLevel(IPC::Message* reply_msg) {
1700 AtomViewHostMsg_GetZoomLevel::WriteReplyParams(reply_msg, GetZoomLevel());
1704 v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
1705 WebContentsPreferences* web_preferences =
1706 WebContentsPreferences::FromWebContents(web_contents());
1707 return mate::ConvertToV8(isolate, *web_preferences->web_preferences());
1710 v8::Local<v8::Value> WebContents::GetOwnerBrowserWindow() {
1712 return Window::From(isolate(), owner_window());
1714 return v8::Null(isolate());
1717 int32_t WebContents::ID() const {
1718 return weak_map_id();
1721 v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
1722 return v8::Local<v8::Value>::New(isolate, session_);
1725 content::WebContents* WebContents::HostWebContents() {
1728 return embedder_->web_contents();
1731 void WebContents::SetEmbedder(const WebContents* embedder) {
1733 NativeWindow* owner_window = nullptr;
1734 auto relay = NativeWindowRelay::FromWebContents(embedder->web_contents());
1736 owner_window = relay->window.get();
1739 SetOwnerWindow(owner_window);
1741 content::RenderWidgetHostView* rwhv =
1742 web_contents()->GetRenderWidgetHostView();
1750 v8::Local<v8::Value> WebContents::DevToolsWebContents(v8::Isolate* isolate) {
1751 if (devtools_web_contents_.IsEmpty())
1752 return v8::Null(isolate);
1754 return v8::Local<v8::Value>::New(isolate, devtools_web_contents_);
1757 v8::Local<v8::Value> WebContents::Debugger(v8::Isolate* isolate) {
1758 if (debugger_.IsEmpty()) {
1759 auto handle = atom::api::Debugger::Create(isolate, web_contents());
1760 debugger_.Reset(isolate, handle.ToV8());
1762 return v8::Local<v8::Value>::New(isolate, debugger_);
1766 void WebContents::BuildPrototype(v8::Isolate* isolate,
1767 v8::Local<v8::FunctionTemplate> prototype) {
1768 prototype->SetClassName(mate::StringToV8(isolate, "WebContents"));
1769 mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
1771 .SetMethod("getId", &WebContents::GetID)
1772 .SetMethod("getProcessId", &WebContents::GetProcessID)
1773 .SetMethod("equal", &WebContents::Equal)
1774 .SetMethod("_loadURL", &WebContents::LoadURL)
1775 .SetMethod("downloadURL", &WebContents::DownloadURL)
1776 .SetMethod("_getURL", &WebContents::GetURL)
1777 .SetMethod("getTitle", &WebContents::GetTitle)
1778 .SetMethod("isLoading", &WebContents::IsLoading)
1779 .SetMethod("isLoadingMainFrame", &WebContents::IsLoadingMainFrame)
1780 .SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse)
1781 .SetMethod("_stop", &WebContents::Stop)
1782 .SetMethod("_goBack", &WebContents::GoBack)
1783 .SetMethod("_goForward", &WebContents::GoForward)
1784 .SetMethod("_goToOffset", &WebContents::GoToOffset)
1785 .SetMethod("isCrashed", &WebContents::IsCrashed)
1786 .SetMethod("setUserAgent", &WebContents::SetUserAgent)
1787 .SetMethod("getUserAgent", &WebContents::GetUserAgent)
1788 .SetMethod("savePage", &WebContents::SavePage)
1789 .SetMethod("openDevTools", &WebContents::OpenDevTools)
1790 .SetMethod("closeDevTools", &WebContents::CloseDevTools)
1791 .SetMethod("isDevToolsOpened", &WebContents::IsDevToolsOpened)
1792 .SetMethod("isDevToolsFocused", &WebContents::IsDevToolsFocused)
1793 .SetMethod("enableDeviceEmulation",
1794 &WebContents::EnableDeviceEmulation)
1795 .SetMethod("disableDeviceEmulation",
1796 &WebContents::DisableDeviceEmulation)
1797 .SetMethod("toggleDevTools", &WebContents::ToggleDevTools)
1798 .SetMethod("inspectElement", &WebContents::InspectElement)
1799 .SetMethod("setAudioMuted", &WebContents::SetAudioMuted)
1800 .SetMethod("isAudioMuted", &WebContents::IsAudioMuted)
1801 .SetMethod("undo", &WebContents::Undo)
1802 .SetMethod("redo", &WebContents::Redo)
1803 .SetMethod("cut", &WebContents::Cut)
1804 .SetMethod("copy", &WebContents::Copy)
1805 .SetMethod("paste", &WebContents::Paste)
1806 .SetMethod("pasteAndMatchStyle", &WebContents::PasteAndMatchStyle)
1807 .SetMethod("delete", &WebContents::Delete)
1808 .SetMethod("selectAll", &WebContents::SelectAll)
1809 .SetMethod("unselect", &WebContents::Unselect)
1810 .SetMethod("replace", &WebContents::Replace)
1811 .SetMethod("replaceMisspelling", &WebContents::ReplaceMisspelling)
1812 .SetMethod("findInPage", &WebContents::FindInPage)
1813 .SetMethod("stopFindInPage", &WebContents::StopFindInPage)
1814 .SetMethod("focus", &WebContents::Focus)
1815 .SetMethod("isFocused", &WebContents::IsFocused)
1816 .SetMethod("tabTraverse", &WebContents::TabTraverse)
1817 .SetMethod("_send", &WebContents::SendIPCMessage)
1818 .SetMethod("sendInputEvent", &WebContents::SendInputEvent)
1819 .SetMethod("beginFrameSubscription",
1820 &WebContents::BeginFrameSubscription)
1821 .SetMethod("endFrameSubscription", &WebContents::EndFrameSubscription)
1822 .SetMethod("startDrag", &WebContents::StartDrag)
1823 .SetMethod("setSize", &WebContents::SetSize)
1824 .SetMethod("isGuest", &WebContents::IsGuest)
1825 .SetMethod("isOffscreen", &WebContents::IsOffScreen)
1826 .SetMethod("startPainting", &WebContents::StartPainting)
1827 .SetMethod("stopPainting", &WebContents::StopPainting)
1828 .SetMethod("isPainting", &WebContents::IsPainting)
1829 .SetMethod("setFrameRate", &WebContents::SetFrameRate)
1830 .SetMethod("getFrameRate", &WebContents::GetFrameRate)
1831 .SetMethod("invalidate", &WebContents::Invalidate)
1832 .SetMethod("setZoomLevel", &WebContents::SetZoomLevel)
1833 .SetMethod("_getZoomLevel", &WebContents::GetZoomLevel)
1834 .SetMethod("setZoomFactor", &WebContents::SetZoomFactor)
1835 .SetMethod("_getZoomFactor", &WebContents::GetZoomFactor)
1836 .SetMethod("getType", &WebContents::GetType)
1837 .SetMethod("getWebPreferences", &WebContents::GetWebPreferences)
1838 .SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow)
1839 .SetMethod("hasServiceWorker", &WebContents::HasServiceWorker)
1840 .SetMethod("unregisterServiceWorker",
1841 &WebContents::UnregisterServiceWorker)
1842 .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
1843 .SetMethod("print", &WebContents::Print)
1844 .SetMethod("_printToPDF", &WebContents::PrintToPDF)
1845 .SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
1846 .SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
1847 .SetMethod("showDefinitionForSelection",
1848 &WebContents::ShowDefinitionForSelection)
1849 .SetMethod("copyImageAt", &WebContents::CopyImageAt)
1850 .SetMethod("capturePage", &WebContents::CapturePage)
1851 .SetMethod("setEmbedder", &WebContents::SetEmbedder)
1852 .SetMethod("setWebRTCIPHandlingPolicy",
1853 &WebContents::SetWebRTCIPHandlingPolicy)
1854 .SetMethod("getWebRTCIPHandlingPolicy",
1855 &WebContents::GetWebRTCIPHandlingPolicy)
1856 .SetProperty("id", &WebContents::ID)
1857 .SetProperty("session", &WebContents::Session)
1858 .SetProperty("hostWebContents", &WebContents::HostWebContents)
1859 .SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
1860 .SetProperty("debugger", &WebContents::Debugger);
1863 AtomBrowserContext* WebContents::GetBrowserContext() const {
1864 return static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext());
1867 void WebContents::OnRendererMessage(const base::string16& channel,
1868 const base::ListValue& args) {
1869 // webContents.emit(channel, new Event(), args...);
1870 Emit(base::UTF16ToUTF8(channel), args);
1873 void WebContents::OnRendererMessageSync(const base::string16& channel,
1874 const base::ListValue& args,
1875 IPC::Message* message) {
1876 // webContents.emit(channel, new Event(sender, message), args...);
1877 EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args);
1881 mate::Handle<WebContents> WebContents::CreateFrom(
1882 v8::Isolate* isolate, content::WebContents* web_contents) {
1883 // We have an existing WebContents object in JS.
1884 auto existing = TrackableObject::FromWrappedClass(isolate, web_contents);
1886 return mate::CreateHandle(isolate, static_cast<WebContents*>(existing));
1888 // Otherwise create a new WebContents wrapper object.
1889 return mate::CreateHandle(isolate, new WebContents(isolate, web_contents,
1893 mate::Handle<WebContents> WebContents::CreateFrom(
1894 v8::Isolate* isolate, content::WebContents* web_contents, Type type) {
1895 // Otherwise create a new WebContents wrapper object.
1896 return mate::CreateHandle(isolate, new WebContents(isolate, web_contents,
1901 mate::Handle<WebContents> WebContents::Create(
1902 v8::Isolate* isolate, const mate::Dictionary& options) {
1903 return mate::CreateHandle(isolate, new WebContents(isolate, options));
1912 using atom::api::WebContents;
1914 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
1915 v8::Local<v8::Context> context, void* priv) {
1916 v8::Isolate* isolate = context->GetIsolate();
1917 mate::Dictionary dict(isolate, exports);
1918 dict.Set("WebContents", WebContents::GetConstructor(isolate)->GetFunction());
1919 dict.SetMethod("create", &WebContents::Create);
1920 dict.SetMethod("fromId", &mate::TrackableObject<WebContents>::FromWeakMapID);
1921 dict.SetMethod("getAllWebContents",
1922 &mate::TrackableObject<WebContents>::GetAll);
1927 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_web_contents, Initialize)