File: hosted_app_browser_controller.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (250 lines) | stat: -rw-r--r-- 8,615 bytes parent folder | download | duplicates (3)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/extensions/hosted_app_browser_controller.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/extensions/app_tab_helper.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/web_app_launch_utils.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/common/extensions/api/url_handlers/url_handlers_parser.h"
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "components/security_state/content/security_state_tab_helper.h"
#include "components/security_state/core/security_state.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/management_policy.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom.h"
#include "ui/base/models/image_model.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/native_widget_types.h"
#include "url/gurl.h"

namespace extensions {

namespace {

// 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) {
  return (app_url.host_piece() == page_url.host_piece() ||
          std::string("www.") + app_url.host() == page_url.host_piece()) &&
         app_url.port() == page_url.port();
}

}  // namespace

HostedAppBrowserController::HostedAppBrowserController(Browser* browser)
    : AppBrowserController(
          browser,
          web_app::GetAppIdFromApplicationName(browser->app_name())) {}

HostedAppBrowserController::~HostedAppBrowserController() = default;

bool HostedAppBrowserController::HasMinimalUiButtons() const {
  return false;
}

ui::ImageModel HostedAppBrowserController::GetWindowAppIcon() const {
  // TODO(calamity): Use the app name to retrieve the app icon without using the
  // extensions tab helper to make icon load more immediate.
#if BUILDFLAG(IS_CHROMEOS)
  if (apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(
          browser()->profile())) {
    if (!app_icon_.isNull()) {
      return ui::ImageModel::FromImageSkia(app_icon_);
    }

    const Extension* extension = GetExtension();
    if (extension &&
        apps::AppServiceProxyFactory::GetForProfile(browser()->profile())
                ->AppRegistryCache()
                .GetAppType(extension->id()) != apps::AppType::kUnknown) {
      LoadAppIcon(true /* allow_placeholder_icon */);
      return GetFallbackAppIcon();
    }
  }
#endif

  content::WebContents* contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  if (!contents) {
    return GetFallbackAppIcon();
  }

  extensions::AppTabHelper* extensions_tab_helper =
      extensions::AppTabHelper::FromWebContents(contents);
  if (!extensions_tab_helper) {
    return GetFallbackAppIcon();
  }

  const SkBitmap* icon_bitmap = extensions_tab_helper->GetExtensionAppIcon();
  if (!icon_bitmap) {
    return GetFallbackAppIcon();
  }

  return ui::ImageModel::FromImageSkia(
      gfx::ImageSkia::CreateFrom1xBitmap(*icon_bitmap));
}

ui::ImageModel HostedAppBrowserController::GetWindowIcon() const {
  if (IsWebApp(browser())) {
    return GetWindowAppIcon();
  }

  return ui::ImageModel::FromImage(browser()->GetCurrentPageIcon());
}

std::u16string HostedAppBrowserController::GetTitle() const {
  // When showing the toolbar, display the name of the app, instead of the
  // current page as the title.
  if (ShouldShowCustomTabBar()) {
    const Extension* extension = GetExtension();
    return base::UTF8ToUTF16(extension->name());
  }

  return AppBrowserController::GetTitle();
}

GURL HostedAppBrowserController::GetAppStartUrl() const {
  const Extension* extension = GetExtension();
  if (!extension) {
    return GURL();
  }

  return AppLaunchInfo::GetLaunchWebURL(extension);
}

bool HostedAppBrowserController::IsUrlInAppScope(const GURL& url) const {
  const Extension* extension = GetExtension();

  if (!extension) {
    return false;
  }

  return IsSameHostAndPort(GetAppStartUrl(), url);
}

const Extension* HostedAppBrowserController::GetExtension() const {
  return ExtensionRegistry::Get(browser()->profile())
      ->GetExtensionById(app_id(), ExtensionRegistry::EVERYTHING);
}

std::u16string HostedAppBrowserController::GetAppShortName() const {
  const Extension* extension = GetExtension();
  return extension ? base::UTF8ToUTF16(extension->short_name())
                   : std::u16string();
}

std::u16string HostedAppBrowserController::GetFormattedUrlOrigin() const {
  const Extension* extension = GetExtension();
  return extension ? FormatUrlOrigin(AppLaunchInfo::GetLaunchWebURL(extension))
                   : std::u16string();
}

bool HostedAppBrowserController::CanUserUninstall() const {
  if (uninstall_dialog_) {
    return false;
  }

  const Extension* extension = GetExtension();
  if (!extension) {
    return false;
  }

  return extensions::ExtensionSystem::Get(browser()->profile())
      ->management_policy()
      ->UserMayModifySettings(extension, nullptr);
}

void HostedAppBrowserController::Uninstall(
    webapps::WebappUninstallSource webapp_uninstall_source) {
  const Extension* extension = GetExtension();
  if (!extension) {
    return;
  }

  DCHECK(!uninstall_dialog_);
  uninstall_dialog_ = ExtensionUninstallDialog::Create(
      browser()->profile(),
      browser()->window() ? browser()->window()->GetNativeWindow()
                          : gfx::NativeWindow(),
      this);

  // The dialog can be closed by UI system whenever it likes, but
  // OnExtensionUninstallDialogClosed will be called anyway.
  uninstall_dialog_->ConfirmUninstall(extension,
                                      UNINSTALL_REASON_USER_INITIATED,
                                      UNINSTALL_SOURCE_HOSTED_APP_MENU);
}

bool HostedAppBrowserController::IsInstalled() const {
  return GetExtension();
}

bool HostedAppBrowserController::IsHostedApp() const {
  return true;
}

void HostedAppBrowserController::OnExtensionUninstallDialogClosed(
    bool success,
    const std::u16string& error) {
  uninstall_dialog_.reset();
}

void HostedAppBrowserController::OnTabInserted(content::WebContents* contents) {
  AppBrowserController::OnTabInserted(contents);

  const Extension* extension = GetExtension();
  extensions::AppTabHelper::FromWebContents(contents)->SetExtensionApp(
      extension);
}

void HostedAppBrowserController::OnTabRemoved(content::WebContents* contents) {
  AppBrowserController::OnTabRemoved(contents);

  extensions::AppTabHelper::FromWebContents(contents)->SetExtensionApp(nullptr);
}

void HostedAppBrowserController::LoadAppIcon(
    bool allow_placeholder_icon) const {
  apps::AppServiceProxyFactory::GetForProfile(browser()->profile())
      ->LoadIcon(GetExtension()->id(), apps::IconType::kStandard,
                 extension_misc::EXTENSION_ICON_SMALL, allow_placeholder_icon,
                 base::BindOnce(&HostedAppBrowserController::OnLoadIcon,
                                weak_ptr_factory_.GetMutableWeakPtr()));
}

void HostedAppBrowserController::OnLoadIcon(apps::IconValuePtr icon_value) {
  if (!icon_value || icon_value->icon_type != apps::IconType::kStandard) {
    return;
  }

  app_icon_ = icon_value->uncompressed;

  if (icon_value->is_placeholder_icon) {
    LoadAppIcon(false /* allow_placeholder_icon */);
  }
}

}  // namespace extensions