File: web_app_publisher_helper.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 (469 lines) | stat: -rw-r--r-- 16,981 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright 2021 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_WEB_APPLICATIONS_APP_SERVICE_WEB_APP_PUBLISHER_HELPER_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_APP_SERVICE_WEB_APP_PUBLISHER_HELPER_H_

#include <stdint.h>

#include <map>
#include <optional>
#include <string>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/types/id_type.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/apps/app_service/launch_result_type.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_install_manager_observer.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_registrar_observer.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/services/app_service/public/cpp/app.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/services/app_service/public/cpp/icon_types.h"
#include "components/services/app_service/public/cpp/intent.h"
#include "components/services/app_service/public/cpp/intent_filter.h"
#include "components/services/app_service/public/cpp/permission.h"
#include "components/webapps/common/web_app_id.h"
#include "ui/gfx/native_widget_types.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/apps/app_service/app_notifications.h"
#include "chrome/browser/apps/app_service/media_requests.h"
#include "chrome/browser/apps/app_service/paused_apps.h"
#include "chrome/browser/badging/badge_manager_delegate.h"
#include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
#include "chrome/browser/notifications/notification_common.h"
#include "chrome/browser/notifications/notification_display_service.h"
#endif

class Browser;
class ContentSettingsPattern;
class ContentSettingsTypeSet;
class Profile;
class GURL;

namespace apps {
struct AppLaunchParams;
enum class RunOnOsLoginMode;
enum IconEffects : uint32_t;
}  // namespace apps

namespace badging {
class BadgeManager;
}

namespace base {
class FilePath;
class Time;
}  // namespace base

namespace blink::mojom {
enum class DisplayMode : int32_t;
}

namespace content {
class WebContents;
}

#if BUILDFLAG(IS_CHROMEOS)
namespace message_center {
class Notification;
}
#endif

namespace ui {
enum ResourceScaleFactor : int;
}

namespace webapps {
enum class WebappUninstallSource;
}

namespace web_app {

class ComputedAppSizeWithOrigin;
class WebApp;
class WebAppProvider;
enum class RunOnOsLoginMode;

namespace mojom {
enum class UserDisplayMode : int32_t;
}

struct ShortcutIdTypeMarker {};

typedef base::IdTypeU32<ShortcutIdTypeMarker> ShortcutId;

void UninstallImpl(WebAppProvider* provider,
                   const std::string& app_id,
                   apps::UninstallSource uninstall_source,
                   gfx::NativeWindow parent_window);

class WebAppPublisherHelper : public WebAppRegistrarObserver,
                              public WebAppInstallManagerObserver,
#if BUILDFLAG(IS_CHROMEOS)
                              public NotificationDisplayService::Observer,
                              public MediaStreamCaptureIndicator::Observer,
#endif
                              public content_settings::Observer {
 public:
  class Delegate {
   public:
    Delegate();
    Delegate(const Delegate&) = delete;
    Delegate& operator=(const Delegate&) = delete;
    ~Delegate();

    virtual void PublishWebApps(std::vector<apps::AppPtr> apps) = 0;
    virtual void PublishWebApp(apps::AppPtr app) = 0;

    virtual void ModifyWebAppCapabilityAccess(
        const std::string& app_id,
        std::optional<bool> accessing_camera,
        std::optional<bool> accessing_microphone) = 0;
  };

  using LoadIconCallback = base::OnceCallback<void(apps::IconValuePtr)>;

  WebAppPublisherHelper(Profile* profile,
                        WebAppProvider* provider,
                        Delegate* delegate);
  WebAppPublisherHelper(const WebAppPublisherHelper&) = delete;
  WebAppPublisherHelper& operator=(const WebAppPublisherHelper&) = delete;
  ~WebAppPublisherHelper() override;

  // Indicates if |permission_type| is supported by Web Applications.
  static bool IsSupportedWebAppPermissionType(
      ContentSettingsType permission_type);

  // Must be called before profile keyed services are destroyed.
  void Shutdown();

  // Populates the various show_in_* fields of |app|.
  void SetWebAppShowInFields(const WebApp* web_app, apps::App& app);

  // Creates permissions for `web_app`.
  apps::Permissions CreatePermissions(const WebApp* web_app);

  // Creates an |apps::AppPtr| describing |web_app|.
  // Note: migration in progress. Changes should be made to both |CreateWebApp|
  // and |ConvertWebApp| until complete.
  apps::AppPtr CreateWebApp(const WebApp* web_app);

  // Constructs an App with only the information required to identify an
  // uninstallation.
  apps::AppPtr ConvertUninstalledWebApp(
      const webapps::AppId& app_id,
      webapps::WebappUninstallSource uninstall_source);

  // Constructs an App with only the information required to update
  // last launch time.
  apps::AppPtr ConvertLaunchedWebApp(const WebApp* web_app);

  // Directly uninstalls |web_app| without prompting the user.
  // If |clear_site_data| is true, any site data associated with the app will
  // be removed.
  // If |report_abuse| is true, the app will be reported for abuse to the Web
  // Store.
  void UninstallWebApp(const WebApp* web_app,
                       apps::UninstallSource uninstall_source,
                       bool clear_site_data,
                       bool report_abuse);

  void SetIconEffect(const std::string& app_id);

#if BUILDFLAG(IS_CHROMEOS)
  void PauseApp(const std::string& app_id);

  void UnpauseApp(const std::string& app_id);

  bool IsPaused(const std::string& app_id);

  void StopApp(const std::string& app_id);

  void GetCompressedIconData(const std::string& app_id,
                             int32_t size_in_dip,
                             ui::ResourceScaleFactor scale_factor,
                             apps::LoadIconCallback callback);
#endif  // BUILDFLAG(IS_CHROMEOS)

  void LoadIcon(const std::string& app_id,
                apps::IconType icon_type,
                int32_t size_hint_in_dip,
                apps::IconEffects icon_effects,
                apps::LoadIconCallback callback);

  void Launch(const std::string& app_id,
              int32_t event_flags,
              apps::LaunchSource launch_source,
              apps::WindowInfoPtr window_info,
              base::OnceCallback<void(content::WebContents*)> on_complete);

  void LaunchAppWithFiles(const std::string& app_id,
                          int32_t event_flags,
                          apps::LaunchSource launch_source,
                          std::vector<base::FilePath> file_paths);

  void LaunchAppWithIntent(const std::string& app_id,
                           int32_t event_flags,
                           apps::IntentPtr intent,
                           apps::LaunchSource launch_source,
                           apps::WindowInfoPtr window_info,
                           apps::LaunchCallback callback);

  void LaunchAppWithParams(
      apps::AppLaunchParams params,
      base::OnceCallback<void(content::WebContents*)> on_complete);

  void SetPermission(const std::string& app_id, apps::PermissionPtr permission);

  void OpenNativeSettings(const std::string& app_id);

  apps::WindowMode GetWindowMode(const std::string& app_id);

  void UpdateAppSize(const std::string& app_id);

  void SetWindowMode(const std::string& app_id, apps::WindowMode window_mode);

  // Converts |display_mode| to a |window_mode|.
  apps::WindowMode ConvertDisplayModeToWindowMode(
      blink::mojom::DisplayMode display_mode);

  void PublishWindowModeUpdate(const std::string& app_id,
                               blink::mojom::DisplayMode display_mode);

  void PublishRunOnOsLoginModeUpdate(const std::string& app_id,
                                     RunOnOsLoginMode run_on_os_login_mode);

  std::string GenerateShortcutId();

  void StoreShortcutId(const std::string& shortcut_id,
                       const WebAppShortcutsMenuItemInfo& menu_item_info);

  // Execute the user command from the context menu items. Currently
  // on the web app shortcut need to be execute in the publisher.
  // The |app_id| represent the app that user selected, the |shortcut_id|
  // represents which shortcut item that user selected. The |display_id|
  // represent where to display the app.
  void ExecuteContextMenuCommand(
      const std::string& app_id,
      const std::string& shortcut_id,
      int64_t display_id,
      base::OnceCallback<void(content::WebContents*)> on_complete);

  // Checks that the user permits the app launch (possibly presenting a blocking
  // user choice dialog). Launches the app with read access to the files in
  // `params.launch_files` and returns all the created WebContentses via
  // `callback`, or doesn't launch the app and returns an empty vector in
  // `callback`.
  void LaunchAppWithFilesCheckingUserPermission(
      const std::string& app_id,
      apps::AppLaunchParams params,
      base::OnceCallback<void(std::vector<content::WebContents*>)> callback);

  Profile* profile() const { return profile_; }

  WebAppRegistrar& registrar() const;
  WebAppInstallManager& install_manager() const;

  bool IsShuttingDown() const;

  static apps::IntentFilters CreateIntentFiltersForWebApp(
      const WebAppProvider& provider,
      const web_app::WebApp& app);

 private:
#if BUILDFLAG(IS_CHROMEOS)
  class BadgeManagerDelegate : public badging::BadgeManagerDelegate {
   public:
    explicit BadgeManagerDelegate(
        const base::WeakPtr<WebAppPublisherHelper>& publisher_helper);

    ~BadgeManagerDelegate() override;

    void OnAppBadgeUpdated(const webapps::AppId& app_id) override;

   private:
    base::WeakPtr<WebAppPublisherHelper> publisher_helper_;
  };
#endif  // BUILDFLAG(IS_CHROMEOS)

  // WebAppInstallManagerObserver:
  void OnWebAppInstalled(const webapps::AppId& app_id) override;
  void OnWebAppInstalledWithOsHooks(const webapps::AppId& app_id) override;
  void OnWebAppManifestUpdated(const webapps::AppId& app_id) override;
  void OnWebAppUninstalled(
      const webapps::AppId& app_id,
      webapps::WebappUninstallSource uninstall_source) override;
  void OnWebAppInstallManagerDestroyed() override;

  // WebAppRegistrarObserver:
  void OnAppRegistrarDestroyed() override;
  void OnWebAppProtocolSettingsChanged(const webapps::AppId& app_id) override;
  void OnWebAppFileHandlerApprovalStateChanged(
      const webapps::AppId& app_id) override;
  void OnWebAppLastLaunchTimeChanged(
      const std::string& app_id,
      const base::Time& last_launch_time) override;
  void OnWebAppUserDisplayModeChanged(
      const webapps::AppId& app_id,
      mojom::UserDisplayMode user_display_mode) override;
  void OnWebAppRunOnOsLoginModeChanged(
      const webapps::AppId& app_id,
      RunOnOsLoginMode run_on_os_login_mode) override;
  void OnWebAppSettingsPolicyChanged() override;

#if BUILDFLAG(IS_CHROMEOS)
  void OnWebAppDisabledStateChanged(const webapps::AppId& app_id,
                                    bool is_disabled) override;
  void OnWebAppsDisabledModeChanged() override;

  // NotificationDisplayService::Observer overrides.
  void OnNotificationDisplayed(
      const message_center::Notification& notification,
      const NotificationCommon::Metadata* metadata) override;
  void OnNotificationClosed(const std::string& notification_id) override;
  void OnNotificationDisplayServiceDestroyed(
      NotificationDisplayService* service) override;
#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_CHROMEOS)
  // MediaStreamCaptureIndicator::Observer:
  void OnIsCapturingVideoChanged(content::WebContents* web_contents,
                                 bool is_capturing_video) override;
  void OnIsCapturingAudioChanged(content::WebContents* web_contents,
                                 bool is_capturing_audio) override;
#endif  // BUILDFLAG(IS_CHROMEOS)

  // content_settings::Observer:
  void OnContentSettingChanged(
      const ContentSettingsPattern& primary_pattern,
      const ContentSettingsPattern& secondary_pattern,
      ContentSettingsTypeSet content_type_set) override;

  void Init();

  void ObserveWebAppSubsystems();

  apps::IconEffects GetIconEffects(const WebApp* web_app);

  const WebApp* GetWebApp(const webapps::AppId& app_id) const;

  // Returns all the WebContents instances launched via `callback`. This value
  // may be empty if the launch fails. There may be more than one `WebContents`
  // if each file is launched in a different window.
  void LaunchAppWithIntentImpl(
      const std::string& app_id,
      int32_t event_flags,
      apps::IntentPtr intent,
      apps::LaunchSource launch_source,
      int64_t display_id,
      base::OnceCallback<void(std::vector<content::WebContents*>)> callback);

  // Checks that the user permits the app launch (possibly presenting a blocking
  // user choice dialog).
  void LaunchAppFromProtocolCheckingUserPermission(
      apps::AppLaunchParams params,
      base::OnceCallback<void(content::WebContents*)> callback);

  apps::PackageId GetPackageId(const WebApp& web_app) const;

#if BUILDFLAG(IS_CHROMEOS)
  // Updates app visibility.
  void UpdateAppDisabledMode(apps::App& app);

  bool MaybeAddNotification(const std::string& app_id,
                            const std::string& notification_id);
  void MaybeAddWebPageNotifications(
      const message_center::Notification& notification,
      const NotificationCommon::Metadata* metadata);

  // Returns whether the app should show a badge.
  bool ShouldShowBadge(const std::string& app_id,
                       bool has_notification_indicator);
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Called after the user has allowed or denied an app launch with files.
  void OnFileHandlerDialogCompleted(
      std::string app_id,
      apps::AppLaunchParams params,
      base::OnceCallback<void(std::vector<content::WebContents*>)> callback,
      bool allowed,
      bool remember_user_choice);

  // Called after the user has allowed or denied an app launch from protocol
  // url.
  void OnProtocolHandlerDialogCompleted(
      apps::AppLaunchParams params,
      base::OnceCallback<void(content::WebContents*)> on_complete,
      bool allowed,
      bool remember_user_choice);

  void OnLaunchCompleted(
      apps::AppLaunchParams params_for_restore,
      bool is_system_web_app,
      std::optional<GURL> override_url,
      base::OnceCallback<void(content::WebContents*)> on_complete,
      base::WeakPtr<Browser> browser,
      base::WeakPtr<content::WebContents> web_contents,
      apps::LaunchContainer container);

  void OnGetWebAppSize(webapps::AppId app_id,
                       std::optional<ComputedAppSizeWithOrigin> size);

  const raw_ptr<Profile, DanglingUntriaged> profile_;

  const raw_ptr<WebAppProvider, DanglingUntriaged> provider_;

  const raw_ptr<Delegate, DanglingUntriaged> delegate_;

  base::ScopedObservation<WebAppRegistrar, WebAppRegistrarObserver>
      registrar_observation_{this};

  base::ScopedObservation<WebAppInstallManager, WebAppInstallManagerObserver>
      install_manager_observation_{this};

  base::ScopedObservation<HostContentSettingsMap, content_settings::Observer>
      content_settings_observation_{this};

  bool is_shutting_down_ = false;

#if BUILDFLAG(IS_CHROMEOS)
  apps::PausedApps paused_apps_;

  base::ScopedObservation<NotificationDisplayService,
                          NotificationDisplayService::Observer>
      notification_display_service_{this};

  apps::AppNotifications app_notifications_;

  raw_ptr<badging::BadgeManager, DanglingUntriaged> badge_manager_ = nullptr;

  base::ScopedObservation<MediaStreamCaptureIndicator,
                          MediaStreamCaptureIndicator::Observer>
      media_indicator_observation_{this};

  apps::MediaRequests media_requests_;
#endif  // BUILDFLAG(IS_CHROMEOS)

  std::map<std::string, WebAppShortcutsMenuItemInfo> shortcut_id_map_;
  ShortcutId::Generator shortcut_id_generator_;

  base::WeakPtrFactory<WebAppPublisherHelper> weak_ptr_factory_{this};
};

}  // namespace web_app

#endif  // CHROME_BROWSER_WEB_APPLICATIONS_APP_SERVICE_WEB_APP_PUBLISHER_HELPER_H_