1 // Copyright 2020 The Chromium Authors. All rights reserved.
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 
8 #include <memory>
9 
10 #include "base/containers/flat_map.h"
11 #include "base/strings/string16.h"
12 #include "build/build_config.h"
13 #include "ui/gfx/geometry/size.h"
14 #include "ui/gfx/native_widget_types.h"
15 
16 #if defined(OS_MAC)
17 #include "content/public/browser/native_web_keyboard_event.h"
18 #endif
19 
20 class GURL;
21 
22 namespace content {
23 class JavaScriptDialogManager;
24 class Shell;
25 class ShellPlatformDataAura;
26 class WebContents;
27 
28 class ShellPlatformDelegate {
29  public:
30   enum UIControl { BACK_BUTTON, FORWARD_BUTTON, STOP_BUTTON };
31 
32   ShellPlatformDelegate();
33   virtual ~ShellPlatformDelegate();
34 
35   // Helper for one time initialization of application.
36   virtual void Initialize(const gfx::Size& default_window_size);
37 
38   // Called after creating a Shell instance, with its initial size.
39   virtual void CreatePlatformWindow(Shell* shell,
40                                     const gfx::Size& initial_size);
41 
42   // Notifies of a top-level or nested web contents being created for, or
43   // attached to, the Shell.
44   virtual void DidCreateOrAttachWebContents(Shell* shell,
45                                             WebContents* web_contents);
46 
47   // Called from the Shell destructor to let each platform do any necessary
48   // cleanup.
49   virtual void CleanUp(Shell* shell);
50 
51   // Links the WebContents into the newly created window.
52   virtual void SetContents(Shell* shell);
53 
54   // Resize the web contents in the shell window to the given size.
55   virtual void ResizeWebContent(Shell* shell, const gfx::Size& content_size);
56 
57   // Enable/disable a button.
58   virtual void EnableUIControl(Shell* shell,
59                                UIControl control,
60                                bool is_enabled);
61 
62   // Updates the url in the url bar.
63   virtual void SetAddressBarURL(Shell* shell, const GURL& url);
64 
65   // Sets whether the spinner is spinning.
66   virtual void SetIsLoading(Shell* shell, bool loading);
67 
68   // Set the title of shell window
69   virtual void SetTitle(Shell* shell, const base::string16& title);
70 
71   // Called when a RenderView is created for a renderer process; forwarded from
72   // WebContentsObserver.
73   virtual void RenderViewReady(Shell* shell);
74 
75   // Allows platforms to override the JavascriptDialogManager. By default
76   // returns null, which signals that the Shell should use its own instance.
77   virtual std::unique_ptr<JavaScriptDialogManager>
78   CreateJavaScriptDialogManager(Shell* shell);
79 
80   // Requests handling of locking the mouse. This returns true if the request
81   // has been handled, otherwise false.
82   virtual bool HandleRequestToLockMouse(Shell* shell,
83                                         WebContents* web_contents,
84                                         bool user_gesture,
85                                         bool last_unlocked_by_target);
86 
87   // Allows platforms to prevent running insecure content. By default returns
88   // false, only allowing what Shell allows on its own.
89   virtual bool ShouldAllowRunningInsecureContent(Shell* shell);
90 
91   // Destroy the Shell. Returns true if the ShellPlatformDelegate did the
92   // destruction. Returns false if the Shell should destroy itself.
93   virtual bool DestroyShell(Shell* shell);
94 
95 #if !defined(OS_ANDROID)
96   // Returns the native window. Valid after calling CreatePlatformWindow().
97   virtual gfx::NativeWindow GetNativeWindow(Shell* shell);
98 #endif
99 
100 #if defined(OS_MAC)
101   // Activate (make key) the native window, and focus the web contents.
102   virtual void ActivateContents(Shell* shell, WebContents* contents);
103 
104   virtual void DidNavigateMainFramePostCommit(Shell* shell,
105                                               WebContents* contents);
106 
107   virtual bool HandleKeyboardEvent(Shell* shell,
108                                    WebContents* source,
109                                    const NativeWebKeyboardEvent& event);
110 #endif
111 
112 #if defined(OS_ANDROID)
113   void ToggleFullscreenModeForTab(Shell* shell,
114                                   WebContents* web_contents,
115                                   bool enter_fullscreen);
116 
117   bool IsFullscreenForTabOrPending(Shell* shell,
118                                    const WebContents* web_contents) const;
119 
120   // Forwarded from WebContentsDelegate.
121   void SetOverlayMode(Shell* shell, bool use_overlay_mode);
122 
123   // Forwarded from WebContentsObserver.
124   void LoadProgressChanged(Shell* shell, double progress);
125 #endif
126 
127  protected:
128 #if defined(USE_AURA) && !defined(TOOLKIT_VIEWS)
129   // Helper to avoid duplicating aura's ShellPlatformDelegate in web tests. If
130   // this hack gets expanded to become more expansive then we should just
131   // duplicate the aura ShellPlatformDelegate code to the web test code impl in
132   // WebTestShellPlatformDelegate.
133   ShellPlatformDataAura* GetShellPlatformDataAura();
134 #endif
135 
136  private:
137   // Data held for each Shell instance, since there is one ShellPlatformDelegate
138   // for the whole browser process (shared across Shells). This is defined for
139   // each platform implementation.
140   struct ShellData;
141   // Holds an instance of ShellData for each Shell.
142   base::flat_map<Shell*, ShellData> shell_data_map_;
143 
144   // Data held in ShellPlatformDelegate that is shared between all Shells. This
145   // is created in Initialize(), and is defined for each platform
146   // implementation.
147   struct PlatformData;
148   std::unique_ptr<PlatformData> platform_;
149 };
150 
151 }  // namespace content
152 
153 #endif  // CONTENT_SHELL_BROWSER_SHELL_PLATFORM_DELEGATE_H_
154