2014-10-31 18:17:05 +00:00
|
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2014-04-24 08:45:25 +00:00
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
|
#include "shell/browser/api/electron_api_web_contents.h"
|
2014-04-24 08:45:25 +00:00
|
|
|
|
|
2020-03-13 23:13:05 +00:00
|
|
|
|
#include <limits>
|
2018-09-13 00:25:56 +00:00
|
|
|
|
#include <memory>
|
2015-04-25 04:43:52 +00:00
|
|
|
|
#include <set>
|
2015-12-11 15:54:32 +00:00
|
|
|
|
#include <string>
|
2020-03-12 01:07:54 +00:00
|
|
|
|
#include <unordered_set>
|
2018-09-13 00:25:56 +00:00
|
|
|
|
#include <utility>
|
2019-10-02 12:38:27 +00:00
|
|
|
|
#include <vector>
|
2015-04-25 04:43:52 +00:00
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
#include "base/containers/id_map.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "base/files/file_util.h"
|
|
|
|
|
#include "base/json/json_reader.h"
|
2019-01-10 16:28:15 +00:00
|
|
|
|
#include "base/no_destructor.h"
|
2019-05-21 18:34:04 +00:00
|
|
|
|
#include "base/optional.h"
|
2014-04-25 08:13:16 +00:00
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2020-07-22 05:34:34 +00:00
|
|
|
|
#include "base/task/current_thread.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "base/task/post_task.h"
|
|
|
|
|
#include "base/task/thread_pool.h"
|
|
|
|
|
#include "base/threading/scoped_blocking_call.h"
|
|
|
|
|
#include "base/threading/sequenced_task_runner_handle.h"
|
2018-09-18 18:00:31 +00:00
|
|
|
|
#include "base/threading/thread_restrictions.h"
|
2016-11-30 07:30:03 +00:00
|
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2017-02-06 09:18:04 +00:00
|
|
|
|
#include "base/values.h"
|
2018-04-02 03:47:00 +00:00
|
|
|
|
#include "chrome/browser/browser_process.h"
|
2017-01-24 01:51:11 +00:00
|
|
|
|
#include "chrome/browser/ssl/security_state_tab_helper.h"
|
2021-01-21 21:02:29 +00:00
|
|
|
|
#include "chrome/browser/ui/views/eye_dropper/eye_dropper.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "chrome/common/pref_names.h"
|
|
|
|
|
#include "components/prefs/pref_service.h"
|
|
|
|
|
#include "components/prefs/scoped_user_pref_update.h"
|
|
|
|
|
#include "components/security_state/content/content_utils.h"
|
|
|
|
|
#include "components/security_state/core/security_state.h"
|
2020-09-21 08:00:36 +00:00
|
|
|
|
#include "content/browser/renderer_host/frame_tree_node.h" // nogncheck
|
|
|
|
|
#include "content/browser/renderer_host/render_frame_host_manager.h" // nogncheck
|
2019-03-05 05:08:55 +00:00
|
|
|
|
#include "content/browser/renderer_host/render_widget_host_impl.h" // nogncheck
|
|
|
|
|
#include "content/browser/renderer_host/render_widget_host_view_base.h" // nogncheck
|
2017-06-30 22:47:41 +00:00
|
|
|
|
#include "content/public/browser/child_process_security_policy.h"
|
2020-03-14 20:54:14 +00:00
|
|
|
|
#include "content/public/browser/context_menu_params.h"
|
2018-04-12 07:30:51 +00:00
|
|
|
|
#include "content/public/browser/download_request_utils.h"
|
2015-04-20 06:50:04 +00:00
|
|
|
|
#include "content/public/browser/favicon_status.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "content/public/browser/file_select_listener.h"
|
2015-09-18 06:20:31 +00:00
|
|
|
|
#include "content/public/browser/native_web_keyboard_event.h"
|
2014-12-09 22:38:43 +00:00
|
|
|
|
#include "content/public/browser/navigation_details.h"
|
2015-03-05 14:39:27 +00:00
|
|
|
|
#include "content/public/browser/navigation_entry.h"
|
2016-07-14 00:19:28 +00:00
|
|
|
|
#include "content/public/browser/navigation_handle.h"
|
2015-05-12 19:35:56 +00:00
|
|
|
|
#include "content/public/browser/plugin_service.h"
|
2014-07-28 07:29:51 +00:00
|
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2018-03-16 07:28:06 +00:00
|
|
|
|
#include "content/public/browser/render_process_host.h"
|
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
2016-03-08 14:28:53 +00:00
|
|
|
|
#include "content/public/browser/render_widget_host.h"
|
2015-09-18 06:20:31 +00:00
|
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "content/public/browser/security_style_explanation.h"
|
|
|
|
|
#include "content/public/browser/security_style_explanations.h"
|
2015-04-30 13:15:19 +00:00
|
|
|
|
#include "content/public/browser/service_worker_context.h"
|
2014-10-24 12:20:56 +00:00
|
|
|
|
#include "content/public/browser/site_instance.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
|
#include "content/public/browser/storage_partition.h"
|
2014-04-24 08:45:25 +00:00
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2020-03-26 18:05:45 +00:00
|
|
|
|
#include "content/public/common/referrer_type_converters.h"
|
2020-11-14 00:16:56 +00:00
|
|
|
|
#include "content/public/common/webplugininfo.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
|
#include "electron/buildflags/buildflags.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "electron/shell/common/api/api.mojom.h"
|
2020-07-20 18:07:02 +00:00
|
|
|
|
#include "gin/arguments.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
|
#include "gin/data_object_builder.h"
|
|
|
|
|
#include "gin/handle.h"
|
|
|
|
|
#include "gin/object_template_builder.h"
|
|
|
|
|
#include "gin/wrappable.h"
|
2019-10-28 22:12:35 +00:00
|
|
|
|
#include "mojo/public/cpp/bindings/associated_remote.h"
|
2020-06-22 17:35:10 +00:00
|
|
|
|
#include "mojo/public/cpp/bindings/pending_receiver.h"
|
|
|
|
|
#include "mojo/public/cpp/bindings/remote.h"
|
2019-04-02 22:38:16 +00:00
|
|
|
|
#include "mojo/public/cpp/system/platform_handle.h"
|
2019-10-09 17:59:37 +00:00
|
|
|
|
#include "ppapi/buildflags/buildflags.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "printing/buildflags/buildflags.h"
|
2021-02-09 20:16:21 +00:00
|
|
|
|
#include "services/service_manager/public/cpp/interface_provider.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
|
#include "shell/browser/api/electron_api_browser_window.h"
|
|
|
|
|
#include "shell/browser/api/electron_api_debugger.h"
|
|
|
|
|
#include "shell/browser/api/electron_api_session.h"
|
2020-10-09 16:50:46 +00:00
|
|
|
|
#include "shell/browser/api/electron_api_web_frame_main.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
|
#include "shell/browser/api/message_port.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/browser/browser.h"
|
|
|
|
|
#include "shell/browser/child_web_contents_tracker.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
|
#include "shell/browser/electron_autofill_driver_factory.h"
|
|
|
|
|
#include "shell/browser/electron_browser_client.h"
|
|
|
|
|
#include "shell/browser/electron_browser_context.h"
|
|
|
|
|
#include "shell/browser/electron_browser_main_parts.h"
|
|
|
|
|
#include "shell/browser/electron_javascript_dialog_manager.h"
|
|
|
|
|
#include "shell/browser/electron_navigation_throttle.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/browser/lib/bluetooth_chooser.h"
|
|
|
|
|
#include "shell/browser/native_window.h"
|
2019-07-03 15:05:45 +00:00
|
|
|
|
#include "shell/browser/session_preferences.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/browser/ui/drag_util.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "shell/browser/ui/file_dialog.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/browser/ui/inspectable_web_contents.h"
|
|
|
|
|
#include "shell/browser/ui/inspectable_web_contents_view.h"
|
|
|
|
|
#include "shell/browser/web_contents_permission_helper.h"
|
|
|
|
|
#include "shell/browser/web_contents_preferences.h"
|
|
|
|
|
#include "shell/browser/web_contents_zoom_controller.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "shell/browser/web_dialog_helper.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/browser/web_view_guest_delegate.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
|
#include "shell/common/api/electron_api_native_image.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/common/color_util.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "shell/common/electron_constants.h"
|
2020-11-17 19:13:58 +00:00
|
|
|
|
#include "shell/common/gin_converters/base_converter.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
|
#include "shell/common/gin_converters/blink_converter.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
|
#include "shell/common/gin_converters/content_converter.h"
|
|
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
2020-10-09 16:50:46 +00:00
|
|
|
|
#include "shell/common/gin_converters/frame_converter.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
|
#include "shell/common/gin_converters/gfx_converter.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
|
|
|
|
#include "shell/common/gin_converters/image_converter.h"
|
|
|
|
|
#include "shell/common/gin_converters/net_converter.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
|
#include "shell/common/gin_converters/value_converter.h"
|
2019-10-18 00:31:29 +00:00
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2020-05-04 17:49:29 +00:00
|
|
|
|
#include "shell/common/language_util.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/common/mouse_util.h"
|
|
|
|
|
#include "shell/common/node_includes.h"
|
|
|
|
|
#include "shell/common/options_switches.h"
|
2021-01-13 09:01:51 +00:00
|
|
|
|
#include "shell/common/process_util.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
|
#include "shell/common/v8_value_serializer.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "storage/browser/file_system/isolated_context.h"
|
2019-04-02 22:38:16 +00:00
|
|
|
|
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
|
2020-01-17 18:41:52 +00:00
|
|
|
|
#include "third_party/blink/public/common/input/web_input_event.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
|
#include "third_party/blink/public/common/messaging/transferable_message_mojom_traits.h"
|
2019-10-18 19:57:34 +00:00
|
|
|
|
#include "third_party/blink/public/common/page/page_zoom.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
|
#include "third_party/blink/public/mojom/frame/find_in_page.mojom.h"
|
2019-10-28 22:12:35 +00:00
|
|
|
|
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
|
#include "third_party/blink/public/mojom/messaging/transferable_message.mojom.h"
|
2020-05-04 17:49:29 +00:00
|
|
|
|
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
|
2020-03-11 11:15:07 +00:00
|
|
|
|
#include "ui/base/cursor/cursor.h"
|
2020-05-26 20:06:26 +00:00
|
|
|
|
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
|
2016-07-21 12:03:38 +00:00
|
|
|
|
#include "ui/display/screen.h"
|
2017-04-13 10:21:30 +00:00
|
|
|
|
#include "ui/events/base_event_utils.h"
|
2014-04-24 08:45:25 +00:00
|
|
|
|
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2019-06-19 20:46:59 +00:00
|
|
|
|
#include "shell/browser/osr/osr_render_widget_host_view.h"
|
|
|
|
|
#include "shell/browser/osr/osr_web_contents_view.h"
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
|
#if !defined(OS_MAC)
|
2016-08-15 21:13:24 +00:00
|
|
|
|
#include "ui/aura/window.h"
|
2019-08-30 14:39:46 +00:00
|
|
|
|
#else
|
|
|
|
|
#include "ui/base/cocoa/defaults_utils.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
|
#include "ui/views/linux_ui/linux_ui.h"
|
2016-08-15 21:13:24 +00:00
|
|
|
|
#endif
|
2014-10-23 06:04:13 +00:00
|
|
|
|
|
2018-02-22 08:56:48 +00:00
|
|
|
|
#if defined(OS_LINUX) || defined(OS_WIN)
|
|
|
|
|
#include "ui/gfx/font_render_params.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
2020-03-05 14:56:21 +00:00
|
|
|
|
#include "extensions/browser/script_executor.h"
|
2020-09-08 11:55:40 +00:00
|
|
|
|
#include "extensions/browser/view_type_utils.h"
|
2020-02-03 22:01:10 +00:00
|
|
|
|
#include "shell/browser/extensions/electron_extension_web_contents_observer.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-08-15 01:51:28 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "chrome/browser/printing/print_view_manager_basic.h"
|
|
|
|
|
#include "components/printing/browser/print_manager_utils.h"
|
2020-11-17 22:14:09 +00:00
|
|
|
|
#include "printing/backend/print_backend.h" // nogncheck
|
2020-08-15 01:51:28 +00:00
|
|
|
|
#include "printing/mojom/print.mojom.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#include "shell/browser/printing/print_preview_message_handler.h"
|
2020-11-17 22:14:09 +00:00
|
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
|
#include "printing/backend/win_helper.h"
|
|
|
|
|
#endif
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_COLOR_CHOOSER)
|
|
|
|
|
#include "chrome/browser/ui/color_chooser.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PICTURE_IN_PICTURE)
|
|
|
|
|
#include "chrome/browser/picture_in_picture/picture_in_picture_window_manager.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
|
|
|
#include "components/pdf/browser/pdf_web_contents_helper.h" // nogncheck
|
|
|
|
|
#include "shell/browser/electron_pdf_web_contents_helper_client.h"
|
2020-08-15 01:51:28 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-11-16 17:47:09 +00:00
|
|
|
|
#ifndef MAS_BUILD
|
|
|
|
|
#include "chrome/browser/hang_monitor/hang_crash_dump.h" // nogncheck
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
|
namespace gin {
|
2015-05-29 03:12:55 +00:00
|
|
|
|
|
2018-11-09 03:42:34 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2019-06-30 05:12:00 +00:00
|
|
|
|
template <>
|
2020-08-15 01:51:28 +00:00
|
|
|
|
struct Converter<printing::mojom::MarginType> {
|
2019-06-30 05:12:00 +00:00
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
|
v8::Local<v8::Value> val,
|
2020-08-15 01:51:28 +00:00
|
|
|
|
printing::mojom::MarginType* out) {
|
2019-06-30 05:12:00 +00:00
|
|
|
|
std::string type;
|
|
|
|
|
if (ConvertFromV8(isolate, val, &type)) {
|
|
|
|
|
if (type == "default") {
|
2020-08-15 01:51:28 +00:00
|
|
|
|
*out = printing::mojom::MarginType::kDefaultMargins;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (type == "none") {
|
2020-08-15 01:51:28 +00:00
|
|
|
|
*out = printing::mojom::MarginType::kNoMargins;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (type == "printableArea") {
|
2020-08-15 01:51:28 +00:00
|
|
|
|
*out = printing::mojom::MarginType::kPrintableAreaMargins;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (type == "custom") {
|
2020-08-15 01:51:28 +00:00
|
|
|
|
*out = printing::mojom::MarginType::kCustomMargins;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <>
|
2020-05-26 20:06:26 +00:00
|
|
|
|
struct Converter<printing::mojom::DuplexMode> {
|
2019-06-30 05:12:00 +00:00
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
|
v8::Local<v8::Value> val,
|
2020-05-26 20:06:26 +00:00
|
|
|
|
printing::mojom::DuplexMode* out) {
|
2019-06-30 05:12:00 +00:00
|
|
|
|
std::string mode;
|
|
|
|
|
if (ConvertFromV8(isolate, val, &mode)) {
|
|
|
|
|
if (mode == "simplex") {
|
2020-05-26 20:06:26 +00:00
|
|
|
|
*out = printing::mojom::DuplexMode::kSimplex;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (mode == "longEdge") {
|
2020-05-26 20:06:26 +00:00
|
|
|
|
*out = printing::mojom::DuplexMode::kLongEdge;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (mode == "shortEdge") {
|
2020-05-26 20:06:26 +00:00
|
|
|
|
*out = printing::mojom::DuplexMode::kShortEdge;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-09 03:42:34 +00:00
|
|
|
|
#endif
|
2017-02-06 09:18:04 +00:00
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
template <>
|
2015-06-23 08:34:22 +00:00
|
|
|
|
struct Converter<WindowOpenDisposition> {
|
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
|
WindowOpenDisposition val) {
|
|
|
|
|
std::string disposition = "other";
|
|
|
|
|
switch (val) {
|
2017-01-24 07:50:02 +00:00
|
|
|
|
case WindowOpenDisposition::CURRENT_TAB:
|
|
|
|
|
disposition = "default";
|
|
|
|
|
break;
|
|
|
|
|
case WindowOpenDisposition::NEW_FOREGROUND_TAB:
|
|
|
|
|
disposition = "foreground-tab";
|
|
|
|
|
break;
|
|
|
|
|
case WindowOpenDisposition::NEW_BACKGROUND_TAB:
|
|
|
|
|
disposition = "background-tab";
|
|
|
|
|
break;
|
|
|
|
|
case WindowOpenDisposition::NEW_POPUP:
|
|
|
|
|
case WindowOpenDisposition::NEW_WINDOW:
|
|
|
|
|
disposition = "new-window";
|
|
|
|
|
break;
|
|
|
|
|
case WindowOpenDisposition::SAVE_TO_DISK:
|
|
|
|
|
disposition = "save-to-disk";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2015-06-23 08:34:22 +00:00
|
|
|
|
}
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::ConvertToV8(isolate, disposition);
|
2015-06-23 08:34:22 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
template <>
|
2015-10-14 04:41:31 +00:00
|
|
|
|
struct Converter<content::SavePageType> {
|
2018-04-18 01:55:30 +00:00
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
|
v8::Local<v8::Value> val,
|
2015-10-14 04:41:31 +00:00
|
|
|
|
content::SavePageType* out) {
|
|
|
|
|
std::string save_type;
|
|
|
|
|
if (!ConvertFromV8(isolate, val, &save_type))
|
|
|
|
|
return false;
|
2015-12-07 11:56:23 +00:00
|
|
|
|
save_type = base::ToLowerASCII(save_type);
|
2015-11-02 13:19:00 +00:00
|
|
|
|
if (save_type == "htmlonly") {
|
2015-10-14 04:41:31 +00:00
|
|
|
|
*out = content::SAVE_PAGE_TYPE_AS_ONLY_HTML;
|
2015-11-02 13:19:00 +00:00
|
|
|
|
} else if (save_type == "htmlcomplete") {
|
2015-10-14 04:41:31 +00:00
|
|
|
|
*out = content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML;
|
2015-11-02 13:19:00 +00:00
|
|
|
|
} else if (save_type == "mhtml") {
|
2015-10-14 04:41:31 +00:00
|
|
|
|
*out = content::SAVE_PAGE_TYPE_AS_MHTML;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
template <>
|
2019-06-19 21:23:04 +00:00
|
|
|
|
struct Converter<electron::api::WebContents::Type> {
|
2016-06-14 16:09:54 +00:00
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
2019-06-19 21:23:04 +00:00
|
|
|
|
electron::api::WebContents::Type val) {
|
|
|
|
|
using Type = electron::api::WebContents::Type;
|
2019-09-13 14:26:59 +00:00
|
|
|
|
std::string type;
|
2016-06-14 16:09:54 +00:00
|
|
|
|
switch (val) {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
case Type::kBackgroundPage:
|
2018-04-18 01:55:30 +00:00
|
|
|
|
type = "backgroundPage";
|
|
|
|
|
break;
|
2020-10-27 17:51:45 +00:00
|
|
|
|
case Type::kBrowserWindow:
|
2018-04-18 01:55:30 +00:00
|
|
|
|
type = "window";
|
|
|
|
|
break;
|
2020-10-27 17:51:45 +00:00
|
|
|
|
case Type::kBrowserView:
|
2018-04-18 01:55:30 +00:00
|
|
|
|
type = "browserView";
|
|
|
|
|
break;
|
2020-10-27 17:51:45 +00:00
|
|
|
|
case Type::kRemote:
|
2018-04-18 01:55:30 +00:00
|
|
|
|
type = "remote";
|
|
|
|
|
break;
|
2020-10-27 17:51:45 +00:00
|
|
|
|
case Type::kWebView:
|
2018-04-18 01:55:30 +00:00
|
|
|
|
type = "webview";
|
|
|
|
|
break;
|
2020-10-27 17:51:45 +00:00
|
|
|
|
case Type::kOffScreen:
|
2018-04-18 01:55:30 +00:00
|
|
|
|
type = "offscreen";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2016-06-14 16:09:54 +00:00
|
|
|
|
}
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::ConvertToV8(isolate, type);
|
2016-06-14 16:09:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
|
v8::Local<v8::Value> val,
|
2019-06-19 21:23:04 +00:00
|
|
|
|
electron::api::WebContents::Type* out) {
|
|
|
|
|
using Type = electron::api::WebContents::Type;
|
2016-06-14 16:23:03 +00:00
|
|
|
|
std::string type;
|
|
|
|
|
if (!ConvertFromV8(isolate, val, &type))
|
|
|
|
|
return false;
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 17:47:30 +00:00
|
|
|
|
if (type == "backgroundPage") {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
*out = Type::kBackgroundPage;
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 17:47:30 +00:00
|
|
|
|
} else if (type == "browserView") {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
*out = Type::kBrowserView;
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 17:47:30 +00:00
|
|
|
|
} else if (type == "webview") {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
*out = Type::kWebView;
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2016-07-28 10:10:56 +00:00
|
|
|
|
} else if (type == "offscreen") {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
*out = Type::kOffScreen;
|
2017-06-26 09:13:05 +00:00
|
|
|
|
#endif
|
2016-06-14 16:23:03 +00:00
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-06-14 16:09:54 +00:00
|
|
|
|
|
2019-10-02 12:38:27 +00:00
|
|
|
|
template <>
|
|
|
|
|
struct Converter<scoped_refptr<content::DevToolsAgentHost>> {
|
|
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
|
const scoped_refptr<content::DevToolsAgentHost>& val) {
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
|
2019-10-02 12:38:27 +00:00
|
|
|
|
dict.Set("id", val->GetId());
|
|
|
|
|
dict.Set("url", val->GetURL().spec());
|
|
|
|
|
return dict.GetHandle();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
|
} // namespace gin
|
2015-05-29 03:12:55 +00:00
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
|
namespace electron {
|
2014-04-24 08:45:25 +00:00
|
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
2014-10-23 05:31:10 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
base::IDMap<WebContents*>& GetAllWebContents() {
|
|
|
|
|
static base::NoDestructor<base::IDMap<WebContents*>> s_all_web_contents;
|
|
|
|
|
return *s_all_web_contents;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 16:32:58 +00:00
|
|
|
|
// Called when CapturePage is done.
|
2019-11-01 06:10:32 +00:00
|
|
|
|
void OnCapturePageDone(gin_helper::Promise<gfx::Image> promise,
|
2019-08-23 00:03:28 +00:00
|
|
|
|
const SkBitmap& bitmap) {
|
2018-04-10 10:07:15 +00:00
|
|
|
|
// Hack to enable transparency in captured image
|
2019-11-01 06:10:32 +00:00
|
|
|
|
promise.Resolve(gfx::Image::CreateFrom1xBitmap(bitmap));
|
2016-07-06 16:32:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 18:43:21 +00:00
|
|
|
|
base::Optional<base::TimeDelta> GetCursorBlinkInterval() {
|
2020-08-12 18:33:58 +00:00
|
|
|
|
#if defined(OS_MAC)
|
2019-09-19 18:43:21 +00:00
|
|
|
|
base::TimeDelta interval;
|
|
|
|
|
if (ui::TextInsertionCaretBlinkPeriod(&interval))
|
|
|
|
|
return interval;
|
|
|
|
|
#elif defined(OS_LINUX)
|
|
|
|
|
if (auto* linux_ui = views::LinuxUI::instance())
|
|
|
|
|
return linux_ui->GetCursorBlinkInterval();
|
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
const auto system_msec = ::GetCaretBlinkTime();
|
|
|
|
|
if (system_msec != 0) {
|
|
|
|
|
return (system_msec == INFINITE)
|
|
|
|
|
? base::TimeDelta()
|
|
|
|
|
: base::TimeDelta::FromMilliseconds(system_msec);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return base::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 02:49:13 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
|
|
|
|
// This will return false if no printer with the provided device_name can be
|
|
|
|
|
// found on the network. We need to check this because Chromium does not do
|
|
|
|
|
// sanity checking of device_name validity and so will crash on invalid names.
|
|
|
|
|
bool IsDeviceNameValid(const base::string16& device_name) {
|
2020-08-12 18:33:58 +00:00
|
|
|
|
#if defined(OS_MAC)
|
2020-01-31 02:49:13 +00:00
|
|
|
|
base::ScopedCFTypeRef<CFStringRef> new_printer_id(
|
|
|
|
|
base::SysUTF16ToCFStringRef(device_name));
|
|
|
|
|
PMPrinter new_printer = PMPrinterCreateFromPrinterID(new_printer_id.get());
|
|
|
|
|
bool printer_exists = new_printer != nullptr;
|
|
|
|
|
PMRelease(new_printer);
|
|
|
|
|
return printer_exists;
|
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
printing::ScopedPrinterHandle printer;
|
|
|
|
|
return printer.OpenPrinterWithName(device_name.c_str());
|
|
|
|
|
#endif
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-01-31 19:20:11 +00:00
|
|
|
|
|
|
|
|
|
base::string16 GetDefaultPrinterAsync() {
|
2021-01-13 17:17:11 +00:00
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
|
// Blocking is needed here because Windows printer drivers are oftentimes
|
|
|
|
|
// not thread-safe and have to be accessed on the UI thread.
|
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
|
|
|
|
#endif
|
2020-01-31 19:20:11 +00:00
|
|
|
|
|
2020-09-21 20:42:27 +00:00
|
|
|
|
scoped_refptr<printing::PrintBackend> print_backend =
|
2020-01-31 19:20:11 +00:00
|
|
|
|
printing::PrintBackend::CreateInstance(
|
2020-09-21 08:00:36 +00:00
|
|
|
|
g_browser_process->GetApplicationLocale());
|
2020-09-21 20:42:27 +00:00
|
|
|
|
std::string printer_name = print_backend->GetDefaultPrinterName();
|
|
|
|
|
|
|
|
|
|
// Some devices won't have a default printer, so we should
|
|
|
|
|
// also check for existing printers and pick the first
|
|
|
|
|
// one should it exist.
|
|
|
|
|
if (printer_name.empty()) {
|
|
|
|
|
printing::PrinterList printers;
|
|
|
|
|
print_backend->EnumeratePrinters(&printers);
|
2020-10-22 20:24:59 +00:00
|
|
|
|
if (!printers.empty())
|
2020-09-21 20:42:27 +00:00
|
|
|
|
printer_name = printers.front().printer_name;
|
|
|
|
|
}
|
2020-01-31 19:20:11 +00:00
|
|
|
|
return base::UTF8ToUTF16(printer_name);
|
|
|
|
|
}
|
2021-01-13 17:17:11 +00:00
|
|
|
|
|
|
|
|
|
// Copied from
|
|
|
|
|
// chrome/browser/ui/webui/print_preview/local_printer_handler_default.cc:L36-L54
|
|
|
|
|
scoped_refptr<base::TaskRunner> CreatePrinterHandlerTaskRunner() {
|
|
|
|
|
// USER_VISIBLE because the result is displayed in the print preview dialog.
|
|
|
|
|
#if !defined(OS_WIN)
|
|
|
|
|
static constexpr base::TaskTraits kTraits = {
|
|
|
|
|
base::MayBlock(), base::TaskPriority::USER_VISIBLE};
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(USE_CUPS)
|
|
|
|
|
// CUPS is thread safe.
|
|
|
|
|
return base::ThreadPool::CreateTaskRunner(kTraits);
|
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
// Windows drivers are likely not thread-safe and need to be accessed on the
|
|
|
|
|
// UI thread.
|
|
|
|
|
return content::GetUIThreadTaskRunner(
|
|
|
|
|
{base::MayBlock(), base::TaskPriority::USER_VISIBLE});
|
|
|
|
|
#else
|
|
|
|
|
// Be conservative on unsupported platforms.
|
|
|
|
|
return base::ThreadPool::CreateSingleThreadTaskRunner(kTraits);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2020-01-31 02:49:13 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
struct UserDataLink : public base::SupportsUserData::Data {
|
|
|
|
|
explicit UserDataLink(base::WeakPtr<WebContents> contents)
|
|
|
|
|
: web_contents(contents) {}
|
|
|
|
|
|
|
|
|
|
base::WeakPtr<WebContents> web_contents;
|
|
|
|
|
};
|
|
|
|
|
const void* kElectronApiWebContentsKey = &kElectronApiWebContentsKey;
|
|
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
|
const char kRootName[] = "<root>";
|
|
|
|
|
|
|
|
|
|
struct FileSystem {
|
|
|
|
|
FileSystem() = default;
|
|
|
|
|
FileSystem(const std::string& type,
|
|
|
|
|
const std::string& file_system_name,
|
|
|
|
|
const std::string& root_url,
|
|
|
|
|
const std::string& file_system_path)
|
|
|
|
|
: type(type),
|
|
|
|
|
file_system_name(file_system_name),
|
|
|
|
|
root_url(root_url),
|
|
|
|
|
file_system_path(file_system_path) {}
|
|
|
|
|
|
|
|
|
|
std::string type;
|
|
|
|
|
std::string file_system_name;
|
|
|
|
|
std::string root_url;
|
|
|
|
|
std::string file_system_path;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string RegisterFileSystem(content::WebContents* web_contents,
|
|
|
|
|
const base::FilePath& path) {
|
|
|
|
|
auto* isolated_context = storage::IsolatedContext::GetInstance();
|
|
|
|
|
std::string root_name(kRootName);
|
|
|
|
|
storage::IsolatedContext::ScopedFSHandle file_system =
|
|
|
|
|
isolated_context->RegisterFileSystemForPath(
|
|
|
|
|
storage::kFileSystemTypeNativeLocal, std::string(), path, &root_name);
|
|
|
|
|
|
|
|
|
|
content::ChildProcessSecurityPolicy* policy =
|
|
|
|
|
content::ChildProcessSecurityPolicy::GetInstance();
|
|
|
|
|
content::RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
|
|
|
|
|
int renderer_id = render_view_host->GetProcess()->GetID();
|
|
|
|
|
policy->GrantReadFileSystem(renderer_id, file_system.id());
|
|
|
|
|
policy->GrantWriteFileSystem(renderer_id, file_system.id());
|
|
|
|
|
policy->GrantCreateFileForFileSystem(renderer_id, file_system.id());
|
|
|
|
|
policy->GrantDeleteFromFileSystem(renderer_id, file_system.id());
|
|
|
|
|
|
|
|
|
|
if (!policy->CanReadFile(renderer_id, path))
|
|
|
|
|
policy->GrantReadFile(renderer_id, path);
|
|
|
|
|
|
|
|
|
|
return file_system.id();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileSystem CreateFileSystemStruct(content::WebContents* web_contents,
|
|
|
|
|
const std::string& file_system_id,
|
|
|
|
|
const std::string& file_system_path,
|
|
|
|
|
const std::string& type) {
|
|
|
|
|
const GURL origin = web_contents->GetURL().GetOrigin();
|
|
|
|
|
std::string file_system_name =
|
|
|
|
|
storage::GetIsolatedFileSystemName(origin, file_system_id);
|
|
|
|
|
std::string root_url = storage::GetIsolatedFileSystemRootURIString(
|
|
|
|
|
origin, file_system_id, kRootName);
|
|
|
|
|
return FileSystem(type, file_system_name, root_url, file_system_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<base::DictionaryValue> CreateFileSystemValue(
|
|
|
|
|
const FileSystem& file_system) {
|
|
|
|
|
std::unique_ptr<base::DictionaryValue> file_system_value(
|
|
|
|
|
new base::DictionaryValue());
|
|
|
|
|
file_system_value->SetString("type", file_system.type);
|
|
|
|
|
file_system_value->SetString("fileSystemName", file_system.file_system_name);
|
|
|
|
|
file_system_value->SetString("rootURL", file_system.root_url);
|
|
|
|
|
file_system_value->SetString("fileSystemPath", file_system.file_system_path);
|
|
|
|
|
return file_system_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteToFile(const base::FilePath& path, const std::string& content) {
|
|
|
|
|
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
|
|
|
|
|
base::BlockingType::WILL_BLOCK);
|
|
|
|
|
DCHECK(!path.empty());
|
|
|
|
|
|
|
|
|
|
base::WriteFile(path, content.data(), content.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AppendToFile(const base::FilePath& path, const std::string& content) {
|
|
|
|
|
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
|
|
|
|
|
base::BlockingType::WILL_BLOCK);
|
|
|
|
|
DCHECK(!path.empty());
|
|
|
|
|
|
|
|
|
|
base::AppendToFile(path, content.data(), content.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PrefService* GetPrefService(content::WebContents* web_contents) {
|
|
|
|
|
auto* context = web_contents->GetBrowserContext();
|
|
|
|
|
return static_cast<electron::ElectronBrowserContext*>(context)->prefs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::map<std::string, std::string> GetAddedFileSystemPaths(
|
|
|
|
|
content::WebContents* web_contents) {
|
|
|
|
|
auto* pref_service = GetPrefService(web_contents);
|
|
|
|
|
const base::DictionaryValue* file_system_paths_value =
|
|
|
|
|
pref_service->GetDictionary(prefs::kDevToolsFileSystemPaths);
|
|
|
|
|
std::map<std::string, std::string> result;
|
|
|
|
|
if (file_system_paths_value) {
|
|
|
|
|
base::DictionaryValue::Iterator it(*file_system_paths_value);
|
|
|
|
|
for (; !it.IsAtEnd(); it.Advance()) {
|
|
|
|
|
std::string type =
|
|
|
|
|
it.value().is_string() ? it.value().GetString() : std::string();
|
|
|
|
|
result[it.key()] = type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IsDevToolsFileSystemAdded(content::WebContents* web_contents,
|
|
|
|
|
const std::string& file_system_path) {
|
|
|
|
|
auto file_system_paths = GetAddedFileSystemPaths(web_contents);
|
|
|
|
|
return file_system_paths.find(file_system_path) != file_system_paths.end();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 05:31:10 +00:00
|
|
|
|
} // namespace
|
|
|
|
|
|
2020-09-08 11:55:40 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
|
|
|
|
|
|
WebContents::Type GetTypeFromViewType(extensions::ViewType view_type) {
|
|
|
|
|
switch (view_type) {
|
|
|
|
|
case extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE:
|
2020-10-27 17:51:45 +00:00
|
|
|
|
return WebContents::Type::kBackgroundPage;
|
2020-09-08 11:55:40 +00:00
|
|
|
|
|
|
|
|
|
case extensions::VIEW_TYPE_APP_WINDOW:
|
|
|
|
|
case extensions::VIEW_TYPE_COMPONENT:
|
|
|
|
|
case extensions::VIEW_TYPE_EXTENSION_DIALOG:
|
|
|
|
|
case extensions::VIEW_TYPE_EXTENSION_POPUP:
|
|
|
|
|
case extensions::VIEW_TYPE_BACKGROUND_CONTENTS:
|
|
|
|
|
case extensions::VIEW_TYPE_EXTENSION_GUEST:
|
|
|
|
|
case extensions::VIEW_TYPE_TAB_CONTENTS:
|
|
|
|
|
case extensions::VIEW_TYPE_INVALID:
|
2020-10-27 17:51:45 +00:00
|
|
|
|
return WebContents::Type::kRemote;
|
2020-09-08 11:55:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-10-19 08:52:07 +00:00
|
|
|
|
WebContents::WebContents(v8::Isolate* isolate,
|
|
|
|
|
content::WebContents* web_contents)
|
2019-06-14 02:44:36 +00:00
|
|
|
|
: content::WebContentsObserver(web_contents),
|
2020-10-27 17:51:45 +00:00
|
|
|
|
type_(Type::kRemote),
|
2020-07-30 16:17:57 +00:00
|
|
|
|
id_(GetAllWebContents().Add(this)),
|
2020-11-10 17:06:56 +00:00
|
|
|
|
devtools_file_system_indexer_(new DevToolsFileSystemIndexer),
|
|
|
|
|
file_task_runner_(
|
2021-01-26 18:16:21 +00:00
|
|
|
|
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}))
|
2021-01-25 16:27:25 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2021-01-26 18:16:21 +00:00
|
|
|
|
,
|
|
|
|
|
print_task_runner_(CreatePrinterHandlerTaskRunner())
|
2021-01-25 16:27:25 +00:00
|
|
|
|
#endif
|
2021-01-26 18:16:21 +00:00
|
|
|
|
{
|
2020-09-08 11:55:40 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
|
// WebContents created by extension host will have valid ViewType set.
|
|
|
|
|
extensions::ViewType view_type = extensions::GetViewType(web_contents);
|
|
|
|
|
if (view_type != extensions::VIEW_TYPE_INVALID) {
|
|
|
|
|
InitWithExtensionView(isolate, web_contents, view_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extensions::ElectronExtensionWebContentsObserver::CreateForWebContents(
|
|
|
|
|
web_contents);
|
|
|
|
|
script_executor_.reset(new extensions::ScriptExecutor(web_contents));
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-06-11 03:57:51 +00:00
|
|
|
|
auto session = Session::CreateFrom(isolate, GetBrowserContext());
|
|
|
|
|
session_.Reset(isolate, session.ToV8());
|
|
|
|
|
|
2020-04-13 23:39:26 +00:00
|
|
|
|
web_contents->SetUserAgentOverride(blink::UserAgentOverride::UserAgentOnly(
|
|
|
|
|
GetBrowserContext()->GetUserAgent()),
|
2018-10-19 08:52:07 +00:00
|
|
|
|
false);
|
2020-07-30 16:17:57 +00:00
|
|
|
|
web_contents->SetUserData(kElectronApiWebContentsKey,
|
|
|
|
|
std::make_unique<UserDataLink>(GetWeakPtr()));
|
2019-10-25 13:03:28 +00:00
|
|
|
|
InitZoomController(web_contents, gin::Dictionary::CreateEmpty(isolate));
|
2018-10-19 08:52:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
|
WebContents::WebContents(v8::Isolate* isolate,
|
2018-10-22 18:02:25 +00:00
|
|
|
|
std::unique_ptr<content::WebContents> web_contents,
|
2016-08-17 00:15:10 +00:00
|
|
|
|
Type type)
|
2019-06-14 02:44:36 +00:00
|
|
|
|
: content::WebContentsObserver(web_contents.get()),
|
|
|
|
|
type_(type),
|
2020-07-30 16:17:57 +00:00
|
|
|
|
id_(GetAllWebContents().Add(this)),
|
2020-11-10 17:06:56 +00:00
|
|
|
|
devtools_file_system_indexer_(new DevToolsFileSystemIndexer),
|
|
|
|
|
file_task_runner_(
|
2021-01-26 18:16:21 +00:00
|
|
|
|
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}))
|
2021-01-25 16:27:25 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2021-01-26 18:16:21 +00:00
|
|
|
|
,
|
|
|
|
|
print_task_runner_(CreatePrinterHandlerTaskRunner())
|
2021-01-25 16:27:25 +00:00
|
|
|
|
#endif
|
2021-01-26 18:16:21 +00:00
|
|
|
|
{
|
2020-10-27 17:51:45 +00:00
|
|
|
|
DCHECK(type != Type::kRemote)
|
2019-05-03 18:11:41 +00:00
|
|
|
|
<< "Can't take ownership of a remote WebContents";
|
2018-10-19 08:52:07 +00:00
|
|
|
|
auto session = Session::CreateFrom(isolate, GetBrowserContext());
|
|
|
|
|
session_.Reset(isolate, session.ToV8());
|
2018-10-22 18:02:25 +00:00
|
|
|
|
InitWithSessionAndOptions(isolate, std::move(web_contents), session,
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Dictionary::CreateEmpty(isolate));
|
2014-10-23 06:04:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
|
WebContents::WebContents(v8::Isolate* isolate,
|
|
|
|
|
const gin_helper::Dictionary& options)
|
2020-11-10 17:06:56 +00:00
|
|
|
|
: id_(GetAllWebContents().Add(this)),
|
|
|
|
|
devtools_file_system_indexer_(new DevToolsFileSystemIndexer),
|
|
|
|
|
file_task_runner_(
|
2021-01-26 18:16:21 +00:00
|
|
|
|
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}))
|
2021-01-25 16:27:25 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2021-01-26 18:16:21 +00:00
|
|
|
|
,
|
|
|
|
|
print_task_runner_(CreatePrinterHandlerTaskRunner())
|
2021-01-25 16:27:25 +00:00
|
|
|
|
#endif
|
2021-01-26 18:16:21 +00:00
|
|
|
|
{
|
2016-04-08 06:54:33 +00:00
|
|
|
|
// Read options.
|
|
|
|
|
options.Get("backgroundThrottling", &background_throttling_);
|
|
|
|
|
|
2019-05-20 10:55:46 +00:00
|
|
|
|
// Get type
|
|
|
|
|
options.Get("type", &type_);
|
|
|
|
|
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2019-07-31 12:34:04 +00:00
|
|
|
|
bool b = false;
|
2019-05-20 10:55:46 +00:00
|
|
|
|
if (options.Get(options::kOffscreen, &b) && b)
|
2020-10-27 17:51:45 +00:00
|
|
|
|
type_ = Type::kOffScreen;
|
2017-06-26 09:13:05 +00:00
|
|
|
|
#endif
|
2015-06-24 15:29:32 +00:00
|
|
|
|
|
2017-03-04 02:09:16 +00:00
|
|
|
|
// Init embedder earlier
|
|
|
|
|
options.Get("embedder", &embedder_);
|
|
|
|
|
|
2016-09-07 02:16:52 +00:00
|
|
|
|
// Whether to enable DevTools.
|
|
|
|
|
options.Get("devTools", &enable_devtools_);
|
2016-09-05 08:27:56 +00:00
|
|
|
|
|
2019-12-05 17:37:42 +00:00
|
|
|
|
// BrowserViews are not attached to a window initially so they should start
|
|
|
|
|
// off as hidden. This is also important for compositor recycling. See:
|
|
|
|
|
// https://github.com/electron/electron/pull/21372
|
2020-10-27 17:51:45 +00:00
|
|
|
|
initially_shown_ = type_ != Type::kBrowserView;
|
2020-09-04 06:57:29 +00:00
|
|
|
|
options.Get(options::kShow, &initially_shown_);
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
2015-09-05 14:39:48 +00:00
|
|
|
|
// Obtain the session.
|
|
|
|
|
std::string partition;
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<api::Session> session;
|
2018-02-12 19:46:29 +00:00
|
|
|
|
if (options.Get("session", &session) && !session.IsEmpty()) {
|
2016-07-12 12:01:49 +00:00
|
|
|
|
} else if (options.Get("partition", &partition)) {
|
|
|
|
|
session = Session::FromPartition(isolate, partition);
|
2015-09-05 14:39:48 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Use the default session if not specified.
|
2016-07-12 12:01:49 +00:00
|
|
|
|
session = Session::FromPartition(isolate, "");
|
2015-09-05 14:39:48 +00:00
|
|
|
|
}
|
|
|
|
|
session_.Reset(isolate, session.ToV8());
|
|
|
|
|
|
2018-09-15 00:16:22 +00:00
|
|
|
|
std::unique_ptr<content::WebContents> web_contents;
|
2016-06-14 16:23:03 +00:00
|
|
|
|
if (IsGuest()) {
|
2016-05-23 03:28:59 +00:00
|
|
|
|
scoped_refptr<content::SiteInstance> site_instance =
|
2018-04-18 01:55:30 +00:00
|
|
|
|
content::SiteInstance::CreateForURL(session->browser_context(),
|
|
|
|
|
GURL("chrome-guest://fake-host"));
|
|
|
|
|
content::WebContents::CreateParams params(session->browser_context(),
|
|
|
|
|
site_instance);
|
2019-09-16 22:12:00 +00:00
|
|
|
|
guest_delegate_ =
|
|
|
|
|
std::make_unique<WebViewGuestDelegate>(embedder_->web_contents(), this);
|
2015-06-25 06:28:13 +00:00
|
|
|
|
params.guest_delegate = guest_delegate_.get();
|
2017-03-05 15:18:57 +00:00
|
|
|
|
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2017-03-04 02:09:16 +00:00
|
|
|
|
if (embedder_ && embedder_->IsOffScreen()) {
|
2018-04-18 01:55:30 +00:00
|
|
|
|
auto* view = new OffScreenWebContentsView(
|
2019-05-03 19:08:41 +00:00
|
|
|
|
false,
|
|
|
|
|
base::BindRepeating(&WebContents::OnPaint, base::Unretained(this)));
|
2017-03-04 02:09:16 +00:00
|
|
|
|
params.view = view;
|
|
|
|
|
params.delegate_view = view;
|
2017-03-05 15:18:57 +00:00
|
|
|
|
|
2017-03-04 02:09:16 +00:00
|
|
|
|
web_contents = content::WebContents::Create(params);
|
2018-10-05 18:03:35 +00:00
|
|
|
|
view->SetWebContents(web_contents.get());
|
2017-03-04 02:09:16 +00:00
|
|
|
|
} else {
|
2017-06-26 09:13:05 +00:00
|
|
|
|
#endif
|
2017-03-04 02:09:16 +00:00
|
|
|
|
web_contents = content::WebContents::Create(params);
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2017-03-04 02:09:16 +00:00
|
|
|
|
}
|
2016-07-31 10:19:56 +00:00
|
|
|
|
} else if (IsOffScreen()) {
|
2016-08-01 11:27:39 +00:00
|
|
|
|
bool transparent = false;
|
|
|
|
|
options.Get("transparent", &transparent);
|
|
|
|
|
|
2016-07-29 12:50:27 +00:00
|
|
|
|
content::WebContents::CreateParams params(session->browser_context());
|
2018-04-18 01:55:30 +00:00
|
|
|
|
auto* view = new OffScreenWebContentsView(
|
2019-05-03 19:08:41 +00:00
|
|
|
|
transparent,
|
|
|
|
|
base::BindRepeating(&WebContents::OnPaint, base::Unretained(this)));
|
2016-08-03 04:04:36 +00:00
|
|
|
|
params.view = view;
|
|
|
|
|
params.delegate_view = view;
|
2016-07-25 13:55:00 +00:00
|
|
|
|
|
2015-06-24 15:29:32 +00:00
|
|
|
|
web_contents = content::WebContents::Create(params);
|
2018-10-05 18:03:35 +00:00
|
|
|
|
view->SetWebContents(web_contents.get());
|
2017-06-26 09:13:05 +00:00
|
|
|
|
#endif
|
2016-07-29 12:50:27 +00:00
|
|
|
|
} else {
|
|
|
|
|
content::WebContents::CreateParams params(session->browser_context());
|
2020-09-04 06:57:29 +00:00
|
|
|
|
params.initially_hidden = !initially_shown_;
|
2016-07-27 23:58:23 +00:00
|
|
|
|
web_contents = content::WebContents::Create(params);
|
2015-06-24 14:14:46 +00:00
|
|
|
|
}
|
2015-06-09 07:50:20 +00:00
|
|
|
|
|
2018-10-22 18:02:25 +00:00
|
|
|
|
InitWithSessionAndOptions(isolate, std::move(web_contents), session, options);
|
2016-08-17 00:15:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 20:21:24 +00:00
|
|
|
|
void WebContents::InitZoomController(content::WebContents* web_contents,
|
2019-10-25 13:03:28 +00:00
|
|
|
|
const gin_helper::Dictionary& options) {
|
2017-10-21 20:21:24 +00:00
|
|
|
|
WebContentsZoomController::CreateForWebContents(web_contents);
|
|
|
|
|
zoom_controller_ = WebContentsZoomController::FromWebContents(web_contents);
|
|
|
|
|
double zoom_factor;
|
|
|
|
|
if (options.Get(options::kZoomFactor, &zoom_factor))
|
|
|
|
|
zoom_controller_->SetDefaultZoomFactor(zoom_factor);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 18:02:25 +00:00
|
|
|
|
void WebContents::InitWithSessionAndOptions(
|
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
|
std::unique_ptr<content::WebContents> owned_web_contents,
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<api::Session> session,
|
|
|
|
|
const gin_helper::Dictionary& options) {
|
2018-10-22 18:02:25 +00:00
|
|
|
|
Observe(owned_web_contents.get());
|
|
|
|
|
// TODO(zcbenz): Make InitWithWebContents take unique_ptr.
|
|
|
|
|
// At the time of writing we are going through a refactoring and I don't want
|
|
|
|
|
// to make other people's work harder.
|
|
|
|
|
InitWithWebContents(owned_web_contents.release(), session->browser_context(),
|
|
|
|
|
IsGuest());
|
2015-06-05 07:01:51 +00:00
|
|
|
|
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->GetView()->SetDelegate(this);
|
2015-10-01 03:14:19 +00:00
|
|
|
|
|
2018-10-22 18:02:25 +00:00
|
|
|
|
auto* prefs = web_contents()->GetMutableRendererPrefs();
|
2020-05-04 17:49:29 +00:00
|
|
|
|
|
|
|
|
|
// Collect preferred languages from OS and browser process. accept_languages
|
|
|
|
|
// effects HTTP header, navigator.languages, and CJK fallback font selection.
|
|
|
|
|
//
|
|
|
|
|
// Note that an application locale set to the browser process might be
|
|
|
|
|
// different with the one set to the preference list.
|
|
|
|
|
// (e.g. overridden with --lang)
|
|
|
|
|
std::string accept_languages =
|
|
|
|
|
g_browser_process->GetApplicationLocale() + ",";
|
|
|
|
|
for (auto const& language : electron::GetPreferredLanguages()) {
|
|
|
|
|
if (language == g_browser_process->GetApplicationLocale())
|
|
|
|
|
continue;
|
|
|
|
|
accept_languages += language + ",";
|
|
|
|
|
}
|
|
|
|
|
accept_languages.pop_back();
|
|
|
|
|
prefs->accept_languages = accept_languages;
|
2018-04-02 03:47:00 +00:00
|
|
|
|
|
2018-02-22 08:56:48 +00:00
|
|
|
|
#if defined(OS_LINUX) || defined(OS_WIN)
|
|
|
|
|
// Update font settings.
|
2019-01-10 16:28:15 +00:00
|
|
|
|
static const base::NoDestructor<gfx::FontRenderParams> params(
|
|
|
|
|
gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(), nullptr));
|
|
|
|
|
prefs->should_antialias_text = params->antialiasing;
|
|
|
|
|
prefs->use_subpixel_positioning = params->subpixel_positioning;
|
|
|
|
|
prefs->hinting = params->hinting;
|
|
|
|
|
prefs->use_autohinter = params->autohinter;
|
|
|
|
|
prefs->use_bitmaps = params->use_bitmaps;
|
|
|
|
|
prefs->subpixel_rendering = params->subpixel_rendering;
|
2018-02-22 08:56:48 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2019-09-19 18:43:21 +00:00
|
|
|
|
// Honor the system's cursor blink rate settings
|
|
|
|
|
if (auto interval = GetCursorBlinkInterval())
|
|
|
|
|
prefs->caret_blink_interval = *interval;
|
2019-08-30 14:39:46 +00:00
|
|
|
|
|
2015-09-22 13:56:56 +00:00
|
|
|
|
// Save the preferences in C++.
|
2020-11-10 17:06:03 +00:00
|
|
|
|
// If there's already a WebContentsPreferences object, we created it as part
|
|
|
|
|
// of the webContents.setWindowOpenHandler path, so don't overwrite it.
|
|
|
|
|
if (!WebContentsPreferences::From(web_contents())) {
|
|
|
|
|
new WebContentsPreferences(web_contents(), options);
|
|
|
|
|
}
|
2020-08-12 18:33:58 +00:00
|
|
|
|
// Trigger re-calculation of webkit prefs.
|
|
|
|
|
web_contents()->NotifyPreferencesChanged();
|
2015-09-04 16:44:22 +00:00
|
|
|
|
|
2018-10-22 18:02:25 +00:00
|
|
|
|
WebContentsPermissionHelper::CreateForWebContents(web_contents());
|
|
|
|
|
SecurityStateTabHelper::CreateForWebContents(web_contents());
|
|
|
|
|
InitZoomController(web_contents(), options);
|
2019-07-24 23:01:08 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
2020-02-03 22:01:10 +00:00
|
|
|
|
extensions::ElectronExtensionWebContentsObserver::CreateForWebContents(
|
2019-07-24 23:01:08 +00:00
|
|
|
|
web_contents());
|
2020-01-15 23:11:51 +00:00
|
|
|
|
script_executor_.reset(new extensions::ScriptExecutor(web_contents()));
|
2019-07-24 23:01:08 +00:00
|
|
|
|
#endif
|
2016-01-23 13:29:47 +00:00
|
|
|
|
|
2019-08-19 20:13:24 +00:00
|
|
|
|
AutofillDriverFactory::CreateForWebContents(web_contents());
|
2019-04-02 22:38:16 +00:00
|
|
|
|
|
2020-04-13 23:39:26 +00:00
|
|
|
|
web_contents()->SetUserAgentOverride(blink::UserAgentOverride::UserAgentOnly(
|
|
|
|
|
GetBrowserContext()->GetUserAgent()),
|
2018-10-22 18:02:25 +00:00
|
|
|
|
false);
|
2015-07-14 19:13:25 +00:00
|
|
|
|
|
2016-06-14 16:23:03 +00:00
|
|
|
|
if (IsGuest()) {
|
2015-06-24 15:29:32 +00:00
|
|
|
|
NativeWindow* owner_window = nullptr;
|
2017-03-04 02:09:16 +00:00
|
|
|
|
if (embedder_) {
|
2015-06-24 15:29:32 +00:00
|
|
|
|
// New WebContents's owner_window is the embedder's owner_window.
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* relay =
|
2016-02-17 08:52:19 +00:00
|
|
|
|
NativeWindowRelay::FromWebContents(embedder_->web_contents());
|
2015-06-24 15:29:32 +00:00
|
|
|
|
if (relay)
|
2018-10-02 22:14:43 +00:00
|
|
|
|
owner_window = relay->GetNativeWindow();
|
2015-06-24 15:29:32 +00:00
|
|
|
|
}
|
|
|
|
|
if (owner_window)
|
|
|
|
|
SetOwnerWindow(owner_window);
|
|
|
|
|
}
|
2016-04-25 01:17:54 +00:00
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
web_contents()->SetUserData(kElectronApiWebContentsKey,
|
|
|
|
|
std::make_unique<UserDataLink>(GetWeakPtr()));
|
2014-10-23 06:04:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 11:55:40 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
|
void WebContents::InitWithExtensionView(v8::Isolate* isolate,
|
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
|
extensions::ViewType view_type) {
|
|
|
|
|
// Must reassign type prior to calling `Init`.
|
|
|
|
|
type_ = GetTypeFromViewType(view_type);
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (GetType() == Type::kRemote)
|
2020-09-08 11:55:40 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Allow toggling DevTools for background pages
|
|
|
|
|
Observe(web_contents);
|
|
|
|
|
InitWithWebContents(web_contents, GetBrowserContext(), IsGuest());
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->GetView()->SetDelegate(this);
|
2020-09-08 11:55:40 +00:00
|
|
|
|
SecurityStateTabHelper::CreateForWebContents(web_contents);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
|
void WebContents::InitWithWebContents(content::WebContents* web_contents,
|
|
|
|
|
ElectronBrowserContext* browser_context,
|
|
|
|
|
bool is_guest) {
|
|
|
|
|
browser_context_ = browser_context;
|
|
|
|
|
web_contents->SetDelegate(this);
|
|
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
|
|
|
|
PrintPreviewMessageHandler::CreateForWebContents(web_contents);
|
|
|
|
|
printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
|
|
|
|
|
printing::CreateCompositeClientIfNeeded(web_contents,
|
|
|
|
|
browser_context->GetUserAgent());
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
|
|
|
pdf::PDFWebContentsHelper::CreateForWebContentsWithClient(
|
|
|
|
|
web_contents, std::make_unique<ElectronPDFWebContentsHelperClient>());
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Determine whether the WebContents is offscreen.
|
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(web_contents);
|
|
|
|
|
offscreen_ =
|
|
|
|
|
web_preferences && web_preferences->IsEnabled(options::kOffscreen);
|
|
|
|
|
|
|
|
|
|
// Create InspectableWebContents.
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_.reset(new InspectableWebContents(
|
2020-11-10 17:06:56 +00:00
|
|
|
|
web_contents, browser_context->prefs(), is_guest));
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->SetDelegate(this);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 06:04:13 +00:00
|
|
|
|
WebContents::~WebContents() {
|
2020-07-30 16:17:57 +00:00
|
|
|
|
MarkDestroyed();
|
2016-01-13 09:59:59 +00:00
|
|
|
|
// The destroy() is called.
|
2020-11-17 22:15:20 +00:00
|
|
|
|
if (inspectable_web_contents_) {
|
2020-09-08 11:55:40 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kBackgroundPage) {
|
2020-09-08 11:55:40 +00:00
|
|
|
|
// Background pages are owned by extensions::ExtensionHost
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->ReleaseWebContents();
|
2020-09-08 11:55:40 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->GetView()->SetDelegate(nullptr);
|
2018-02-22 07:52:08 +00:00
|
|
|
|
|
2020-09-08 11:55:40 +00:00
|
|
|
|
if (web_contents()) {
|
|
|
|
|
RenderViewDeleted(web_contents()->GetRenderViewHost());
|
|
|
|
|
}
|
2017-04-03 20:03:51 +00:00
|
|
|
|
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kBrowserWindow && owner_window()) {
|
2018-11-08 15:57:28 +00:00
|
|
|
|
// For BrowserWindow we should close the window and clean up everything
|
|
|
|
|
// before WebContents is destroyed.
|
|
|
|
|
for (ExtendedWebContentsObserver& observer : observers_)
|
|
|
|
|
observer.OnCloseContents();
|
|
|
|
|
// BrowserWindow destroys WebContents asynchronously, manually emit the
|
|
|
|
|
// destroyed event here.
|
|
|
|
|
WebContentsDestroyed();
|
|
|
|
|
} else if (Browser::Get()->is_shutting_down()) {
|
|
|
|
|
// Destroy WebContents directly when app is shutting down.
|
2018-09-07 06:41:48 +00:00
|
|
|
|
DestroyWebContents(false /* async */);
|
2017-04-03 20:03:51 +00:00
|
|
|
|
} else {
|
2018-11-08 15:57:28 +00:00
|
|
|
|
// Destroy WebContents asynchronously unless app is shutting down,
|
|
|
|
|
// because destroy() might be called inside WebContents's event handler.
|
2020-10-27 17:51:45 +00:00
|
|
|
|
bool is_browser_view = type_ == Type::kBrowserView;
|
2020-08-26 03:04:13 +00:00
|
|
|
|
DestroyWebContents(!(IsGuest() || is_browser_view) /* async */);
|
2018-09-07 06:41:48 +00:00
|
|
|
|
// The WebContentsDestroyed will not be called automatically because we
|
|
|
|
|
// destroy the webContents in the next tick. So we have to manually
|
|
|
|
|
// call it here to make sure "destroyed" event is emitted.
|
|
|
|
|
WebContentsDestroyed();
|
2017-04-03 20:03:51 +00:00
|
|
|
|
}
|
2016-01-13 09:59:59 +00:00
|
|
|
|
}
|
2014-04-24 08:45:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 20:03:51 +00:00
|
|
|
|
void WebContents::DestroyWebContents(bool async) {
|
2017-03-18 19:40:30 +00:00
|
|
|
|
// This event is only for internal use, which is emitted when WebContents is
|
|
|
|
|
// being destroyed.
|
|
|
|
|
Emit("will-destroy");
|
2017-04-03 20:03:51 +00:00
|
|
|
|
ResetManagedWebContents(async);
|
2017-03-18 19:40:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 00:18:22 +00:00
|
|
|
|
bool WebContents::DidAddMessageToConsole(
|
|
|
|
|
content::WebContents* source,
|
|
|
|
|
blink::mojom::ConsoleMessageLevel level,
|
|
|
|
|
const base::string16& message,
|
|
|
|
|
int32_t line_no,
|
|
|
|
|
const base::string16& source_id) {
|
|
|
|
|
return Emit("console-message", static_cast<int32_t>(level), message, line_no,
|
|
|
|
|
source_id);
|
2014-10-25 02:01:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 23:30:38 +00:00
|
|
|
|
void WebContents::OnCreateWindow(
|
|
|
|
|
const GURL& target_url,
|
2018-04-05 23:13:24 +00:00
|
|
|
|
const content::Referrer& referrer,
|
2016-10-09 23:30:38 +00:00
|
|
|
|
const std::string& frame_name,
|
|
|
|
|
WindowOpenDisposition disposition,
|
2020-03-26 18:05:45 +00:00
|
|
|
|
const std::string& features,
|
2018-04-11 08:42:14 +00:00
|
|
|
|
const scoped_refptr<network::ResourceRequestBody>& body) {
|
2020-03-26 18:05:45 +00:00
|
|
|
|
Emit("-new-window", target_url, frame_name, disposition, features, referrer,
|
|
|
|
|
body);
|
2014-10-25 05:21:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 18:05:45 +00:00
|
|
|
|
void WebContents::WebContentsCreatedWithFullParams(
|
|
|
|
|
content::WebContents* source_contents,
|
|
|
|
|
int opener_render_process_id,
|
|
|
|
|
int opener_render_frame_id,
|
|
|
|
|
const content::mojom::CreateNewWindowParams& params,
|
|
|
|
|
content::WebContents* new_contents) {
|
2018-10-22 18:02:25 +00:00
|
|
|
|
ChildWebContentsTracker::CreateForWebContents(new_contents);
|
|
|
|
|
auto* tracker = ChildWebContentsTracker::FromWebContents(new_contents);
|
2020-03-26 18:05:45 +00:00
|
|
|
|
tracker->url = params.target_url;
|
|
|
|
|
tracker->frame_name = params.frame_name;
|
|
|
|
|
tracker->referrer = params.referrer.To<content::Referrer>();
|
|
|
|
|
tracker->raw_features = params.raw_features;
|
|
|
|
|
tracker->body = params.body;
|
2020-11-10 17:06:03 +00:00
|
|
|
|
|
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
|
|
gin_helper::Dictionary dict;
|
|
|
|
|
gin::ConvertFromV8(isolate, pending_child_web_preferences_.Get(isolate),
|
|
|
|
|
&dict);
|
|
|
|
|
pending_child_web_preferences_.Reset();
|
|
|
|
|
|
|
|
|
|
// Associate the preferences passed in via `setWindowOpenHandler` with the
|
|
|
|
|
// content::WebContents that was just created for the child window. These
|
|
|
|
|
// preferences will be picked up by the RenderWidgetHost via its call to the
|
|
|
|
|
// delegate's OverrideWebkitPrefs.
|
|
|
|
|
new WebContentsPreferences(new_contents, dict);
|
2016-08-16 00:13:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 15:37:41 +00:00
|
|
|
|
bool WebContents::IsWebContentsCreationOverridden(
|
|
|
|
|
content::SiteInstance* source_site_instance,
|
|
|
|
|
content::mojom::WindowContainerType window_container_type,
|
|
|
|
|
const GURL& opener_url,
|
2020-11-10 17:06:03 +00:00
|
|
|
|
const content::mojom::CreateNewWindowParams& params) {
|
|
|
|
|
bool default_prevented = Emit("-will-add-new-contents", params.target_url,
|
|
|
|
|
params.frame_name, params.raw_features);
|
|
|
|
|
// If the app prevented the default, redirect to CreateCustomWebContents,
|
|
|
|
|
// which always returns nullptr, which will result in the window open being
|
|
|
|
|
// prevented (window.open() will return null in the renderer).
|
|
|
|
|
return default_prevented;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::SetNextChildWebPreferences(
|
|
|
|
|
const gin_helper::Dictionary preferences) {
|
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
|
// Store these prefs for when Chrome calls WebContentsCreatedWithFullParams
|
|
|
|
|
// with the new child contents.
|
|
|
|
|
pending_child_web_preferences_.Reset(isolate, preferences.GetHandle());
|
2020-04-13 15:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content::WebContents* WebContents::CreateCustomWebContents(
|
|
|
|
|
content::RenderFrameHost* opener,
|
|
|
|
|
content::SiteInstance* source_site_instance,
|
|
|
|
|
bool is_new_browsing_instance,
|
|
|
|
|
const GURL& opener_url,
|
|
|
|
|
const std::string& frame_name,
|
|
|
|
|
const GURL& target_url,
|
|
|
|
|
const std::string& partition_id,
|
|
|
|
|
content::SessionStorageNamespace* session_storage_namespace) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 00:16:22 +00:00
|
|
|
|
void WebContents::AddNewContents(
|
|
|
|
|
content::WebContents* source,
|
|
|
|
|
std::unique_ptr<content::WebContents> new_contents,
|
2020-05-26 20:06:26 +00:00
|
|
|
|
const GURL& target_url,
|
2018-09-15 00:16:22 +00:00
|
|
|
|
WindowOpenDisposition disposition,
|
|
|
|
|
const gfx::Rect& initial_rect,
|
|
|
|
|
bool user_gesture,
|
|
|
|
|
bool* was_blocked) {
|
2018-10-22 18:02:25 +00:00
|
|
|
|
auto* tracker = ChildWebContentsTracker::FromWebContents(new_contents.get());
|
|
|
|
|
DCHECK(tracker);
|
|
|
|
|
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2018-10-22 18:02:25 +00:00
|
|
|
|
auto api_web_contents =
|
2020-10-27 17:51:45 +00:00
|
|
|
|
CreateAndTake(isolate, std::move(new_contents), Type::kBrowserWindow);
|
2017-01-10 23:40:07 +00:00
|
|
|
|
if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture,
|
2018-04-18 01:55:30 +00:00
|
|
|
|
initial_rect.x(), initial_rect.y(), initial_rect.width(),
|
2020-03-26 18:05:45 +00:00
|
|
|
|
initial_rect.height(), tracker->url, tracker->frame_name,
|
|
|
|
|
tracker->referrer, tracker->raw_features, tracker->body)) {
|
2020-10-23 06:04:07 +00:00
|
|
|
|
api_web_contents->DestroyWebContents(false /* async */);
|
2017-01-10 23:40:07 +00:00
|
|
|
|
}
|
2016-08-16 00:13:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-24 10:06:32 +00:00
|
|
|
|
content::WebContents* WebContents::OpenURLFromTab(
|
|
|
|
|
content::WebContents* source,
|
|
|
|
|
const content::OpenURLParams& params) {
|
2020-07-30 16:17:57 +00:00
|
|
|
|
auto weak_this = GetWeakPtr();
|
2017-01-24 07:50:02 +00:00
|
|
|
|
if (params.disposition != WindowOpenDisposition::CURRENT_TAB) {
|
2020-03-26 18:05:45 +00:00
|
|
|
|
Emit("-new-window", params.url, "", params.disposition, "", params.referrer,
|
|
|
|
|
params.post_data);
|
2014-12-17 22:55:22 +00:00
|
|
|
|
return nullptr;
|
2014-11-04 09:59:15 +00:00
|
|
|
|
}
|
2014-10-24 10:06:32 +00:00
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
if (!weak_this)
|
2016-12-21 00:51:03 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
|
content::NavigationController::LoadURLParams load_url_params(params.url);
|
|
|
|
|
load_url_params.referrer = params.referrer;
|
|
|
|
|
load_url_params.transition_type = params.transition;
|
|
|
|
|
load_url_params.extra_headers = params.extra_headers;
|
|
|
|
|
load_url_params.should_replace_current_entry =
|
|
|
|
|
params.should_replace_current_entry;
|
|
|
|
|
load_url_params.is_renderer_initiated = params.is_renderer_initiated;
|
|
|
|
|
load_url_params.started_from_context_menu = params.started_from_context_menu;
|
|
|
|
|
load_url_params.initiator_origin = params.initiator_origin;
|
|
|
|
|
load_url_params.source_site_instance = params.source_site_instance;
|
|
|
|
|
load_url_params.frame_tree_node_id = params.frame_tree_node_id;
|
|
|
|
|
load_url_params.redirect_chain = params.redirect_chain;
|
|
|
|
|
load_url_params.has_user_gesture = params.user_gesture;
|
|
|
|
|
load_url_params.blob_url_loader_factory = params.blob_url_loader_factory;
|
|
|
|
|
load_url_params.href_translate = params.href_translate;
|
|
|
|
|
load_url_params.reload_type = params.reload_type;
|
|
|
|
|
|
|
|
|
|
if (params.post_data) {
|
|
|
|
|
load_url_params.load_type =
|
|
|
|
|
content::NavigationController::LOAD_TYPE_HTTP_POST;
|
|
|
|
|
load_url_params.post_data = params.post_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
source->GetController().LoadURLWithParams(load_url_params);
|
|
|
|
|
return source;
|
2014-10-24 10:06:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 03:07:23 +00:00
|
|
|
|
void WebContents::BeforeUnloadFired(content::WebContents* tab,
|
|
|
|
|
bool proceed,
|
|
|
|
|
bool* proceed_to_fire_unload) {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kBrowserWindow || type_ == Type::kOffScreen)
|
2015-06-25 03:07:23 +00:00
|
|
|
|
*proceed_to_fire_unload = proceed;
|
|
|
|
|
else
|
|
|
|
|
*proceed_to_fire_unload = true;
|
2020-05-26 13:21:38 +00:00
|
|
|
|
// Note that Chromium does not emit this for navigations.
|
|
|
|
|
Emit("before-unload-fired", proceed);
|
2015-06-25 03:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 21:56:09 +00:00
|
|
|
|
void WebContents::SetContentsBounds(content::WebContents* source,
|
2020-02-11 00:37:46 +00:00
|
|
|
|
const gfx::Rect& rect) {
|
|
|
|
|
for (ExtendedWebContentsObserver& observer : observers_)
|
|
|
|
|
observer.OnSetContentBounds(rect);
|
2015-06-25 03:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::CloseContents(content::WebContents* source) {
|
2015-07-10 02:38:15 +00:00
|
|
|
|
Emit("close");
|
2019-08-19 20:13:24 +00:00
|
|
|
|
|
|
|
|
|
auto* autofill_driver_factory =
|
|
|
|
|
AutofillDriverFactory::FromWebContents(web_contents());
|
|
|
|
|
if (autofill_driver_factory) {
|
|
|
|
|
autofill_driver_factory->CloseAllPopups();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 22:15:20 +00:00
|
|
|
|
if (inspectable_web_contents_)
|
|
|
|
|
inspectable_web_contents_->GetView()->SetDelegate(nullptr);
|
2018-02-22 07:52:08 +00:00
|
|
|
|
for (ExtendedWebContentsObserver& observer : observers_)
|
|
|
|
|
observer.OnCloseContents();
|
2015-06-25 03:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::ActivateContents(content::WebContents* source) {
|
2020-02-11 00:37:46 +00:00
|
|
|
|
for (ExtendedWebContentsObserver& observer : observers_)
|
|
|
|
|
observer.OnActivateContents();
|
2015-06-25 03:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 06:56:19 +00:00
|
|
|
|
void WebContents::UpdateTargetURL(content::WebContents* source,
|
|
|
|
|
const GURL& url) {
|
|
|
|
|
Emit("update-target-url", url);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 20:01:49 +00:00
|
|
|
|
bool WebContents::HandleKeyboardEvent(
|
2014-10-24 13:46:47 +00:00
|
|
|
|
content::WebContents* source,
|
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kWebView && embedder_) {
|
2015-06-25 03:07:23 +00:00
|
|
|
|
// Send the unhandled keyboard events back to the embedder.
|
2019-01-09 20:01:49 +00:00
|
|
|
|
return embedder_->HandleKeyboardEvent(source, event);
|
2016-05-18 04:47:50 +00:00
|
|
|
|
} else {
|
2020-11-10 17:06:56 +00:00
|
|
|
|
return PlatformHandleKeyboardEvent(source, event);
|
2015-06-25 03:07:23 +00:00
|
|
|
|
}
|
2014-10-24 13:46:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
|
#if !defined(OS_MAC)
|
|
|
|
|
// NOTE: The macOS version of this function is found in
|
|
|
|
|
// electron_api_web_contents_mac.mm, as it requires calling into objective-C
|
|
|
|
|
// code.
|
|
|
|
|
bool WebContents::PlatformHandleKeyboardEvent(
|
|
|
|
|
content::WebContents* source,
|
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
|
|
|
|
// Escape exits tabbed fullscreen mode.
|
|
|
|
|
if (event.windows_key_code == ui::VKEY_ESCAPE && is_html_fullscreen()) {
|
|
|
|
|
ExitFullscreenModeForTab(source);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the webContents has preferences and to ignore shortcuts
|
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(source);
|
|
|
|
|
if (web_preferences &&
|
|
|
|
|
web_preferences->IsEnabled("ignoreMenuShortcuts", false))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Let the NativeWindow handle other parts.
|
|
|
|
|
if (owner_window()) {
|
|
|
|
|
owner_window()->HandleKeyboardEvent(source, event);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-06-16 21:35:43 +00:00
|
|
|
|
content::KeyboardEventProcessingResult WebContents::PreHandleKeyboardEvent(
|
2016-12-06 22:41:18 +00:00
|
|
|
|
content::WebContents* source,
|
2017-06-16 21:35:43 +00:00
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
2017-06-16 20:42:33 +00:00
|
|
|
|
if (event.GetType() == blink::WebInputEvent::Type::kRawKeyDown ||
|
|
|
|
|
event.GetType() == blink::WebInputEvent::Type::kKeyUp) {
|
2017-06-16 21:35:43 +00:00
|
|
|
|
bool prevent_default = Emit("before-input-event", event);
|
|
|
|
|
if (prevent_default) {
|
|
|
|
|
return content::KeyboardEventProcessingResult::HANDLED;
|
|
|
|
|
}
|
2017-06-25 19:01:05 +00:00
|
|
|
|
}
|
2017-06-16 21:35:43 +00:00
|
|
|
|
|
|
|
|
|
return content::KeyboardEventProcessingResult::NOT_HANDLED;
|
2016-12-06 22:41:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 06:40:40 +00:00
|
|
|
|
void WebContents::ContentsZoomChange(bool zoom_in) {
|
|
|
|
|
Emit("zoom-changed", zoom_in ? "in" : "out");
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 00:17:50 +00:00
|
|
|
|
void WebContents::EnterFullscreenModeForTab(
|
2020-06-01 20:34:34 +00:00
|
|
|
|
content::RenderFrameHost* requesting_frame,
|
2019-10-28 22:12:35 +00:00
|
|
|
|
const blink::mojom::FullscreenOptions& options) {
|
2020-06-01 20:34:34 +00:00
|
|
|
|
auto* source = content::WebContents::FromRenderFrameHost(requesting_frame);
|
2018-04-18 10:09:45 +00:00
|
|
|
|
auto* permission_helper =
|
|
|
|
|
WebContentsPermissionHelper::FromWebContents(source);
|
2019-05-03 19:08:41 +00:00
|
|
|
|
auto callback =
|
|
|
|
|
base::BindRepeating(&WebContents::OnEnterFullscreenModeForTab,
|
2020-06-01 20:34:34 +00:00
|
|
|
|
base::Unretained(this), requesting_frame, options);
|
2016-02-01 10:03:38 +00:00
|
|
|
|
permission_helper->RequestFullscreenPermission(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 00:17:50 +00:00
|
|
|
|
void WebContents::OnEnterFullscreenModeForTab(
|
2020-06-01 20:34:34 +00:00
|
|
|
|
content::RenderFrameHost* requesting_frame,
|
2019-10-28 22:12:35 +00:00
|
|
|
|
const blink::mojom::FullscreenOptions& options,
|
2018-09-15 00:17:50 +00:00
|
|
|
|
bool allowed) {
|
2016-02-01 10:03:38 +00:00
|
|
|
|
if (!allowed)
|
|
|
|
|
return;
|
2020-11-10 17:06:56 +00:00
|
|
|
|
if (!owner_window_)
|
|
|
|
|
return;
|
|
|
|
|
auto* source = content::WebContents::FromRenderFrameHost(requesting_frame);
|
|
|
|
|
if (IsFullscreenForTabOrPending(source)) {
|
|
|
|
|
DCHECK_EQ(fullscreen_frame_, source->GetFocusedFrame());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SetHtmlApiFullscreen(true);
|
|
|
|
|
owner_window_->NotifyWindowEnterHtmlFullScreen();
|
|
|
|
|
|
|
|
|
|
if (native_fullscreen_) {
|
|
|
|
|
// Explicitly trigger a view resize, as the size is not actually changing if
|
|
|
|
|
// the browser is fullscreened, too.
|
|
|
|
|
source->GetRenderViewHost()->GetWidget()->SynchronizeVisualProperties();
|
|
|
|
|
}
|
2015-06-05 09:27:24 +00:00
|
|
|
|
Emit("enter-html-full-screen");
|
2015-05-08 06:18:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-09 12:17:40 +00:00
|
|
|
|
void WebContents::ExitFullscreenModeForTab(content::WebContents* source) {
|
2020-11-10 17:06:56 +00:00
|
|
|
|
if (!owner_window_)
|
|
|
|
|
return;
|
|
|
|
|
SetHtmlApiFullscreen(false);
|
|
|
|
|
owner_window_->NotifyWindowLeaveHtmlFullScreen();
|
|
|
|
|
|
|
|
|
|
if (native_fullscreen_) {
|
|
|
|
|
// Explicitly trigger a view resize, as the size is not actually changing if
|
|
|
|
|
// the browser is fullscreened, too. Chrome does this indirectly from
|
|
|
|
|
// `chrome/browser/ui/exclusive_access/fullscreen_controller.cc`.
|
|
|
|
|
source->GetRenderViewHost()->GetWidget()->SynchronizeVisualProperties();
|
|
|
|
|
}
|
2015-06-05 09:27:24 +00:00
|
|
|
|
Emit("leave-html-full-screen");
|
2015-05-08 06:18:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 09:59:40 +00:00
|
|
|
|
void WebContents::RendererUnresponsive(
|
|
|
|
|
content::WebContents* source,
|
2018-10-02 21:57:14 +00:00
|
|
|
|
content::RenderWidgetHost* render_widget_host,
|
|
|
|
|
base::RepeatingClosure hang_monitor_restarter) {
|
2015-06-25 03:07:23 +00:00
|
|
|
|
Emit("unresponsive");
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 15:23:16 +00:00
|
|
|
|
void WebContents::RendererResponsive(
|
|
|
|
|
content::WebContents* source,
|
|
|
|
|
content::RenderWidgetHost* render_widget_host) {
|
2015-06-25 03:07:23 +00:00
|
|
|
|
Emit("responsive");
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 07:49:59 +00:00
|
|
|
|
bool WebContents::HandleContextMenu(content::RenderFrameHost* render_frame_host,
|
|
|
|
|
const content::ContextMenuParams& params) {
|
2016-05-03 00:02:33 +00:00
|
|
|
|
if (params.custom_context.is_pepper_menu) {
|
2018-04-18 01:55:30 +00:00
|
|
|
|
Emit("pepper-context-menu", std::make_pair(params, web_contents()),
|
2019-05-03 19:08:41 +00:00
|
|
|
|
base::BindOnce(&content::WebContents::NotifyContextMenuClosed,
|
|
|
|
|
base::Unretained(web_contents()),
|
|
|
|
|
params.custom_context));
|
2016-05-03 00:02:33 +00:00
|
|
|
|
} else {
|
|
|
|
|
Emit("context-menu", std::make_pair(params, web_contents()));
|
|
|
|
|
}
|
2015-11-02 15:28:45 +00:00
|
|
|
|
|
2015-10-31 13:39:07 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-19 19:53:47 +00:00
|
|
|
|
bool WebContents::OnGoToEntryOffset(int offset) {
|
|
|
|
|
GoToOffset(offset);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-17 17:27:56 +00:00
|
|
|
|
void WebContents::FindReply(content::WebContents* web_contents,
|
|
|
|
|
int request_id,
|
|
|
|
|
int number_of_matches,
|
|
|
|
|
const gfx::Rect& selection_rect,
|
|
|
|
|
int active_match_ordinal,
|
|
|
|
|
bool final_update) {
|
2016-09-09 10:50:43 +00:00
|
|
|
|
if (!final_update)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
|
gin_helper::Dictionary result = gin::Dictionary::CreateEmpty(isolate);
|
2016-09-08 05:27:10 +00:00
|
|
|
|
result.Set("requestId", request_id);
|
|
|
|
|
result.Set("matches", number_of_matches);
|
|
|
|
|
result.Set("selectionArea", selection_rect);
|
|
|
|
|
result.Set("activeMatchOrdinal", active_match_ordinal);
|
2016-09-09 10:50:43 +00:00
|
|
|
|
result.Set("finalUpdate", final_update); // Deprecate after 2.0
|
2019-10-21 07:05:40 +00:00
|
|
|
|
Emit("found-in-page", result.GetHandle());
|
2015-12-17 17:27:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-09 14:30:28 +00:00
|
|
|
|
bool WebContents::CheckMediaAccessPermission(
|
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
|
const GURL& security_origin,
|
2019-07-03 01:22:09 +00:00
|
|
|
|
blink::mojom::MediaStreamType type) {
|
2018-09-09 14:30:28 +00:00
|
|
|
|
auto* web_contents =
|
|
|
|
|
content::WebContents::FromRenderFrameHost(render_frame_host);
|
2018-08-28 14:05:08 +00:00
|
|
|
|
auto* permission_helper =
|
|
|
|
|
WebContentsPermissionHelper::FromWebContents(web_contents);
|
|
|
|
|
return permission_helper->CheckMediaAccessPermission(security_origin, type);
|
2016-02-26 10:17:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 13:29:47 +00:00
|
|
|
|
void WebContents::RequestMediaAccessPermission(
|
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
|
const content::MediaStreamRequest& request,
|
2018-10-02 18:09:25 +00:00
|
|
|
|
content::MediaResponseCallback callback) {
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* permission_helper =
|
2016-01-23 13:29:47 +00:00
|
|
|
|
WebContentsPermissionHelper::FromWebContents(web_contents);
|
2018-10-02 18:09:25 +00:00
|
|
|
|
permission_helper->RequestMediaAccessPermission(request, std::move(callback));
|
2016-01-23 13:29:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
void WebContents::RequestToLockMouse(content::WebContents* web_contents,
|
|
|
|
|
bool user_gesture,
|
|
|
|
|
bool last_unlocked_by_target) {
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* permission_helper =
|
2016-02-01 09:43:49 +00:00
|
|
|
|
WebContentsPermissionHelper::FromWebContents(web_contents);
|
|
|
|
|
permission_helper->RequestPointerLockPermission(user_gesture);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
|
2017-04-28 00:28:48 +00:00
|
|
|
|
content::WebContents* source) {
|
|
|
|
|
if (!dialog_manager_)
|
2020-07-16 23:16:05 +00:00
|
|
|
|
dialog_manager_ = std::make_unique<ElectronJavaScriptDialogManager>();
|
2017-04-28 00:28:48 +00:00
|
|
|
|
|
|
|
|
|
return dialog_manager_.get();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 00:18:14 +00:00
|
|
|
|
void WebContents::OnAudioStateChanged(bool audible) {
|
2018-09-05 21:00:05 +00:00
|
|
|
|
Emit("-audio-state-changed", audible);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
|
void WebContents::BeforeUnloadFired(bool proceed,
|
|
|
|
|
const base::TimeTicks& proceed_time) {
|
2015-06-25 03:07:23 +00:00
|
|
|
|
// Do nothing, we override this method just to avoid compilation error since
|
|
|
|
|
// there are two virtual functions named BeforeUnloadFired.
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-28 23:44:44 +00:00
|
|
|
|
void WebContents::RenderViewCreated(content::RenderViewHost* render_view_host) {
|
2019-12-02 18:29:03 +00:00
|
|
|
|
if (!background_throttling_)
|
|
|
|
|
render_view_host->SetSchedulerThrottling(false);
|
2021-02-10 20:03:48 +00:00
|
|
|
|
|
|
|
|
|
// Set the background color of RenderWidgetHostView.
|
|
|
|
|
auto* const view = web_contents()->GetRenderWidgetHostView();
|
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(web_contents());
|
|
|
|
|
if (view && web_preferences) {
|
|
|
|
|
std::string color_name;
|
|
|
|
|
if (web_preferences->GetPreference(options::kBackgroundColor,
|
|
|
|
|
&color_name)) {
|
|
|
|
|
view->SetBackgroundColor(ParseHexColor(color_name));
|
|
|
|
|
} else {
|
|
|
|
|
view->SetBackgroundColor(SK_ColorTRANSPARENT);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-02 18:29:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::RenderFrameCreated(
|
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
|
auto* rwhv = render_frame_host->GetView();
|
|
|
|
|
if (!rwhv)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto* rwh_impl =
|
|
|
|
|
static_cast<content::RenderWidgetHostImpl*>(rwhv->GetRenderWidgetHost());
|
|
|
|
|
if (rwh_impl)
|
|
|
|
|
rwh_impl->disable_hidden_ = !background_throttling_;
|
2021-02-09 20:16:21 +00:00
|
|
|
|
|
|
|
|
|
WebFrameMain::RenderFrameCreated(render_frame_host);
|
2016-12-28 23:44:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 04:40:04 +00:00
|
|
|
|
void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) {
|
2018-12-05 08:03:39 +00:00
|
|
|
|
// This event is necessary for tracking any states with respect to
|
|
|
|
|
// intermediate render view hosts aka speculative render view hosts. Currently
|
|
|
|
|
// used by object-registry.js to ref count remote objects.
|
2018-09-11 18:18:10 +00:00
|
|
|
|
Emit("render-view-deleted", render_view_host->GetProcess()->GetID());
|
2018-12-05 08:03:39 +00:00
|
|
|
|
|
2020-10-07 23:11:19 +00:00
|
|
|
|
if (web_contents()->GetRenderViewHost() == render_view_host) {
|
2018-12-05 08:03:39 +00:00
|
|
|
|
// When the RVH that has been deleted is the current RVH it means that the
|
|
|
|
|
// the web contents are being closed. This is communicated by this event.
|
2020-11-10 17:06:03 +00:00
|
|
|
|
// Currently tracked by guest-window-manager.ts to destroy the
|
2018-12-05 08:03:39 +00:00
|
|
|
|
// BrowserWindow.
|
|
|
|
|
Emit("current-render-view-deleted",
|
|
|
|
|
render_view_host->GetProcess()->GetID());
|
|
|
|
|
}
|
2014-04-25 03:51:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::RenderProcessGone(base::TerminationStatus status) {
|
2016-09-18 00:00:45 +00:00
|
|
|
|
Emit("crashed", status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED);
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
|
gin_helper::Dictionary details = gin_helper::Dictionary::CreateEmpty(isolate);
|
2020-05-11 20:42:24 +00:00
|
|
|
|
details.Set("reason", status);
|
2021-02-09 01:08:11 +00:00
|
|
|
|
details.Set("exitCode", web_contents()->GetCrashedErrorCode());
|
2020-05-11 20:42:24 +00:00
|
|
|
|
Emit("render-process-gone", details);
|
2014-04-25 03:51:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 19:35:56 +00:00
|
|
|
|
void WebContents::PluginCrashed(const base::FilePath& plugin_path,
|
|
|
|
|
base::ProcessId plugin_pid) {
|
2019-10-09 17:59:37 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PLUGINS)
|
2015-05-12 19:35:56 +00:00
|
|
|
|
content::WebPluginInfo info;
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* plugin_service = content::PluginService::GetInstance();
|
2015-05-12 19:35:56 +00:00
|
|
|
|
plugin_service->GetPluginInfoByPath(plugin_path, &info);
|
|
|
|
|
Emit("plugin-crashed", info.name, info.version);
|
2020-10-13 17:25:21 +00:00
|
|
|
|
#endif // BUILDFLAG(ENABLE_PLUGINS)
|
2015-05-12 19:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 09:59:40 +00:00
|
|
|
|
void WebContents::MediaStartedPlaying(const MediaPlayerInfo& video_type,
|
2019-04-20 17:20:37 +00:00
|
|
|
|
const content::MediaPlayerId& id) {
|
2015-12-20 03:16:22 +00:00
|
|
|
|
Emit("media-started-playing");
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 15:27:14 +00:00
|
|
|
|
void WebContents::MediaStoppedPlaying(
|
|
|
|
|
const MediaPlayerInfo& video_type,
|
2019-04-20 17:20:37 +00:00
|
|
|
|
const content::MediaPlayerId& id,
|
2018-04-11 15:27:14 +00:00
|
|
|
|
content::WebContentsObserver::MediaStoppedReason reason) {
|
2015-12-20 03:16:22 +00:00
|
|
|
|
Emit("media-paused");
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 18:41:52 +00:00
|
|
|
|
void WebContents::DidChangeThemeColor() {
|
|
|
|
|
auto theme_color = web_contents()->GetThemeColor();
|
2019-04-20 17:20:37 +00:00
|
|
|
|
if (theme_color) {
|
2019-06-19 21:23:04 +00:00
|
|
|
|
Emit("did-change-theme-color", electron::ToRGBHex(theme_color.value()));
|
2017-08-20 14:48:13 +00:00
|
|
|
|
} else {
|
2017-11-13 07:20:54 +00:00
|
|
|
|
Emit("did-change-theme-color", nullptr);
|
2017-08-20 14:48:13 +00:00
|
|
|
|
}
|
2015-12-22 22:16:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
|
void WebContents::OnInterfaceRequestFromFrame(
|
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
|
const std::string& interface_name,
|
|
|
|
|
mojo::ScopedMessagePipeHandle* interface_pipe) {
|
|
|
|
|
registry_.TryBindInterface(interface_name, interface_pipe, render_frame_host);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 21:02:56 +00:00
|
|
|
|
void WebContents::DidAcquireFullscreen(content::RenderFrameHost* rfh) {
|
|
|
|
|
set_fullscreen_frame(rfh);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 19:57:34 +00:00
|
|
|
|
void WebContents::DOMContentLoaded(
|
2015-04-29 13:49:31 +00:00
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2016-08-04 04:03:24 +00:00
|
|
|
|
if (!render_frame_host->GetParent())
|
2015-04-29 13:49:31 +00:00
|
|
|
|
Emit("dom-ready");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-11 11:11:34 +00:00
|
|
|
|
void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
|
|
|
|
|
const GURL& validated_url) {
|
|
|
|
|
bool is_main_frame = !render_frame_host->GetParent();
|
2018-04-26 10:17:55 +00:00
|
|
|
|
int frame_process_id = render_frame_host->GetProcess()->GetID();
|
|
|
|
|
int frame_routing_id = render_frame_host->GetRoutingID();
|
2019-09-30 22:00:47 +00:00
|
|
|
|
auto weak_this = GetWeakPtr();
|
2018-05-10 20:52:42 +00:00
|
|
|
|
Emit("did-frame-finish-load", is_main_frame, frame_process_id,
|
2018-04-26 10:17:55 +00:00
|
|
|
|
frame_routing_id);
|
2014-06-09 04:03:19 +00:00
|
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
|
// ⚠️WARNING!⚠️
|
|
|
|
|
// Emit() triggers JS which can call destroy() on |this|. It's not safe to
|
|
|
|
|
// assume that |this| points to valid memory at this point.
|
|
|
|
|
if (is_main_frame && weak_this)
|
2014-06-09 04:03:19 +00:00
|
|
|
|
Emit("did-finish-load");
|
2014-04-25 04:22:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-24 15:05:25 +00:00
|
|
|
|
void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host,
|
2016-04-13 10:33:59 +00:00
|
|
|
|
const GURL& url,
|
2020-01-29 12:01:37 +00:00
|
|
|
|
int error_code) {
|
2016-04-05 02:24:58 +00:00
|
|
|
|
bool is_main_frame = !render_frame_host->GetParent();
|
2018-04-26 10:17:55 +00:00
|
|
|
|
int frame_process_id = render_frame_host->GetProcess()->GetID();
|
|
|
|
|
int frame_routing_id = render_frame_host->GetRoutingID();
|
2020-01-29 12:01:37 +00:00
|
|
|
|
Emit("did-fail-load", error_code, "", url, is_main_frame, frame_process_id,
|
|
|
|
|
frame_routing_id);
|
2014-10-24 15:05:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 07:24:34 +00:00
|
|
|
|
void WebContents::DidStartLoading() {
|
2014-04-25 04:22:16 +00:00
|
|
|
|
Emit("did-start-loading");
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 07:24:34 +00:00
|
|
|
|
void WebContents::DidStopLoading() {
|
2020-10-21 22:44:19 +00:00
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(web_contents());
|
|
|
|
|
if (web_preferences &&
|
|
|
|
|
web_preferences->IsEnabled(options::kEnablePreferredSizeMode))
|
|
|
|
|
web_contents()->GetRenderViewHost()->EnablePreferredSizeMode();
|
2020-11-30 05:03:07 +00:00
|
|
|
|
|
|
|
|
|
Emit("did-stop-loading");
|
2014-04-25 04:22:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 15:42:43 +00:00
|
|
|
|
bool WebContents::EmitNavigationEvent(
|
|
|
|
|
const std::string& event,
|
2018-04-26 10:17:55 +00:00
|
|
|
|
content::NavigationHandle* navigation_handle) {
|
|
|
|
|
bool is_main_frame = navigation_handle->IsInMainFrame();
|
|
|
|
|
int frame_tree_node_id = navigation_handle->GetFrameTreeNodeId();
|
|
|
|
|
content::FrameTreeNode* frame_tree_node =
|
2018-05-10 20:52:42 +00:00
|
|
|
|
content::FrameTreeNode::GloballyFindByID(frame_tree_node_id);
|
2018-04-26 10:17:55 +00:00
|
|
|
|
content::RenderFrameHostManager* render_manager =
|
2018-05-10 20:52:42 +00:00
|
|
|
|
frame_tree_node->render_manager();
|
2018-04-26 10:17:55 +00:00
|
|
|
|
content::RenderFrameHost* frame_host = nullptr;
|
|
|
|
|
if (render_manager) {
|
|
|
|
|
frame_host = render_manager->speculative_frame_host();
|
|
|
|
|
if (!frame_host)
|
|
|
|
|
frame_host = render_manager->current_frame_host();
|
|
|
|
|
}
|
|
|
|
|
int frame_process_id = -1, frame_routing_id = -1;
|
|
|
|
|
if (frame_host) {
|
|
|
|
|
frame_process_id = frame_host->GetProcess()->GetID();
|
|
|
|
|
frame_routing_id = frame_host->GetRoutingID();
|
|
|
|
|
}
|
|
|
|
|
bool is_same_document = navigation_handle->IsSameDocument();
|
|
|
|
|
auto url = navigation_handle->GetURL();
|
2018-09-15 15:42:43 +00:00
|
|
|
|
return Emit(event, url, is_same_document, is_main_frame, frame_process_id,
|
|
|
|
|
frame_routing_id);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
|
void WebContents::Message(bool internal,
|
|
|
|
|
const std::string& channel,
|
2020-12-14 18:57:36 +00:00
|
|
|
|
blink::CloneableMessage arguments,
|
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2019-12-06 21:15:23 +00:00
|
|
|
|
TRACE_EVENT1("electron", "WebContents::Message", "channel", channel);
|
2019-04-02 22:38:16 +00:00
|
|
|
|
// webContents.emit('-ipc-message', new Event(), internal, channel,
|
|
|
|
|
// arguments);
|
2020-12-14 18:57:36 +00:00
|
|
|
|
EmitWithSender("-ipc-message", render_frame_host,
|
|
|
|
|
electron::mojom::ElectronBrowser::InvokeCallback(), internal,
|
|
|
|
|
channel, std::move(arguments));
|
2019-04-02 22:38:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 18:57:36 +00:00
|
|
|
|
void WebContents::Invoke(
|
|
|
|
|
bool internal,
|
|
|
|
|
const std::string& channel,
|
|
|
|
|
blink::CloneableMessage arguments,
|
|
|
|
|
electron::mojom::ElectronBrowser::InvokeCallback callback,
|
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2019-12-06 21:15:23 +00:00
|
|
|
|
TRACE_EVENT1("electron", "WebContents::Invoke", "channel", channel);
|
2019-08-23 22:45:50 +00:00
|
|
|
|
// webContents.emit('-ipc-invoke', new Event(), internal, channel, arguments);
|
2020-12-14 18:57:36 +00:00
|
|
|
|
EmitWithSender("-ipc-invoke", render_frame_host, std::move(callback),
|
|
|
|
|
internal, channel, std::move(arguments));
|
2019-05-31 17:25:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 18:57:36 +00:00
|
|
|
|
void WebContents::OnFirstNonEmptyLayout(
|
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
|
if (render_frame_host == web_contents()->GetMainFrame()) {
|
2020-09-28 20:36:45 +00:00
|
|
|
|
Emit("ready-to-show");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 18:57:36 +00:00
|
|
|
|
void WebContents::ReceivePostMessage(
|
|
|
|
|
const std::string& channel,
|
|
|
|
|
blink::TransferableMessage message,
|
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2020-03-12 01:07:54 +00:00
|
|
|
|
auto wrapped_ports =
|
2020-07-16 21:11:44 +00:00
|
|
|
|
MessagePort::EntanglePorts(isolate, std::move(message.ports));
|
2020-03-12 01:07:54 +00:00
|
|
|
|
v8::Local<v8::Value> message_value =
|
2020-07-16 21:11:44 +00:00
|
|
|
|
electron::DeserializeV8Value(isolate, message);
|
2020-12-14 18:57:36 +00:00
|
|
|
|
EmitWithSender("-ipc-ports", render_frame_host,
|
|
|
|
|
electron::mojom::ElectronBrowser::InvokeCallback(), false,
|
|
|
|
|
channel, message_value, std::move(wrapped_ports));
|
2020-03-12 01:07:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 18:57:36 +00:00
|
|
|
|
void WebContents::MessageSync(
|
|
|
|
|
bool internal,
|
|
|
|
|
const std::string& channel,
|
|
|
|
|
blink::CloneableMessage arguments,
|
|
|
|
|
electron::mojom::ElectronBrowser::MessageSyncCallback callback,
|
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2019-12-06 21:15:23 +00:00
|
|
|
|
TRACE_EVENT1("electron", "WebContents::MessageSync", "channel", channel);
|
2019-04-02 22:38:16 +00:00
|
|
|
|
// webContents.emit('-ipc-message-sync', new Event(sender, message), internal,
|
|
|
|
|
// channel, arguments);
|
2020-12-14 18:57:36 +00:00
|
|
|
|
EmitWithSender("-ipc-message-sync", render_frame_host, std::move(callback),
|
|
|
|
|
internal, channel, std::move(arguments));
|
2019-04-02 22:38:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 21:22:23 +00:00
|
|
|
|
void WebContents::MessageTo(bool internal,
|
|
|
|
|
int32_t web_contents_id,
|
|
|
|
|
const std::string& channel,
|
2019-10-09 17:59:08 +00:00
|
|
|
|
blink::CloneableMessage arguments) {
|
2019-12-06 21:15:23 +00:00
|
|
|
|
TRACE_EVENT1("electron", "WebContents::MessageTo", "channel", channel);
|
2021-02-09 20:16:21 +00:00
|
|
|
|
auto* target_web_contents = FromID(web_contents_id);
|
|
|
|
|
|
|
|
|
|
if (target_web_contents) {
|
|
|
|
|
content::RenderFrameHost* frame = target_web_contents->MainFrame();
|
|
|
|
|
DCHECK(frame);
|
|
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope(JavascriptEnvironment::GetIsolate());
|
|
|
|
|
gin::Handle<WebFrameMain> web_frame_main =
|
|
|
|
|
WebFrameMain::From(JavascriptEnvironment::GetIsolate(), frame);
|
2019-04-03 21:22:23 +00:00
|
|
|
|
|
2021-02-09 20:16:21 +00:00
|
|
|
|
int32_t sender_id = ID();
|
|
|
|
|
web_frame_main->GetRendererApi()->Message(internal, channel,
|
|
|
|
|
std::move(arguments), sender_id);
|
2019-04-03 21:22:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::MessageHost(const std::string& channel,
|
2020-12-14 18:57:36 +00:00
|
|
|
|
blink::CloneableMessage arguments,
|
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2019-12-06 21:15:23 +00:00
|
|
|
|
TRACE_EVENT1("electron", "WebContents::MessageHost", "channel", channel);
|
2019-04-03 21:22:23 +00:00
|
|
|
|
// webContents.emit('ipc-message-host', new Event(), channel, args);
|
2020-12-14 18:57:36 +00:00
|
|
|
|
EmitWithSender("ipc-message-host", render_frame_host,
|
|
|
|
|
electron::mojom::ElectronBrowser::InvokeCallback(), channel,
|
|
|
|
|
std::move(arguments));
|
2019-04-03 21:22:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:43:04 +00:00
|
|
|
|
void WebContents::UpdateDraggableRegions(
|
|
|
|
|
std::vector<mojom::DraggableRegionPtr> regions) {
|
|
|
|
|
for (ExtendedWebContentsObserver& observer : observers_)
|
|
|
|
|
observer.OnDraggableRegionsUpdated(regions);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
|
void WebContents::RenderFrameDeleted(
|
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2020-10-09 16:50:46 +00:00
|
|
|
|
// A WebFrameMain can outlive its RenderFrameHost so we need to mark it as
|
|
|
|
|
// disposed to prevent access to it.
|
|
|
|
|
WebFrameMain::RenderFrameDeleted(render_frame_host);
|
2019-04-02 22:38:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 15:42:43 +00:00
|
|
|
|
void WebContents::DidStartNavigation(
|
|
|
|
|
content::NavigationHandle* navigation_handle) {
|
|
|
|
|
EmitNavigationEvent("did-start-navigation", navigation_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DidRedirectNavigation(
|
|
|
|
|
content::NavigationHandle* navigation_handle) {
|
|
|
|
|
EmitNavigationEvent("did-redirect-navigation", navigation_handle);
|
2018-04-26 10:17:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 00:19:28 +00:00
|
|
|
|
void WebContents::DidFinishNavigation(
|
|
|
|
|
content::NavigationHandle* navigation_handle) {
|
2018-04-26 10:17:55 +00:00
|
|
|
|
if (!navigation_handle->HasCommitted())
|
|
|
|
|
return;
|
2016-07-14 00:19:28 +00:00
|
|
|
|
bool is_main_frame = navigation_handle->IsInMainFrame();
|
2018-04-26 10:17:55 +00:00
|
|
|
|
content::RenderFrameHost* frame_host =
|
2018-05-10 20:52:42 +00:00
|
|
|
|
navigation_handle->GetRenderFrameHost();
|
2018-04-26 10:17:55 +00:00
|
|
|
|
int frame_process_id = -1, frame_routing_id = -1;
|
|
|
|
|
if (frame_host) {
|
|
|
|
|
frame_process_id = frame_host->GetProcess()->GetID();
|
|
|
|
|
frame_routing_id = frame_host->GetRoutingID();
|
|
|
|
|
}
|
|
|
|
|
if (!navigation_handle->IsErrorPage()) {
|
2019-09-30 22:00:47 +00:00
|
|
|
|
// FIXME: All the Emit() calls below could potentially result in |this|
|
|
|
|
|
// being destroyed (by JS listening for the event and calling
|
|
|
|
|
// webContents.destroy()).
|
2016-07-14 00:19:28 +00:00
|
|
|
|
auto url = navigation_handle->GetURL();
|
2017-08-08 00:49:20 +00:00
|
|
|
|
bool is_same_document = navigation_handle->IsSameDocument();
|
2018-05-01 04:34:41 +00:00
|
|
|
|
if (is_same_document) {
|
2018-05-10 20:52:42 +00:00
|
|
|
|
Emit("did-navigate-in-page", url, is_main_frame, frame_process_id,
|
|
|
|
|
frame_routing_id);
|
2018-05-01 04:34:41 +00:00
|
|
|
|
} else {
|
2018-05-10 20:52:42 +00:00
|
|
|
|
const net::HttpResponseHeaders* http_response =
|
|
|
|
|
navigation_handle->GetResponseHeaders();
|
2018-05-01 04:34:41 +00:00
|
|
|
|
std::string http_status_text;
|
|
|
|
|
int http_response_code = -1;
|
|
|
|
|
if (http_response) {
|
|
|
|
|
http_status_text = http_response->GetStatusText();
|
|
|
|
|
http_response_code = http_response->response_code();
|
|
|
|
|
}
|
2018-05-10 20:52:42 +00:00
|
|
|
|
Emit("did-frame-navigate", url, http_response_code, http_status_text,
|
|
|
|
|
is_main_frame, frame_process_id, frame_routing_id);
|
2018-05-01 04:34:41 +00:00
|
|
|
|
if (is_main_frame) {
|
|
|
|
|
Emit("did-navigate", url, http_response_code, http_status_text);
|
|
|
|
|
}
|
2016-07-14 00:19:28 +00:00
|
|
|
|
}
|
2018-08-16 22:57:40 +00:00
|
|
|
|
if (IsGuest())
|
|
|
|
|
Emit("load-commit", url, is_main_frame);
|
2016-07-14 00:19:28 +00:00
|
|
|
|
} else {
|
|
|
|
|
auto url = navigation_handle->GetURL();
|
|
|
|
|
int code = navigation_handle->GetNetErrorCode();
|
|
|
|
|
auto description = net::ErrorToShortString(code);
|
2018-05-10 20:52:42 +00:00
|
|
|
|
Emit("did-fail-provisional-load", code, description, url, is_main_frame,
|
|
|
|
|
frame_process_id, frame_routing_id);
|
2016-07-14 00:19:28 +00:00
|
|
|
|
|
|
|
|
|
// Do not emit "did-fail-load" for canceled requests.
|
2021-01-13 09:01:51 +00:00
|
|
|
|
if (code != net::ERR_ABORTED) {
|
|
|
|
|
EmitWarning(
|
|
|
|
|
node::Environment::GetCurrent(JavascriptEnvironment::GetIsolate()),
|
|
|
|
|
"Failed to load URL: " + url.possibly_invalid_spec() +
|
|
|
|
|
" with error: " + description,
|
|
|
|
|
"electron");
|
2018-05-10 20:52:42 +00:00
|
|
|
|
Emit("did-fail-load", code, description, url, is_main_frame,
|
|
|
|
|
frame_process_id, frame_routing_id);
|
2021-01-13 09:01:51 +00:00
|
|
|
|
}
|
2016-07-14 00:19:28 +00:00
|
|
|
|
}
|
2014-12-09 22:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 00:19:21 +00:00
|
|
|
|
void WebContents::TitleWasSet(content::NavigationEntry* entry) {
|
2017-12-18 07:54:44 +00:00
|
|
|
|
base::string16 final_title;
|
2017-12-24 12:42:23 +00:00
|
|
|
|
bool explicit_set = true;
|
|
|
|
|
if (entry) {
|
|
|
|
|
auto title = entry->GetTitle();
|
|
|
|
|
auto url = entry->GetURL();
|
|
|
|
|
if (url.SchemeIsFile() && title.empty()) {
|
|
|
|
|
final_title = base::UTF8ToUTF16(url.ExtractFileName());
|
|
|
|
|
explicit_set = false;
|
|
|
|
|
} else {
|
|
|
|
|
final_title = title;
|
|
|
|
|
}
|
2017-12-18 00:19:21 +00:00
|
|
|
|
}
|
2020-02-11 00:37:46 +00:00
|
|
|
|
for (ExtendedWebContentsObserver& observer : observers_)
|
|
|
|
|
observer.OnPageTitleUpdated(final_title, explicit_set);
|
2017-12-18 07:54:44 +00:00
|
|
|
|
Emit("page-title-updated", final_title, explicit_set);
|
2015-04-05 09:20:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-17 19:55:31 +00:00
|
|
|
|
void WebContents::DidUpdateFaviconURL(
|
2020-06-22 17:35:10 +00:00
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
2020-03-03 21:35:05 +00:00
|
|
|
|
const std::vector<blink::mojom::FaviconURLPtr>& urls) {
|
2015-04-17 19:55:31 +00:00
|
|
|
|
std::set<GURL> unique_urls;
|
2016-07-10 11:32:40 +00:00
|
|
|
|
for (const auto& iter : urls) {
|
2020-03-03 21:35:05 +00:00
|
|
|
|
if (iter->icon_type != blink::mojom::FaviconIconType::kFavicon)
|
2015-04-17 19:55:31 +00:00
|
|
|
|
continue;
|
2020-03-03 21:35:05 +00:00
|
|
|
|
const GURL& url = iter->icon_url;
|
2015-04-17 19:55:31 +00:00
|
|
|
|
if (url.is_valid())
|
|
|
|
|
unique_urls.insert(url);
|
|
|
|
|
}
|
2015-04-20 06:50:04 +00:00
|
|
|
|
Emit("page-favicon-updated", unique_urls);
|
2015-04-17 19:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-12 07:36:12 +00:00
|
|
|
|
void WebContents::DevToolsReloadPage() {
|
|
|
|
|
Emit("devtools-reload-page");
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 03:14:19 +00:00
|
|
|
|
void WebContents::DevToolsFocused() {
|
|
|
|
|
Emit("devtools-focused");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsOpened() {
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
DCHECK(inspectable_web_contents_);
|
|
|
|
|
auto handle = FromOrCreate(
|
|
|
|
|
isolate, inspectable_web_contents_->GetDevToolsWebContents());
|
2020-07-16 21:11:44 +00:00
|
|
|
|
devtools_web_contents_.Reset(isolate, handle.ToV8());
|
2015-10-01 03:14:19 +00:00
|
|
|
|
|
2016-05-26 10:29:39 +00:00
|
|
|
|
// Set inspected tabID.
|
2017-04-05 08:34:53 +00:00
|
|
|
|
base::Value tab_id(ID());
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction("DevToolsAPI.setInspectedTabId",
|
|
|
|
|
&tab_id, nullptr, nullptr);
|
2016-05-26 10:29:39 +00:00
|
|
|
|
|
2017-12-01 02:11:37 +00:00
|
|
|
|
// Inherit owner window in devtools when it doesn't have one.
|
2020-11-17 22:15:20 +00:00
|
|
|
|
auto* devtools = inspectable_web_contents_->GetDevToolsWebContents();
|
2019-01-21 16:56:54 +00:00
|
|
|
|
bool has_window = devtools->GetUserData(NativeWindowRelay::UserDataKey());
|
2017-12-01 02:11:37 +00:00
|
|
|
|
if (owner_window() && !has_window)
|
2017-11-30 07:37:26 +00:00
|
|
|
|
handle->SetOwnerWindow(devtools, owner_window());
|
2015-10-01 06:41:01 +00:00
|
|
|
|
|
2015-10-01 03:14:19 +00:00
|
|
|
|
Emit("devtools-opened");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsClosed() {
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2015-10-01 03:14:19 +00:00
|
|
|
|
devtools_web_contents_.Reset();
|
|
|
|
|
|
|
|
|
|
Emit("devtools-closed");
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 21:54:04 +00:00
|
|
|
|
void WebContents::DevToolsResized() {
|
|
|
|
|
for (ExtendedWebContentsObserver& observer : observers_)
|
|
|
|
|
observer.OnDevToolsResized();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
|
bool WebContents::OnMessageReceived(const IPC::Message& message) {
|
|
|
|
|
bool handled = true;
|
|
|
|
|
IPC_BEGIN_MESSAGE_MAP(WebContents, message)
|
2017-05-26 01:38:27 +00:00
|
|
|
|
IPC_MESSAGE_UNHANDLED(handled = false)
|
|
|
|
|
IPC_END_MESSAGE_MAP()
|
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
|
void WebContents::SetOwnerWindow(NativeWindow* owner_window) {
|
|
|
|
|
SetOwnerWindow(GetWebContents(), owner_window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::SetOwnerWindow(content::WebContents* web_contents,
|
|
|
|
|
NativeWindow* owner_window) {
|
|
|
|
|
if (owner_window) {
|
|
|
|
|
owner_window_ = owner_window->GetWeakPtr();
|
|
|
|
|
NativeWindowRelay::CreateForWebContents(web_contents,
|
|
|
|
|
owner_window->GetWeakPtr());
|
|
|
|
|
} else {
|
|
|
|
|
owner_window_ = nullptr;
|
|
|
|
|
web_contents->RemoveUserData(NativeWindowRelay::UserDataKey());
|
|
|
|
|
}
|
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
|
|
|
|
auto* osr_wcv = GetOffScreenWebContentsView();
|
|
|
|
|
if (osr_wcv)
|
|
|
|
|
osr_wcv->SetNativeWindow(owner_window);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::ResetManagedWebContents(bool async) {
|
|
|
|
|
if (async) {
|
|
|
|
|
// Browser context should be destroyed only after the WebContents,
|
|
|
|
|
// this is guaranteed in the sync mode by the order of declaration,
|
|
|
|
|
// in the async version we maintain a reference until the WebContents
|
|
|
|
|
// is destroyed.
|
|
|
|
|
// //electron/patches/chromium/content_browser_main_loop.patch
|
|
|
|
|
// is required to get the right quit closure for the main message loop.
|
2020-11-17 22:15:20 +00:00
|
|
|
|
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(
|
|
|
|
|
FROM_HERE, inspectable_web_contents_.release());
|
2020-11-10 17:06:56 +00:00
|
|
|
|
} else {
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_.reset();
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content::WebContents* WebContents::GetWebContents() const {
|
2020-11-17 22:15:20 +00:00
|
|
|
|
if (!inspectable_web_contents_)
|
2020-11-10 17:06:56 +00:00
|
|
|
|
return nullptr;
|
2020-11-17 22:15:20 +00:00
|
|
|
|
return inspectable_web_contents_->GetWebContents();
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content::WebContents* WebContents::GetDevToolsWebContents() const {
|
2020-11-17 22:15:20 +00:00
|
|
|
|
if (!inspectable_web_contents_)
|
2020-11-10 17:06:56 +00:00
|
|
|
|
return nullptr;
|
2020-11-17 22:15:20 +00:00
|
|
|
|
return inspectable_web_contents_->GetDevToolsWebContents();
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
void WebContents::MarkDestroyed() {
|
|
|
|
|
if (GetAllWebContents().Lookup(id_))
|
|
|
|
|
GetAllWebContents().Remove(id_);
|
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
v8::Local<v8::Object> wrapper;
|
|
|
|
|
if (!GetWrapper(isolate).ToLocal(&wrapper))
|
|
|
|
|
return;
|
|
|
|
|
wrapper->SetAlignedPointerInInternalField(0, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 09:59:59 +00:00
|
|
|
|
// There are three ways of destroying a webContents:
|
2016-07-28 22:48:01 +00:00
|
|
|
|
// 1. call webContents.destroy();
|
2016-01-13 09:59:59 +00:00
|
|
|
|
// 2. garbage collection;
|
|
|
|
|
// 3. user closes the window of webContents;
|
2018-08-16 22:57:40 +00:00
|
|
|
|
// 4. the embedder detaches the frame.
|
2019-07-11 22:25:27 +00:00
|
|
|
|
// For webview only #4 will happen, for BrowserWindow both #1 and #3 may
|
2016-01-13 09:59:59 +00:00
|
|
|
|
// happen. The #2 should never happen for webContents, because webview is
|
|
|
|
|
// managed by GuestViewManager, and BrowserWindow's webContents is managed
|
2018-02-22 03:49:17 +00:00
|
|
|
|
// by api::BrowserWindow.
|
2016-01-13 09:59:59 +00:00
|
|
|
|
// For #1, the destructor will do the cleanup work and we only need to make
|
|
|
|
|
// sure "destroyed" event is emitted. For #3, the content::WebContents will
|
|
|
|
|
// be destroyed on close, and WebContentsDestroyed would be called for it, so
|
|
|
|
|
// we need to make sure the api::WebContents is also deleted.
|
2018-08-16 22:57:40 +00:00
|
|
|
|
// For #4, the WebContents will be destroyed by embedder.
|
2014-07-28 07:29:51 +00:00
|
|
|
|
void WebContents::WebContentsDestroyed() {
|
2020-07-28 20:00:44 +00:00
|
|
|
|
// Give chance for guest delegate to cleanup its observers
|
|
|
|
|
// since the native class is only destroyed in the next tick.
|
|
|
|
|
if (guest_delegate_)
|
|
|
|
|
guest_delegate_->WillDestroy();
|
|
|
|
|
|
2016-01-13 06:47:39 +00:00
|
|
|
|
// Cleanup relationships with other parts.
|
2016-01-12 13:10:24 +00:00
|
|
|
|
|
|
|
|
|
// We can not call Destroy here because we need to call Emit first, but we
|
|
|
|
|
// also do not want any method to be used, so just mark as destroyed here.
|
|
|
|
|
MarkDestroyed();
|
|
|
|
|
|
|
|
|
|
Emit("destroyed");
|
|
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
|
// For guest view based on OOPIF, the WebContents is released by the embedder
|
|
|
|
|
// frame, and we need to clear the reference to the memory.
|
2020-11-17 22:15:20 +00:00
|
|
|
|
if (IsGuest() && inspectable_web_contents_) {
|
|
|
|
|
inspectable_web_contents_->ReleaseWebContents();
|
2018-08-16 22:57:40 +00:00
|
|
|
|
ResetManagedWebContents(false);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-12 13:10:24 +00:00
|
|
|
|
// Destroy the native class in next tick.
|
2020-07-30 16:17:57 +00:00
|
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
|
|
|
|
FROM_HERE, base::BindOnce(
|
|
|
|
|
[](base::WeakPtr<WebContents> wc) {
|
|
|
|
|
if (wc)
|
|
|
|
|
delete wc.get();
|
|
|
|
|
},
|
|
|
|
|
GetWeakPtr()));
|
2014-04-25 03:22:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 14:39:27 +00:00
|
|
|
|
void WebContents::NavigationEntryCommitted(
|
2015-05-11 05:51:52 +00:00
|
|
|
|
const content::LoadCommittedDetails& details) {
|
2020-01-29 09:42:25 +00:00
|
|
|
|
Emit("navigation-entry-committed", details.entry->GetURL(),
|
2017-08-08 00:49:20 +00:00
|
|
|
|
details.is_same_document, details.did_replace_entry);
|
2015-03-05 14:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 13:11:45 +00:00
|
|
|
|
bool WebContents::GetBackgroundThrottling() const {
|
|
|
|
|
return background_throttling_;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 00:32:22 +00:00
|
|
|
|
void WebContents::SetBackgroundThrottling(bool allowed) {
|
|
|
|
|
background_throttling_ = allowed;
|
|
|
|
|
|
2019-12-02 18:29:03 +00:00
|
|
|
|
auto* rfh = web_contents()->GetMainFrame();
|
|
|
|
|
if (!rfh)
|
2018-10-19 00:32:22 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-12-02 18:29:03 +00:00
|
|
|
|
auto* rwhv = rfh->GetView();
|
|
|
|
|
if (!rwhv)
|
2018-10-19 00:32:22 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-12-02 18:29:03 +00:00
|
|
|
|
auto* rwh_impl =
|
|
|
|
|
static_cast<content::RenderWidgetHostImpl*>(rwhv->GetRenderWidgetHost());
|
|
|
|
|
if (!rwh_impl)
|
2018-10-19 00:32:22 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-12-02 18:29:03 +00:00
|
|
|
|
rwh_impl->disable_hidden_ = !background_throttling_;
|
|
|
|
|
web_contents()->GetRenderViewHost()->SetSchedulerThrottling(allowed);
|
2018-10-19 00:32:22 +00:00
|
|
|
|
|
2019-12-02 18:29:03 +00:00
|
|
|
|
if (rwh_impl->is_hidden()) {
|
2020-09-21 08:00:36 +00:00
|
|
|
|
rwh_impl->WasShown({});
|
2018-10-19 00:32:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 01:28:40 +00:00
|
|
|
|
int WebContents::GetProcessID() const {
|
2017-12-18 02:46:23 +00:00
|
|
|
|
return web_contents()->GetMainFrame()->GetProcess()->GetID();
|
2015-06-23 08:16:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 20:29:34 +00:00
|
|
|
|
base::ProcessId WebContents::GetOSProcessID() const {
|
2018-06-25 20:30:00 +00:00
|
|
|
|
base::ProcessHandle process_handle =
|
2018-09-15 00:13:02 +00:00
|
|
|
|
web_contents()->GetMainFrame()->GetProcess()->GetProcess().Handle();
|
2017-04-18 10:31:20 +00:00
|
|
|
|
return base::GetProcId(process_handle);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 16:09:54 +00:00
|
|
|
|
WebContents::Type WebContents::GetType() const {
|
|
|
|
|
return type_;
|
2016-06-08 18:41:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-23 08:16:10 +00:00
|
|
|
|
bool WebContents::Equal(const WebContents* web_contents) const {
|
2018-07-23 09:08:36 +00:00
|
|
|
|
return ID() == web_contents->ID();
|
2015-06-23 08:16:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
|
void WebContents::LoadURL(const GURL& url,
|
|
|
|
|
const gin_helper::Dictionary& options) {
|
2017-01-21 09:29:20 +00:00
|
|
|
|
if (!url.is_valid() || url.spec().size() > url::kMaxURLChars) {
|
2018-04-18 01:55:30 +00:00
|
|
|
|
Emit("did-fail-load", static_cast<int>(net::ERR_INVALID_URL),
|
2016-02-11 02:54:50 +00:00
|
|
|
|
net::ErrorToShortString(net::ERR_INVALID_URL),
|
2018-04-18 01:55:30 +00:00
|
|
|
|
url.possibly_invalid_spec(), true);
|
2016-02-11 02:54:50 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 04:52:30 +00:00
|
|
|
|
content::NavigationController::LoadURLParams params(url);
|
2014-11-06 19:29:41 +00:00
|
|
|
|
|
2018-04-05 23:13:24 +00:00
|
|
|
|
if (!options.Get("httpReferrer", ¶ms.referrer)) {
|
|
|
|
|
GURL http_referrer;
|
|
|
|
|
if (options.Get("httpReferrer", &http_referrer))
|
2019-01-09 20:13:49 +00:00
|
|
|
|
params.referrer =
|
|
|
|
|
content::Referrer(http_referrer.GetAsReferrer(),
|
|
|
|
|
network::mojom::ReferrerPolicy::kDefault);
|
2018-04-05 23:13:24 +00:00
|
|
|
|
}
|
2014-11-06 19:29:41 +00:00
|
|
|
|
|
2015-05-06 04:01:41 +00:00
|
|
|
|
std::string user_agent;
|
2015-06-09 01:27:57 +00:00
|
|
|
|
if (options.Get("userAgent", &user_agent))
|
2020-04-13 23:39:26 +00:00
|
|
|
|
web_contents()->SetUserAgentOverride(
|
|
|
|
|
blink::UserAgentOverride::UserAgentOnly(user_agent), false);
|
2015-05-06 04:01:41 +00:00
|
|
|
|
|
2015-10-26 21:18:16 +00:00
|
|
|
|
std::string extra_headers;
|
|
|
|
|
if (options.Get("extraHeaders", &extra_headers))
|
|
|
|
|
params.extra_headers = extra_headers;
|
|
|
|
|
|
2018-04-11 08:42:14 +00:00
|
|
|
|
scoped_refptr<network::ResourceRequestBody> body;
|
2016-10-09 23:30:38 +00:00
|
|
|
|
if (options.Get("postData", &body)) {
|
|
|
|
|
params.post_data = body;
|
|
|
|
|
params.load_type = content::NavigationController::LOAD_TYPE_HTTP_POST;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 00:49:03 +00:00
|
|
|
|
GURL base_url_for_data_url;
|
|
|
|
|
if (options.Get("baseURLForDataURL", &base_url_for_data_url)) {
|
|
|
|
|
params.base_url_for_data_url = base_url_for_data_url;
|
|
|
|
|
params.load_type = content::NavigationController::LOAD_TYPE_DATA;
|
|
|
|
|
}
|
2017-03-02 18:50:48 +00:00
|
|
|
|
|
2019-11-25 19:12:58 +00:00
|
|
|
|
bool reload_ignoring_cache = false;
|
|
|
|
|
if (options.Get("reloadIgnoringCache", &reload_ignoring_cache) &&
|
|
|
|
|
reload_ignoring_cache) {
|
|
|
|
|
params.reload_type = content::ReloadType::BYPASSING_CACHE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
|
// Calling LoadURLWithParams() can trigger JS which destroys |this|.
|
|
|
|
|
auto weak_this = GetWeakPtr();
|
|
|
|
|
|
2014-12-07 15:43:26 +00:00
|
|
|
|
params.transition_type = ui::PAGE_TRANSITION_TYPED;
|
2015-04-26 13:31:48 +00:00
|
|
|
|
params.should_clear_history_list = true;
|
2014-04-25 04:52:30 +00:00
|
|
|
|
params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
|
2019-03-28 18:13:05 +00:00
|
|
|
|
// Discord non-committed entries to ensure that we don't re-use a pending
|
|
|
|
|
// entry
|
|
|
|
|
web_contents()->GetController().DiscardNonCommittedEntries();
|
2017-02-19 20:43:17 +00:00
|
|
|
|
web_contents()->GetController().LoadURLWithParams(params);
|
2017-04-07 17:34:14 +00:00
|
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
|
// ⚠️WARNING!⚠️
|
|
|
|
|
// LoadURLWithParams() triggers JS events which can call destroy() on |this|.
|
|
|
|
|
// It's not safe to assume that |this| points to valid memory at this point.
|
|
|
|
|
if (!weak_this)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-02-09 20:16:21 +00:00
|
|
|
|
// Required to make beforeunload handler work.
|
|
|
|
|
NotifyUserActivation();
|
2014-04-25 04:52:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 21:49:30 +00:00
|
|
|
|
void WebContents::DownloadURL(const GURL& url) {
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* browser_context = web_contents()->GetBrowserContext();
|
|
|
|
|
auto* download_manager =
|
2018-04-18 01:55:30 +00:00
|
|
|
|
content::BrowserContext::GetDownloadManager(browser_context);
|
2018-04-12 07:30:51 +00:00
|
|
|
|
std::unique_ptr<download::DownloadUrlParameters> download_params(
|
|
|
|
|
content::DownloadRequestUtils::CreateDownloadForWebContentsMainFrame(
|
2019-07-03 21:48:55 +00:00
|
|
|
|
web_contents(), url, MISSING_TRAFFIC_ANNOTATION));
|
2018-04-12 07:30:51 +00:00
|
|
|
|
download_manager->DownloadUrl(std::move(download_params));
|
2015-12-02 21:49:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-15 01:17:45 +00:00
|
|
|
|
GURL WebContents::GetURL() const {
|
|
|
|
|
return web_contents()->GetURL();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 11:36:57 +00:00
|
|
|
|
base::string16 WebContents::GetTitle() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
|
return web_contents()->GetTitle();
|
2014-04-24 09:00:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebContents::IsLoading() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
|
return web_contents()->IsLoading();
|
2014-04-24 09:00:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-18 17:37:08 +00:00
|
|
|
|
bool WebContents::IsLoadingMainFrame() const {
|
2018-04-06 07:22:52 +00:00
|
|
|
|
return web_contents()->IsLoadingToDifferentDocument();
|
2016-04-18 17:37:08 +00:00
|
|
|
|
}
|
2014-04-24 09:00:41 +00:00
|
|
|
|
|
|
|
|
|
bool WebContents::IsWaitingForResponse() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
|
return web_contents()->IsWaitingForResponse();
|
2014-04-24 09:00:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::Stop() {
|
2014-04-25 05:10:16 +00:00
|
|
|
|
web_contents()->Stop();
|
2014-04-24 09:00:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 06:30:26 +00:00
|
|
|
|
void WebContents::GoBack() {
|
2020-02-24 19:43:55 +00:00
|
|
|
|
if (!ElectronBrowserClient::Get()->CanUseCustomSiteInstance()) {
|
|
|
|
|
electron::ElectronBrowserClient::SuppressRendererProcessRestartForOnce();
|
|
|
|
|
}
|
2015-05-11 06:30:26 +00:00
|
|
|
|
web_contents()->GetController().GoBack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::GoForward() {
|
2020-02-24 19:43:55 +00:00
|
|
|
|
if (!ElectronBrowserClient::Get()->CanUseCustomSiteInstance()) {
|
|
|
|
|
electron::ElectronBrowserClient::SuppressRendererProcessRestartForOnce();
|
|
|
|
|
}
|
2015-05-11 06:30:26 +00:00
|
|
|
|
web_contents()->GetController().GoForward();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 08:44:01 +00:00
|
|
|
|
void WebContents::GoToOffset(int offset) {
|
2020-02-24 19:43:55 +00:00
|
|
|
|
if (!ElectronBrowserClient::Get()->CanUseCustomSiteInstance()) {
|
|
|
|
|
electron::ElectronBrowserClient::SuppressRendererProcessRestartForOnce();
|
|
|
|
|
}
|
2015-05-11 08:44:01 +00:00
|
|
|
|
web_contents()->GetController().GoToOffset(offset);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 03:06:26 +00:00
|
|
|
|
const std::string WebContents::GetWebRTCIPHandlingPolicy() const {
|
2018-04-18 01:55:30 +00:00
|
|
|
|
return web_contents()->GetMutableRendererPrefs()->webrtc_ip_handling_policy;
|
2017-03-08 14:55:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::SetWebRTCIPHandlingPolicy(
|
2017-03-09 03:06:26 +00:00
|
|
|
|
const std::string& webrtc_ip_handling_policy) {
|
2017-03-08 14:55:59 +00:00
|
|
|
|
if (GetWebRTCIPHandlingPolicy() == webrtc_ip_handling_policy)
|
|
|
|
|
return;
|
|
|
|
|
web_contents()->GetMutableRendererPrefs()->webrtc_ip_handling_policy =
|
2018-04-18 01:55:30 +00:00
|
|
|
|
webrtc_ip_handling_policy;
|
2017-03-08 14:55:59 +00:00
|
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
|
web_contents()->SyncRendererPrefs();
|
2017-03-08 14:55:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-24 09:00:41 +00:00
|
|
|
|
bool WebContents::IsCrashed() const {
|
2014-04-25 05:10:16 +00:00
|
|
|
|
return web_contents()->IsCrashed();
|
2014-04-24 09:00:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 21:50:24 +00:00
|
|
|
|
void WebContents::ForcefullyCrashRenderer() {
|
|
|
|
|
content::RenderWidgetHostView* view =
|
|
|
|
|
web_contents()->GetRenderWidgetHostView();
|
|
|
|
|
if (!view)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
content::RenderWidgetHost* rwh = view->GetRenderWidgetHost();
|
|
|
|
|
if (!rwh)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
content::RenderProcessHost* rph = rwh->GetProcess();
|
|
|
|
|
if (rph) {
|
|
|
|
|
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
|
|
|
|
|
// A generic |CrashDumpHungChildProcess()| is not implemented for Linux.
|
|
|
|
|
// Instead we send an explicit IPC to crash on the renderer's IO thread.
|
|
|
|
|
rph->ForceCrash();
|
|
|
|
|
#else
|
|
|
|
|
// Try to generate a crash report for the hung process.
|
2020-11-16 17:47:09 +00:00
|
|
|
|
#ifndef MAS_BUILD
|
2020-10-02 21:50:24 +00:00
|
|
|
|
CrashDumpHungChildProcess(rph->GetProcess().Handle());
|
2020-11-16 17:47:09 +00:00
|
|
|
|
#endif
|
2020-10-02 21:50:24 +00:00
|
|
|
|
rph->Shutdown(content::RESULT_CODE_HUNG);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
void WebContents::SetUserAgent(const std::string& user_agent) {
|
2020-04-13 23:39:26 +00:00
|
|
|
|
web_contents()->SetUserAgentOverride(
|
|
|
|
|
blink::UserAgentOverride::UserAgentOnly(user_agent), false);
|
2014-10-24 12:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-22 04:25:10 +00:00
|
|
|
|
std::string WebContents::GetUserAgent() {
|
2020-04-13 23:39:26 +00:00
|
|
|
|
return web_contents()->GetUserAgentOverride().ua_string_override;
|
2015-07-22 04:25:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 17:03:28 +00:00
|
|
|
|
v8::Local<v8::Promise> WebContents::SavePage(
|
|
|
|
|
const base::FilePath& full_file_path,
|
|
|
|
|
const content::SavePageType& save_type) {
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
gin_helper::Promise<void> promise(isolate);
|
2019-03-13 21:30:21 +00:00
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2019-02-14 17:03:28 +00:00
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
|
auto* handler = new SavePageHandler(web_contents(), std::move(promise));
|
|
|
|
|
handler->Handle(full_file_path, save_type);
|
2019-03-13 21:30:21 +00:00
|
|
|
|
|
|
|
|
|
return handle;
|
2015-10-14 04:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
void WebContents::OpenDevTools(gin::Arguments* args) {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2015-06-05 07:41:03 +00:00
|
|
|
|
return;
|
2015-06-24 14:23:38 +00:00
|
|
|
|
|
2016-09-07 02:16:52 +00:00
|
|
|
|
if (!enable_devtools_)
|
2016-09-05 08:27:56 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2016-04-19 12:42:05 +00:00
|
|
|
|
std::string state;
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kWebView || type_ == Type::kBackgroundPage ||
|
2020-09-08 11:55:40 +00:00
|
|
|
|
!owner_window()) {
|
2016-04-19 12:42:05 +00:00
|
|
|
|
state = "detach";
|
2017-12-01 02:11:37 +00:00
|
|
|
|
}
|
2018-11-27 09:34:44 +00:00
|
|
|
|
bool activate = true;
|
2017-12-01 02:11:37 +00:00
|
|
|
|
if (args && args->Length() == 1) {
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin_helper::Dictionary options;
|
2016-04-19 12:42:05 +00:00
|
|
|
|
if (args->GetNext(&options)) {
|
|
|
|
|
options.Get("mode", &state);
|
2018-11-27 09:34:44 +00:00
|
|
|
|
options.Get("activate", &activate);
|
2016-04-19 12:42:05 +00:00
|
|
|
|
}
|
2015-06-05 09:01:17 +00:00
|
|
|
|
}
|
2020-09-08 11:55:40 +00:00
|
|
|
|
|
2020-11-17 22:15:20 +00:00
|
|
|
|
DCHECK(inspectable_web_contents_);
|
|
|
|
|
inspectable_web_contents_->SetDockState(state);
|
|
|
|
|
inspectable_web_contents_->ShowDevTools(activate);
|
2014-11-02 14:34:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-03 14:12:56 +00:00
|
|
|
|
void WebContents::CloseDevTools() {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2015-06-05 07:41:03 +00:00
|
|
|
|
return;
|
2015-06-24 14:23:38 +00:00
|
|
|
|
|
2020-11-17 22:15:20 +00:00
|
|
|
|
DCHECK(inspectable_web_contents_);
|
|
|
|
|
inspectable_web_contents_->CloseDevTools();
|
2014-11-03 14:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebContents::IsDevToolsOpened() {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2015-06-05 07:41:03 +00:00
|
|
|
|
return false;
|
2015-06-25 06:32:38 +00:00
|
|
|
|
|
2020-11-17 22:15:20 +00:00
|
|
|
|
DCHECK(inspectable_web_contents_);
|
|
|
|
|
return inspectable_web_contents_->IsDevToolsViewShowing();
|
2014-11-03 14:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-01 05:11:21 +00:00
|
|
|
|
bool WebContents::IsDevToolsFocused() {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2016-01-01 05:11:21 +00:00
|
|
|
|
return false;
|
2016-01-01 09:45:03 +00:00
|
|
|
|
|
2020-11-17 22:15:20 +00:00
|
|
|
|
DCHECK(inspectable_web_contents_);
|
|
|
|
|
return inspectable_web_contents_->GetView()->IsDevToolsViewFocused();
|
2016-01-01 05:11:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-18 03:06:38 +00:00
|
|
|
|
void WebContents::EnableDeviceEmulation(
|
2020-08-12 18:33:58 +00:00
|
|
|
|
const blink::DeviceEmulationParams& params) {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2015-08-31 09:19:19 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2020-09-08 11:55:40 +00:00
|
|
|
|
DCHECK(web_contents());
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* frame_host = web_contents()->GetMainFrame();
|
2017-12-30 16:25:09 +00:00
|
|
|
|
if (frame_host) {
|
2020-08-12 18:33:58 +00:00
|
|
|
|
auto* widget_host_impl =
|
|
|
|
|
frame_host ? static_cast<content::RenderWidgetHostImpl*>(
|
|
|
|
|
frame_host->GetView()->GetRenderWidgetHost())
|
|
|
|
|
: nullptr;
|
|
|
|
|
if (widget_host_impl) {
|
|
|
|
|
auto& frame_widget = widget_host_impl->GetAssociatedFrameWidget();
|
|
|
|
|
frame_widget->EnableDeviceEmulation(params);
|
|
|
|
|
}
|
2017-12-30 16:25:09 +00:00
|
|
|
|
}
|
2015-08-31 09:19:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DisableDeviceEmulation() {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2015-08-31 09:19:19 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2020-09-08 11:55:40 +00:00
|
|
|
|
DCHECK(web_contents());
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* frame_host = web_contents()->GetMainFrame();
|
2017-12-30 16:25:09 +00:00
|
|
|
|
if (frame_host) {
|
2020-08-12 18:33:58 +00:00
|
|
|
|
auto* widget_host_impl =
|
|
|
|
|
frame_host ? static_cast<content::RenderWidgetHostImpl*>(
|
|
|
|
|
frame_host->GetView()->GetRenderWidgetHost())
|
|
|
|
|
: nullptr;
|
|
|
|
|
if (widget_host_impl) {
|
|
|
|
|
auto& frame_widget = widget_host_impl->GetAssociatedFrameWidget();
|
|
|
|
|
frame_widget->DisableDeviceEmulation();
|
|
|
|
|
}
|
2017-12-30 16:25:09 +00:00
|
|
|
|
}
|
2015-08-31 09:19:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-05 09:01:17 +00:00
|
|
|
|
void WebContents::ToggleDevTools() {
|
|
|
|
|
if (IsDevToolsOpened())
|
|
|
|
|
CloseDevTools();
|
|
|
|
|
else
|
|
|
|
|
OpenDevTools(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-22 07:30:10 +00:00
|
|
|
|
void WebContents::InspectElement(int x, int y) {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2015-06-05 07:41:03 +00:00
|
|
|
|
return;
|
2015-06-24 14:23:38 +00:00
|
|
|
|
|
2016-09-07 02:16:52 +00:00
|
|
|
|
if (!enable_devtools_)
|
2016-09-06 09:53:42 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2020-11-17 22:15:20 +00:00
|
|
|
|
DCHECK(inspectable_web_contents_);
|
|
|
|
|
if (!inspectable_web_contents_->GetDevToolsWebContents())
|
2016-08-23 22:26:13 +00:00
|
|
|
|
OpenDevTools(nullptr);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->InspectElement(x, y);
|
2015-04-22 07:30:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 12:38:27 +00:00
|
|
|
|
void WebContents::InspectSharedWorkerById(const std::string& workerId) {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2019-10-02 12:38:27 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!enable_devtools_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
|
|
|
|
|
if (agent_host->GetType() ==
|
|
|
|
|
content::DevToolsAgentHost::kTypeSharedWorker) {
|
|
|
|
|
if (agent_host->GetId() == workerId) {
|
|
|
|
|
OpenDevTools(nullptr);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->AttachTo(agent_host);
|
2019-10-02 12:38:27 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<scoped_refptr<content::DevToolsAgentHost>>
|
|
|
|
|
WebContents::GetAllSharedWorkers() {
|
|
|
|
|
std::vector<scoped_refptr<content::DevToolsAgentHost>> shared_workers;
|
|
|
|
|
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2019-10-02 12:38:27 +00:00
|
|
|
|
return shared_workers;
|
|
|
|
|
|
|
|
|
|
if (!enable_devtools_)
|
|
|
|
|
return shared_workers;
|
|
|
|
|
|
|
|
|
|
for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
|
|
|
|
|
if (agent_host->GetType() ==
|
|
|
|
|
content::DevToolsAgentHost::kTypeSharedWorker) {
|
|
|
|
|
shared_workers.push_back(agent_host);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return shared_workers;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 18:44:24 +00:00
|
|
|
|
void WebContents::InspectSharedWorker() {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2019-02-01 18:44:24 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!enable_devtools_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
|
|
|
|
|
if (agent_host->GetType() ==
|
|
|
|
|
content::DevToolsAgentHost::kTypeSharedWorker) {
|
|
|
|
|
OpenDevTools(nullptr);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->AttachTo(agent_host);
|
2019-02-01 18:44:24 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-05 07:41:03 +00:00
|
|
|
|
void WebContents::InspectServiceWorker() {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (type_ == Type::kRemote)
|
2015-06-05 07:41:03 +00:00
|
|
|
|
return;
|
2015-06-24 14:23:38 +00:00
|
|
|
|
|
2016-09-07 02:16:52 +00:00
|
|
|
|
if (!enable_devtools_)
|
2016-09-06 09:53:42 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2015-06-05 07:41:03 +00:00
|
|
|
|
for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
|
|
|
|
|
if (agent_host->GetType() ==
|
2017-01-24 07:50:02 +00:00
|
|
|
|
content::DevToolsAgentHost::kTypeServiceWorker) {
|
2015-06-05 09:01:17 +00:00
|
|
|
|
OpenDevTools(nullptr);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->AttachTo(agent_host);
|
2015-06-05 07:41:03 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 19:01:05 +00:00
|
|
|
|
void WebContents::SetIgnoreMenuShortcuts(bool ignore) {
|
2018-09-10 10:06:16 +00:00
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(web_contents());
|
|
|
|
|
DCHECK(web_preferences);
|
|
|
|
|
web_preferences->preference()->SetKey("ignoreMenuShortcuts",
|
|
|
|
|
base::Value(ignore));
|
2017-06-25 19:01:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-15 13:40:49 +00:00
|
|
|
|
void WebContents::SetAudioMuted(bool muted) {
|
|
|
|
|
web_contents()->SetAudioMuted(muted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebContents::IsAudioMuted() {
|
|
|
|
|
return web_contents()->IsAudioMuted();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 11:35:11 +00:00
|
|
|
|
bool WebContents::IsCurrentlyAudible() {
|
|
|
|
|
return web_contents()->IsCurrentlyAudible();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-13 01:57:04 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2020-01-31 19:20:11 +00:00
|
|
|
|
void WebContents::OnGetDefaultPrinter(
|
|
|
|
|
base::Value print_settings,
|
|
|
|
|
printing::CompletionCallback print_callback,
|
|
|
|
|
base::string16 device_name,
|
|
|
|
|
bool silent,
|
|
|
|
|
base::string16 default_printer) {
|
2020-02-05 15:48:51 +00:00
|
|
|
|
// The content::WebContents might be already deleted at this point, and the
|
|
|
|
|
// PrintViewManagerBasic class does not do null check.
|
|
|
|
|
if (!web_contents()) {
|
|
|
|
|
if (print_callback)
|
|
|
|
|
std::move(print_callback).Run(false, "failed");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 19:20:11 +00:00
|
|
|
|
base::string16 printer_name =
|
|
|
|
|
device_name.empty() ? default_printer : device_name;
|
2020-03-04 02:20:31 +00:00
|
|
|
|
|
|
|
|
|
// If there are no valid printers available on the network, we bail.
|
|
|
|
|
if (printer_name.empty() || !IsDeviceNameValid(printer_name)) {
|
|
|
|
|
if (print_callback)
|
|
|
|
|
std::move(print_callback).Run(false, "no valid printers available");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 19:20:11 +00:00
|
|
|
|
print_settings.SetStringKey(printing::kSettingDeviceName, printer_name);
|
|
|
|
|
|
|
|
|
|
auto* print_view_manager =
|
|
|
|
|
printing::PrintViewManagerBasic::FromWebContents(web_contents());
|
|
|
|
|
auto* focused_frame = web_contents()->GetFocusedFrame();
|
|
|
|
|
auto* rfh = focused_frame && focused_frame->HasSelection()
|
|
|
|
|
? focused_frame
|
|
|
|
|
: web_contents()->GetMainFrame();
|
|
|
|
|
|
|
|
|
|
print_view_manager->PrintNow(rfh, silent, std::move(print_settings),
|
|
|
|
|
std::move(print_callback));
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
void WebContents::Print(gin::Arguments* args) {
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin_helper::Dictionary options =
|
|
|
|
|
gin::Dictionary::CreateEmpty(args->isolate());
|
2019-11-08 18:01:50 +00:00
|
|
|
|
base::Value settings(base::Value::Type::DICTIONARY);
|
2019-08-07 14:47:24 +00:00
|
|
|
|
|
2018-11-09 03:42:34 +00:00
|
|
|
|
if (args->Length() >= 1 && !args->GetNext(&options)) {
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
|
.ThrowError("webContents.print(): Invalid print settings specified.");
|
2015-06-13 13:23:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-07 14:47:24 +00:00
|
|
|
|
|
2018-11-09 03:42:34 +00:00
|
|
|
|
printing::CompletionCallback callback;
|
|
|
|
|
if (args->Length() == 2 && !args->GetNext(&callback)) {
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
|
.ThrowError("webContents.print(): Invalid optional callback provided.");
|
2018-11-09 03:42:34 +00:00
|
|
|
|
return;
|
2017-07-21 07:16:27 +00:00
|
|
|
|
}
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
|
|
|
|
// Set optional silent printing
|
|
|
|
|
bool silent = false;
|
2018-11-09 03:42:34 +00:00
|
|
|
|
options.Get("silent", &silent);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2019-08-07 14:47:24 +00:00
|
|
|
|
bool print_background = false;
|
|
|
|
|
options.Get("printBackground", &print_background);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetBoolKey(printing::kSettingShouldPrintBackgrounds,
|
2019-08-07 14:47:24 +00:00
|
|
|
|
print_background);
|
|
|
|
|
|
2019-06-30 05:12:00 +00:00
|
|
|
|
// Set custom margin settings
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin_helper::Dictionary margins =
|
|
|
|
|
gin::Dictionary::CreateEmpty(args->isolate());
|
2019-06-30 05:12:00 +00:00
|
|
|
|
if (options.Get("margins", &margins)) {
|
2020-08-15 01:51:28 +00:00
|
|
|
|
printing::mojom::MarginType margin_type =
|
|
|
|
|
printing::mojom::MarginType::kDefaultMargins;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
margins.Get("marginType", &margin_type);
|
2020-08-15 01:51:28 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingMarginsType,
|
|
|
|
|
static_cast<int>(margin_type));
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2020-08-15 01:51:28 +00:00
|
|
|
|
if (margin_type == printing::mojom::MarginType::kCustomMargins) {
|
2020-02-13 17:15:25 +00:00
|
|
|
|
base::Value custom_margins(base::Value::Type::DICTIONARY);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
int top = 0;
|
|
|
|
|
margins.Get("top", &top);
|
2020-02-13 17:15:25 +00:00
|
|
|
|
custom_margins.SetIntKey(printing::kSettingMarginTop, top);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
int bottom = 0;
|
|
|
|
|
margins.Get("bottom", &bottom);
|
2020-02-13 17:15:25 +00:00
|
|
|
|
custom_margins.SetIntKey(printing::kSettingMarginBottom, bottom);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
int left = 0;
|
|
|
|
|
margins.Get("left", &left);
|
2020-02-13 17:15:25 +00:00
|
|
|
|
custom_margins.SetIntKey(printing::kSettingMarginLeft, left);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
int right = 0;
|
|
|
|
|
margins.Get("right", &right);
|
2020-02-13 17:15:25 +00:00
|
|
|
|
custom_margins.SetIntKey(printing::kSettingMarginRight, right);
|
|
|
|
|
settings.SetPath(printing::kSettingMarginsCustom,
|
|
|
|
|
std::move(custom_margins));
|
2019-06-30 05:12:00 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-08-15 01:51:28 +00:00
|
|
|
|
settings.SetIntKey(
|
|
|
|
|
printing::kSettingMarginsType,
|
|
|
|
|
static_cast<int>(printing::mojom::MarginType::kDefaultMargins));
|
2019-06-30 05:12:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set whether to print color or greyscale
|
|
|
|
|
bool print_color = true;
|
|
|
|
|
options.Get("color", &print_color);
|
2020-09-21 08:00:36 +00:00
|
|
|
|
auto const color_model = print_color ? printing::mojom::ColorModel::kColor
|
|
|
|
|
: printing::mojom::ColorModel::kGray;
|
|
|
|
|
settings.SetIntKey(printing::kSettingColor, static_cast<int>(color_model));
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2019-08-09 20:16:25 +00:00
|
|
|
|
// Is the orientation landscape or portrait.
|
2019-06-30 05:12:00 +00:00
|
|
|
|
bool landscape = false;
|
|
|
|
|
options.Get("landscape", &landscape);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetBoolKey(printing::kSettingLandscape, landscape);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2020-01-31 19:20:11 +00:00
|
|
|
|
// We set the default to the system's default printer and only update
|
|
|
|
|
// if at the Chromium level if the user overrides.
|
2019-08-09 20:16:25 +00:00
|
|
|
|
// Printer device name as opened by the OS.
|
2019-06-30 05:12:00 +00:00
|
|
|
|
base::string16 device_name;
|
|
|
|
|
options.Get("deviceName", &device_name);
|
2020-01-31 02:49:13 +00:00
|
|
|
|
if (!device_name.empty() && !IsDeviceNameValid(device_name)) {
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
|
.ThrowError("webContents.print(): Invalid deviceName provided.");
|
2020-01-31 02:49:13 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
|
|
|
|
int scale_factor = 100;
|
|
|
|
|
options.Get("scaleFactor", &scale_factor);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingScaleFactor, scale_factor);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
|
|
|
|
int pages_per_sheet = 1;
|
|
|
|
|
options.Get("pagesPerSheet", &pages_per_sheet);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingPagesPerSheet, pages_per_sheet);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2019-08-09 20:16:25 +00:00
|
|
|
|
// True if the user wants to print with collate.
|
2019-06-30 05:12:00 +00:00
|
|
|
|
bool collate = true;
|
|
|
|
|
options.Get("collate", &collate);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetBoolKey(printing::kSettingCollate, collate);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2019-08-09 20:16:25 +00:00
|
|
|
|
// The number of individual copies to print
|
2019-06-30 05:12:00 +00:00
|
|
|
|
int copies = 1;
|
|
|
|
|
options.Get("copies", &copies);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingCopies, copies);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2019-08-09 20:16:25 +00:00
|
|
|
|
// Strings to be printed as headers and footers if requested by the user.
|
|
|
|
|
std::string header;
|
|
|
|
|
options.Get("header", &header);
|
|
|
|
|
std::string footer;
|
|
|
|
|
options.Get("footer", &footer);
|
|
|
|
|
|
|
|
|
|
if (!(header.empty() && footer.empty())) {
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetBoolKey(printing::kSettingHeaderFooterEnabled, true);
|
2019-08-09 20:16:25 +00:00
|
|
|
|
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetStringKey(printing::kSettingHeaderFooterTitle, header);
|
|
|
|
|
settings.SetStringKey(printing::kSettingHeaderFooterURL, footer);
|
2019-08-24 01:14:23 +00:00
|
|
|
|
} else {
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetBoolKey(printing::kSettingHeaderFooterEnabled, false);
|
2019-08-09 20:16:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We don't want to allow the user to enable these settings
|
2019-06-30 05:12:00 +00:00
|
|
|
|
// but we need to set them or a CHECK is hit.
|
2020-04-15 00:14:38 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingPrinterType,
|
|
|
|
|
static_cast<int>(printing::PrinterType::kLocal));
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetBoolKey(printing::kSettingShouldPrintSelectionOnly, false);
|
|
|
|
|
settings.SetBoolKey(printing::kSettingRasterizePdf, false);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
|
|
|
|
// Set custom page ranges to print
|
2019-10-25 13:03:28 +00:00
|
|
|
|
std::vector<gin_helper::Dictionary> page_ranges;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
if (options.Get("pageRanges", &page_ranges)) {
|
2019-11-08 18:01:50 +00:00
|
|
|
|
base::Value page_range_list(base::Value::Type::LIST);
|
2019-09-13 14:26:59 +00:00
|
|
|
|
for (auto& range : page_ranges) {
|
2019-06-30 05:12:00 +00:00
|
|
|
|
int from, to;
|
2019-09-13 14:26:59 +00:00
|
|
|
|
if (range.Get("from", &from) && range.Get("to", &to)) {
|
2019-11-08 18:01:50 +00:00
|
|
|
|
base::Value range(base::Value::Type::DICTIONARY);
|
2020-08-28 03:21:29 +00:00
|
|
|
|
// Chromium uses 1-based page ranges, so increment each by 1.
|
|
|
|
|
range.SetIntKey(printing::kSettingPageRangeFrom, from + 1);
|
|
|
|
|
range.SetIntKey(printing::kSettingPageRangeTo, to + 1);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
page_range_list.Append(std::move(range));
|
2019-06-30 05:12:00 +00:00
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-22 20:24:59 +00:00
|
|
|
|
if (!page_range_list.GetList().empty())
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetPath(printing::kSettingPageRange, std::move(page_range_list));
|
2018-11-09 03:42:34 +00:00
|
|
|
|
}
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2019-08-09 20:16:25 +00:00
|
|
|
|
// Duplex type user wants to use.
|
2020-07-13 15:31:39 +00:00
|
|
|
|
printing::mojom::DuplexMode duplex_mode =
|
|
|
|
|
printing::mojom::DuplexMode::kSimplex;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
options.Get("duplexMode", &duplex_mode);
|
2020-05-26 20:06:26 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingDuplexMode,
|
|
|
|
|
static_cast<int>(duplex_mode));
|
2019-06-30 05:12:00 +00:00
|
|
|
|
|
2020-02-05 04:25:02 +00:00
|
|
|
|
// We've already done necessary parameter sanitization at the
|
|
|
|
|
// JS level, so we can simply pass this through.
|
|
|
|
|
base::Value media_size(base::Value::Type::DICTIONARY);
|
|
|
|
|
if (options.Get("mediaSize", &media_size))
|
|
|
|
|
settings.SetKey(printing::kSettingMediaSize, std::move(media_size));
|
|
|
|
|
|
2019-06-30 05:12:00 +00:00
|
|
|
|
// Set custom dots per inch (dpi)
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin_helper::Dictionary dpi_settings;
|
2019-06-30 05:12:00 +00:00
|
|
|
|
int dpi = 72;
|
|
|
|
|
if (options.Get("dpi", &dpi_settings)) {
|
|
|
|
|
int horizontal = 72;
|
|
|
|
|
dpi_settings.Get("horizontal", &horizontal);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingDpiHorizontal, horizontal);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
int vertical = 72;
|
|
|
|
|
dpi_settings.Get("vertical", &vertical);
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingDpiVertical, vertical);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
} else {
|
2019-11-08 18:01:50 +00:00
|
|
|
|
settings.SetIntKey(printing::kSettingDpiHorizontal, dpi);
|
|
|
|
|
settings.SetIntKey(printing::kSettingDpiVertical, dpi);
|
2019-06-30 05:12:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 17:17:11 +00:00
|
|
|
|
print_task_runner_->PostTaskAndReplyWithResult(
|
|
|
|
|
FROM_HERE, base::BindOnce(&GetDefaultPrinterAsync),
|
2020-01-31 19:20:11 +00:00
|
|
|
|
base::BindOnce(&WebContents::OnGetDefaultPrinter,
|
|
|
|
|
weak_factory_.GetWeakPtr(), std::move(settings),
|
|
|
|
|
std::move(callback), device_name, silent));
|
2017-02-06 09:18:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 05:30:59 +00:00
|
|
|
|
v8::Local<v8::Promise> WebContents::PrintToPDF(base::DictionaryValue settings) {
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
2019-02-21 12:32:44 +00:00
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2018-11-09 03:42:34 +00:00
|
|
|
|
PrintPreviewMessageHandler::FromWebContents(web_contents())
|
2019-10-30 05:30:59 +00:00
|
|
|
|
->PrintToPDF(std::move(settings), std::move(promise));
|
2019-02-21 12:32:44 +00:00
|
|
|
|
return handle;
|
2015-06-09 05:07:40 +00:00
|
|
|
|
}
|
2018-11-09 03:42:34 +00:00
|
|
|
|
#endif
|
2015-06-09 05:07:40 +00:00
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
void WebContents::AddWorkSpace(gin::Arguments* args,
|
2015-09-07 08:12:31 +00:00
|
|
|
|
const base::FilePath& path) {
|
2015-07-24 09:39:11 +00:00
|
|
|
|
if (path.empty()) {
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
|
.ThrowError("path cannot be empty");
|
2015-07-24 09:39:11 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-09-12 13:45:08 +00:00
|
|
|
|
DevToolsAddFileSystem(std::string(), path);
|
2015-07-24 09:39:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
void WebContents::RemoveWorkSpace(gin::Arguments* args,
|
2015-09-07 08:12:31 +00:00
|
|
|
|
const base::FilePath& path) {
|
2015-07-24 09:39:11 +00:00
|
|
|
|
if (path.empty()) {
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
|
.ThrowError("path cannot be empty");
|
2015-07-24 09:39:11 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DevToolsRemoveFileSystem(path);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 20:09:47 +00:00
|
|
|
|
void WebContents::Undo() {
|
|
|
|
|
web_contents()->Undo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::Redo() {
|
|
|
|
|
web_contents()->Redo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::Cut() {
|
|
|
|
|
web_contents()->Cut();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::Copy() {
|
|
|
|
|
web_contents()->Copy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::Paste() {
|
|
|
|
|
web_contents()->Paste();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-15 07:15:19 +00:00
|
|
|
|
void WebContents::PasteAndMatchStyle() {
|
|
|
|
|
web_contents()->PasteAndMatchStyle();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 20:09:47 +00:00
|
|
|
|
void WebContents::Delete() {
|
|
|
|
|
web_contents()->Delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::SelectAll() {
|
|
|
|
|
web_contents()->SelectAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::Unselect() {
|
2017-04-13 10:21:30 +00:00
|
|
|
|
web_contents()->CollapseSelection();
|
2015-01-19 20:09:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::Replace(const base::string16& word) {
|
|
|
|
|
web_contents()->Replace(word);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::ReplaceMisspelling(const base::string16& word) {
|
|
|
|
|
web_contents()->ReplaceMisspelling(word);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
uint32_t WebContents::FindInPage(gin::Arguments* args) {
|
2015-12-17 17:27:56 +00:00
|
|
|
|
base::string16 search_text;
|
2015-12-17 23:10:42 +00:00
|
|
|
|
if (!args->GetNext(&search_text) || search_text.empty()) {
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
|
.ThrowError("Must provide a non-empty search content");
|
2015-12-17 23:10:42 +00:00
|
|
|
|
return 0;
|
2015-12-17 17:27:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 22:15:07 +00:00
|
|
|
|
uint32_t request_id = ++find_in_page_request_id_;
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin_helper::Dictionary dict;
|
2019-01-12 01:00:43 +00:00
|
|
|
|
auto options = blink::mojom::FindOptions::New();
|
|
|
|
|
if (args->GetNext(&dict)) {
|
|
|
|
|
dict.Get("forward", &options->forward);
|
|
|
|
|
dict.Get("matchCase", &options->match_case);
|
2020-06-22 17:35:10 +00:00
|
|
|
|
dict.Get("findNext", &options->new_session);
|
2019-01-12 01:00:43 +00:00
|
|
|
|
}
|
2015-12-17 17:27:56 +00:00
|
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
|
web_contents()->Find(request_id, search_text, std::move(options));
|
2015-12-17 23:10:42 +00:00
|
|
|
|
return request_id;
|
2015-12-17 17:27:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::StopFindInPage(content::StopFindAction action) {
|
|
|
|
|
web_contents()->StopFinding(action);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 17:12:38 +00:00
|
|
|
|
void WebContents::ShowDefinitionForSelection() {
|
2020-08-12 18:33:58 +00:00
|
|
|
|
#if defined(OS_MAC)
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* const view = web_contents()->GetRenderWidgetHostView();
|
2016-06-02 17:12:38 +00:00
|
|
|
|
if (view)
|
|
|
|
|
view->ShowDefinitionForSelection();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-26 19:06:11 +00:00
|
|
|
|
void WebContents::CopyImageAt(int x, int y) {
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* const host = web_contents()->GetMainFrame();
|
2016-07-26 19:06:11 +00:00
|
|
|
|
if (host)
|
|
|
|
|
host->CopyImageAt(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-24 04:58:28 +00:00
|
|
|
|
void WebContents::Focus() {
|
2020-03-02 07:51:02 +00:00
|
|
|
|
// Focusing on WebContents does not automatically focus the window on macOS
|
|
|
|
|
// and Linux, do it manually to match the behavior on Windows.
|
2020-08-12 18:33:58 +00:00
|
|
|
|
#if defined(OS_MAC) || defined(OS_LINUX)
|
2020-03-02 07:51:02 +00:00
|
|
|
|
if (owner_window())
|
|
|
|
|
owner_window()->Focus(true);
|
|
|
|
|
#endif
|
2015-07-24 04:58:28 +00:00
|
|
|
|
web_contents()->Focus();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
|
#if !defined(OS_MAC)
|
2016-08-03 03:29:55 +00:00
|
|
|
|
bool WebContents::IsFocused() const {
|
2018-05-16 19:12:45 +00:00
|
|
|
|
auto* view = web_contents()->GetRenderWidgetHostView();
|
2018-04-18 01:55:30 +00:00
|
|
|
|
if (!view)
|
|
|
|
|
return false;
|
2016-08-15 21:13:24 +00:00
|
|
|
|
|
2020-10-27 17:51:45 +00:00
|
|
|
|
if (GetType() != Type::kBackgroundPage) {
|
2018-05-16 19:12:45 +00:00
|
|
|
|
auto* window = web_contents()->GetNativeView()->GetToplevelWindow();
|
2016-08-15 21:13:24 +00:00
|
|
|
|
if (window && !window->IsVisible())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view->HasFocus();
|
2016-08-03 03:29:55 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-09-18 06:20:31 +00:00
|
|
|
|
void WebContents::SendInputEvent(v8::Isolate* isolate,
|
|
|
|
|
v8::Local<v8::Value> input_event) {
|
2018-04-12 12:54:04 +00:00
|
|
|
|
content::RenderWidgetHostView* view =
|
|
|
|
|
web_contents()->GetRenderWidgetHostView();
|
2015-09-18 06:20:31 +00:00
|
|
|
|
if (!view)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-04-12 12:54:04 +00:00
|
|
|
|
content::RenderWidgetHost* rwh = view->GetRenderWidgetHost();
|
2018-04-18 01:55:30 +00:00
|
|
|
|
blink::WebInputEvent::Type type =
|
2019-10-31 07:56:00 +00:00
|
|
|
|
gin::GetWebInputEventType(isolate, input_event);
|
2017-06-16 20:42:33 +00:00
|
|
|
|
if (blink::WebInputEvent::IsMouseEventType(type)) {
|
2015-09-18 06:20:31 +00:00
|
|
|
|
blink::WebMouseEvent mouse_event;
|
2019-10-31 07:56:00 +00:00
|
|
|
|
if (gin::ConvertFromV8(isolate, input_event, &mouse_event)) {
|
2018-05-15 01:59:22 +00:00
|
|
|
|
if (IsOffScreen()) {
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2018-05-15 01:59:22 +00:00
|
|
|
|
GetOffScreenRenderWidgetHostView()->SendMouseEvent(mouse_event);
|
|
|
|
|
#endif
|
|
|
|
|
} else {
|
|
|
|
|
rwh->ForwardMouseEvent(mouse_event);
|
|
|
|
|
}
|
2015-09-18 06:20:31 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-16 20:42:33 +00:00
|
|
|
|
} else if (blink::WebInputEvent::IsKeyboardEventType(type)) {
|
2017-04-13 10:21:30 +00:00
|
|
|
|
content::NativeWebKeyboardEvent keyboard_event(
|
2020-05-26 20:06:26 +00:00
|
|
|
|
blink::WebKeyboardEvent::Type::kRawKeyDown,
|
|
|
|
|
blink::WebInputEvent::Modifiers::kNoModifiers, ui::EventTimeForNow());
|
2019-10-25 13:03:28 +00:00
|
|
|
|
if (gin::ConvertFromV8(isolate, input_event, &keyboard_event)) {
|
2018-04-12 12:54:04 +00:00
|
|
|
|
rwh->ForwardKeyboardEvent(keyboard_event);
|
2015-09-18 06:20:31 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-26 20:06:26 +00:00
|
|
|
|
} else if (type == blink::WebInputEvent::Type::kMouseWheel) {
|
2015-09-18 06:20:31 +00:00
|
|
|
|
blink::WebMouseWheelEvent mouse_wheel_event;
|
2019-10-31 07:56:00 +00:00
|
|
|
|
if (gin::ConvertFromV8(isolate, input_event, &mouse_wheel_event)) {
|
2018-05-15 01:59:22 +00:00
|
|
|
|
if (IsOffScreen()) {
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2018-05-15 01:59:22 +00:00
|
|
|
|
GetOffScreenRenderWidgetHostView()->SendMouseWheelEvent(
|
|
|
|
|
mouse_wheel_event);
|
|
|
|
|
#endif
|
|
|
|
|
} else {
|
2019-04-16 16:23:38 +00:00
|
|
|
|
// Chromium expects phase info in wheel events (and applies a
|
|
|
|
|
// DCHECK to verify it). See: https://crbug.com/756524.
|
|
|
|
|
mouse_wheel_event.phase = blink::WebMouseWheelEvent::kPhaseBegan;
|
2020-04-21 00:25:38 +00:00
|
|
|
|
mouse_wheel_event.dispatch_type =
|
|
|
|
|
blink::WebInputEvent::DispatchType::kBlocking;
|
2019-04-16 16:23:38 +00:00
|
|
|
|
rwh->ForwardWheelEvent(mouse_wheel_event);
|
|
|
|
|
|
|
|
|
|
// Send a synthetic wheel event with phaseEnded to finish scrolling.
|
|
|
|
|
mouse_wheel_event.has_synthetic_phase = true;
|
|
|
|
|
mouse_wheel_event.delta_x = 0;
|
|
|
|
|
mouse_wheel_event.delta_y = 0;
|
|
|
|
|
mouse_wheel_event.phase = blink::WebMouseWheelEvent::kPhaseEnded;
|
|
|
|
|
mouse_wheel_event.dispatch_type =
|
2020-04-21 00:25:38 +00:00
|
|
|
|
blink::WebInputEvent::DispatchType::kEventNonBlocking;
|
2018-05-15 01:59:22 +00:00
|
|
|
|
rwh->ForwardWheelEvent(mouse_wheel_event);
|
|
|
|
|
}
|
2015-09-18 06:20:31 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
isolate->ThrowException(
|
2019-10-25 13:03:28 +00:00
|
|
|
|
v8::Exception::Error(gin::StringToV8(isolate, "Invalid event object")));
|
2015-09-18 06:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
void WebContents::BeginFrameSubscription(gin::Arguments* args) {
|
2018-05-14 17:09:05 +00:00
|
|
|
|
bool only_dirty = false;
|
2016-06-26 02:46:40 +00:00
|
|
|
|
FrameSubscriber::FrameCaptureCallback callback;
|
2016-06-21 00:15:39 +00:00
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
if (args->Length() > 1) {
|
|
|
|
|
if (!args->GetNext(&only_dirty)) {
|
|
|
|
|
args->ThrowError();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-25 16:23:40 +00:00
|
|
|
|
if (!args->GetNext(&callback)) {
|
2016-06-26 02:46:40 +00:00
|
|
|
|
args->ThrowError();
|
|
|
|
|
return;
|
2016-06-25 16:23:40 +00:00
|
|
|
|
}
|
2016-06-21 00:15:39 +00:00
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
|
frame_subscriber_ =
|
|
|
|
|
std::make_unique<FrameSubscriber>(web_contents(), callback, only_dirty);
|
2015-09-18 07:57:43 +00:00
|
|
|
|
}
|
2015-09-18 06:20:31 +00:00
|
|
|
|
|
2015-09-18 07:57:43 +00:00
|
|
|
|
void WebContents::EndFrameSubscription() {
|
2018-04-18 00:45:15 +00:00
|
|
|
|
frame_subscriber_.reset();
|
2015-09-18 07:57:43 +00:00
|
|
|
|
}
|
2015-09-18 06:20:31 +00:00
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
|
void WebContents::StartDrag(const gin_helper::Dictionary& item,
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin::Arguments* args) {
|
2016-07-03 04:58:31 +00:00
|
|
|
|
base::FilePath file;
|
|
|
|
|
std::vector<base::FilePath> files;
|
|
|
|
|
if (!item.Get("files", &files) && item.Get("file", &file)) {
|
|
|
|
|
files.push_back(file);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
|
v8::Local<v8::Value> icon_value;
|
|
|
|
|
if (!item.Get("icon", &icon_value)) {
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
2021-01-04 20:58:31 +00:00
|
|
|
|
.ThrowError("'icon' parameter is required");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NativeImage* icon = nullptr;
|
|
|
|
|
if (!NativeImage::TryConvertNativeImage(args->isolate(), icon_value, &icon) ||
|
|
|
|
|
icon->image().IsEmpty()) {
|
2016-07-03 04:58:31 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start dragging.
|
|
|
|
|
if (!files.empty()) {
|
2020-07-22 05:34:34 +00:00
|
|
|
|
base::CurrentThread::ScopedNestableTaskAllower allow;
|
2016-07-03 04:58:31 +00:00
|
|
|
|
DragFileItems(files, icon->image(), web_contents()->GetNativeView());
|
|
|
|
|
} else {
|
2020-07-20 18:07:02 +00:00
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
|
.ThrowError("Must specify either 'file' or 'files' option");
|
2016-07-03 04:58:31 +00:00
|
|
|
|
}
|
2016-07-03 03:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
|
2016-07-05 22:43:57 +00:00
|
|
|
|
gfx::Rect rect;
|
2020-07-16 21:11:44 +00:00
|
|
|
|
gin_helper::Promise<gfx::Image> promise(args->isolate());
|
2019-02-21 12:32:44 +00:00
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2016-07-05 22:43:57 +00:00
|
|
|
|
|
2018-11-28 04:50:53 +00:00
|
|
|
|
// get rect arguments if they exist
|
|
|
|
|
args->GetNext(&rect);
|
2016-07-05 22:43:57 +00:00
|
|
|
|
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* const view = web_contents()->GetRenderWidgetHostView();
|
2017-04-13 10:21:30 +00:00
|
|
|
|
if (!view) {
|
2019-11-01 06:10:32 +00:00
|
|
|
|
promise.Resolve(gfx::Image());
|
2019-02-21 12:32:44 +00:00
|
|
|
|
return handle;
|
2016-07-05 22:43:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Capture full page if user doesn't specify a |rect|.
|
2018-04-18 01:55:30 +00:00
|
|
|
|
const gfx::Size view_size =
|
|
|
|
|
rect.IsEmpty() ? view->GetViewBounds().size() : rect.size();
|
2016-07-05 22:43:57 +00:00
|
|
|
|
|
|
|
|
|
// By default, the requested bitmap size is the view size in screen
|
|
|
|
|
// coordinates. However, if there's more pixel detail available on the
|
|
|
|
|
// current system, increase the requested bitmap size to capture it all.
|
|
|
|
|
gfx::Size bitmap_size = view_size;
|
|
|
|
|
const gfx::NativeView native_view = view->GetNativeView();
|
2018-04-18 01:55:30 +00:00
|
|
|
|
const float scale = display::Screen::GetScreen()
|
|
|
|
|
->GetDisplayNearestView(native_view)
|
|
|
|
|
.device_scale_factor();
|
2016-07-05 22:43:57 +00:00
|
|
|
|
if (scale > 1.0f)
|
|
|
|
|
bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
view->CopyFromSurface(gfx::Rect(rect.origin(), view_size), bitmap_size,
|
2019-02-21 12:32:44 +00:00
|
|
|
|
base::BindOnce(&OnCapturePageDone, std::move(promise)));
|
|
|
|
|
return handle;
|
2016-07-05 22:43:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
void WebContents::IncrementCapturerCount(gin::Arguments* args) {
|
2020-01-25 00:43:42 +00:00
|
|
|
|
gfx::Size size;
|
|
|
|
|
bool stay_hidden = false;
|
|
|
|
|
|
|
|
|
|
// get size arguments if they exist
|
|
|
|
|
args->GetNext(&size);
|
|
|
|
|
// get stayHidden arguments if they exist
|
|
|
|
|
args->GetNext(&stay_hidden);
|
|
|
|
|
|
|
|
|
|
web_contents()->IncrementCapturerCount(size, stay_hidden);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:07:02 +00:00
|
|
|
|
void WebContents::DecrementCapturerCount(gin::Arguments* args) {
|
2020-01-25 00:43:42 +00:00
|
|
|
|
bool stay_hidden = false;
|
|
|
|
|
|
|
|
|
|
// get stayHidden arguments if they exist
|
|
|
|
|
args->GetNext(&stay_hidden);
|
|
|
|
|
|
|
|
|
|
web_contents()->DecrementCapturerCount(stay_hidden);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebContents::IsBeingCaptured() {
|
|
|
|
|
return web_contents()->IsBeingCaptured();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 20:06:26 +00:00
|
|
|
|
void WebContents::OnCursorChanged(const content::WebCursor& webcursor) {
|
2020-03-14 20:54:14 +00:00
|
|
|
|
const ui::Cursor& cursor = webcursor.cursor();
|
2016-01-31 16:00:14 +00:00
|
|
|
|
|
2020-03-14 20:54:14 +00:00
|
|
|
|
if (cursor.type() == ui::mojom::CursorType::kCustom) {
|
|
|
|
|
Emit("cursor-changed", CursorTypeToString(cursor),
|
|
|
|
|
gfx::Image::CreateFrom1xBitmap(cursor.custom_bitmap()),
|
|
|
|
|
cursor.image_scale_factor(),
|
|
|
|
|
gfx::Size(cursor.custom_bitmap().width(),
|
|
|
|
|
cursor.custom_bitmap().height()),
|
|
|
|
|
cursor.custom_hotspot());
|
2016-01-31 16:00:14 +00:00
|
|
|
|
} else {
|
2020-03-14 20:54:14 +00:00
|
|
|
|
Emit("cursor-changed", CursorTypeToString(cursor));
|
2016-01-31 16:00:14 +00:00
|
|
|
|
}
|
2016-01-30 05:33:55 +00:00
|
|
|
|
}
|
2016-07-18 06:54:52 +00:00
|
|
|
|
|
2015-06-05 07:18:15 +00:00
|
|
|
|
bool WebContents::IsGuest() const {
|
2020-10-27 17:51:45 +00:00
|
|
|
|
return type_ == Type::kWebView;
|
2015-06-05 07:18:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
|
void WebContents::AttachToIframe(content::WebContents* embedder_web_contents,
|
|
|
|
|
int embedder_frame_id) {
|
|
|
|
|
if (guest_delegate_)
|
|
|
|
|
guest_delegate_->AttachToIframe(embedder_web_contents, embedder_frame_id);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 10:10:56 +00:00
|
|
|
|
bool WebContents::IsOffScreen() const {
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2020-10-27 17:51:45 +00:00
|
|
|
|
return type_ == Type::kOffScreen;
|
2017-06-26 09:13:05 +00:00
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2016-07-28 10:10:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2016-08-04 04:47:52 +00:00
|
|
|
|
void WebContents::OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap) {
|
2017-04-12 14:16:27 +00:00
|
|
|
|
Emit("paint", dirty_rect, gfx::Image::CreateFrom1xBitmap(bitmap));
|
2016-08-03 03:29:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::StartPainting() {
|
2018-03-20 01:25:51 +00:00
|
|
|
|
auto* osr_wcv = GetOffScreenWebContentsView();
|
2018-01-25 13:46:30 +00:00
|
|
|
|
if (osr_wcv)
|
|
|
|
|
osr_wcv->SetPainting(true);
|
2016-08-03 03:29:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::StopPainting() {
|
2018-03-20 01:25:51 +00:00
|
|
|
|
auto* osr_wcv = GetOffScreenWebContentsView();
|
2018-01-25 13:46:30 +00:00
|
|
|
|
if (osr_wcv)
|
|
|
|
|
osr_wcv->SetPainting(false);
|
2016-08-03 03:29:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebContents::IsPainting() const {
|
2018-03-20 01:25:51 +00:00
|
|
|
|
auto* osr_wcv = GetOffScreenWebContentsView();
|
2018-01-25 13:46:30 +00:00
|
|
|
|
return osr_wcv && osr_wcv->IsPainting();
|
2016-08-03 03:29:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::SetFrameRate(int frame_rate) {
|
2018-03-20 01:25:51 +00:00
|
|
|
|
auto* osr_wcv = GetOffScreenWebContentsView();
|
2018-01-25 13:46:30 +00:00
|
|
|
|
if (osr_wcv)
|
|
|
|
|
osr_wcv->SetFrameRate(frame_rate);
|
2016-08-03 03:29:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int WebContents::GetFrameRate() const {
|
2018-03-20 01:25:51 +00:00
|
|
|
|
auto* osr_wcv = GetOffScreenWebContentsView();
|
2018-01-25 13:46:30 +00:00
|
|
|
|
return osr_wcv ? osr_wcv->GetFrameRate() : 0;
|
2016-08-03 03:29:55 +00:00
|
|
|
|
}
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#endif
|
2016-08-03 03:29:55 +00:00
|
|
|
|
|
2016-09-01 17:25:12 +00:00
|
|
|
|
void WebContents::Invalidate() {
|
2017-02-08 07:08:03 +00:00
|
|
|
|
if (IsOffScreen()) {
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2018-05-15 01:59:22 +00:00
|
|
|
|
auto* osr_rwhv = GetOffScreenRenderWidgetHostView();
|
2017-02-08 07:08:03 +00:00
|
|
|
|
if (osr_rwhv)
|
|
|
|
|
osr_rwhv->Invalidate();
|
2017-06-26 09:13:05 +00:00
|
|
|
|
#endif
|
2017-02-08 08:05:16 +00:00
|
|
|
|
} else {
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* const window = owner_window();
|
2017-02-14 20:30:23 +00:00
|
|
|
|
if (window)
|
|
|
|
|
window->Invalidate();
|
2017-02-08 07:08:03 +00:00
|
|
|
|
}
|
2016-09-01 17:25:12 +00:00
|
|
|
|
}
|
2016-08-03 03:29:55 +00:00
|
|
|
|
|
2019-07-03 01:22:09 +00:00
|
|
|
|
gfx::Size WebContents::GetSizeForNewRenderView(content::WebContents* wc) {
|
2017-05-11 21:48:14 +00:00
|
|
|
|
if (IsOffScreen() && wc == web_contents()) {
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* relay = NativeWindowRelay::FromWebContents(web_contents());
|
2017-05-11 21:48:14 +00:00
|
|
|
|
if (relay) {
|
2018-10-02 22:14:43 +00:00
|
|
|
|
auto* owner_window = relay->GetNativeWindow();
|
|
|
|
|
return owner_window ? owner_window->GetSize() : gfx::Size();
|
2017-05-11 21:48:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gfx::Size();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 14:13:20 +00:00
|
|
|
|
void WebContents::SetZoomLevel(double level) {
|
2017-01-30 11:18:40 +00:00
|
|
|
|
zoom_controller_->SetZoomLevel(level);
|
2017-01-29 14:13:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 17:15:23 +00:00
|
|
|
|
double WebContents::GetZoomLevel() const {
|
2017-01-30 11:18:40 +00:00
|
|
|
|
return zoom_controller_->GetZoomLevel();
|
2017-01-29 14:13:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 23:13:05 +00:00
|
|
|
|
void WebContents::SetZoomFactor(gin_helper::ErrorThrower thrower,
|
|
|
|
|
double factor) {
|
|
|
|
|
if (factor < std::numeric_limits<double>::epsilon()) {
|
|
|
|
|
thrower.ThrowError("'zoomFactor' must be a double greater than 0.0");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 19:57:34 +00:00
|
|
|
|
auto level = blink::PageZoomFactorToZoomLevel(factor);
|
2017-01-29 14:13:20 +00:00
|
|
|
|
SetZoomLevel(level);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 17:15:23 +00:00
|
|
|
|
double WebContents::GetZoomFactor() const {
|
2017-01-29 14:13:20 +00:00
|
|
|
|
auto level = GetZoomLevel();
|
2019-10-18 19:57:34 +00:00
|
|
|
|
return blink::PageZoomLevelToZoomFactor(level);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 01:18:23 +00:00
|
|
|
|
void WebContents::SetTemporaryZoomLevel(double level) {
|
2017-01-30 17:06:50 +00:00
|
|
|
|
zoom_controller_->SetTemporaryZoomLevel(level);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 18:57:36 +00:00
|
|
|
|
void WebContents::DoGetZoomLevel(
|
|
|
|
|
electron::mojom::ElectronBrowser::DoGetZoomLevelCallback callback) {
|
2019-06-04 01:18:23 +00:00
|
|
|
|
std::move(callback).Run(GetZoomLevel());
|
2016-09-01 17:25:12 +00:00
|
|
|
|
}
|
2016-08-03 03:29:55 +00:00
|
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
|
std::vector<base::FilePath> WebContents::GetPreloadPaths() const {
|
2019-07-03 15:05:45 +00:00
|
|
|
|
auto result = SessionPreferences::GetValidPreloads(GetBrowserContext());
|
|
|
|
|
|
2018-08-10 22:19:49 +00:00
|
|
|
|
if (auto* web_preferences = WebContentsPreferences::From(web_contents())) {
|
2020-11-10 17:06:03 +00:00
|
|
|
|
base::FilePath preload;
|
2018-08-10 22:19:49 +00:00
|
|
|
|
if (web_preferences->GetPreloadPath(&preload)) {
|
2019-07-03 15:05:45 +00:00
|
|
|
|
result.emplace_back(preload);
|
2018-08-10 22:19:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-03 15:05:45 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
2018-08-10 22:19:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 17:15:23 +00:00
|
|
|
|
v8::Local<v8::Value> WebContents::GetWebPreferences(
|
|
|
|
|
v8::Isolate* isolate) const {
|
2018-03-08 07:12:45 +00:00
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(web_contents());
|
2018-03-14 02:19:22 +00:00
|
|
|
|
if (!web_preferences)
|
|
|
|
|
return v8::Null(isolate);
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::ConvertToV8(isolate, *web_preferences->preference());
|
2015-09-22 13:56:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 17:15:23 +00:00
|
|
|
|
v8::Local<v8::Value> WebContents::GetLastWebPreferences(
|
|
|
|
|
v8::Isolate* isolate) const {
|
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(web_contents());
|
2018-03-15 04:56:46 +00:00
|
|
|
|
if (!web_preferences)
|
|
|
|
|
return v8::Null(isolate);
|
2019-10-25 13:03:28 +00:00
|
|
|
|
return gin::ConvertToV8(isolate, *web_preferences->last_preference());
|
2018-03-15 04:56:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Local<v8::Value> WebContents::GetOwnerBrowserWindow(
|
|
|
|
|
v8::Isolate* isolate) const {
|
2015-10-01 05:45:59 +00:00
|
|
|
|
if (owner_window())
|
2020-07-16 21:11:44 +00:00
|
|
|
|
return BrowserWindow::From(isolate, owner_window());
|
2015-10-01 05:45:59 +00:00
|
|
|
|
else
|
2020-07-16 21:11:44 +00:00
|
|
|
|
return v8::Null(isolate);
|
2015-10-01 05:45:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 03:14:19 +00:00
|
|
|
|
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
|
|
|
|
|
return v8::Local<v8::Value>::New(isolate, session_);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-19 20:13:24 +00:00
|
|
|
|
content::WebContents* WebContents::HostWebContents() const {
|
2016-02-17 08:52:19 +00:00
|
|
|
|
if (!embedder_)
|
|
|
|
|
return nullptr;
|
|
|
|
|
return embedder_->web_contents();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
|
void WebContents::SetEmbedder(const WebContents* embedder) {
|
|
|
|
|
if (embedder) {
|
|
|
|
|
NativeWindow* owner_window = nullptr;
|
2018-04-17 22:41:47 +00:00
|
|
|
|
auto* relay = NativeWindowRelay::FromWebContents(embedder->web_contents());
|
2016-09-08 17:01:01 +00:00
|
|
|
|
if (relay) {
|
2018-10-02 22:14:43 +00:00
|
|
|
|
owner_window = relay->GetNativeWindow();
|
2016-09-08 17:01:01 +00:00
|
|
|
|
}
|
|
|
|
|
if (owner_window)
|
|
|
|
|
SetOwnerWindow(owner_window);
|
|
|
|
|
|
|
|
|
|
content::RenderWidgetHostView* rwhv =
|
|
|
|
|
web_contents()->GetRenderWidgetHostView();
|
|
|
|
|
if (rwhv) {
|
|
|
|
|
rwhv->Hide();
|
|
|
|
|
rwhv->Show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 07:37:26 +00:00
|
|
|
|
void WebContents::SetDevToolsWebContents(const WebContents* devtools) {
|
2020-11-17 22:15:20 +00:00
|
|
|
|
if (inspectable_web_contents_)
|
|
|
|
|
inspectable_web_contents_->SetDevToolsWebContents(devtools->web_contents());
|
2017-11-30 07:37:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Local<v8::Value> WebContents::GetNativeView(v8::Isolate* isolate) const {
|
2017-08-21 10:50:12 +00:00
|
|
|
|
gfx::NativeView ptr = web_contents()->GetNativeView();
|
2020-07-16 21:11:44 +00:00
|
|
|
|
auto buffer = node::Buffer::Copy(isolate, reinterpret_cast<char*>(&ptr),
|
2018-04-18 01:55:30 +00:00
|
|
|
|
sizeof(gfx::NativeView));
|
2017-08-21 10:50:12 +00:00
|
|
|
|
if (buffer.IsEmpty())
|
2020-07-16 21:11:44 +00:00
|
|
|
|
return v8::Null(isolate);
|
2017-08-21 10:50:12 +00:00
|
|
|
|
else
|
|
|
|
|
return buffer.ToLocalChecked();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 03:14:19 +00:00
|
|
|
|
v8::Local<v8::Value> WebContents::DevToolsWebContents(v8::Isolate* isolate) {
|
|
|
|
|
if (devtools_web_contents_.IsEmpty())
|
|
|
|
|
return v8::Null(isolate);
|
|
|
|
|
else
|
|
|
|
|
return v8::Local<v8::Value>::New(isolate, devtools_web_contents_);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 18:22:23 +00:00
|
|
|
|
v8::Local<v8::Value> WebContents::Debugger(v8::Isolate* isolate) {
|
|
|
|
|
if (debugger_.IsEmpty()) {
|
2019-06-19 21:23:04 +00:00
|
|
|
|
auto handle = electron::api::Debugger::Create(isolate, web_contents());
|
2016-01-21 18:22:23 +00:00
|
|
|
|
debugger_.Reset(isolate, handle.ToV8());
|
|
|
|
|
}
|
|
|
|
|
return v8::Local<v8::Value>::New(isolate, debugger_);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 06:57:29 +00:00
|
|
|
|
bool WebContents::WasInitiallyShown() {
|
|
|
|
|
return initially_shown_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 16:50:46 +00:00
|
|
|
|
content::RenderFrameHost* WebContents::MainFrame() {
|
|
|
|
|
return web_contents()->GetMainFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 22:47:41 +00:00
|
|
|
|
void WebContents::GrantOriginAccess(const GURL& url) {
|
2018-10-02 19:01:04 +00:00
|
|
|
|
content::ChildProcessSecurityPolicy::GetInstance()->GrantCommitOrigin(
|
2018-04-12 07:37:54 +00:00
|
|
|
|
web_contents()->GetMainFrame()->GetProcess()->GetID(),
|
|
|
|
|
url::Origin::Create(url));
|
2017-06-30 22:47:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 13:21:38 +00:00
|
|
|
|
void WebContents::NotifyUserActivation() {
|
2021-02-09 20:16:21 +00:00
|
|
|
|
content::RenderFrameHost* frame = web_contents()->GetMainFrame();
|
|
|
|
|
if (frame)
|
|
|
|
|
frame->NotifyUserActivation(
|
|
|
|
|
blink::mojom::UserActivationNotificationType::kInteraction);
|
2020-05-26 13:21:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 17:11:11 +00:00
|
|
|
|
v8::Local<v8::Promise> WebContents::TakeHeapSnapshot(
|
2020-07-16 21:11:44 +00:00
|
|
|
|
v8::Isolate* isolate,
|
2019-04-29 17:11:11 +00:00
|
|
|
|
const base::FilePath& file_path) {
|
2020-07-16 21:11:44 +00:00
|
|
|
|
gin_helper::Promise<void> promise(isolate);
|
2019-04-29 17:11:11 +00:00
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2018-09-18 18:00:31 +00:00
|
|
|
|
|
2019-04-29 17:11:11 +00:00
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
2018-09-18 18:00:31 +00:00
|
|
|
|
base::File file(file_path,
|
|
|
|
|
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
|
2019-04-02 22:38:16 +00:00
|
|
|
|
if (!file.IsValid()) {
|
2019-04-29 17:11:11 +00:00
|
|
|
|
promise.RejectWithErrorMessage("takeHeapSnapshot failed");
|
|
|
|
|
return handle;
|
2019-04-02 22:38:16 +00:00
|
|
|
|
}
|
2018-09-18 18:00:31 +00:00
|
|
|
|
|
|
|
|
|
auto* frame_host = web_contents()->GetMainFrame();
|
2019-04-02 22:38:16 +00:00
|
|
|
|
if (!frame_host) {
|
2019-04-29 17:11:11 +00:00
|
|
|
|
promise.RejectWithErrorMessage("takeHeapSnapshot failed");
|
|
|
|
|
return handle;
|
2019-04-02 22:38:16 +00:00
|
|
|
|
}
|
2018-09-18 18:00:31 +00:00
|
|
|
|
|
2021-02-09 20:16:21 +00:00
|
|
|
|
if (!frame_host->IsRenderFrameCreated()) {
|
|
|
|
|
promise.RejectWithErrorMessage("takeHeapSnapshot failed");
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
|
// This dance with `base::Owned` is to ensure that the interface stays alive
|
|
|
|
|
// until the callback is called. Otherwise it would be closed at the end of
|
|
|
|
|
// this function.
|
2019-10-28 22:12:35 +00:00
|
|
|
|
auto electron_renderer =
|
2021-02-09 20:16:21 +00:00
|
|
|
|
std::make_unique<mojo::Remote<mojom::ElectronRenderer>>();
|
|
|
|
|
frame_host->GetRemoteInterfaces()->GetInterface(
|
|
|
|
|
electron_renderer->BindNewPipeAndPassReceiver());
|
2019-10-28 22:12:35 +00:00
|
|
|
|
auto* raw_ptr = electron_renderer.get();
|
2019-04-02 22:38:16 +00:00
|
|
|
|
(*raw_ptr)->TakeHeapSnapshot(
|
2020-10-28 00:33:04 +00:00
|
|
|
|
mojo::WrapPlatformFile(base::ScopedPlatformFile(file.TakePlatformFile())),
|
2019-04-29 17:11:11 +00:00
|
|
|
|
base::BindOnce(
|
2021-02-09 20:16:21 +00:00
|
|
|
|
[](mojo::Remote<mojom::ElectronRenderer>* ep,
|
2019-11-01 06:10:32 +00:00
|
|
|
|
gin_helper::Promise<void> promise, bool success) {
|
2019-04-29 17:11:11 +00:00
|
|
|
|
if (success) {
|
|
|
|
|
promise.Resolve();
|
|
|
|
|
} else {
|
|
|
|
|
promise.RejectWithErrorMessage("takeHeapSnapshot failed");
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-10-28 22:12:35 +00:00
|
|
|
|
base::Owned(std::move(electron_renderer)), std::move(promise)));
|
2019-04-29 17:11:11 +00:00
|
|
|
|
return handle;
|
2018-09-18 18:00:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 22:44:19 +00:00
|
|
|
|
void WebContents::UpdatePreferredSize(content::WebContents* web_contents,
|
|
|
|
|
const gfx::Size& pref_size) {
|
|
|
|
|
Emit("preferred-size-changed", pref_size);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
|
bool WebContents::CanOverscrollContent() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content::ColorChooser* WebContents::OpenColorChooser(
|
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
|
SkColor color,
|
|
|
|
|
const std::vector<blink::mojom::ColorSuggestionPtr>& suggestions) {
|
|
|
|
|
#if BUILDFLAG(ENABLE_COLOR_CHOOSER)
|
|
|
|
|
return chrome::ShowColorChooser(web_contents, color);
|
|
|
|
|
#else
|
|
|
|
|
return nullptr;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 21:02:29 +00:00
|
|
|
|
std::unique_ptr<content::EyeDropper> WebContents::OpenEyeDropper(
|
|
|
|
|
content::RenderFrameHost* frame,
|
|
|
|
|
content::EyeDropperListener* listener) {
|
|
|
|
|
return ShowEyeDropper(frame, listener);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
|
void WebContents::RunFileChooser(
|
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
|
scoped_refptr<content::FileSelectListener> listener,
|
|
|
|
|
const blink::mojom::FileChooserParams& params) {
|
|
|
|
|
if (!web_dialog_helper_)
|
|
|
|
|
web_dialog_helper_ =
|
|
|
|
|
std::make_unique<WebDialogHelper>(owner_window(), offscreen_);
|
|
|
|
|
web_dialog_helper_->RunFileChooser(render_frame_host, std::move(listener),
|
|
|
|
|
params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::EnumerateDirectory(
|
|
|
|
|
content::WebContents* guest,
|
|
|
|
|
scoped_refptr<content::FileSelectListener> listener,
|
|
|
|
|
const base::FilePath& path) {
|
|
|
|
|
if (!web_dialog_helper_)
|
|
|
|
|
web_dialog_helper_ =
|
|
|
|
|
std::make_unique<WebDialogHelper>(owner_window(), offscreen_);
|
|
|
|
|
web_dialog_helper_->EnumerateDirectory(guest, std::move(listener), path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebContents::IsFullscreenForTabOrPending(
|
|
|
|
|
const content::WebContents* source) {
|
|
|
|
|
return html_fullscreen_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blink::SecurityStyle WebContents::GetSecurityStyle(
|
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
|
content::SecurityStyleExplanations* security_style_explanations) {
|
|
|
|
|
SecurityStateTabHelper* helper =
|
|
|
|
|
SecurityStateTabHelper::FromWebContents(web_contents);
|
|
|
|
|
DCHECK(helper);
|
|
|
|
|
return security_state::GetSecurityStyle(helper->GetSecurityLevel(),
|
|
|
|
|
*helper->GetVisibleSecurityState(),
|
|
|
|
|
security_style_explanations);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebContents::TakeFocus(content::WebContents* source, bool reverse) {
|
|
|
|
|
if (source && source->GetOutermostWebContents() == source) {
|
|
|
|
|
// If this is the outermost web contents and the user has tabbed or
|
|
|
|
|
// shift + tabbed through all the elements, reset the focus back to
|
|
|
|
|
// the first or last element so that it doesn't stay in the body.
|
|
|
|
|
source->FocusThroughTabTraversal(reverse);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content::PictureInPictureResult WebContents::EnterPictureInPicture(
|
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
|
const viz::SurfaceId& surface_id,
|
|
|
|
|
const gfx::Size& natural_size) {
|
|
|
|
|
#if BUILDFLAG(ENABLE_PICTURE_IN_PICTURE)
|
|
|
|
|
return PictureInPictureWindowManager::GetInstance()->EnterPictureInPicture(
|
|
|
|
|
web_contents, surface_id, natural_size);
|
|
|
|
|
#else
|
|
|
|
|
return content::PictureInPictureResult::kNotSupported;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::ExitPictureInPicture() {
|
|
|
|
|
#if BUILDFLAG(ENABLE_PICTURE_IN_PICTURE)
|
|
|
|
|
PictureInPictureWindowManager::GetInstance()->ExitPictureInPicture();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsSaveToFile(const std::string& url,
|
|
|
|
|
const std::string& content,
|
|
|
|
|
bool save_as) {
|
|
|
|
|
base::FilePath path;
|
|
|
|
|
auto it = saved_files_.find(url);
|
|
|
|
|
if (it != saved_files_.end() && !save_as) {
|
|
|
|
|
path = it->second;
|
|
|
|
|
} else {
|
|
|
|
|
file_dialog::DialogSettings settings;
|
|
|
|
|
settings.parent_window = owner_window();
|
|
|
|
|
settings.force_detached = offscreen_;
|
|
|
|
|
settings.title = url;
|
|
|
|
|
settings.default_path = base::FilePath::FromUTF8Unsafe(url);
|
|
|
|
|
if (!file_dialog::ShowSaveDialogSync(settings, &path)) {
|
|
|
|
|
base::Value url_value(url);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.canceledSaveURL", &url_value, nullptr, nullptr);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saved_files_[url] = path;
|
|
|
|
|
// Notify DevTools.
|
|
|
|
|
base::Value url_value(url);
|
|
|
|
|
base::Value file_system_path_value(path.AsUTF8Unsafe());
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.savedURL", &url_value, &file_system_path_value, nullptr);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
file_task_runner_->PostTask(FROM_HERE,
|
|
|
|
|
base::BindOnce(&WriteToFile, path, content));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsAppendToFile(const std::string& url,
|
|
|
|
|
const std::string& content) {
|
|
|
|
|
auto it = saved_files_.find(url);
|
|
|
|
|
if (it == saved_files_.end())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Notify DevTools.
|
|
|
|
|
base::Value url_value(url);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction("DevToolsAPI.appendedToURL",
|
|
|
|
|
&url_value, nullptr, nullptr);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
file_task_runner_->PostTask(
|
|
|
|
|
FROM_HERE, base::BindOnce(&AppendToFile, it->second, content));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsRequestFileSystems() {
|
|
|
|
|
auto file_system_paths = GetAddedFileSystemPaths(GetDevToolsWebContents());
|
|
|
|
|
if (file_system_paths.empty()) {
|
|
|
|
|
base::ListValue empty_file_system_value;
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.fileSystemsLoaded", &empty_file_system_value, nullptr,
|
|
|
|
|
nullptr);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<FileSystem> file_systems;
|
|
|
|
|
for (const auto& file_system_path : file_system_paths) {
|
|
|
|
|
base::FilePath path =
|
|
|
|
|
base::FilePath::FromUTF8Unsafe(file_system_path.first);
|
|
|
|
|
std::string file_system_id =
|
|
|
|
|
RegisterFileSystem(GetDevToolsWebContents(), path);
|
|
|
|
|
FileSystem file_system =
|
|
|
|
|
CreateFileSystemStruct(GetDevToolsWebContents(), file_system_id,
|
|
|
|
|
file_system_path.first, file_system_path.second);
|
|
|
|
|
file_systems.push_back(file_system);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base::ListValue file_system_value;
|
|
|
|
|
for (const auto& file_system : file_systems)
|
|
|
|
|
file_system_value.Append(CreateFileSystemValue(file_system));
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.fileSystemsLoaded", &file_system_value, nullptr, nullptr);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsAddFileSystem(
|
|
|
|
|
const std::string& type,
|
|
|
|
|
const base::FilePath& file_system_path) {
|
|
|
|
|
base::FilePath path = file_system_path;
|
|
|
|
|
if (path.empty()) {
|
|
|
|
|
std::vector<base::FilePath> paths;
|
|
|
|
|
file_dialog::DialogSettings settings;
|
|
|
|
|
settings.parent_window = owner_window();
|
|
|
|
|
settings.force_detached = offscreen_;
|
|
|
|
|
settings.properties = file_dialog::OPEN_DIALOG_OPEN_DIRECTORY;
|
|
|
|
|
if (!file_dialog::ShowOpenDialogSync(settings, &paths))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
path = paths[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string file_system_id =
|
|
|
|
|
RegisterFileSystem(GetDevToolsWebContents(), path);
|
|
|
|
|
if (IsDevToolsFileSystemAdded(GetDevToolsWebContents(), path.AsUTF8Unsafe()))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FileSystem file_system = CreateFileSystemStruct(
|
|
|
|
|
GetDevToolsWebContents(), file_system_id, path.AsUTF8Unsafe(), type);
|
|
|
|
|
std::unique_ptr<base::DictionaryValue> file_system_value(
|
|
|
|
|
CreateFileSystemValue(file_system));
|
|
|
|
|
|
|
|
|
|
auto* pref_service = GetPrefService(GetDevToolsWebContents());
|
|
|
|
|
DictionaryPrefUpdate update(pref_service, prefs::kDevToolsFileSystemPaths);
|
|
|
|
|
update.Get()->SetWithoutPathExpansion(path.AsUTF8Unsafe(),
|
|
|
|
|
std::make_unique<base::Value>(type));
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.fileSystemAdded", nullptr, file_system_value.get(), nullptr);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsRemoveFileSystem(
|
|
|
|
|
const base::FilePath& file_system_path) {
|
2020-11-17 22:15:20 +00:00
|
|
|
|
if (!inspectable_web_contents_)
|
2020-11-10 17:06:56 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
std::string path = file_system_path.AsUTF8Unsafe();
|
|
|
|
|
storage::IsolatedContext::GetInstance()->RevokeFileSystemByPath(
|
|
|
|
|
file_system_path);
|
|
|
|
|
|
|
|
|
|
auto* pref_service = GetPrefService(GetDevToolsWebContents());
|
|
|
|
|
DictionaryPrefUpdate update(pref_service, prefs::kDevToolsFileSystemPaths);
|
|
|
|
|
update.Get()->RemoveWithoutPathExpansion(path, nullptr);
|
|
|
|
|
|
|
|
|
|
base::Value file_system_path_value(path);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction("DevToolsAPI.fileSystemRemoved",
|
|
|
|
|
&file_system_path_value,
|
|
|
|
|
nullptr, nullptr);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsIndexPath(
|
|
|
|
|
int request_id,
|
|
|
|
|
const std::string& file_system_path,
|
|
|
|
|
const std::string& excluded_folders_message) {
|
|
|
|
|
if (!IsDevToolsFileSystemAdded(GetDevToolsWebContents(), file_system_path)) {
|
|
|
|
|
OnDevToolsIndexingDone(request_id, file_system_path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (devtools_indexing_jobs_.count(request_id) != 0)
|
|
|
|
|
return;
|
|
|
|
|
std::vector<std::string> excluded_folders;
|
|
|
|
|
std::unique_ptr<base::Value> parsed_excluded_folders =
|
|
|
|
|
base::JSONReader::ReadDeprecated(excluded_folders_message);
|
|
|
|
|
if (parsed_excluded_folders && parsed_excluded_folders->is_list()) {
|
|
|
|
|
for (const base::Value& folder_path : parsed_excluded_folders->GetList()) {
|
|
|
|
|
if (folder_path.is_string())
|
|
|
|
|
excluded_folders.push_back(folder_path.GetString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
devtools_indexing_jobs_[request_id] =
|
|
|
|
|
scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob>(
|
|
|
|
|
devtools_file_system_indexer_->IndexPath(
|
|
|
|
|
file_system_path, excluded_folders,
|
|
|
|
|
base::BindRepeating(
|
|
|
|
|
&WebContents::OnDevToolsIndexingWorkCalculated,
|
|
|
|
|
weak_factory_.GetWeakPtr(), request_id, file_system_path),
|
|
|
|
|
base::BindRepeating(&WebContents::OnDevToolsIndexingWorked,
|
|
|
|
|
weak_factory_.GetWeakPtr(), request_id,
|
|
|
|
|
file_system_path),
|
|
|
|
|
base::BindRepeating(&WebContents::OnDevToolsIndexingDone,
|
|
|
|
|
weak_factory_.GetWeakPtr(), request_id,
|
|
|
|
|
file_system_path)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsStopIndexing(int request_id) {
|
|
|
|
|
auto it = devtools_indexing_jobs_.find(request_id);
|
|
|
|
|
if (it == devtools_indexing_jobs_.end())
|
|
|
|
|
return;
|
|
|
|
|
it->second->Stop();
|
|
|
|
|
devtools_indexing_jobs_.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::DevToolsSearchInPath(int request_id,
|
|
|
|
|
const std::string& file_system_path,
|
|
|
|
|
const std::string& query) {
|
|
|
|
|
if (!IsDevToolsFileSystemAdded(GetDevToolsWebContents(), file_system_path)) {
|
|
|
|
|
OnDevToolsSearchCompleted(request_id, file_system_path,
|
|
|
|
|
std::vector<std::string>());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
devtools_file_system_indexer_->SearchInPath(
|
|
|
|
|
file_system_path, query,
|
|
|
|
|
base::BindRepeating(&WebContents::OnDevToolsSearchCompleted,
|
|
|
|
|
weak_factory_.GetWeakPtr(), request_id,
|
|
|
|
|
file_system_path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(TOOLKIT_VIEWS) && !defined(OS_MAC)
|
|
|
|
|
gfx::ImageSkia WebContents::GetDevToolsWindowIcon() {
|
|
|
|
|
if (!owner_window())
|
|
|
|
|
return gfx::ImageSkia();
|
|
|
|
|
return owner_window()->GetWindowAppIcon();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
|
void WebContents::GetDevToolsWindowWMClass(std::string* name,
|
|
|
|
|
std::string* class_name) {
|
|
|
|
|
*class_name = Browser::Get()->GetName();
|
|
|
|
|
*name = base::ToLowerASCII(*class_name);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void WebContents::OnDevToolsIndexingWorkCalculated(
|
|
|
|
|
int request_id,
|
|
|
|
|
const std::string& file_system_path,
|
|
|
|
|
int total_work) {
|
|
|
|
|
base::Value request_id_value(request_id);
|
|
|
|
|
base::Value file_system_path_value(file_system_path);
|
|
|
|
|
base::Value total_work_value(total_work);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.indexingTotalWorkCalculated", &request_id_value,
|
|
|
|
|
&file_system_path_value, &total_work_value);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::OnDevToolsIndexingWorked(int request_id,
|
|
|
|
|
const std::string& file_system_path,
|
|
|
|
|
int worked) {
|
|
|
|
|
base::Value request_id_value(request_id);
|
|
|
|
|
base::Value file_system_path_value(file_system_path);
|
|
|
|
|
base::Value worked_value(worked);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.indexingWorked", &request_id_value, &file_system_path_value,
|
|
|
|
|
&worked_value);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::OnDevToolsIndexingDone(int request_id,
|
|
|
|
|
const std::string& file_system_path) {
|
|
|
|
|
devtools_indexing_jobs_.erase(request_id);
|
|
|
|
|
base::Value request_id_value(request_id);
|
|
|
|
|
base::Value file_system_path_value(file_system_path);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.indexingDone", &request_id_value, &file_system_path_value,
|
|
|
|
|
nullptr);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::OnDevToolsSearchCompleted(
|
|
|
|
|
int request_id,
|
|
|
|
|
const std::string& file_system_path,
|
|
|
|
|
const std::vector<std::string>& file_paths) {
|
|
|
|
|
base::ListValue file_paths_value;
|
|
|
|
|
for (const auto& file_path : file_paths) {
|
|
|
|
|
file_paths_value.AppendString(file_path);
|
|
|
|
|
}
|
|
|
|
|
base::Value request_id_value(request_id);
|
|
|
|
|
base::Value file_system_path_value(file_system_path);
|
2020-11-17 22:15:20 +00:00
|
|
|
|
inspectable_web_contents_->CallClientFunction(
|
|
|
|
|
"DevToolsAPI.searchCompleted", &request_id_value, &file_system_path_value,
|
|
|
|
|
&file_paths_value);
|
2020-11-10 17:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebContents::SetHtmlApiFullscreen(bool enter_fullscreen) {
|
|
|
|
|
// Window is already in fullscreen mode, save the state.
|
|
|
|
|
if (enter_fullscreen && owner_window_->IsFullscreen()) {
|
|
|
|
|
native_fullscreen_ = true;
|
|
|
|
|
html_fullscreen_ = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exit html fullscreen state but not window's fullscreen mode.
|
|
|
|
|
if (!enter_fullscreen && native_fullscreen_) {
|
|
|
|
|
html_fullscreen_ = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set fullscreen on window if allowed.
|
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(GetWebContents());
|
|
|
|
|
bool html_fullscreenable =
|
|
|
|
|
web_preferences ? !web_preferences->IsEnabled(
|
|
|
|
|
options::kDisableHtmlFullscreenWindowResize)
|
|
|
|
|
: true;
|
|
|
|
|
|
|
|
|
|
if (html_fullscreenable) {
|
|
|
|
|
owner_window_->SetFullScreen(enter_fullscreen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
html_fullscreen_ = enter_fullscreen;
|
|
|
|
|
native_fullscreen_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-03 08:04:46 +00:00
|
|
|
|
// static
|
2020-07-30 16:17:57 +00:00
|
|
|
|
v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
|
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
|
v8::Local<v8::ObjectTemplate> templ) {
|
|
|
|
|
gin::InvokerOptions options;
|
|
|
|
|
options.holder_is_first_argument = true;
|
|
|
|
|
options.holder_type = "WebContents";
|
|
|
|
|
templ->Set(
|
|
|
|
|
gin::StringToSymbol(isolate, "isDestroyed"),
|
|
|
|
|
gin::CreateFunctionTemplate(
|
|
|
|
|
isolate, base::BindRepeating(&gin_helper::Destroyable::IsDestroyed),
|
|
|
|
|
options));
|
|
|
|
|
templ->Set(
|
|
|
|
|
gin::StringToSymbol(isolate, "destroy"),
|
|
|
|
|
gin::CreateFunctionTemplate(
|
|
|
|
|
isolate, base::BindRepeating([](gin::Handle<WebContents> handle) {
|
|
|
|
|
delete handle.get();
|
|
|
|
|
}),
|
|
|
|
|
options));
|
|
|
|
|
// We use gin_helper::ObjectTemplateBuilder instead of
|
|
|
|
|
// gin::ObjectTemplateBuilder here to handle the fact that WebContents is
|
|
|
|
|
// destroyable.
|
|
|
|
|
return gin_helper::ObjectTemplateBuilder(isolate, templ)
|
2020-05-14 13:11:45 +00:00
|
|
|
|
.SetMethod("getBackgroundThrottling",
|
|
|
|
|
&WebContents::GetBackgroundThrottling)
|
2018-10-19 00:32:22 +00:00
|
|
|
|
.SetMethod("setBackgroundThrottling",
|
|
|
|
|
&WebContents::SetBackgroundThrottling)
|
2016-09-06 01:28:40 +00:00
|
|
|
|
.SetMethod("getProcessId", &WebContents::GetProcessID)
|
2017-04-18 10:31:20 +00:00
|
|
|
|
.SetMethod("getOSProcessId", &WebContents::GetOSProcessID)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("equal", &WebContents::Equal)
|
|
|
|
|
.SetMethod("_loadURL", &WebContents::LoadURL)
|
2015-12-03 18:31:51 +00:00
|
|
|
|
.SetMethod("downloadURL", &WebContents::DownloadURL)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("_getURL", &WebContents::GetURL)
|
|
|
|
|
.SetMethod("getTitle", &WebContents::GetTitle)
|
|
|
|
|
.SetMethod("isLoading", &WebContents::IsLoading)
|
2016-04-18 17:37:08 +00:00
|
|
|
|
.SetMethod("isLoadingMainFrame", &WebContents::IsLoadingMainFrame)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse)
|
|
|
|
|
.SetMethod("_stop", &WebContents::Stop)
|
|
|
|
|
.SetMethod("_goBack", &WebContents::GoBack)
|
|
|
|
|
.SetMethod("_goForward", &WebContents::GoForward)
|
|
|
|
|
.SetMethod("_goToOffset", &WebContents::GoToOffset)
|
|
|
|
|
.SetMethod("isCrashed", &WebContents::IsCrashed)
|
2020-10-02 21:50:24 +00:00
|
|
|
|
.SetMethod("forcefullyCrashRenderer",
|
|
|
|
|
&WebContents::ForcefullyCrashRenderer)
|
2020-03-13 17:16:08 +00:00
|
|
|
|
.SetMethod("setUserAgent", &WebContents::SetUserAgent)
|
|
|
|
|
.SetMethod("getUserAgent", &WebContents::GetUserAgent)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("savePage", &WebContents::SavePage)
|
|
|
|
|
.SetMethod("openDevTools", &WebContents::OpenDevTools)
|
|
|
|
|
.SetMethod("closeDevTools", &WebContents::CloseDevTools)
|
|
|
|
|
.SetMethod("isDevToolsOpened", &WebContents::IsDevToolsOpened)
|
2016-01-01 05:11:21 +00:00
|
|
|
|
.SetMethod("isDevToolsFocused", &WebContents::IsDevToolsFocused)
|
2017-06-25 19:01:05 +00:00
|
|
|
|
.SetMethod("enableDeviceEmulation", &WebContents::EnableDeviceEmulation)
|
|
|
|
|
.SetMethod("disableDeviceEmulation", &WebContents::DisableDeviceEmulation)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("toggleDevTools", &WebContents::ToggleDevTools)
|
|
|
|
|
.SetMethod("inspectElement", &WebContents::InspectElement)
|
2018-04-18 01:55:30 +00:00
|
|
|
|
.SetMethod("setIgnoreMenuShortcuts", &WebContents::SetIgnoreMenuShortcuts)
|
2020-03-13 17:16:08 +00:00
|
|
|
|
.SetMethod("setAudioMuted", &WebContents::SetAudioMuted)
|
|
|
|
|
.SetMethod("isAudioMuted", &WebContents::IsAudioMuted)
|
2018-07-12 11:35:11 +00:00
|
|
|
|
.SetMethod("isCurrentlyAudible", &WebContents::IsCurrentlyAudible)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("undo", &WebContents::Undo)
|
|
|
|
|
.SetMethod("redo", &WebContents::Redo)
|
|
|
|
|
.SetMethod("cut", &WebContents::Cut)
|
|
|
|
|
.SetMethod("copy", &WebContents::Copy)
|
|
|
|
|
.SetMethod("paste", &WebContents::Paste)
|
|
|
|
|
.SetMethod("pasteAndMatchStyle", &WebContents::PasteAndMatchStyle)
|
|
|
|
|
.SetMethod("delete", &WebContents::Delete)
|
|
|
|
|
.SetMethod("selectAll", &WebContents::SelectAll)
|
|
|
|
|
.SetMethod("unselect", &WebContents::Unselect)
|
|
|
|
|
.SetMethod("replace", &WebContents::Replace)
|
|
|
|
|
.SetMethod("replaceMisspelling", &WebContents::ReplaceMisspelling)
|
2015-12-17 17:27:56 +00:00
|
|
|
|
.SetMethod("findInPage", &WebContents::FindInPage)
|
|
|
|
|
.SetMethod("stopFindInPage", &WebContents::StopFindInPage)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("focus", &WebContents::Focus)
|
2016-08-03 03:29:55 +00:00
|
|
|
|
.SetMethod("isFocused", &WebContents::IsFocused)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("sendInputEvent", &WebContents::SendInputEvent)
|
2017-06-25 19:01:05 +00:00
|
|
|
|
.SetMethod("beginFrameSubscription", &WebContents::BeginFrameSubscription)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("endFrameSubscription", &WebContents::EndFrameSubscription)
|
2016-07-03 03:26:43 +00:00
|
|
|
|
.SetMethod("startDrag", &WebContents::StartDrag)
|
2018-08-16 22:57:40 +00:00
|
|
|
|
.SetMethod("attachToIframe", &WebContents::AttachToIframe)
|
2019-01-12 06:50:33 +00:00
|
|
|
|
.SetMethod("detachFromOuterFrame", &WebContents::DetachFromOuterFrame)
|
2016-08-03 03:29:55 +00:00
|
|
|
|
.SetMethod("isOffscreen", &WebContents::IsOffScreen)
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2016-08-03 03:29:55 +00:00
|
|
|
|
.SetMethod("startPainting", &WebContents::StartPainting)
|
|
|
|
|
.SetMethod("stopPainting", &WebContents::StopPainting)
|
|
|
|
|
.SetMethod("isPainting", &WebContents::IsPainting)
|
2020-03-13 17:16:08 +00:00
|
|
|
|
.SetMethod("setFrameRate", &WebContents::SetFrameRate)
|
|
|
|
|
.SetMethod("getFrameRate", &WebContents::GetFrameRate)
|
2018-10-01 20:00:53 +00:00
|
|
|
|
#endif
|
2016-09-01 17:25:12 +00:00
|
|
|
|
.SetMethod("invalidate", &WebContents::Invalidate)
|
2020-03-13 17:16:08 +00:00
|
|
|
|
.SetMethod("setZoomLevel", &WebContents::SetZoomLevel)
|
|
|
|
|
.SetMethod("getZoomLevel", &WebContents::GetZoomLevel)
|
|
|
|
|
.SetMethod("setZoomFactor", &WebContents::SetZoomFactor)
|
|
|
|
|
.SetMethod("getZoomFactor", &WebContents::GetZoomFactor)
|
2016-06-08 18:41:14 +00:00
|
|
|
|
.SetMethod("getType", &WebContents::GetType)
|
2019-07-03 15:05:45 +00:00
|
|
|
|
.SetMethod("_getPreloadPaths", &WebContents::GetPreloadPaths)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("getWebPreferences", &WebContents::GetWebPreferences)
|
2018-03-15 04:56:46 +00:00
|
|
|
|
.SetMethod("getLastWebPreferences", &WebContents::GetLastWebPreferences)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow)
|
|
|
|
|
.SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
|
2019-02-01 18:44:24 +00:00
|
|
|
|
.SetMethod("inspectSharedWorker", &WebContents::InspectSharedWorker)
|
2019-10-02 12:38:27 +00:00
|
|
|
|
.SetMethod("inspectSharedWorkerById",
|
|
|
|
|
&WebContents::InspectSharedWorkerById)
|
|
|
|
|
.SetMethod("getAllSharedWorkers", &WebContents::GetAllSharedWorkers)
|
2018-11-09 03:42:34 +00:00
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
|
|
|
|
.SetMethod("_print", &WebContents::Print)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("_printToPDF", &WebContents::PrintToPDF)
|
2018-11-09 03:42:34 +00:00
|
|
|
|
#endif
|
2020-11-10 17:06:03 +00:00
|
|
|
|
.SetMethod("_setNextChildWebPreferences",
|
|
|
|
|
&WebContents::SetNextChildWebPreferences)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
|
|
|
|
|
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
|
2016-06-07 20:13:49 +00:00
|
|
|
|
.SetMethod("showDefinitionForSelection",
|
|
|
|
|
&WebContents::ShowDefinitionForSelection)
|
2016-07-26 19:06:11 +00:00
|
|
|
|
.SetMethod("copyImageAt", &WebContents::CopyImageAt)
|
2016-07-05 22:43:57 +00:00
|
|
|
|
.SetMethod("capturePage", &WebContents::CapturePage)
|
2016-09-08 17:01:01 +00:00
|
|
|
|
.SetMethod("setEmbedder", &WebContents::SetEmbedder)
|
2017-11-30 07:37:26 +00:00
|
|
|
|
.SetMethod("setDevToolsWebContents", &WebContents::SetDevToolsWebContents)
|
2017-08-21 10:50:12 +00:00
|
|
|
|
.SetMethod("getNativeView", &WebContents::GetNativeView)
|
2020-01-25 00:43:42 +00:00
|
|
|
|
.SetMethod("incrementCapturerCount", &WebContents::IncrementCapturerCount)
|
|
|
|
|
.SetMethod("decrementCapturerCount", &WebContents::DecrementCapturerCount)
|
|
|
|
|
.SetMethod("isBeingCaptured", &WebContents::IsBeingCaptured)
|
2017-03-08 14:55:59 +00:00
|
|
|
|
.SetMethod("setWebRTCIPHandlingPolicy",
|
|
|
|
|
&WebContents::SetWebRTCIPHandlingPolicy)
|
|
|
|
|
.SetMethod("getWebRTCIPHandlingPolicy",
|
|
|
|
|
&WebContents::GetWebRTCIPHandlingPolicy)
|
2017-06-30 22:47:41 +00:00
|
|
|
|
.SetMethod("_grantOriginAccess", &WebContents::GrantOriginAccess)
|
2019-04-29 17:11:11 +00:00
|
|
|
|
.SetMethod("takeHeapSnapshot", &WebContents::TakeHeapSnapshot)
|
2016-05-17 12:56:47 +00:00
|
|
|
|
.SetProperty("id", &WebContents::ID)
|
2015-12-03 08:04:46 +00:00
|
|
|
|
.SetProperty("session", &WebContents::Session)
|
2016-02-17 08:52:19 +00:00
|
|
|
|
.SetProperty("hostWebContents", &WebContents::HostWebContents)
|
2016-01-21 18:22:23 +00:00
|
|
|
|
.SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
|
2020-07-30 16:17:57 +00:00
|
|
|
|
.SetProperty("debugger", &WebContents::Debugger)
|
2020-09-04 06:57:29 +00:00
|
|
|
|
.SetProperty("_initiallyShown", &WebContents::WasInitiallyShown)
|
2020-10-09 16:50:46 +00:00
|
|
|
|
.SetProperty("mainFrame", &WebContents::MainFrame)
|
2020-07-30 16:17:57 +00:00
|
|
|
|
.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* WebContents::GetTypeName() {
|
|
|
|
|
return "WebContents";
|
2015-07-06 10:21:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
|
ElectronBrowserContext* WebContents::GetBrowserContext() const {
|
|
|
|
|
return static_cast<ElectronBrowserContext*>(
|
|
|
|
|
web_contents()->GetBrowserContext());
|
2015-07-14 19:13:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-24 08:45:25 +00:00
|
|
|
|
// static
|
2020-11-17 22:14:33 +00:00
|
|
|
|
gin::Handle<WebContents> WebContents::New(
|
2019-10-25 13:03:28 +00:00
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
|
const gin_helper::Dictionary& options) {
|
2020-07-30 16:17:57 +00:00
|
|
|
|
gin::Handle<WebContents> handle =
|
|
|
|
|
gin::CreateHandle(isolate, new WebContents(isolate, options));
|
|
|
|
|
gin_helper::CallMethod(isolate, handle.get(), "_init");
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 08:52:07 +00:00
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<WebContents> WebContents::CreateAndTake(
|
2018-04-18 01:55:30 +00:00
|
|
|
|
v8::Isolate* isolate,
|
2018-10-22 18:02:25 +00:00
|
|
|
|
std::unique_ptr<content::WebContents> web_contents,
|
2018-04-18 01:55:30 +00:00
|
|
|
|
Type type) {
|
2020-07-30 16:17:57 +00:00
|
|
|
|
gin::Handle<WebContents> handle = gin::CreateHandle(
|
2018-10-22 18:02:25 +00:00
|
|
|
|
isolate, new WebContents(isolate, std::move(web_contents), type));
|
2020-07-30 16:17:57 +00:00
|
|
|
|
gin_helper::CallMethod(isolate, handle.get(), "_init");
|
|
|
|
|
return handle;
|
2014-04-24 08:45:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 06:04:13 +00:00
|
|
|
|
// static
|
2020-07-16 23:16:05 +00:00
|
|
|
|
WebContents* WebContents::From(content::WebContents* web_contents) {
|
2020-07-30 16:17:57 +00:00
|
|
|
|
if (!web_contents)
|
|
|
|
|
return nullptr;
|
2020-10-26 18:56:31 +00:00
|
|
|
|
auto* data = static_cast<UserDataLink*>(
|
2020-07-30 16:17:57 +00:00
|
|
|
|
web_contents->GetUserData(kElectronApiWebContentsKey));
|
|
|
|
|
return data ? data->web_contents.get() : nullptr;
|
2018-10-19 08:52:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin::Handle<WebContents> WebContents::FromOrCreate(
|
2018-10-19 08:52:07 +00:00
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
|
content::WebContents* web_contents) {
|
2020-07-16 23:16:05 +00:00
|
|
|
|
WebContents* api_web_contents = From(web_contents);
|
2020-07-30 16:17:57 +00:00
|
|
|
|
if (!api_web_contents) {
|
2020-07-16 23:16:05 +00:00
|
|
|
|
api_web_contents = new WebContents(isolate, web_contents);
|
2020-07-30 16:17:57 +00:00
|
|
|
|
gin_helper::CallMethod(isolate, api_web_contents, "_init");
|
|
|
|
|
}
|
2020-07-16 23:16:05 +00:00
|
|
|
|
return gin::CreateHandle(isolate, api_web_contents);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 23:52:43 +00:00
|
|
|
|
// static
|
|
|
|
|
gin::Handle<WebContents> WebContents::CreateFromWebPreferences(
|
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
|
const gin_helper::Dictionary& web_preferences) {
|
|
|
|
|
// Check if webPreferences has |webContents| option.
|
|
|
|
|
gin::Handle<WebContents> web_contents;
|
|
|
|
|
if (web_preferences.GetHidden("webContents", &web_contents) &&
|
|
|
|
|
!web_contents.IsEmpty()) {
|
|
|
|
|
// Set webPreferences from options if using an existing webContents.
|
|
|
|
|
// These preferences will be used when the webContent launches new
|
|
|
|
|
// render processes.
|
|
|
|
|
auto* existing_preferences =
|
|
|
|
|
WebContentsPreferences::From(web_contents->web_contents());
|
|
|
|
|
base::DictionaryValue web_preferences_dict;
|
|
|
|
|
if (gin::ConvertFromV8(isolate, web_preferences.GetHandle(),
|
|
|
|
|
&web_preferences_dict)) {
|
|
|
|
|
existing_preferences->Clear();
|
|
|
|
|
existing_preferences->Merge(web_preferences_dict);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Create one if not.
|
|
|
|
|
web_contents = WebContents::New(isolate, web_preferences);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return web_contents;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 23:16:05 +00:00
|
|
|
|
// static
|
|
|
|
|
WebContents* WebContents::FromID(int32_t id) {
|
2020-07-30 16:17:57 +00:00
|
|
|
|
return GetAllWebContents().Lookup(id);
|
2014-10-23 06:04:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
// static
|
|
|
|
|
gin::WrapperInfo WebContents::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
|
2014-04-24 08:45:25 +00:00
|
|
|
|
} // namespace api
|
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
|
} // namespace electron
|
2014-10-23 06:04:13 +00:00
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
using electron::api::GetAllWebContents;
|
2019-06-19 21:23:04 +00:00
|
|
|
|
using electron::api::WebContents;
|
2016-08-02 11:38:35 +00:00
|
|
|
|
|
2020-11-23 17:21:00 +00:00
|
|
|
|
gin::Handle<WebContents> WebContentsFromID(v8::Isolate* isolate, int32_t id) {
|
|
|
|
|
WebContents* contents = WebContents::FromID(id);
|
|
|
|
|
return contents ? gin::CreateHandle(isolate, contents)
|
|
|
|
|
: gin::Handle<WebContents>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 16:17:57 +00:00
|
|
|
|
std::vector<gin::Handle<WebContents>> GetAllWebContentsAsV8(
|
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
|
std::vector<gin::Handle<WebContents>> list;
|
|
|
|
|
for (auto iter = base::IDMap<WebContents*>::iterator(&GetAllWebContents());
|
|
|
|
|
!iter.IsAtEnd(); iter.Advance()) {
|
|
|
|
|
list.push_back(gin::CreateHandle(isolate, iter.GetCurrentValue()));
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
|
void* priv) {
|
2014-10-23 06:04:13 +00:00
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-10-25 13:03:28 +00:00
|
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2020-07-30 16:17:57 +00:00
|
|
|
|
dict.Set("WebContents", WebContents::GetConstructor(context));
|
2020-11-23 17:21:00 +00:00
|
|
|
|
dict.SetMethod("fromId", &WebContentsFromID);
|
2020-07-30 16:17:57 +00:00
|
|
|
|
dict.SetMethod("getAllWebContents", &GetAllWebContentsAsV8);
|
2014-10-23 06:04:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_web_contents, Initialize)
|