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"
10 #include "atom/browser/api/atom_api_debugger.h"
11 #include "atom/browser/api/atom_api_session.h"
12 #include "atom/browser/api/atom_api_window.h"
13 #include "atom/browser/atom_browser_client.h"
14 #include "atom/browser/atom_browser_context.h"
15 #include "atom/browser/atom_browser_main_parts.h"
16 #include "atom/browser/atom_security_state_model_client.h"
17 #include "atom/browser/lib/bluetooth_chooser.h"
18 #include "atom/browser/native_window.h"
19 #include "atom/browser/net/atom_network_delegate.h"
20 #include "atom/browser/osr/osr_output_device.h"
21 #include "atom/browser/osr/osr_render_widget_host_view.h"
22 #include "atom/browser/osr/osr_web_contents_view.h"
23 #include "atom/browser/ui/drag_util.h"
24 #include "atom/browser/web_contents_permission_helper.h"
25 #include "atom/browser/web_contents_preferences.h"
26 #include "atom/browser/web_view_guest_delegate.h"
27 #include "atom/common/api/api_messages.h"
28 #include "atom/common/api/event_emitter_caller.h"
29 #include "atom/common/color_util.h"
30 #include "atom/common/mouse_util.h"
31 #include "atom/common/native_mate_converters/blink_converter.h"
32 #include "atom/common/native_mate_converters/callback.h"
33 #include "atom/common/native_mate_converters/content_converter.h"
34 #include "atom/common/native_mate_converters/file_path_converter.h"
35 #include "atom/common/native_mate_converters/gfx_converter.h"
36 #include "atom/common/native_mate_converters/gurl_converter.h"
37 #include "atom/common/native_mate_converters/image_converter.h"
38 #include "atom/common/native_mate_converters/net_converter.h"
39 #include "atom/common/native_mate_converters/string16_converter.h"
40 #include "atom/common/native_mate_converters/value_converter.h"
41 #include "atom/common/options_switches.h"
42 #include "base/strings/utf_string_conversions.h"
43 #include "base/threading/thread_task_runner_handle.h"
44 #include "brightray/browser/inspectable_web_contents.h"
45 #include "brightray/browser/inspectable_web_contents_view.h"
46 #include "chrome/browser/printing/print_preview_message_handler.h"
47 #include "chrome/browser/printing/print_view_manager_basic.h"
48 #include "content/browser/renderer_host/render_widget_host_impl.h"
49 #include "content/browser/web_contents/web_contents_impl.h"
50 #include "content/common/view_messages.h"
51 #include "content/public/browser/favicon_status.h"
52 #include "content/public/browser/native_web_keyboard_event.h"
53 #include "content/public/browser/navigation_details.h"
54 #include "content/public/browser/navigation_entry.h"
55 #include "content/public/browser/navigation_handle.h"
56 #include "content/public/browser/plugin_service.h"
57 #include "content/public/browser/render_frame_host.h"
58 #include "content/public/browser/render_process_host.h"
59 #include "content/public/browser/render_view_host.h"
60 #include "content/public/browser/render_widget_host.h"
61 #include "content/public/browser/render_widget_host_view.h"
62 #include "content/public/browser/resource_request_details.h"
63 #include "content/public/browser/service_worker_context.h"
64 #include "content/public/browser/site_instance.h"
65 #include "content/public/browser/storage_partition.h"
66 #include "content/public/browser/web_contents.h"
67 #include "content/public/common/context_menu_params.h"
68 #include "native_mate/dictionary.h"
69 #include "native_mate/object_template_builder.h"
70 #include "net/url_request/url_request_context.h"
71 #include "third_party/WebKit/public/web/WebFindOptions.h"
72 #include "third_party/WebKit/public/web/WebInputEvent.h"
73 #include "ui/display/screen.h"
75 #if !defined(OS_MACOSX)
76 #include "ui/aura/window.h"
79 #include "atom/common/node_includes.h"
83 struct PrintSettings {
85 bool print_background;
93 struct Converter<atom::SetSizeParams> {
94 static bool FromV8(v8::Isolate* isolate,
95 v8::Local<v8::Value> val,
96 atom::SetSizeParams* out) {
97 mate::Dictionary params;
98 if (!ConvertFromV8(isolate, val, ¶ms))
101 if (params.Get("enableAutoSize", &autosize))
102 out->enable_auto_size.reset(new bool(true));
104 if (params.Get("min", &size))
105 out->min_size.reset(new gfx::Size(size));
106 if (params.Get("max", &size))
107 out->max_size.reset(new gfx::Size(size));
108 if (params.Get("normal", &size))
109 out->normal_size.reset(new gfx::Size(size));
115 struct Converter<PrintSettings> {
116 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
117 PrintSettings* out) {
118 mate::Dictionary dict;
119 if (!ConvertFromV8(isolate, val, &dict))
121 dict.Get("silent", &(out->silent));
122 dict.Get("printBackground", &(out->print_background));
128 struct Converter<WindowOpenDisposition> {
129 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
130 WindowOpenDisposition val) {
131 std::string disposition = "other";
133 case CURRENT_TAB: disposition = "default"; break;
134 case NEW_FOREGROUND_TAB: disposition = "foreground-tab"; break;
135 case NEW_BACKGROUND_TAB: disposition = "background-tab"; break;
136 case NEW_POPUP: case NEW_WINDOW: disposition = "new-window"; break;
137 case SAVE_TO_DISK: disposition = "save-to-disk"; break;
140 return mate::ConvertToV8(isolate, disposition);
145 struct Converter<content::SavePageType> {
146 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
147 content::SavePageType* out) {
148 std::string save_type;
149 if (!ConvertFromV8(isolate, val, &save_type))
151 save_type = base::ToLowerASCII(save_type);
152 if (save_type == "htmlonly") {
153 *out = content::SAVE_PAGE_TYPE_AS_ONLY_HTML;
154 } else if (save_type == "htmlcomplete") {
155 *out = content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML;
156 } else if (save_type == "mhtml") {
157 *out = content::SAVE_PAGE_TYPE_AS_MHTML;
166 struct Converter<atom::api::WebContents::Type> {
167 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
168 atom::api::WebContents::Type val) {
169 using Type = atom::api::WebContents::Type;
170 std::string type = "";
172 case Type::BACKGROUND_PAGE: type = "backgroundPage"; break;
173 case Type::BROWSER_WINDOW: type = "window"; break;
174 case Type::REMOTE: type = "remote"; break;
175 case Type::WEB_VIEW: type = "webview"; break;
176 case Type::OFF_SCREEN: type = "offscreen"; break;
179 return mate::ConvertToV8(isolate, type);
182 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
183 atom::api::WebContents::Type* out) {
184 using Type = atom::api::WebContents::Type;
186 if (!ConvertFromV8(isolate, val, &type))
188 if (type == "webview") {
189 *out = Type::WEB_VIEW;
190 } else if (type == "backgroundPage") {
191 *out = Type::BACKGROUND_PAGE;
192 } else if (type == "offscreen") {
193 *out = Type::OFF_SCREEN;
210 content::ServiceWorkerContext* GetServiceWorkerContext(
211 const content::WebContents* web_contents) {
212 auto context = web_contents->GetBrowserContext();
213 auto site_instance = web_contents->GetSiteInstance();
214 if (!context || !site_instance)
217 auto storage_partition =
218 content::BrowserContext::GetStoragePartition(context, site_instance);
219 if (!storage_partition)
222 return storage_partition->GetServiceWorkerContext();
225 // Called when CapturePage is done.
226 void OnCapturePageDone(base::Callback<void(const gfx::Image&)> callback,
227 const SkBitmap& bitmap,
228 content::ReadbackResponse response) {
229 callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap));
234 WebContents::WebContents(v8::Isolate* isolate,
235 content::WebContents* web_contents,
237 : content::WebContentsObserver(web_contents),
241 background_throttling_(true),
242 enable_devtools_(true) {
244 if (type == REMOTE) {
245 web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
247 AttachAsUserData(web_contents);
249 const mate::Dictionary options = mate::Dictionary::CreateEmpty(isolate);
250 auto session = Session::CreateFrom(isolate, GetBrowserContext());
251 session_.Reset(isolate, session.ToV8());
252 InitWithSessionAndOptions(isolate, web_contents, session, options);
256 WebContents::WebContents(v8::Isolate* isolate,
257 const mate::Dictionary& options)
258 : embedder_(nullptr),
259 type_(BROWSER_WINDOW),
261 background_throttling_(true),
262 enable_devtools_(true) {
264 options.Get("backgroundThrottling", &background_throttling_);
266 // FIXME(zcbenz): We should read "type" parameter for better design, but
267 // on Windows we have encountered a compiler bug that if we read "type"
268 // from |options| and then set |type_|, a memory corruption will happen
269 // and Electron will soon crash.
270 // Remvoe this after we upgraded to use VS 2015 Update 3.
272 if (options.Get("isGuest", &b) && b)
274 else if (options.Get("isBackgroundPage", &b) && b)
275 type_ = BACKGROUND_PAGE;
276 else if (options.Get("offscreen", &b) && b)
279 // Whether to enable DevTools.
280 options.Get("devTools", &enable_devtools_);
282 // Obtain the session.
283 std::string partition;
284 mate::Handle<api::Session> session;
285 if (options.Get("session", &session)) {
286 } else if (options.Get("partition", &partition)) {
287 session = Session::FromPartition(isolate, partition);
289 // Use the default session if not specified.
290 session = Session::FromPartition(isolate, "");
292 session_.Reset(isolate, session.ToV8());
294 content::WebContents* web_contents;
296 scoped_refptr<content::SiteInstance> site_instance =
297 content::SiteInstance::CreateForURL(
298 session->browser_context(), GURL("chrome-guest://fake-host"));
299 content::WebContents::CreateParams params(
300 session->browser_context(), site_instance);
301 guest_delegate_.reset(new WebViewGuestDelegate);
302 params.guest_delegate = guest_delegate_.get();
303 web_contents = content::WebContents::Create(params);
304 } else if (IsOffScreen()) {
305 bool transparent = false;
306 options.Get("transparent", &transparent);
308 content::WebContents::CreateParams params(session->browser_context());
309 auto* view = new OffScreenWebContentsView(
310 transparent, base::Bind(&WebContents::OnPaint, base::Unretained(this)));
312 params.delegate_view = view;
314 web_contents = content::WebContents::Create(params);
315 view->SetWebContents(web_contents);
317 content::WebContents::CreateParams params(session->browser_context());
318 web_contents = content::WebContents::Create(params);
321 InitWithSessionAndOptions(isolate, web_contents, session, options);
324 void WebContents::InitWithSessionAndOptions(v8::Isolate* isolate,
325 content::WebContents *web_contents,
326 mate::Handle<api::Session> session,
327 const mate::Dictionary& options) {
328 Observe(web_contents);
329 InitWithWebContents(web_contents, session->browser_context());
331 managed_web_contents()->GetView()->SetDelegate(this);
333 // Save the preferences in C++.
334 new WebContentsPreferences(web_contents, options);
336 // Intialize permission helper.
337 WebContentsPermissionHelper::CreateForWebContents(web_contents);
338 // Intialize security state client.
339 AtomSecurityStateModelClient::CreateForWebContents(web_contents);
341 web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
344 guest_delegate_->Initialize(this);
346 NativeWindow* owner_window = nullptr;
347 if (options.Get("embedder", &embedder_) && embedder_) {
348 // New WebContents's owner_window is the embedder's owner_window.
350 NativeWindowRelay::FromWebContents(embedder_->web_contents());
352 owner_window = relay->window.get();
355 SetOwnerWindow(owner_window);
359 AttachAsUserData(web_contents);
362 WebContents::~WebContents() {
363 // The destroy() is called.
364 if (managed_web_contents()) {
365 // For webview we need to tell content module to do some cleanup work before
367 if (type_ == WEB_VIEW)
368 guest_delegate_->Destroy();
370 // The WebContentsDestroyed will not be called automatically because we
371 // unsubscribe from webContents before destroying it. So we have to manually
372 // call it here to make sure "destroyed" event is emitted.
373 RenderViewDeleted(web_contents()->GetRenderViewHost());
374 WebContentsDestroyed();
378 bool WebContents::DidAddMessageToConsole(content::WebContents* source,
380 const base::string16& message,
382 const base::string16& source_id) {
383 if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) {
386 Emit("console-message", level, message, line_no, source_id);
391 void WebContents::OnCreateWindow(
392 const GURL& target_url,
393 const std::string& frame_name,
394 WindowOpenDisposition disposition,
395 const std::vector<std::string>& features,
396 const scoped_refptr<content::ResourceRequestBodyImpl>& body) {
397 if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
398 Emit("-new-window", target_url, frame_name, disposition, features, body);
400 Emit("new-window", target_url, frame_name, disposition, features);
403 void WebContents::WebContentsCreated(content::WebContents* source_contents,
404 int opener_render_process_id,
405 int opener_render_frame_id,
406 const std::string& frame_name,
407 const GURL& target_url,
408 content::WebContents* new_contents) {
409 v8::Locker locker(isolate());
410 v8::HandleScope handle_scope(isolate());
411 auto api_web_contents = CreateFrom(isolate(), new_contents, BROWSER_WINDOW);
412 Emit("-web-contents-created", api_web_contents, target_url, frame_name);
415 void WebContents::AddNewContents(content::WebContents* source,
416 content::WebContents* new_contents,
417 WindowOpenDisposition disposition,
418 const gfx::Rect& initial_rect,
421 v8::Locker locker(isolate());
422 v8::HandleScope handle_scope(isolate());
423 auto api_web_contents = CreateFrom(isolate(), new_contents);
424 if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture,
425 initial_rect.x(), initial_rect.y(), initial_rect.width(),
426 initial_rect.height())) {
427 api_web_contents->DestroyWebContents();
431 content::WebContents* WebContents::OpenURLFromTab(
432 content::WebContents* source,
433 const content::OpenURLParams& params) {
434 if (params.disposition != CURRENT_TAB) {
435 if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
436 Emit("-new-window", params.url, "", params.disposition);
438 Emit("new-window", params.url, "", params.disposition);
442 // Give user a chance to cancel navigation.
443 if (Emit("will-navigate", params.url))
446 // Don't load the URL if the web contents was marked as destroyed from a
447 // will-navigate event listener
451 return CommonWebContentsDelegate::OpenURLFromTab(source, params);
454 void WebContents::BeforeUnloadFired(content::WebContents* tab,
456 bool* proceed_to_fire_unload) {
457 if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
458 *proceed_to_fire_unload = proceed;
460 *proceed_to_fire_unload = true;
463 void WebContents::MoveContents(content::WebContents* source,
464 const gfx::Rect& pos) {
468 void WebContents::CloseContents(content::WebContents* source) {
471 if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) && owner_window())
472 owner_window()->CloseContents(source);
475 void WebContents::ActivateContents(content::WebContents* source) {
479 void WebContents::UpdateTargetURL(content::WebContents* source,
481 Emit("update-target-url", url);
484 bool WebContents::IsPopupOrPanel(const content::WebContents* source) const {
485 return type_ == BROWSER_WINDOW;
488 void WebContents::HandleKeyboardEvent(
489 content::WebContents* source,
490 const content::NativeWebKeyboardEvent& event) {
491 if (type_ == WEB_VIEW && embedder_) {
492 // Send the unhandled keyboard events back to the embedder.
493 embedder_->HandleKeyboardEvent(source, event);
495 // Go to the default keyboard handling.
496 CommonWebContentsDelegate::HandleKeyboardEvent(source, event);
500 bool WebContents::PreHandleKeyboardEvent(
501 content::WebContents* source,
502 const content::NativeWebKeyboardEvent& event,
503 bool* is_keyboard_shortcut) {
504 if (event.type == blink::WebInputEvent::Type::RawKeyDown
505 || event.type == blink::WebInputEvent::Type::KeyUp)
506 return Emit("before-input-event", event);
511 void WebContents::EnterFullscreenModeForTab(content::WebContents* source,
512 const GURL& origin) {
513 auto permission_helper =
514 WebContentsPermissionHelper::FromWebContents(source);
515 auto callback = base::Bind(&WebContents::OnEnterFullscreenModeForTab,
516 base::Unretained(this), source, origin);
517 permission_helper->RequestFullscreenPermission(callback);
520 void WebContents::OnEnterFullscreenModeForTab(content::WebContents* source,
525 CommonWebContentsDelegate::EnterFullscreenModeForTab(source, origin);
526 Emit("enter-html-full-screen");
529 void WebContents::ExitFullscreenModeForTab(content::WebContents* source) {
530 CommonWebContentsDelegate::ExitFullscreenModeForTab(source);
531 Emit("leave-html-full-screen");
534 void WebContents::RendererUnresponsive(
535 content::WebContents* source,
536 const content::WebContentsUnresponsiveState& unresponsive_state) {
537 Emit("unresponsive");
538 if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) && owner_window())
539 owner_window()->RendererUnresponsive(source);
542 void WebContents::RendererResponsive(content::WebContents* source) {
544 if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) && owner_window())
545 owner_window()->RendererResponsive(source);
548 bool WebContents::HandleContextMenu(const content::ContextMenuParams& params) {
549 if (params.custom_context.is_pepper_menu) {
550 Emit("pepper-context-menu", std::make_pair(params, web_contents()));
551 web_contents()->NotifyContextMenuClosed(params.custom_context);
553 Emit("context-menu", std::make_pair(params, web_contents()));
559 bool WebContents::OnGoToEntryOffset(int offset) {
564 void WebContents::FindReply(content::WebContents* web_contents,
566 int number_of_matches,
567 const gfx::Rect& selection_rect,
568 int active_match_ordinal,
573 v8::Locker locker(isolate());
574 v8::HandleScope handle_scope(isolate());
575 mate::Dictionary result = mate::Dictionary::CreateEmpty(isolate());
576 result.Set("requestId", request_id);
577 result.Set("matches", number_of_matches);
578 result.Set("selectionArea", selection_rect);
579 result.Set("activeMatchOrdinal", active_match_ordinal);
580 result.Set("finalUpdate", final_update); // Deprecate after 2.0
581 Emit("found-in-page", result);
584 bool WebContents::CheckMediaAccessPermission(
585 content::WebContents* web_contents,
586 const GURL& security_origin,
587 content::MediaStreamType type) {
591 void WebContents::RequestMediaAccessPermission(
592 content::WebContents* web_contents,
593 const content::MediaStreamRequest& request,
594 const content::MediaResponseCallback& callback) {
595 auto permission_helper =
596 WebContentsPermissionHelper::FromWebContents(web_contents);
597 permission_helper->RequestMediaAccessPermission(request, callback);
600 void WebContents::RequestToLockMouse(
601 content::WebContents* web_contents,
603 bool last_unlocked_by_target) {
604 auto permission_helper =
605 WebContentsPermissionHelper::FromWebContents(web_contents);
606 permission_helper->RequestPointerLockPermission(user_gesture);
609 std::unique_ptr<content::BluetoothChooser> WebContents::RunBluetoothChooser(
610 content::RenderFrameHost* frame,
611 const content::BluetoothChooser::EventHandler& event_handler) {
612 std::unique_ptr<BluetoothChooser> bluetooth_chooser(
613 new BluetoothChooser(this, event_handler));
614 return std::move(bluetooth_chooser);
617 void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
618 // Do nothing, we override this method just to avoid compilation error since
619 // there are two virtual functions named BeforeUnloadFired.
622 void WebContents::RenderViewCreated(content::RenderViewHost* render_view_host) {
623 const auto impl = content::RenderWidgetHostImpl::FromID(
624 render_view_host->GetProcess()->GetID(),
625 render_view_host->GetRoutingID());
627 impl->disable_hidden_ = !background_throttling_;
630 void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) {
631 Emit("render-view-deleted", render_view_host->GetProcess()->GetID());
634 void WebContents::RenderProcessGone(base::TerminationStatus status) {
635 Emit("crashed", status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED);
638 void WebContents::PluginCrashed(const base::FilePath& plugin_path,
639 base::ProcessId plugin_pid) {
640 content::WebPluginInfo info;
641 auto plugin_service = content::PluginService::GetInstance();
642 plugin_service->GetPluginInfoByPath(plugin_path, &info);
643 Emit("plugin-crashed", info.name, info.version);
646 void WebContents::MediaStartedPlaying(const MediaPlayerInfo& video_type,
647 const MediaPlayerId& id) {
648 Emit("media-started-playing");
651 void WebContents::MediaStoppedPlaying(const MediaPlayerInfo& video_type,
652 const MediaPlayerId& id) {
653 Emit("media-paused");
656 void WebContents::DidChangeThemeColor(SkColor theme_color) {
657 Emit("did-change-theme-color", atom::ToRGBHex(theme_color));
660 void WebContents::DocumentLoadedInFrame(
661 content::RenderFrameHost* render_frame_host) {
662 if (!render_frame_host->GetParent())
666 void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
667 const GURL& validated_url) {
668 bool is_main_frame = !render_frame_host->GetParent();
669 Emit("did-frame-finish-load", is_main_frame);
672 Emit("did-finish-load");
675 void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host,
678 const base::string16& error_description,
679 bool was_ignored_by_handler) {
680 bool is_main_frame = !render_frame_host->GetParent();
681 Emit("did-fail-load", error_code, error_description, url, is_main_frame);
684 void WebContents::DidStartLoading() {
685 Emit("did-start-loading");
688 void WebContents::DidStopLoading() {
689 Emit("did-stop-loading");
692 void WebContents::DidGetResourceResponseStart(
693 const content::ResourceRequestDetails& details) {
694 Emit("did-get-response-details",
695 details.socket_address.IsEmpty(),
697 details.original_url,
698 details.http_response_code,
701 details.headers.get(),
702 ResourceTypeToString(details.resource_type));
705 void WebContents::DidGetRedirectForResourceRequest(
706 const content::ResourceRedirectDetails& details) {
707 Emit("did-get-redirect-request",
710 (details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME),
711 details.http_response_code,
714 details.headers.get());
717 void WebContents::DidFinishNavigation(
718 content::NavigationHandle* navigation_handle) {
719 bool is_main_frame = navigation_handle->IsInMainFrame();
720 if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) {
721 auto url = navigation_handle->GetURL();
722 bool is_in_page = navigation_handle->IsSamePage();
723 if (is_main_frame && !is_in_page) {
724 Emit("did-navigate", url);
725 } else if (is_in_page) {
726 Emit("did-navigate-in-page", url, is_main_frame);
729 auto url = navigation_handle->GetURL();
730 int code = navigation_handle->GetNetErrorCode();
731 auto description = net::ErrorToShortString(code);
732 Emit("did-fail-provisional-load", code, description, url, is_main_frame);
734 // Do not emit "did-fail-load" for canceled requests.
735 if (code != net::ERR_ABORTED)
736 Emit("did-fail-load", code, description, url, is_main_frame);
740 void WebContents::TitleWasSet(content::NavigationEntry* entry,
743 Emit("-page-title-updated", entry->GetTitle(), explicit_set);
745 Emit("-page-title-updated", "", explicit_set);
748 void WebContents::DidUpdateFaviconURL(
749 const std::vector<content::FaviconURL>& urls) {
750 std::set<GURL> unique_urls;
751 for (const auto& iter : urls) {
752 if (iter.icon_type != content::FaviconURL::FAVICON)
754 const GURL& url = iter.icon_url;
756 unique_urls.insert(url);
758 Emit("page-favicon-updated", unique_urls);
761 void WebContents::DevToolsReloadPage() {
762 Emit("devtools-reload-page");
765 void WebContents::DevToolsFocused() {
766 Emit("devtools-focused");
769 void WebContents::DevToolsOpened() {
770 v8::Locker locker(isolate());
771 v8::HandleScope handle_scope(isolate());
772 auto handle = WebContents::CreateFrom(
773 isolate(), managed_web_contents()->GetDevToolsWebContents());
774 devtools_web_contents_.Reset(isolate(), handle.ToV8());
776 // Set inspected tabID.
777 base::FundamentalValue tab_id(ID());
778 managed_web_contents()->CallClientFunction(
779 "DevToolsAPI.setInspectedTabId", &tab_id, nullptr, nullptr);
781 // Inherit owner window in devtools.
783 handle->SetOwnerWindow(managed_web_contents()->GetDevToolsWebContents(),
786 Emit("devtools-opened");
789 void WebContents::DevToolsClosed() {
790 v8::Locker locker(isolate());
791 v8::HandleScope handle_scope(isolate());
792 devtools_web_contents_.Reset();
794 Emit("devtools-closed");
797 bool WebContents::OnMessageReceived(const IPC::Message& message) {
799 IPC_BEGIN_MESSAGE_MAP(WebContents, message)
800 IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage)
801 IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync,
802 OnRendererMessageSync)
803 IPC_MESSAGE_HANDLER_CODE(ViewHostMsg_SetCursor, OnCursorChange,
805 IPC_MESSAGE_UNHANDLED(handled = false)
806 IPC_END_MESSAGE_MAP()
811 // There are three ways of destroying a webContents:
812 // 1. call webContents.destroy();
813 // 2. garbage collection;
814 // 3. user closes the window of webContents;
815 // For webview only #1 will happen, for BrowserWindow both #1 and #3 may
816 // happen. The #2 should never happen for webContents, because webview is
817 // managed by GuestViewManager, and BrowserWindow's webContents is managed
819 // For #1, the destructor will do the cleanup work and we only need to make
820 // sure "destroyed" event is emitted. For #3, the content::WebContents will
821 // be destroyed on close, and WebContentsDestroyed would be called for it, so
822 // we need to make sure the api::WebContents is also deleted.
823 void WebContents::WebContentsDestroyed() {
824 // This event is only for internal use, which is emitted when WebContents is
826 Emit("will-destroy");
828 // Cleanup relationships with other parts.
831 // We can not call Destroy here because we need to call Emit first, but we
832 // also do not want any method to be used, so just mark as destroyed here.
837 // Destroy the native class in next tick.
838 base::ThreadTaskRunnerHandle::Get()->PostTask(
839 FROM_HERE, GetDestroyClosure());
842 void WebContents::NavigationEntryCommitted(
843 const content::LoadCommittedDetails& details) {
844 Emit("navigation-entry-commited", details.entry->GetURL(),
845 details.is_in_page, details.did_replace_entry);
848 int64_t WebContents::GetID() const {
849 int64_t process_id = web_contents()->GetRenderProcessHost()->GetID();
850 int64_t routing_id = web_contents()->GetRoutingID();
851 int64_t rv = (process_id << 32) + routing_id;
855 int WebContents::GetProcessID() const {
856 return web_contents()->GetRenderProcessHost()->GetID();
859 WebContents::Type WebContents::GetType() const {
863 bool WebContents::Equal(const WebContents* web_contents) const {
864 return GetID() == web_contents->GetID();
867 void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
868 if (!url.is_valid() || url.spec().size() > url::kMaxURLChars) {
869 Emit("did-fail-load",
870 static_cast<int>(net::ERR_INVALID_URL),
871 net::ErrorToShortString(net::ERR_INVALID_URL),
872 url.possibly_invalid_spec(),
877 content::NavigationController::LoadURLParams params(url);
880 if (options.Get("httpReferrer", &http_referrer))
881 params.referrer = content::Referrer(http_referrer.GetAsReferrer(),
882 blink::WebReferrerPolicyDefault);
884 std::string user_agent;
885 if (options.Get("userAgent", &user_agent))
886 web_contents()->SetUserAgentOverride(user_agent);
888 std::string extra_headers;
889 if (options.Get("extraHeaders", &extra_headers))
890 params.extra_headers = extra_headers;
892 scoped_refptr<content::ResourceRequestBodyImpl> body;
893 if (options.Get("postData", &body)) {
894 params.post_data = body;
895 params.load_type = content::NavigationController::LOAD_TYPE_HTTP_POST;
898 params.transition_type = ui::PAGE_TRANSITION_TYPED;
899 params.should_clear_history_list = true;
900 params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
901 web_contents()->GetController().LoadURLWithParams(params);
903 // Set the background color of RenderWidgetHostView.
904 // We have to call it right after LoadURL because the RenderViewHost is only
905 // created after loading a page.
906 const auto view = web_contents()->GetRenderWidgetHostView();
908 WebContentsPreferences* web_preferences =
909 WebContentsPreferences::FromWebContents(web_contents());
910 std::string color_name;
911 if (web_preferences->web_preferences()->GetString(options::kBackgroundColor,
913 view->SetBackgroundColor(ParseHexColor(color_name));
915 view->SetBackgroundColor(SK_ColorTRANSPARENT);
920 void WebContents::DownloadURL(const GURL& url) {
921 auto browser_context = web_contents()->GetBrowserContext();
922 auto download_manager =
923 content::BrowserContext::GetDownloadManager(browser_context);
925 download_manager->DownloadUrl(
926 content::DownloadUrlParameters::CreateForWebContentsMainFrame(
927 web_contents(), url));
930 GURL WebContents::GetURL() const {
931 return web_contents()->GetURL();
934 base::string16 WebContents::GetTitle() const {
935 return web_contents()->GetTitle();
938 bool WebContents::IsLoading() const {
939 return web_contents()->IsLoading();
942 bool WebContents::IsLoadingMainFrame() const {
943 // Comparing site instances works because Electron always creates a new site
944 // instance when navigating, regardless of origin. See AtomBrowserClient.
945 return (web_contents()->GetLastCommittedURL().is_empty() ||
946 web_contents()->GetSiteInstance() !=
947 web_contents()->GetPendingSiteInstance()) && IsLoading();
950 bool WebContents::IsWaitingForResponse() const {
951 return web_contents()->IsWaitingForResponse();
954 void WebContents::Stop() {
955 web_contents()->Stop();
958 void WebContents::GoBack() {
959 atom::AtomBrowserClient::SuppressRendererProcessRestartForOnce();
960 web_contents()->GetController().GoBack();
963 void WebContents::GoForward() {
964 atom::AtomBrowserClient::SuppressRendererProcessRestartForOnce();
965 web_contents()->GetController().GoForward();
968 void WebContents::GoToOffset(int offset) {
969 atom::AtomBrowserClient::SuppressRendererProcessRestartForOnce();
970 web_contents()->GetController().GoToOffset(offset);
973 bool WebContents::IsCrashed() const {
974 return web_contents()->IsCrashed();
977 void WebContents::SetUserAgent(const std::string& user_agent,
978 mate::Arguments* args) {
979 web_contents()->SetUserAgentOverride(user_agent);
982 std::string WebContents::GetUserAgent() {
983 return web_contents()->GetUserAgentOverride();
986 bool WebContents::SavePage(const base::FilePath& full_file_path,
987 const content::SavePageType& save_type,
988 const SavePageHandler::SavePageCallback& callback) {
989 auto handler = new SavePageHandler(web_contents(), callback);
990 return handler->Handle(full_file_path, save_type);
993 void WebContents::OpenDevTools(mate::Arguments* args) {
997 if (!enable_devtools_)
1001 if (type_ == WEB_VIEW || !owner_window()) {
1003 } else if (args && args->Length() == 1) {
1004 bool detach = false;
1005 mate::Dictionary options;
1006 if (args->GetNext(&options)) {
1007 options.Get("mode", &state);
1009 // TODO(kevinsawicki) Remove in 2.0
1010 options.Get("detach", &detach);
1011 if (state.empty() && detach)
1015 managed_web_contents()->SetDockState(state);
1016 managed_web_contents()->ShowDevTools();
1019 void WebContents::CloseDevTools() {
1020 if (type_ == REMOTE)
1023 managed_web_contents()->CloseDevTools();
1026 bool WebContents::IsDevToolsOpened() {
1027 if (type_ == REMOTE)
1030 return managed_web_contents()->IsDevToolsViewShowing();
1033 bool WebContents::IsDevToolsFocused() {
1034 if (type_ == REMOTE)
1037 return managed_web_contents()->GetView()->IsDevToolsViewFocused();
1040 void WebContents::EnableDeviceEmulation(
1041 const blink::WebDeviceEmulationParams& params) {
1042 if (type_ == REMOTE)
1045 Send(new ViewMsg_EnableDeviceEmulation(routing_id(), params));
1048 void WebContents::DisableDeviceEmulation() {
1049 if (type_ == REMOTE)
1052 Send(new ViewMsg_DisableDeviceEmulation(routing_id()));
1055 void WebContents::ToggleDevTools() {
1056 if (IsDevToolsOpened())
1059 OpenDevTools(nullptr);
1062 void WebContents::InspectElement(int x, int y) {
1063 if (type_ == REMOTE)
1066 if (!enable_devtools_)
1069 if (!managed_web_contents()->GetDevToolsWebContents())
1070 OpenDevTools(nullptr);
1071 managed_web_contents()->InspectElement(x, y);
1074 void WebContents::InspectServiceWorker() {
1075 if (type_ == REMOTE)
1078 if (!enable_devtools_)
1081 for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
1082 if (agent_host->GetType() ==
1083 content::DevToolsAgentHost::TYPE_SERVICE_WORKER) {
1084 OpenDevTools(nullptr);
1085 managed_web_contents()->AttachTo(agent_host);
1091 void WebContents::HasServiceWorker(
1092 const base::Callback<void(bool)>& callback) {
1093 auto context = GetServiceWorkerContext(web_contents());
1097 context->CheckHasServiceWorker(web_contents()->GetLastCommittedURL(),
1102 void WebContents::UnregisterServiceWorker(
1103 const base::Callback<void(bool)>& callback) {
1104 auto context = GetServiceWorkerContext(web_contents());
1108 context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(),
1112 void WebContents::SetAudioMuted(bool muted) {
1113 web_contents()->SetAudioMuted(muted);
1116 bool WebContents::IsAudioMuted() {
1117 return web_contents()->IsAudioMuted();
1120 void WebContents::Print(mate::Arguments* args) {
1121 PrintSettings settings = { false, false };
1122 if (args->Length() == 1 && !args->GetNext(&settings)) {
1127 printing::PrintViewManagerBasic::FromWebContents(web_contents())->
1128 PrintNow(settings.silent, settings.print_background);
1131 void WebContents::PrintToPDF(const base::DictionaryValue& setting,
1132 const PrintToPDFCallback& callback) {
1133 printing::PrintPreviewMessageHandler::FromWebContents(web_contents())->
1134 PrintToPDF(setting, callback);
1137 void WebContents::AddWorkSpace(mate::Arguments* args,
1138 const base::FilePath& path) {
1140 args->ThrowError("path cannot be empty");
1143 DevToolsAddFileSystem(path);
1146 void WebContents::RemoveWorkSpace(mate::Arguments* args,
1147 const base::FilePath& path) {
1149 args->ThrowError("path cannot be empty");
1152 DevToolsRemoveFileSystem(path);
1155 void WebContents::Undo() {
1156 web_contents()->Undo();
1159 void WebContents::Redo() {
1160 web_contents()->Redo();
1163 void WebContents::Cut() {
1164 web_contents()->Cut();
1167 void WebContents::Copy() {
1168 web_contents()->Copy();
1171 void WebContents::Paste() {
1172 web_contents()->Paste();
1175 void WebContents::PasteAndMatchStyle() {
1176 web_contents()->PasteAndMatchStyle();
1179 void WebContents::Delete() {
1180 web_contents()->Delete();
1183 void WebContents::SelectAll() {
1184 web_contents()->SelectAll();
1187 void WebContents::Unselect() {
1188 web_contents()->Unselect();
1191 void WebContents::Replace(const base::string16& word) {
1192 web_contents()->Replace(word);
1195 void WebContents::ReplaceMisspelling(const base::string16& word) {
1196 web_contents()->ReplaceMisspelling(word);
1199 uint32_t WebContents::FindInPage(mate::Arguments* args) {
1200 uint32_t request_id = GetNextRequestId();
1201 base::string16 search_text;
1202 blink::WebFindOptions options;
1203 if (!args->GetNext(&search_text) || search_text.empty()) {
1204 args->ThrowError("Must provide a non-empty search content");
1208 args->GetNext(&options);
1210 web_contents()->Find(request_id, search_text, options);
1214 void WebContents::StopFindInPage(content::StopFindAction action) {
1215 web_contents()->StopFinding(action);
1218 void WebContents::ShowDefinitionForSelection() {
1219 #if defined(OS_MACOSX)
1220 const auto view = web_contents()->GetRenderWidgetHostView();
1222 view->ShowDefinitionForSelection();
1226 void WebContents::CopyImageAt(int x, int y) {
1227 const auto host = web_contents()->GetMainFrame();
1229 host->CopyImageAt(x, y);
1232 void WebContents::Focus() {
1233 web_contents()->Focus();
1236 #if !defined(OS_MACOSX)
1237 bool WebContents::IsFocused() const {
1238 auto view = web_contents()->GetRenderWidgetHostView();
1239 if (!view) return false;
1241 if (GetType() != BACKGROUND_PAGE) {
1242 auto window = web_contents()->GetNativeView()->GetToplevelWindow();
1243 if (window && !window->IsVisible())
1247 return view->HasFocus();
1251 void WebContents::TabTraverse(bool reverse) {
1252 web_contents()->FocusThroughTabTraversal(reverse);
1255 bool WebContents::SendIPCMessage(bool all_frames,
1256 const base::string16& channel,
1257 const base::ListValue& args) {
1258 return Send(new AtomViewMsg_Message(routing_id(), all_frames, channel, args));
1261 void WebContents::SendInputEvent(v8::Isolate* isolate,
1262 v8::Local<v8::Value> input_event) {
1263 const auto view = web_contents()->GetRenderWidgetHostView();
1266 const auto host = view->GetRenderWidgetHost();
1270 int type = mate::GetWebInputEventType(isolate, input_event);
1271 if (blink::WebInputEvent::isMouseEventType(type)) {
1272 blink::WebMouseEvent mouse_event;
1273 if (mate::ConvertFromV8(isolate, input_event, &mouse_event)) {
1274 host->ForwardMouseEvent(mouse_event);
1277 } else if (blink::WebInputEvent::isKeyboardEventType(type)) {
1278 content::NativeWebKeyboardEvent keyboard_event;
1279 if (mate::ConvertFromV8(isolate, input_event, &keyboard_event)) {
1280 host->ForwardKeyboardEvent(keyboard_event);
1283 } else if (type == blink::WebInputEvent::MouseWheel) {
1284 blink::WebMouseWheelEvent mouse_wheel_event;
1285 if (mate::ConvertFromV8(isolate, input_event, &mouse_wheel_event)) {
1286 host->ForwardWheelEvent(mouse_wheel_event);
1291 isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
1292 isolate, "Invalid event object")));
1295 void WebContents::BeginFrameSubscription(mate::Arguments* args) {
1296 bool only_dirty = false;
1297 FrameSubscriber::FrameCaptureCallback callback;
1299 args->GetNext(&only_dirty);
1300 if (!args->GetNext(&callback)) {
1305 const auto view = web_contents()->GetRenderWidgetHostView();
1307 std::unique_ptr<FrameSubscriber> frame_subscriber(new FrameSubscriber(
1308 isolate(), view, callback, only_dirty));
1309 view->BeginFrameSubscription(std::move(frame_subscriber));
1313 void WebContents::EndFrameSubscription() {
1314 const auto view = web_contents()->GetRenderWidgetHostView();
1316 view->EndFrameSubscription();
1319 void WebContents::StartDrag(const mate::Dictionary& item,
1320 mate::Arguments* args) {
1321 base::FilePath file;
1322 std::vector<base::FilePath> files;
1323 if (!item.Get("files", &files) && item.Get("file", &file)) {
1324 files.push_back(file);
1327 mate::Handle<NativeImage> icon;
1328 if (!item.Get("icon", &icon) && !file.empty()) {
1329 // TODO(zcbenz): Set default icon from file.
1333 if (icon.IsEmpty()) {
1334 args->ThrowError("Must specify 'icon' option");
1338 #if defined(OS_MACOSX)
1339 // NSWindow.dragImage requires a non-empty NSImage
1340 if (icon->image().IsEmpty()) {
1341 args->ThrowError("Must specify non-empty 'icon' option");
1347 if (!files.empty()) {
1348 base::MessageLoop::ScopedNestableTaskAllower allow(
1349 base::MessageLoop::current());
1350 DragFileItems(files, icon->image(), web_contents()->GetNativeView());
1352 args->ThrowError("Must specify either 'file' or 'files' option");
1356 void WebContents::CapturePage(mate::Arguments* args) {
1358 base::Callback<void(const gfx::Image&)> callback;
1360 if (!(args->Length() == 1 && args->GetNext(&callback)) &&
1361 !(args->Length() == 2 && args->GetNext(&rect)
1362 && args->GetNext(&callback))) {
1367 const auto view = web_contents()->GetRenderWidgetHostView();
1368 const auto host = view ? view->GetRenderWidgetHost() : nullptr;
1369 if (!view || !host) {
1370 callback.Run(gfx::Image());
1374 // Capture full page if user doesn't specify a |rect|.
1375 const gfx::Size view_size = rect.IsEmpty() ? view->GetViewBounds().size() :
1378 // By default, the requested bitmap size is the view size in screen
1379 // coordinates. However, if there's more pixel detail available on the
1380 // current system, increase the requested bitmap size to capture it all.
1381 gfx::Size bitmap_size = view_size;
1382 const gfx::NativeView native_view = view->GetNativeView();
1384 display::Screen::GetScreen()->GetDisplayNearestWindow(native_view)
1385 .device_scale_factor();
1387 bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);
1389 host->CopyFromBackingStore(gfx::Rect(rect.origin(), view_size),
1391 base::Bind(&OnCapturePageDone, callback),
1392 kBGRA_8888_SkColorType);
1395 void WebContents::OnCursorChange(const content::WebCursor& cursor) {
1396 content::WebCursor::CursorInfo info;
1397 cursor.GetCursorInfo(&info);
1399 if (cursor.IsCustom()) {
1400 Emit("cursor-changed", CursorTypeToString(info),
1401 gfx::Image::CreateFrom1xBitmap(info.custom_image),
1402 info.image_scale_factor,
1403 gfx::Size(info.custom_image.width(), info.custom_image.height()),
1406 Emit("cursor-changed", CursorTypeToString(info));
1410 void WebContents::SetSize(const SetSizeParams& params) {
1411 if (guest_delegate_)
1412 guest_delegate_->SetSize(params);
1415 bool WebContents::IsGuest() const {
1416 return type_ == WEB_VIEW;
1419 bool WebContents::IsOffScreen() const {
1420 return type_ == OFF_SCREEN;
1423 void WebContents::OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap) {
1424 mate::Handle<NativeImage> image =
1425 NativeImage::Create(isolate(), gfx::Image::CreateFrom1xBitmap(bitmap));
1426 Emit("paint", dirty_rect, image);
1429 void WebContents::StartPainting() {
1433 auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1434 web_contents()->GetRenderWidgetHostView());
1436 osr_rwhv->SetPainting(true);
1439 void WebContents::StopPainting() {
1443 auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1444 web_contents()->GetRenderWidgetHostView());
1446 osr_rwhv->SetPainting(false);
1449 bool WebContents::IsPainting() const {
1453 const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1454 web_contents()->GetRenderWidgetHostView());
1455 return osr_rwhv && osr_rwhv->IsPainting();
1458 void WebContents::SetFrameRate(int frame_rate) {
1462 auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1463 web_contents()->GetRenderWidgetHostView());
1465 osr_rwhv->SetFrameRate(frame_rate);
1468 int WebContents::GetFrameRate() const {
1472 const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1473 web_contents()->GetRenderWidgetHostView());
1474 return osr_rwhv ? osr_rwhv->GetFrameRate() : 0;
1477 void WebContents::Invalidate() {
1481 auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
1482 web_contents()->GetRenderWidgetHostView());
1484 osr_rwhv->Invalidate();
1487 v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
1488 WebContentsPreferences* web_preferences =
1489 WebContentsPreferences::FromWebContents(web_contents());
1490 return mate::ConvertToV8(isolate, *web_preferences->web_preferences());
1493 v8::Local<v8::Value> WebContents::GetOwnerBrowserWindow() {
1495 return Window::From(isolate(), owner_window());
1497 return v8::Null(isolate());
1500 int32_t WebContents::ID() const {
1501 return weak_map_id();
1504 v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
1505 return v8::Local<v8::Value>::New(isolate, session_);
1508 content::WebContents* WebContents::HostWebContents() {
1511 return embedder_->web_contents();
1514 void WebContents::SetEmbedder(const WebContents* embedder) {
1516 NativeWindow* owner_window = nullptr;
1517 auto relay = NativeWindowRelay::FromWebContents(embedder->web_contents());
1519 owner_window = relay->window.get();
1522 SetOwnerWindow(owner_window);
1524 content::RenderWidgetHostView* rwhv =
1525 web_contents()->GetRenderWidgetHostView();
1533 v8::Local<v8::Value> WebContents::DevToolsWebContents(v8::Isolate* isolate) {
1534 if (devtools_web_contents_.IsEmpty())
1535 return v8::Null(isolate);
1537 return v8::Local<v8::Value>::New(isolate, devtools_web_contents_);
1540 v8::Local<v8::Value> WebContents::Debugger(v8::Isolate* isolate) {
1541 if (debugger_.IsEmpty()) {
1542 auto handle = atom::api::Debugger::Create(isolate, web_contents());
1543 debugger_.Reset(isolate, handle.ToV8());
1545 return v8::Local<v8::Value>::New(isolate, debugger_);
1549 void WebContents::BuildPrototype(v8::Isolate* isolate,
1550 v8::Local<v8::FunctionTemplate> prototype) {
1551 prototype->SetClassName(mate::StringToV8(isolate, "WebContents"));
1552 mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
1554 .SetMethod("getId", &WebContents::GetID)
1555 .SetMethod("getProcessId", &WebContents::GetProcessID)
1556 .SetMethod("equal", &WebContents::Equal)
1557 .SetMethod("_loadURL", &WebContents::LoadURL)
1558 .SetMethod("downloadURL", &WebContents::DownloadURL)
1559 .SetMethod("_getURL", &WebContents::GetURL)
1560 .SetMethod("getTitle", &WebContents::GetTitle)
1561 .SetMethod("isLoading", &WebContents::IsLoading)
1562 .SetMethod("isLoadingMainFrame", &WebContents::IsLoadingMainFrame)
1563 .SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse)
1564 .SetMethod("_stop", &WebContents::Stop)
1565 .SetMethod("_goBack", &WebContents::GoBack)
1566 .SetMethod("_goForward", &WebContents::GoForward)
1567 .SetMethod("_goToOffset", &WebContents::GoToOffset)
1568 .SetMethod("isCrashed", &WebContents::IsCrashed)
1569 .SetMethod("setUserAgent", &WebContents::SetUserAgent)
1570 .SetMethod("getUserAgent", &WebContents::GetUserAgent)
1571 .SetMethod("savePage", &WebContents::SavePage)
1572 .SetMethod("openDevTools", &WebContents::OpenDevTools)
1573 .SetMethod("closeDevTools", &WebContents::CloseDevTools)
1574 .SetMethod("isDevToolsOpened", &WebContents::IsDevToolsOpened)
1575 .SetMethod("isDevToolsFocused", &WebContents::IsDevToolsFocused)
1576 .SetMethod("enableDeviceEmulation",
1577 &WebContents::EnableDeviceEmulation)
1578 .SetMethod("disableDeviceEmulation",
1579 &WebContents::DisableDeviceEmulation)
1580 .SetMethod("toggleDevTools", &WebContents::ToggleDevTools)
1581 .SetMethod("inspectElement", &WebContents::InspectElement)
1582 .SetMethod("setAudioMuted", &WebContents::SetAudioMuted)
1583 .SetMethod("isAudioMuted", &WebContents::IsAudioMuted)
1584 .SetMethod("undo", &WebContents::Undo)
1585 .SetMethod("redo", &WebContents::Redo)
1586 .SetMethod("cut", &WebContents::Cut)
1587 .SetMethod("copy", &WebContents::Copy)
1588 .SetMethod("paste", &WebContents::Paste)
1589 .SetMethod("pasteAndMatchStyle", &WebContents::PasteAndMatchStyle)
1590 .SetMethod("delete", &WebContents::Delete)
1591 .SetMethod("selectAll", &WebContents::SelectAll)
1592 .SetMethod("unselect", &WebContents::Unselect)
1593 .SetMethod("replace", &WebContents::Replace)
1594 .SetMethod("replaceMisspelling", &WebContents::ReplaceMisspelling)
1595 .SetMethod("findInPage", &WebContents::FindInPage)
1596 .SetMethod("stopFindInPage", &WebContents::StopFindInPage)
1597 .SetMethod("focus", &WebContents::Focus)
1598 .SetMethod("isFocused", &WebContents::IsFocused)
1599 .SetMethod("tabTraverse", &WebContents::TabTraverse)
1600 .SetMethod("_send", &WebContents::SendIPCMessage)
1601 .SetMethod("sendInputEvent", &WebContents::SendInputEvent)
1602 .SetMethod("beginFrameSubscription",
1603 &WebContents::BeginFrameSubscription)
1604 .SetMethod("endFrameSubscription", &WebContents::EndFrameSubscription)
1605 .SetMethod("startDrag", &WebContents::StartDrag)
1606 .SetMethod("setSize", &WebContents::SetSize)
1607 .SetMethod("isGuest", &WebContents::IsGuest)
1608 .SetMethod("isOffscreen", &WebContents::IsOffScreen)
1609 .SetMethod("startPainting", &WebContents::StartPainting)
1610 .SetMethod("stopPainting", &WebContents::StopPainting)
1611 .SetMethod("isPainting", &WebContents::IsPainting)
1612 .SetMethod("setFrameRate", &WebContents::SetFrameRate)
1613 .SetMethod("getFrameRate", &WebContents::GetFrameRate)
1614 .SetMethod("invalidate", &WebContents::Invalidate)
1615 .SetMethod("getType", &WebContents::GetType)
1616 .SetMethod("getWebPreferences", &WebContents::GetWebPreferences)
1617 .SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow)
1618 .SetMethod("hasServiceWorker", &WebContents::HasServiceWorker)
1619 .SetMethod("unregisterServiceWorker",
1620 &WebContents::UnregisterServiceWorker)
1621 .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
1622 .SetMethod("print", &WebContents::Print)
1623 .SetMethod("_printToPDF", &WebContents::PrintToPDF)
1624 .SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
1625 .SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
1626 .SetMethod("showDefinitionForSelection",
1627 &WebContents::ShowDefinitionForSelection)
1628 .SetMethod("copyImageAt", &WebContents::CopyImageAt)
1629 .SetMethod("capturePage", &WebContents::CapturePage)
1630 .SetMethod("setEmbedder", &WebContents::SetEmbedder)
1631 .SetProperty("id", &WebContents::ID)
1632 .SetProperty("session", &WebContents::Session)
1633 .SetProperty("hostWebContents", &WebContents::HostWebContents)
1634 .SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
1635 .SetProperty("debugger", &WebContents::Debugger);
1638 AtomBrowserContext* WebContents::GetBrowserContext() const {
1639 return static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext());
1642 void WebContents::OnRendererMessage(const base::string16& channel,
1643 const base::ListValue& args) {
1644 // webContents.emit(channel, new Event(), args...);
1645 Emit(base::UTF16ToUTF8(channel), args);
1648 void WebContents::OnRendererMessageSync(const base::string16& channel,
1649 const base::ListValue& args,
1650 IPC::Message* message) {
1651 // webContents.emit(channel, new Event(sender, message), args...);
1652 EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args);
1656 mate::Handle<WebContents> WebContents::CreateFrom(
1657 v8::Isolate* isolate, content::WebContents* web_contents) {
1658 // We have an existing WebContents object in JS.
1659 auto existing = TrackableObject::FromWrappedClass(isolate, web_contents);
1661 return mate::CreateHandle(isolate, static_cast<WebContents*>(existing));
1663 // Otherwise create a new WebContents wrapper object.
1664 return mate::CreateHandle(isolate, new WebContents(isolate, web_contents,
1668 mate::Handle<WebContents> WebContents::CreateFrom(
1669 v8::Isolate* isolate, content::WebContents* web_contents, Type type) {
1670 // Otherwise create a new WebContents wrapper object.
1671 return mate::CreateHandle(isolate, new WebContents(isolate, web_contents,
1676 mate::Handle<WebContents> WebContents::Create(
1677 v8::Isolate* isolate, const mate::Dictionary& options) {
1678 return mate::CreateHandle(isolate, new WebContents(isolate, options));
1687 using atom::api::WebContents;
1689 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
1690 v8::Local<v8::Context> context, void* priv) {
1691 v8::Isolate* isolate = context->GetIsolate();
1692 mate::Dictionary dict(isolate, exports);
1693 dict.Set("WebContents", WebContents::GetConstructor(isolate)->GetFunction());
1694 dict.SetMethod("create", &WebContents::Create);
1695 dict.SetMethod("fromId", &mate::TrackableObject<WebContents>::FromWeakMapID);
1696 dict.SetMethod("getAllWebContents",
1697 &mate::TrackableObject<WebContents>::GetAll);
1702 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_web_contents, Initialize)