File: app_browser_controller.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (360 lines) | stat: -rw-r--r-- 13,928 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_WEB_APPLICATIONS_APP_BROWSER_CONTROLLER_H_
#define CHROME_BROWSER_UI_WEB_APPLICATIONS_APP_BROWSER_CONTROLLER_H_

#include <memory>
#include <optional>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/page_action/page_action_icon_type.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "components/url_formatter/url_formatter.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/browser/web_contents_observer.h"
#include "third_party/blink/public/mojom/page/draggable_region.mojom-forward.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkRegion.h"
#include "ui/actions/action_id.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_provider_key.h"
#include "url/gurl.h"

class Browser;
class BrowserThemePack;
class CustomThemeSupplier;
class TabMenuModelFactory;

#if BUILDFLAG(IS_CHROMEOS)
namespace ash {
class SystemWebAppDelegate;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace gfx {
class Rect;
}  // namespace gfx

namespace ui {
class ImageModel;
}

namespace web_app {

class WebAppBrowserController;

// Returns true if |app_url| and |page_url| are the same origin. To avoid
// breaking Hosted Apps and Bookmark Apps that might redirect to sites in the
// same domain but with "www.", this returns true if |page_url| is secure and in
// the same origin as |app_url| with "www.".
bool IsSameHostAndPort(const GURL& app_url, const GURL& page_url);

// Class to encapsulate logic to control the browser UI for web apps.
class AppBrowserController : public ui::ColorProviderKey::InitializerSupplier,
                             public TabStripModelObserver,
                             public content::WebContentsObserver,
                             public BrowserThemeProviderDelegate {
 public:
  AppBrowserController(const AppBrowserController&) = delete;
  AppBrowserController& operator=(const AppBrowserController&) = delete;
  ~AppBrowserController() override;

  // Returns whether |browser| is a web app window/pop-up.
  static bool IsWebApp(const Browser* browser);
  // Returns whether |browser| is a web app window/pop-up for |app_id|.
  static bool IsForWebApp(const Browser* browser, const webapps::AppId& app_id);
  // Returns a Browser* that is for |app_id| and |profile| if any, searches in
  // order of last browser activation. Ignores pop-up Browsers.
  static Browser* FindForWebApp(const Profile& profile,
                                const webapps::AppId& app_id);

  // Returns the `browser` and `tab_index` for a tab for the given `app_id` in
  // the given `profile`, where the tab does not have an opener, and the browser
  // is of the specified `browser_type`. Prefers more recently activated
  // windows and tabs over less recently used ones.
  struct BrowserAndTabIndex {
    raw_ptr<Browser> browser = nullptr;
    int tab_index = -1;
  };
  enum class HomeTabScope {
    kDontCare,   // The caller doesn't care if the returned tab is a home tab.
    kInScope,    // Only return tabs that are a pinned home tab.
    kOutOfScope  // Only return tabs that are not a pinned home tab.
  };
  static std::optional<BrowserAndTabIndex> FindTopLevelBrowsingContextForWebApp(
      const Profile& profile,
      const webapps::AppId& app_id,
      BrowserWindowInterface::Type browser_type,
      bool for_focus_existing,
      HomeTabScope home_tab_scope = HomeTabScope::kDontCare);
  static std::optional<int> FindTabIndexForApp(
      Browser* browser,
      const webapps::AppId& app_id,
      bool for_focus_existing,
      HomeTabScope home_tab_scope = HomeTabScope::kDontCare);

  // Renders |url|'s origin as Unicode.
  static std::u16string FormatUrlOrigin(
      const GURL& url,
      url_formatter::FormatUrlTypes format_types =
          url_formatter::kFormatUrlOmitUsernamePassword |
          url_formatter::kFormatUrlOmitHTTPS |
          url_formatter::kFormatUrlOmitHTTP |
          url_formatter::kFormatUrlOmitTrailingSlashOnBareHostname |
          url_formatter::kFormatUrlOmitTrivialSubdomains);

  // Initialise, must be called after construction (requires virtual dispatch).
  void Init();

  // Returns a theme built from the current page or app's theme color.
  const ui::ThemeProvider* GetThemeProvider() const;

  // Returns the text to flash in the title bar on app launch.
  std::u16string GetLaunchFlashText() const;

  // Returns whether this controller was created for a
  // Chrome App (platform app or legacy packaged app).
  virtual bool IsHostedApp() const;

  // Whether the custom tab bar should be visible.
  virtual bool ShouldShowCustomTabBar() const;

  // Whether the browser should include the tab strip.
  virtual bool has_tab_strip() const;

  // Whether the browser should show the menu button in the toolbar.
  virtual bool HasTitlebarMenuButton() const;

  // Whether to show app origin text in the titlebar toolbar.
  virtual bool HasTitlebarAppOriginText() const;

  // Whether to show content settings in the titlebar toolbar.
  virtual bool HasTitlebarContentSettings() const;

  // Returns which page actions which should should appear in the titlebar
  // toolbar.
  virtual std::vector<actions::ActionId> GetTitleBarPageActions() const;
  // The page actions framework is currently undergoing a migration.
  // This is method is used by the legacy framework and will be removed once
  // the migration is complete.
  virtual std::vector<PageActionIconType> GetTitleBarPageActionTypes() const;

  // Whether to show the Back and Refresh buttons in the web app toolbar.
  virtual bool HasMinimalUiButtons() const = 0;

  // Returns the app icon for the window to use in the task list.
  virtual ui::ImageModel GetWindowAppIcon() const = 0;

  // Returns the icon to be displayed in the window title bar.
  virtual ui::ImageModel GetWindowIcon() const = 0;

  // Returns the color of the title bar.
  virtual std::optional<SkColor> GetThemeColor() const;

  // Returns the background color of the page.
  virtual std::optional<SkColor> GetBackgroundColor() const;

  // Returns the title to be displayed in the window title bar.
  virtual std::u16string GetTitle() const;

  // Gets the short name of the app.
  virtual std::u16string GetAppShortName() const = 0;

  // Returns the human-readable name for title in Media Controls.
  // If the returned value is an empty string, it means that there is no
  // human-readable name.
  std::string GetTitleForMediaControls() const;

  // Gets the origin of the app start url suitable for display (e.g
  // example.com.au).
  virtual std::u16string GetFormattedUrlOrigin() const = 0;

  // Gets the start_url for the app.
  virtual GURL GetAppStartUrl() const = 0;

  // Gets the new tab URL for tabbed apps.
  virtual GURL GetAppNewTabUrl() const;

  // Whether the app's tab strip should hide the new tab button, e.g. because
  // the app has a pinned home tab at the same URL as the new tab URL.
  virtual bool ShouldHideNewTabButton() const;

  // Returns whether the url is within the scope of the tab strip home tab.
  virtual bool IsUrlInHomeTabScope(const GURL& url) const;

  // Returns whether the app icon should be displayed on the tab instead of the
  // favicon.
  virtual bool ShouldShowAppIconOnTab(int index) const;

  // Determines whether the specified url is 'inside' the app |this| controls.
  virtual bool IsUrlInAppScope(const GURL& url) const = 0;

#if BUILDFLAG(IS_MAC)
  // Whether the toolbar should always be shown when in fullscreen mode.
  virtual bool AlwaysShowToolbarInFullscreen() const;
  virtual void ToggleAlwaysShowToolbarInFullscreen();
#endif

  // Safe downcast:
  virtual WebAppBrowserController* AsWebAppBrowserController();

  virtual bool CanUserUninstall() const;

  virtual void Uninstall(
      webapps::WebappUninstallSource webapp_uninstall_source);

  // Returns whether the app is installed (uninstallation may complete within
  // the lifetime of HostedAppBrowserController).
  virtual bool IsInstalled() const;

  // Returns an optional custom tab menu model factory.
  virtual std::unique_ptr<TabMenuModelFactory> GetTabMenuModelFactory() const;

  // Returns true when an app's effective display mode is
  // window-controls-overlay.
  virtual bool AppUsesWindowControlsOverlay() const;

  // Returns true when an app's effective display mode is borderless.
  virtual bool AppUsesBorderlessMode() const;

  // Returns true when an app's effective display mode is tabbed.
  virtual bool AppUsesTabbed() const;

  virtual bool IsIsolatedWebApp() const;

  // TODO(crbug.com/40222062): Remove this mock when `WebAppBrowserTest`s
  // support creating Isolated Web Apps.
  virtual void SetIsolatedWebAppTrueForTesting();

  // Returns true when the app's effective display mode is
  // window-controls-overlay and the user has toggled WCO on for the app.
  virtual bool IsWindowControlsOverlayEnabled() const;

  virtual void ToggleWindowControlsOverlayEnabled(
      base::OnceClosure on_complete);

  // Returns the default bounds for the app or empty for no defaults.
  virtual gfx::Rect GetDefaultBounds() const;

  // Whether the browser should show the reload button in the toolbar.
  virtual bool HasReloadButton() const;

  // Returns whether prevent close is enabled.
  bool IsPreventCloseEnabled() const;

#if !BUILDFLAG(IS_CHROMEOS)
  // Whether the browser should show the profile menu button in the toolbar.
  // Not appliccable to ChromeOS, because apps can be installed only for
  // one main profile there.
  virtual bool HasProfileMenuButton() const;
  virtual bool IsProfileMenuButtonVisible() const;
#endif  // !BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_CHROMEOS)
  // Returns the SystemWebAppDelegate if any for this controller.
  virtual const ash::SystemWebAppDelegate* system_app() const;
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Updates the custom tab bar's visibility based on whether it should be
  // currently visible or not. If |animate| is set, the change will be
  // animated.
  void UpdateCustomTabBarVisibility(bool animate) const;

  const webapps::AppId& app_id() const { return app_id_; }

  Browser* browser() const { return browser_; }

  // Gets the url that the app browser controller was created with. Note: This
  // may be empty until the web contents begins navigating.
  const GURL& initial_url() const { return initial_url_; }

  // content::WebContentsObserver:
  void DidStartNavigation(content::NavigationHandle* handle) override;
  void DOMContentLoaded(content::RenderFrameHost* render_frame_host) override;
  void DidChangeThemeColor() override;
  void OnBackgroundColorChanged() override;
  void PrimaryPageChanged(content::Page& page) override;

  // TabStripModelObserver:
  void OnTabStripModelChanged(
      TabStripModel* tab_strip_model,
      const TabStripModelChange& change,
      const TabStripSelectionChange& selection) override;

  // BrowserThemeProviderDelegate:
  CustomThemeSupplier* GetThemeSupplier() const override;
  bool ShouldUseCustomFrame() const override;

  // ui::ColorProviderKey::InitializerSupplier
  void AddColorMixers(ui::ColorProvider* provider,
                      const ui::ColorProviderKey& key) const override;

  void DraggableRegionsChanged(
      const std::vector<blink::mojom::DraggableRegionPtr>& regions,
      content::WebContents* contents);
  const std::optional<SkRegion>& draggable_region() const {
    return draggable_region_;
  }

  void SetOnUpdateDraggableRegionForTesting(base::OnceClosure done);

  // Called when this browser is going to receive a reparented web contents
  // from an installation or intent action. If the initial url is not set or
  // isn't within the app scope, set it to the app's start_url, allowing the 'x'
  // button to appear in the toolbar & the user can use it to navigate back to
  // that location.
  void MaybeSetInitialUrlOnReparentTab();

 protected:
  AppBrowserController(Browser* browser,
                       webapps::AppId app_id,
                       bool has_tab_strip);
  AppBrowserController(Browser* browser, webapps::AppId app_id);

  // Called once the app browser controller has determined its initial url.
  virtual void OnReceivedInitialURL();

  // Called by OnTabstripModelChanged().
  virtual void OnTabInserted(content::WebContents* contents);
  virtual void OnTabRemoved(content::WebContents* contents);

  // Gets the icon to use if the app icon is not available.
  ui::ImageModel GetFallbackAppIcon() const;

  void UpdateThemePack();

 private:
  // Sets the url that the app browser controller was created with.
  void SetInitialURL(const GURL& initial_url);

  // Indicates to the WebView whether it should support draggable regions via
  // the app-region CSS property.
  void UpdateSupportsDraggableRegions(bool supports_draggable_regions,
                                      content::RenderFrameHost* host);

  const raw_ptr<Browser> browser_;
  const webapps::AppId app_id_;
  const bool has_tab_strip_;
  GURL initial_url_;

  scoped_refptr<BrowserThemePack> theme_pack_;
  std::unique_ptr<ui::ThemeProvider> theme_provider_;
  std::optional<SkColor> last_theme_color_;
  std::optional<SkColor> last_background_color_;

  std::optional<SkRegion> draggable_region_ = std::nullopt;

  base::OnceClosure on_draggable_region_set_for_testing_;
};

}  // namespace web_app

#endif  // CHROME_BROWSER_UI_WEB_APPLICATIONS_APP_BROWSER_CONTROLLER_H_