Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_SHELL_BROWSER_SHELL_PLATFORM_DELEGATE_H_ |
| 6 | #define CONTENT_SHELL_BROWSER_SHELL_PLATFORM_DELEGATE_H_ |
| 7 | |
Piotr Tworek | 21c18ed5 | 2020-05-18 14:30:29 | [diff] [blame] | 8 | #include <memory> |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 9 | #include <string> |
Piotr Tworek | 21c18ed5 | 2020-05-18 14:30:29 | [diff] [blame] | 10 | |
danakj | 24577b1 | 2020-05-13 22:38:18 | [diff] [blame] | 11 | #include "base/containers/flat_map.h" |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 12 | #include "build/build_config.h" |
| 13 | #include "ui/gfx/geometry/size.h" |
| 14 | #include "ui/gfx/native_widget_types.h" |
| 15 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 16 | #if BUILDFLAG(IS_MAC) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 17 | #include "content/public/browser/native_web_keyboard_event.h" |
Dave Tapuska | 1f7929c | 2023-02-03 18:11:56 | [diff] [blame^] | 18 | #endif |
| 19 | |
| 20 | #if BUILDFLAG(IS_APPLE) |
Mitsuru Oshima | c0a0320 | 2022-08-09 12:04:56 | [diff] [blame] | 21 | #include "ui/display/screen.h" |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | class GURL; |
| 25 | |
| 26 | namespace content { |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 27 | class JavaScriptDialogManager; |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 28 | class Shell; |
danakj | d4b48df5 | 2020-07-02 18:16:48 | [diff] [blame] | 29 | class ShellPlatformDataAura; |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 30 | class WebContents; |
| 31 | |
| 32 | class ShellPlatformDelegate { |
| 33 | public: |
| 34 | enum UIControl { BACK_BUTTON, FORWARD_BUTTON, STOP_BUTTON }; |
| 35 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 36 | ShellPlatformDelegate(); |
| 37 | virtual ~ShellPlatformDelegate(); |
| 38 | |
| 39 | // Helper for one time initialization of application. |
| 40 | virtual void Initialize(const gfx::Size& default_window_size); |
| 41 | |
| 42 | // Called after creating a Shell instance, with its initial size. |
| 43 | virtual void CreatePlatformWindow(Shell* shell, |
| 44 | const gfx::Size& initial_size); |
| 45 | |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 46 | // Notifies of a top-level or nested web contents being created for, or |
| 47 | // attached to, the Shell. |
| 48 | virtual void DidCreateOrAttachWebContents(Shell* shell, |
| 49 | WebContents* web_contents); |
| 50 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 51 | // Called from the Shell destructor to let each platform do any necessary |
| 52 | // cleanup. |
| 53 | virtual void CleanUp(Shell* shell); |
| 54 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 55 | // Called from the Shell destructor after destroying the last one. This is |
| 56 | // usually a good time to call Shell::Shutdown(). |
| 57 | virtual void DidCloseLastWindow(); |
| 58 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 59 | // Links the WebContents into the newly created window. |
| 60 | virtual void SetContents(Shell* shell); |
| 61 | |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 62 | // Resize the web contents in the shell window to the given size. |
danakj | 24577b1 | 2020-05-13 22:38:18 | [diff] [blame] | 63 | virtual void ResizeWebContent(Shell* shell, const gfx::Size& content_size); |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 64 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 65 | // Enable/disable a button. |
| 66 | virtual void EnableUIControl(Shell* shell, |
| 67 | UIControl control, |
| 68 | bool is_enabled); |
| 69 | |
| 70 | // Updates the url in the url bar. |
| 71 | virtual void SetAddressBarURL(Shell* shell, const GURL& url); |
| 72 | |
| 73 | // Sets whether the spinner is spinning. |
| 74 | virtual void SetIsLoading(Shell* shell, bool loading); |
| 75 | |
| 76 | // Set the title of shell window |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 77 | virtual void SetTitle(Shell* shell, const std::u16string& title); |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 78 | |
danakj | 3dd7a610 | 2020-12-30 19:58:39 | [diff] [blame] | 79 | // Called when the main frame is created in the renderer process; forwarded |
| 80 | // from WebContentsObserver. If navigation creates a new main frame, this may |
| 81 | // occur more than once. |
| 82 | virtual void MainFrameCreated(Shell* shell); |
danakj | 24577b1 | 2020-05-13 22:38:18 | [diff] [blame] | 83 | |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 84 | // Allows platforms to override the JavascriptDialogManager. By default |
| 85 | // returns null, which signals that the Shell should use its own instance. |
| 86 | virtual std::unique_ptr<JavaScriptDialogManager> |
| 87 | CreateJavaScriptDialogManager(Shell* shell); |
| 88 | |
Dave Tapuska | b499878 | 2020-10-08 17:22:47 | [diff] [blame] | 89 | // Requests handling of locking the mouse. This returns true if the request |
| 90 | // has been handled, otherwise false. |
| 91 | virtual bool HandleRequestToLockMouse(Shell* shell, |
| 92 | WebContents* web_contents, |
| 93 | bool user_gesture, |
| 94 | bool last_unlocked_by_target); |
| 95 | |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 96 | // Allows platforms to prevent running insecure content. By default returns |
| 97 | // false, only allowing what Shell allows on its own. |
| 98 | virtual bool ShouldAllowRunningInsecureContent(Shell* shell); |
| 99 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 100 | // Destroy the Shell. Returns true if the ShellPlatformDelegate did the |
| 101 | // destruction. Returns false if the Shell should destroy itself. |
| 102 | virtual bool DestroyShell(Shell* shell); |
| 103 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 104 | #if !BUILDFLAG(IS_ANDROID) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 105 | // Returns the native window. Valid after calling CreatePlatformWindow(). |
| 106 | virtual gfx::NativeWindow GetNativeWindow(Shell* shell); |
| 107 | #endif |
| 108 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 109 | #if BUILDFLAG(IS_MAC) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 110 | // Activate (make key) the native window, and focus the web contents. |
| 111 | virtual void ActivateContents(Shell* shell, WebContents* contents); |
| 112 | |
Liviu Tinta | 4090405 | 2021-07-20 15:12:52 | [diff] [blame] | 113 | virtual void DidNavigatePrimaryMainFramePostCommit(Shell* shell, |
| 114 | WebContents* contents); |
Xianzhu Wang | 0f021a8 | 2020-07-03 01:29:47 | [diff] [blame] | 115 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 116 | virtual bool HandleKeyboardEvent(Shell* shell, |
| 117 | WebContents* source, |
| 118 | const NativeWebKeyboardEvent& event); |
| 119 | #endif |
| 120 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 121 | #if BUILDFLAG(IS_ANDROID) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 122 | void ToggleFullscreenModeForTab(Shell* shell, |
| 123 | WebContents* web_contents, |
| 124 | bool enter_fullscreen); |
| 125 | |
| 126 | bool IsFullscreenForTabOrPending(Shell* shell, |
| 127 | const WebContents* web_contents) const; |
| 128 | |
| 129 | // Forwarded from WebContentsDelegate. |
| 130 | void SetOverlayMode(Shell* shell, bool use_overlay_mode); |
| 131 | |
| 132 | // Forwarded from WebContentsObserver. |
| 133 | void LoadProgressChanged(Shell* shell, double progress); |
| 134 | #endif |
| 135 | |
danakj | d4b48df5 | 2020-07-02 18:16:48 | [diff] [blame] | 136 | protected: |
danakj | 2957b064 | 2021-06-11 18:18:04 | [diff] [blame] | 137 | #if defined(USE_AURA) && !defined(SHELL_USE_TOOLKIT_VIEWS) |
danakj | d4b48df5 | 2020-07-02 18:16:48 | [diff] [blame] | 138 | // Helper to avoid duplicating aura's ShellPlatformDelegate in web tests. If |
| 139 | // this hack gets expanded to become more expansive then we should just |
| 140 | // duplicate the aura ShellPlatformDelegate code to the web test code impl in |
| 141 | // WebTestShellPlatformDelegate. |
| 142 | ShellPlatformDataAura* GetShellPlatformDataAura(); |
| 143 | #endif |
| 144 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 145 | private: |
Dave Tapuska | 1f7929c | 2023-02-03 18:11:56 | [diff] [blame^] | 146 | #if BUILDFLAG(IS_APPLE) |
Mitsuru Oshima | c0a0320 | 2022-08-09 12:04:56 | [diff] [blame] | 147 | std::unique_ptr<display::ScopedNativeScreen> screen_; |
| 148 | #endif |
danakj | 24577b1 | 2020-05-13 22:38:18 | [diff] [blame] | 149 | // Data held for each Shell instance, since there is one ShellPlatformDelegate |
| 150 | // for the whole browser process (shared across Shells). This is defined for |
| 151 | // each platform implementation. |
| 152 | struct ShellData; |
danakj | 24577b1 | 2020-05-13 22:38:18 | [diff] [blame] | 153 | // Holds an instance of ShellData for each Shell. |
| 154 | base::flat_map<Shell*, ShellData> shell_data_map_; |
| 155 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 156 | // Data held in ShellPlatformDelegate that is shared between all Shells. This |
| 157 | // is created in Initialize(), and is defined for each platform |
| 158 | // implementation. |
| 159 | struct PlatformData; |
| 160 | std::unique_ptr<PlatformData> platform_; |
| 161 | }; |
| 162 | |
| 163 | } // namespace content |
| 164 | |
| 165 | #endif // CONTENT_SHELL_BROWSER_SHELL_PLATFORM_DELEGATE_H_ |