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