#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/memory/raw_ptr.h"
#include "chrome/browser/themes/browser_theme_provider_delegate.h"
#include "chrome/browser/ui/page_action/page_action_icon_type.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.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/base/unowned_user_data/scoped_unowned_user_data.h"
#include "ui/color/color_provider_key.h"
#include "url/gurl.h"
class Browser;
class BrowserWindowInterface;
class BrowserThemePack;
class CustomThemeSupplier;
class TabMenuModelFactory;
#if BUILDFLAG(IS_CHROMEOS)
namespace ash {
class SystemWebAppDelegate;
}
#endif
namespace base {
class TimeTicks;
}
namespace gfx {
class Rect;
}
namespace ui {
class ColorProvider;
class ImageModel;
class ThemeProvider;
}
namespace web_app {
class WebAppBrowserController;
bool IsSameHostAndPort(const GURL& app_url, const GURL& page_url);
class AppBrowserController : public ui::ColorProviderKey::InitializerSupplier,
public TabStripModelObserver,
public content::WebContentsObserver,
public BrowserThemeProviderDelegate {
public:
DECLARE_USER_DATA(AppBrowserController);
AppBrowserController(const AppBrowserController&) = delete;
AppBrowserController& operator=(const AppBrowserController&) = delete;
~AppBrowserController() override;
static const AppBrowserController* From(
const BrowserWindowInterface* browser);
static AppBrowserController* From(BrowserWindowInterface* browser);
static bool IsWebApp(const BrowserWindowInterface* browser);
static bool IsIsolatedWebApp(const BrowserWindowInterface* browser);
static bool IsForWebApp(const BrowserWindowInterface* browser,
const webapps::AppId& app_id);
static BrowserWindowInterface* FindForWebApp(const Profile& profile,
const webapps::AppId& app_id);
struct BrowserAndTabIndex {
raw_ptr<BrowserWindowInterface> browser = nullptr;
int tab_index = -1;
};
enum class HomeTabScope {
kDontCare,
kInScope,
kOutOfScope
};
static std::optional<BrowserAndTabIndex> FindTopLevelBrowsingContextForWebApp(
const Profile& profile,
const webapps::AppId& app_id,
bool for_app_browser,
bool for_focus_existing,
HomeTabScope home_tab_scope = HomeTabScope::kDontCare);
static std::optional<int> FindTabIndexForApp(
BrowserWindowInterface* browser,
const webapps::AppId& app_id,
bool for_focus_existing,
HomeTabScope home_tab_scope = HomeTabScope::kDontCare);
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);
void Init();
const ui::ThemeProvider* GetThemeProvider() const;
std::u16string GetLaunchFlashText() const;
virtual bool ShouldShowCustomTabBar() const;
virtual bool has_tab_strip() const;
virtual bool HasTitlebarMenuButton() const;
virtual bool HasTitlebarAppOriginText() const;
virtual bool HasTitlebarContentSettings() const;
virtual std::vector<actions::ActionId> GetTitleBarPageActions() const;
virtual std::vector<PageActionIconType> GetTitleBarPageActionTypes() const;
virtual bool HasMinimalUiButtons() const = 0;
virtual ui::ImageModel GetWindowAppIcon() const = 0;
virtual ui::ImageModel GetWindowIcon() const = 0;
virtual std::optional<SkColor> GetThemeColor() const;
virtual std::optional<SkColor> GetBackgroundColor() const;
virtual std::u16string GetTitle() const;
virtual std::u16string GetAppShortName() const = 0;
std::string GetTitleForMediaControls() const;
virtual std::u16string GetFormattedUrlOrigin() const = 0;
virtual const GURL& GetAppStartUrl() const = 0;
virtual const GURL& GetAppNewTabUrl() const;
virtual content::WebContents* GetPinnedHomeTab() const;
virtual bool ShouldHideNewTabButton() const;
virtual bool IsUrlInHomeTabScope(const GURL& url) const;
virtual bool ShouldShowAppIconOnTab(int index) const;
virtual bool IsUrlInAppScope(const GURL& url) const = 0;
#if BUILDFLAG(IS_MAC)
virtual bool AlwaysShowToolbarInFullscreen() const;
virtual void ToggleAlwaysShowToolbarInFullscreen();
#endif
virtual WebAppBrowserController* AsWebAppBrowserController();
virtual bool CanUserUninstall() const;
virtual void Uninstall(
webapps::WebappUninstallSource webapp_uninstall_source);
virtual bool IsInstalled() const;
virtual std::unique_ptr<TabMenuModelFactory> GetTabMenuModelFactory() const;
virtual bool AppUsesWindowControlsOverlay() const;
virtual bool AppUsesBorderlessMode() const;
virtual bool UrlMatchesBorderlessPattern(const GURL& url) const;
virtual bool AppUsesTabbed() const;
virtual bool IsIsolatedWebApp() const;
virtual void SetIsolatedWebAppTrueForTesting();
virtual bool IsWindowControlsOverlayEnabled() const;
virtual void ToggleWindowControlsOverlayEnabled(
base::OnceClosure on_complete);
virtual gfx::Rect GetDefaultBounds() const;
virtual bool HasReloadButton() const;
virtual bool HasPendingUpdate() const;
virtual bool HasPendingUpdateNotIgnoredByUser() const;
virtual void CreateMetadataAndTriggerAppUpdateDialog(
base::TimeTicks start_time) const;
bool IsPreventCloseEnabled() const;
#if !BUILDFLAG(IS_CHROMEOS)
virtual bool HasProfileMenuButton() const;
virtual bool IsProfileMenuButtonVisible() const;
#endif
#if BUILDFLAG(IS_CHROMEOS)
virtual const ash::SystemWebAppDelegate* system_app() const;
#endif
void UpdateCustomTabBarVisibility(bool animate) const;
const webapps::AppId& app_id() const { return app_id_; }
Browser* browser() const { return browser_; }
const GURL& initial_url() const { return initial_url_; }
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;
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override;
CustomThemeSupplier* GetThemeSupplier() const override;
bool ShouldUseCustomFrame() const override;
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);
void MaybeSetInitialUrlOnReparentTab();
protected:
AppBrowserController(Browser* browser,
webapps::AppId app_id,
bool has_tab_strip);
AppBrowserController(Browser* browser, webapps::AppId app_id);
virtual void OnReceivedInitialURL();
virtual void OnTabInserted(content::WebContents* contents);
virtual void OnTabRemoved(content::WebContents* contents);
ui::ImageModel GetFallbackAppIcon() const;
void UpdateThemePack();
private:
void SetInitialURL(const GURL& initial_url);
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_;
ui::ScopedUnownedUserData<AppBrowserController> scoped_unowned_user_data_;
};
}
#endif