// Copyright (c) 2014 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef SHELL_BROWSER_API_ELECTRON_API_WEB_CONTENTS_H_ #define SHELL_BROWSER_API_ELECTRON_API_WEB_CONTENTS_H_ #include #include #include #include #include #include "base/observer_list.h" #include "base/observer_list_types.h" #include "content/common/cursors/webcursor.h" #include "content/common/frame.mojom.h" #include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/keyboard_event_processing_result.h" #include "content/public/browser/render_widget_host.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_observer.h" #include "electron/buildflags/buildflags.h" #include "electron/shell/common/api/api.mojom.h" #include "gin/handle.h" #include "gin/wrappable.h" #include "mojo/public/cpp/bindings/receiver_set.h" #include "printing/buildflags/buildflags.h" #include "services/service_manager/public/cpp/binder_registry.h" #include "shell/browser/api/frame_subscriber.h" #include "shell/browser/api/save_page_handler.h" #include "shell/browser/common_web_contents_delegate.h" #include "shell/browser/event_emitter_mixin.h" #include "shell/browser/extended_web_contents_observer.h" #include "shell/common/gin_helper/cleaned_up_at_exit.h" #include "shell/common/gin_helper/constructible.h" #include "shell/common/gin_helper/error_thrower.h" #include "ui/gfx/image/image.h" #if BUILDFLAG(ENABLE_PRINTING) #include "chrome/browser/printing/print_view_manager_basic.h" #include "components/printing/common/print_messages.h" #include "printing/backend/print_backend.h" // nogncheck #include "shell/browser/printing/print_preview_message_handler.h" #if defined(OS_WIN) #include "printing/backend/win_helper.h" #endif #endif #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) #include "extensions/common/view_type.h" namespace extensions { class ScriptExecutor; } #endif namespace blink { struct DeviceEmulationParams; } namespace gin_helper { class Dictionary; } namespace network { class ResourceRequestBody; } namespace gin { class Arguments; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const base::TerminationStatus& status) { switch (status) { case base::TERMINATION_STATUS_NORMAL_TERMINATION: return gin::ConvertToV8(isolate, "clean-exit"); case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: return gin::ConvertToV8(isolate, "abnormal-exit"); case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: return gin::ConvertToV8(isolate, "killed"); case base::TERMINATION_STATUS_PROCESS_CRASHED: return gin::ConvertToV8(isolate, "crashed"); case base::TERMINATION_STATUS_STILL_RUNNING: return gin::ConvertToV8(isolate, "still-running"); case base::TERMINATION_STATUS_LAUNCH_FAILED: return gin::ConvertToV8(isolate, "launch-failed"); case base::TERMINATION_STATUS_OOM: return gin::ConvertToV8(isolate, "oom"); #if defined(OS_WIN) case base::TERMINATION_STATUS_INTEGRITY_FAILURE: return gin::ConvertToV8(isolate, "integrity-failure"); #endif case base::TERMINATION_STATUS_MAX_ENUM: NOTREACHED(); return gin::ConvertToV8(isolate, ""); } NOTREACHED(); return gin::ConvertToV8(isolate, ""); } }; } // namespace gin namespace electron { class ElectronBrowserContext; class ElectronJavaScriptDialogManager; class InspectableWebContents; class WebContentsZoomController; class WebViewGuestDelegate; class FrameSubscriber; #if BUILDFLAG(ENABLE_OSR) class OffScreenRenderWidgetHostView; #endif namespace api { // Wrapper around the content::WebContents. class WebContents : public gin::Wrappable, public gin_helper::EventEmitterMixin, public gin_helper::Constructible, public gin_helper::CleanedUpAtExit, public CommonWebContentsDelegate, public content::WebContentsObserver, public mojom::ElectronBrowser { public: enum class Type { kBackgroundPage, // An extension background page. kBrowserWindow, // Used by BrowserWindow. kBrowserView, // Used by BrowserView. kRemote, // Thin wrap around an existing WebContents. kWebView, // Used by . kOffScreen, // Used for offscreen rendering }; // Create a new WebContents and return the V8 wrapper of it. static gin::Handle Create(v8::Isolate* isolate, const gin_helper::Dictionary& options); static gin::Handle New(v8::Isolate* isolate, const gin_helper::Dictionary& options); // Create a new V8 wrapper for an existing |web_content|. // // The lifetime of |web_contents| will be managed by this class. static gin::Handle CreateAndTake( v8::Isolate* isolate, std::unique_ptr web_contents, Type type); // Get the api::WebContents associated with |web_contents|. Returns nullptr // if there is no associated wrapper. static WebContents* From(content::WebContents* web_contents); static WebContents* FromID(int32_t id); // Get the V8 wrapper of the |web_contents|, or create one if not existed. // // The lifetime of |web_contents| is NOT managed by this class, and the type // of this wrapper is always REMOTE. static gin::Handle FromOrCreate( v8::Isolate* isolate, content::WebContents* web_contents); // gin::Wrappable static gin::WrapperInfo kWrapperInfo; static v8::Local FillObjectTemplate( v8::Isolate*, v8::Local); const char* GetTypeName() override; base::WeakPtr GetWeakPtr() { return weak_factory_.GetWeakPtr(); } // Destroy the managed content::WebContents instance. // // Note: The |async| should only be |true| when users are expecting to use the // webContents immediately after the call. Always pass |false| if you are not // sure. // See https://github.com/electron/electron/issues/8930. // // Note: When destroying a webContents member inside a destructor, the |async| // should always be |false|, otherwise the destroy task might be delayed after // normal shutdown procedure, resulting in an assertion. // The normal pattern for calling this method in destructor is: // api_web_contents_->DestroyWebContents(!Browser::Get()->is_shutting_down()) // See https://github.com/electron/electron/issues/15133. void DestroyWebContents(bool async); bool GetBackgroundThrottling() const; void SetBackgroundThrottling(bool allowed); int GetProcessID() const; base::ProcessId GetOSProcessID() const; Type GetType() const; bool Equal(const WebContents* web_contents) const; void LoadURL(const GURL& url, const gin_helper::Dictionary& options); void DownloadURL(const GURL& url); GURL GetURL() const; base::string16 GetTitle() const; bool IsLoading() const; bool IsLoadingMainFrame() const; bool IsWaitingForResponse() const; void Stop(); void ReloadIgnoringCache(); void GoBack(); void GoForward(); void GoToOffset(int offset); const std::string GetWebRTCIPHandlingPolicy() const; void SetWebRTCIPHandlingPolicy(const std::string& webrtc_ip_handling_policy); bool IsCrashed() const; void ForcefullyCrashRenderer(); void SetUserAgent(const std::string& user_agent); std::string GetUserAgent(); void InsertCSS(const std::string& css); v8::Local SavePage(const base::FilePath& full_file_path, const content::SavePageType& save_type); void OpenDevTools(gin::Arguments* args); void CloseDevTools(); bool IsDevToolsOpened(); bool IsDevToolsFocused(); void ToggleDevTools(); void EnableDeviceEmulation(const blink::DeviceEmulationParams& params); void DisableDeviceEmulation(); void InspectElement(int x, int y); void InspectSharedWorker(); void InspectSharedWorkerById(const std::string& workerId); std::vector> GetAllSharedWorkers(); void InspectServiceWorker(); void SetIgnoreMenuShortcuts(bool ignore); void SetAudioMuted(bool muted); bool IsAudioMuted(); bool IsCurrentlyAudible(); void SetEmbedder(const WebContents* embedder); void SetDevToolsWebContents(const WebContents* devtools); v8::Local GetNativeView(v8::Isolate* isolate) const; void IncrementCapturerCount(gin::Arguments* args); void DecrementCapturerCount(gin::Arguments* args); bool IsBeingCaptured(); #if BUILDFLAG(ENABLE_PRINTING) void OnGetDefaultPrinter(base::Value print_settings, printing::CompletionCallback print_callback, base::string16 device_name, bool silent, base::string16 default_printer); void Print(gin::Arguments* args); printing::PrinterList GetPrinterList(); // Print current page as PDF. v8::Local PrintToPDF(base::DictionaryValue settings); #endif // DevTools workspace api. void AddWorkSpace(gin::Arguments* args, const base::FilePath& path); void RemoveWorkSpace(gin::Arguments* args, const base::FilePath& path); // Editing commands. void Undo(); void Redo(); void Cut(); void Copy(); void Paste(); void PasteAndMatchStyle(); void Delete(); void SelectAll(); void Unselect(); void Replace(const base::string16& word); void ReplaceMisspelling(const base::string16& word); uint32_t FindInPage(gin::Arguments* args); void StopFindInPage(content::StopFindAction action); void ShowDefinitionForSelection(); void CopyImageAt(int x, int y); // Focus. void Focus(); bool IsFocused() const; void TabTraverse(bool reverse); // Send messages to browser. bool SendIPCMessage(bool internal, bool send_to_all, const std::string& channel, v8::Local args); bool SendIPCMessageWithSender(bool internal, bool send_to_all, const std::string& channel, blink::CloneableMessage args, int32_t sender_id = 0); bool SendIPCMessageToFrame(bool internal, bool send_to_all, int32_t frame_id, const std::string& channel, v8::Local args); void PostMessage(const std::string& channel, v8::Local message, base::Optional> transfer); // Send WebInputEvent to the page. void SendInputEvent(v8::Isolate* isolate, v8::Local input_event); // Subscribe to the frame updates. void BeginFrameSubscription(gin::Arguments* args); void EndFrameSubscription(); // Dragging native items. void StartDrag(const gin_helper::Dictionary& item, gin::Arguments* args); // Captures the page with |rect|, |callback| would be called when capturing is // done. v8::Local CapturePage(gin::Arguments* args); // Methods for creating . bool IsGuest() const; void AttachToIframe(content::WebContents* embedder_web_contents, int embedder_frame_id); void DetachFromOuterFrame(); // Methods for offscreen rendering bool IsOffScreen() const; #if BUILDFLAG(ENABLE_OSR) void OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap); void StartPainting(); void StopPainting(); bool IsPainting() const; void SetFrameRate(int frame_rate); int GetFrameRate() const; #endif void Invalidate(); gfx::Size GetSizeForNewRenderView(content::WebContents*) override; // Methods for zoom handling. void SetZoomLevel(double level); double GetZoomLevel() const; void SetZoomFactor(gin_helper::ErrorThrower thrower, double factor); double GetZoomFactor() const; // Callback triggered on permission response. void OnEnterFullscreenModeForTab( content::RenderFrameHost* requesting_frame, const blink::mojom::FullscreenOptions& options, bool allowed); // Create window with the given disposition. void OnCreateWindow(const GURL& target_url, const content::Referrer& referrer, const std::string& frame_name, WindowOpenDisposition disposition, const std::string& features, const scoped_refptr& body); // Returns the preload script path of current WebContents. std::vector GetPreloadPaths() const; // Returns the web preferences of current WebContents. v8::Local GetWebPreferences(v8::Isolate* isolate) const; v8::Local GetLastWebPreferences(v8::Isolate* isolate) const; // Returns the owner window. v8::Local GetOwnerBrowserWindow(v8::Isolate* isolate) const; // Grants the child process the capability to access URLs with the origin of // the specified URL. void GrantOriginAccess(const GURL& url); // Notifies the web page that there is user interaction. void NotifyUserActivation(); v8::Local TakeHeapSnapshot(v8::Isolate* isolate, const base::FilePath& file_path); // Properties. int32_t ID() const { return id_; } v8::Local Session(v8::Isolate* isolate); content::WebContents* HostWebContents() const; v8::Local DevToolsWebContents(v8::Isolate* isolate); v8::Local Debugger(v8::Isolate* isolate); bool WasInitiallyShown(); content::RenderFrameHost* MainFrame(); WebContentsZoomController* GetZoomController() { return zoom_controller_; } void AddObserver(ExtendedWebContentsObserver* obs) { observers_.AddObserver(obs); } void RemoveObserver(ExtendedWebContentsObserver* obs) { // Trying to remove from an empty collection leads to an access violation if (observers_.might_have_observers()) observers_.RemoveObserver(obs); } bool EmitNavigationEvent(const std::string& event, content::NavigationHandle* navigation_handle); // this.emit(name, new Event(sender, message), args...); template bool EmitWithSender(base::StringPiece name, content::RenderFrameHost* sender, electron::mojom::ElectronBrowser::InvokeCallback callback, Args&&... args) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::HandleScope handle_scope(isolate); v8::Local wrapper; if (!GetWrapper(isolate).ToLocal(&wrapper)) return false; v8::Local event = gin_helper::internal::CreateNativeEvent( isolate, wrapper, sender, std::move(callback)); return EmitCustomEvent(name, event, std::forward(args)...); } void MarkDestroyed(); WebContents* embedder() { return embedder_; } #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) extensions::ScriptExecutor* script_executor() { return script_executor_.get(); } #endif protected: // Does not manage lifetime of |web_contents|. WebContents(v8::Isolate* isolate, content::WebContents* web_contents); // Takes over ownership of |web_contents|. WebContents(v8::Isolate* isolate, std::unique_ptr web_contents, Type type); // Creates a new content::WebContents. WebContents(v8::Isolate* isolate, const gin_helper::Dictionary& options); ~WebContents() override; void InitWithSessionAndOptions( v8::Isolate* isolate, std::unique_ptr web_contents, gin::Handle session, const gin_helper::Dictionary& options); #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) void InitWithExtensionView(v8::Isolate* isolate, content::WebContents* web_contents, extensions::ViewType view_type); #endif // content::WebContentsDelegate: bool DidAddMessageToConsole(content::WebContents* source, blink::mojom::ConsoleMessageLevel level, const base::string16& message, int32_t line_no, const base::string16& source_id) override; bool IsWebContentsCreationOverridden( content::SiteInstance* source_site_instance, content::mojom::WindowContainerType window_container_type, const GURL& opener_url, const std::string& frame_name, const GURL& target_url) override; content::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) override; void WebContentsCreatedWithFullParams( content::WebContents* source_contents, int opener_render_process_id, int opener_render_frame_id, const content::mojom::CreateNewWindowParams& params, content::WebContents* new_contents) override; void AddNewContents(content::WebContents* source, std::unique_ptr new_contents, const GURL& target_url, WindowOpenDisposition disposition, const gfx::Rect& initial_rect, bool user_gesture, bool* was_blocked) override; content::WebContents* OpenURLFromTab( content::WebContents* source, const content::OpenURLParams& params) override; void BeforeUnloadFired(content::WebContents* tab, bool proceed, bool* proceed_to_fire_unload) override; void SetContentsBounds(content::WebContents* source, const gfx::Rect& pos) override; void CloseContents(content::WebContents* source) override; void ActivateContents(content::WebContents* contents) override; void UpdateTargetURL(content::WebContents* source, const GURL& url) override; bool HandleKeyboardEvent( content::WebContents* source, const content::NativeWebKeyboardEvent& event) override; content::KeyboardEventProcessingResult PreHandleKeyboardEvent( content::WebContents* source, const content::NativeWebKeyboardEvent& event) override; void ContentsZoomChange(bool zoom_in) override; void EnterFullscreenModeForTab( content::RenderFrameHost* requesting_frame, const blink::mojom::FullscreenOptions& options) override; void ExitFullscreenModeForTab(content::WebContents* source) override; void RendererUnresponsive( content::WebContents* source, content::RenderWidgetHost* render_widget_host, base::RepeatingClosure hang_monitor_restarter) override; void RendererResponsive( content::WebContents* source, content::RenderWidgetHost* render_widget_host) override; bool HandleContextMenu(content::RenderFrameHost* render_frame_host, const content::ContextMenuParams& params) override; bool OnGoToEntryOffset(int offset) override; void FindReply(content::WebContents* web_contents, int request_id, int number_of_matches, const gfx::Rect& selection_rect, int active_match_ordinal, bool final_update) override; bool CheckMediaAccessPermission(content::RenderFrameHost* render_frame_host, const GURL& security_origin, blink::mojom::MediaStreamType type) override; void RequestMediaAccessPermission( content::WebContents* web_contents, const content::MediaStreamRequest& request, content::MediaResponseCallback callback) override; void RequestToLockMouse(content::WebContents* web_contents, bool user_gesture, bool last_unlocked_by_target) override; std::unique_ptr RunBluetoothChooser( content::RenderFrameHost* frame, const content::BluetoothChooser::EventHandler& handler) override; content::JavaScriptDialogManager* GetJavaScriptDialogManager( content::WebContents* source) override; void OnAudioStateChanged(bool audible) override; void UpdatePreferredSize(content::WebContents* web_contents, const gfx::Size& pref_size) override; // content::WebContentsObserver: void BeforeUnloadFired(bool proceed, const base::TimeTicks& proceed_time) override; void RenderViewCreated(content::RenderViewHost* render_view_host) override; void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override; void RenderViewDeleted(content::RenderViewHost*) override; void RenderProcessGone(base::TerminationStatus status) override; void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; void DOMContentLoaded(content::RenderFrameHost* render_frame_host) override; void DidFinishLoad(content::RenderFrameHost* render_frame_host, const GURL& validated_url) override; void DidFailLoad(content::RenderFrameHost* render_frame_host, const GURL& validated_url, int error_code) override; void DidStartLoading() override; void DidStopLoading() override; void DidStartNavigation( content::NavigationHandle* navigation_handle) override; void DidRedirectNavigation( content::NavigationHandle* navigation_handle) override; void DidFinishNavigation( content::NavigationHandle* navigation_handle) override; bool OnMessageReceived(const IPC::Message& message) override; void WebContentsDestroyed() override; void NavigationEntryCommitted( const content::LoadCommittedDetails& load_details) override; void TitleWasSet(content::NavigationEntry* entry) override; void DidUpdateFaviconURL( content::RenderFrameHost* render_frame_host, const std::vector& urls) override; void PluginCrashed(const base::FilePath& plugin_path, base::ProcessId plugin_pid) override; void MediaStartedPlaying(const MediaPlayerInfo& video_type, const content::MediaPlayerId& id) override; void MediaStoppedPlaying( const MediaPlayerInfo& video_type, const content::MediaPlayerId& id, content::WebContentsObserver::MediaStoppedReason reason) override; void DidChangeThemeColor() override; void OnInterfaceRequestFromFrame( content::RenderFrameHost* render_frame_host, const std::string& interface_name, mojo::ScopedMessagePipeHandle* interface_pipe) override; void OnCursorChanged(const content::WebCursor& cursor) override; void DidAcquireFullscreen(content::RenderFrameHost* rfh) override; // InspectableWebContentsDelegate: void DevToolsReloadPage() override; // InspectableWebContentsViewDelegate: void DevToolsFocused() override; void DevToolsOpened() override; void DevToolsClosed() override; void DevToolsResized() override; private: ElectronBrowserContext* GetBrowserContext() const; // Binds the given request for the ElectronBrowser API. When the // RenderFrameHost is destroyed, all related bindings will be removed. void BindElectronBrowser( mojo::PendingReceiver receiver, content::RenderFrameHost* render_frame_host); void OnElectronBrowserConnectionError(); uint32_t GetNextRequestId() { return ++request_id_; } #if BUILDFLAG(ENABLE_OSR) OffScreenWebContentsView* GetOffScreenWebContentsView() const override; OffScreenRenderWidgetHostView* GetOffScreenRenderWidgetHostView() const; #endif // mojom::ElectronBrowser void Message(bool internal, const std::string& channel, blink::CloneableMessage arguments) override; void Invoke(bool internal, const std::string& channel, blink::CloneableMessage arguments, InvokeCallback callback) override; void OnFirstNonEmptyLayout() override; void ReceivePostMessage(const std::string& channel, blink::TransferableMessage message) override; void MessageSync(bool internal, const std::string& channel, blink::CloneableMessage arguments, MessageSyncCallback callback) override; void MessageTo(bool internal, bool send_to_all, int32_t web_contents_id, const std::string& channel, blink::CloneableMessage arguments) override; void MessageHost(const std::string& channel, blink::CloneableMessage arguments) override; void UpdateDraggableRegions( std::vector regions) override; void SetTemporaryZoomLevel(double level) override; void DoGetZoomLevel(DoGetZoomLevelCallback callback) override; // Called when received a synchronous message from renderer to // get the zoom level. void OnGetZoomLevel(content::RenderFrameHost* frame_host, IPC::Message* reply_msg); void InitZoomController(content::WebContents* web_contents, const gin_helper::Dictionary& options); v8::Global session_; v8::Global devtools_web_contents_; v8::Global debugger_; std::unique_ptr dialog_manager_; std::unique_ptr guest_delegate_; std::unique_ptr frame_subscriber_; #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) std::unique_ptr script_executor_; #endif // The host webcontents that may contain this webcontents. WebContents* embedder_ = nullptr; // The zoom controller for this webContents. WebContentsZoomController* zoom_controller_ = nullptr; // The type of current WebContents. Type type_ = Type::kBrowserWindow; int32_t id_; // Request id used for findInPage request. uint32_t request_id_ = 0; // Whether background throttling is disabled. bool background_throttling_ = true; // Whether to enable devtools. bool enable_devtools_ = true; // Observers of this WebContents. base::ObserverList observers_; bool initially_shown_ = true; service_manager::BinderRegistryWithArgs registry_; mojo::ReceiverSet receivers_; std::map> frame_to_receivers_map_; base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(WebContents); }; } // namespace api } // namespace electron #endif // SHELL_BROWSER_API_ELECTRON_API_WEB_CONTENTS_H_