File: bookmark_app_helper.cc

package info (click to toggle)
chromium 73.0.3683.75-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,792,156 kB
  • sloc: cpp: 13,473,466; ansic: 1,577,080; python: 898,539; javascript: 655,737; xml: 341,883; asm: 306,070; java: 289,969; perl: 80,911; objc: 67,198; sh: 43,184; cs: 27,853; makefile: 12,092; php: 11,064; yacc: 10,373; tcl: 8,875; ruby: 3,941; lex: 1,800; pascal: 1,473; lisp: 812; awk: 41; jsp: 39; sed: 19; sql: 3
file content (588 lines) | stat: -rw-r--r-- 23,286 bytes parent folder | download
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/bookmark_app_helper.h"

#include <stddef.h>

#include <string>
#include <utility>

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/banners/app_banner_manager.h"
#include "chrome/browser/banners/app_banner_manager_desktop.h"
#include "chrome/browser/banners/app_banner_settings_helper.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_delegate.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/convert_web_app.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/launch_util.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/installable/installable_data.h"
#include "chrome/browser/installable/installable_manager.h"
#include "chrome/browser/installable/installable_params.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/app_list/app_list_util.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/web_applications/components/web_app_icon_downloader.h"
#include "chrome/browser/web_applications/components/web_app_install_utils.h"
#include "chrome/browser/web_applications/extensions/bookmark_app_util.h"
#include "chrome/browser/web_applications/extensions/web_app_extension_shortcut.h"
#include "chrome/browser/webshare/share_target_pref_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/extensions/api/url_handlers/url_handlers_parser.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "chrome/common/origin_trials/chrome_origin_trial_policy.h"
#include "chrome/common/url_constants.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/notification_types.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension.h"
#include "extensions/common/url_pattern.h"
#include "net/base/load_flags.h"
#include "net/url_request/url_request.h"
#include "third_party/blink/public/common/manifest/manifest.h"
#include "third_party/skia/include/core/SkBitmap.h"

#if defined(OS_MACOSX)
#include "chrome/browser/web_applications/extensions/web_app_extension_shortcut_mac.h"
#include "chrome/common/chrome_switches.h"
#endif

#if defined(OS_CHROMEOS)
// gn check complains on Linux Ozone.
#include "ash/public/cpp/shelf_model.h"  // nogncheck
#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
#endif

namespace extensions {

namespace {

// Class to handle installing a bookmark app after it has synced. Handles
// downloading and decoding the icons.
class BookmarkAppInstaller : public base::RefCounted<BookmarkAppInstaller>,
                             public content::WebContentsObserver {
 public:
  BookmarkAppInstaller(extensions::ExtensionService* service,
                       const WebApplicationInfo& web_app_info,
                       bool is_locally_installed)
      : service_(service),
        web_app_info_(web_app_info),
        is_locally_installed_(is_locally_installed) {}

  void Run() {
    for (const auto& icon : web_app_info_.icons) {
      if (icon.url.is_valid())
        urls_to_download_.push_back(icon.url);
    }

    if (urls_to_download_.size()) {
      // Matched in OnIconsDownloaded.
      AddRef();
      SetupWebContents();

      return;
    }

    DoInstallation();
  }

  void SetupWebContents() {
    // Spin up a web contents process so we can use WebAppIconDownloader.
    // This is necessary to make sure we pick up all of the images provided
    // in favicon URLs. Without this, bookmark app sync can fail due to
    // missing icons which are not correctly extracted from a favicon.
    // (The eventual error indicates that there are missing files, which
    // are the not-extracted favicon images).
    //
    // TODO(dominickn): refactor bookmark app syncing to reuse one web
    // contents for all pending synced bookmark apps. This will avoid
    // pathological cases where n renderers for n bookmark apps are spun up on
    // first sign-in to a new machine.
    web_contents_ = content::WebContents::Create(
        content::WebContents::CreateParams(service_->profile()));
    Observe(web_contents_.get());

    // Load about:blank so that the process actually starts.
    // Image download continues in DidFinishLoad.
    content::NavigationController::LoadURLParams load_params(
        GURL("about:blank"));
    load_params.transition_type = ui::PAGE_TRANSITION_GENERATED;
    web_contents_->GetController().LoadURLWithParams(load_params);
  }

  void DidFinishLoad(content::RenderFrameHost* render_frame_host,
                     const GURL& validated_url) override {
    web_app_icon_downloader_.reset(new web_app::WebAppIconDownloader(
        web_contents_.get(), urls_to_download_,
        "Extensions.BookmarkApp.Icon.HttpStatusCodeClassOnSync",
        base::BindOnce(&BookmarkAppInstaller::OnIconsDownloaded,
                       base::Unretained(this))));

    // Skip downloading the page favicons as everything in is the URL list.
    web_app_icon_downloader_->SkipPageFavicons();
    web_app_icon_downloader_->Start();
  }

 private:
  friend class base::RefCounted<BookmarkAppInstaller>;
  ~BookmarkAppInstaller() override {}

  void OnIconsDownloaded(bool success,
                         const std::map<GURL, std::vector<SkBitmap>>& bitmaps) {
    // Ignore the unsuccessful case, as the necessary icons will be generated.
    if (success) {
      for (const auto& url_bitmaps : bitmaps) {
        for (const auto& bitmap : url_bitmaps.second) {
          // Only accept square icons.
          if (bitmap.empty() || bitmap.width() != bitmap.height())
            continue;

          downloaded_bitmaps_.push_back(
              web_app::BitmapAndSource(url_bitmaps.first, bitmap));
        }
      }
    }
    DoInstallation();
    Release();
  }

  void DoInstallation() {
    // Ensure that all icons that are in web_app_info are present, by generating
    // icons for any sizes which have failed to download. This ensures that the
    // created manifest for the bookmark app does not contain links to icons
    // which are not actually created and linked on disk.

    // Ensure that all icon widths in the web app info icon array are present in
    // the sizes to generate set. This ensures that we will have all of the
    // icon sizes from when the app was originally added, even if icon URLs are
    // no longer accessible.
    std::set<int> sizes_to_generate = web_app::SizesToGenerate();
    for (const auto& icon : web_app_info_.icons)
      sizes_to_generate.insert(icon.width);

    web_app_info_.generated_icon_color = SK_ColorTRANSPARENT;
    std::map<int, web_app::BitmapAndSource> size_map =
        web_app::ResizeIconsAndGenerateMissing(
            downloaded_bitmaps_, sizes_to_generate, web_app_info_.app_url,
            &web_app_info_.generated_icon_color);

    BookmarkAppHelper::UpdateWebAppIconsWithoutChangingLinks(size_map,
                                                             &web_app_info_);
    scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service_));
    installer->set_error_on_unsupported_requirements(true);
    installer->set_installer_callback(base::BindOnce(
        &BookmarkAppInstaller::OnInstallationDone, this, installer));
    installer->InstallWebApp(web_app_info_);
  }

  void OnInstallationDone(scoped_refptr<CrxInstaller> installer,
                          const base::Optional<CrxInstallError>& result) {
    // No result means success.
    if (!result.has_value()) {
      SetBookmarkAppIsLocallyInstalled(service_->GetBrowserContext(),
                                       installer->extension(),
                                       is_locally_installed_);
    }
  }

  extensions::ExtensionService* service_;
  WebApplicationInfo web_app_info_;
  bool is_locally_installed_;

  std::unique_ptr<content::WebContents> web_contents_;
  std::unique_ptr<web_app::WebAppIconDownloader> web_app_icon_downloader_;
  std::vector<GURL> urls_to_download_;
  std::vector<web_app::BitmapAndSource> downloaded_bitmaps_;
};

}  // namespace

// static
void BookmarkAppHelper::UpdateWebAppIconsWithoutChangingLinks(
    std::map<int, web_app::BitmapAndSource> bitmap_map,
    WebApplicationInfo* web_app_info) {
  // First add in the icon data that have urls with the url / size data from the
  // original web app info, and the data from the new icons (if any).
  for (auto& icon : web_app_info->icons) {
    if (!icon.url.is_empty() && icon.data.empty()) {
      const auto& it = bitmap_map.find(icon.width);
      if (it != bitmap_map.end() && it->second.source_url == icon.url)
        icon.data = it->second.bitmap;
    }
  }

  // Now add in any icons from the updated list that don't have URLs.
  for (const auto& pair : bitmap_map) {
    if (pair.second.source_url.is_empty()) {
      WebApplicationInfo::IconInfo icon_info;
      icon_info.data = pair.second.bitmap;
      icon_info.width = pair.first;
      icon_info.height = pair.first;
      web_app_info->icons.push_back(icon_info);
    }
  }
}

BookmarkAppHelper::BookmarkAppHelper(Profile* profile,
                                     WebApplicationInfo web_app_info,
                                     content::WebContents* contents,
                                     WebappInstallSource install_source)
    : profile_(profile),
      contents_(contents),
      web_app_info_(web_app_info),
      crx_installer_(CrxInstaller::CreateSilent(
          ExtensionSystem::Get(profile)->extension_service())),
      for_installable_site_(web_app::ForInstallableSite::kUnknown),
      install_source_(install_source),
      weak_factory_(this) {
  if (contents)
    installable_manager_ = InstallableManager::FromWebContents(contents);

  // Use the last bookmark app creation type. The launch container is decided by
  // the system for desktop PWAs.
  if (!base::FeatureList::IsEnabled(::features::kDesktopPWAWindowing)) {
    web_app_info_.open_as_window =
        profile_->GetPrefs()->GetInteger(
            pref_names::kBookmarkAppCreationLaunchType) == LAUNCH_TYPE_WINDOW;
  }

  // The default app title is the page title, which can be quite long. Limit the
  // default name used to something sensible.
  const int kMaxDefaultTitle = 40;
  if (web_app_info_.title.length() > kMaxDefaultTitle) {
    web_app_info_.title = web_app_info_.title.substr(0, kMaxDefaultTitle - 3) +
                          base::UTF8ToUTF16("...");
  }

  registrar_.Add(this, NOTIFICATION_CRX_INSTALLER_DONE,
                 content::Source<CrxInstaller>(crx_installer_.get()));

  registrar_.Add(this, NOTIFICATION_EXTENSION_INSTALL_ERROR,
                 content::Source<CrxInstaller>(crx_installer_.get()));

  crx_installer_->set_error_on_unsupported_requirements(true);
}

BookmarkAppHelper::~BookmarkAppHelper() {}

void BookmarkAppHelper::Create(const CreateBookmarkAppCallback& callback) {
  callback_ = callback;

  if (is_no_network_install_) {
    // |for_installable_site_| is determined by fetching a manifest and running
    // the eligibility check on it. If we don't hit the network, assume that the
    // app represetned by |web_app_info_| is installable.
    for_installable_site_ = web_app::ForInstallableSite::kYes;
    OnIconsDownloaded(true, std::map<GURL, std::vector<SkBitmap>>());
    // Do not fetch the manifest for extension URLs.
  } else if (contents_ &&
             !contents_->GetVisibleURL().SchemeIs(kExtensionScheme)) {
    // Null in tests. OnDidPerformInstallableCheck is called via a testing API.
    // TODO(crbug.com/829232) ensure this is consistent with other calls to
    // GetData.
    if (installable_manager_) {
      InstallableParams params;
      params.check_eligibility = true;
      params.valid_primary_icon = true;
      params.valid_manifest = true;
      params.check_webapp_manifest_display = false;
      // Do not wait for a service worker if it doesn't exist.
      params.has_worker = !bypass_service_worker_check_;
      installable_manager_->GetData(
          params, base::Bind(&BookmarkAppHelper::OnDidPerformInstallableCheck,
                             weak_factory_.GetWeakPtr()));
    }
  } else {
    for_installable_site_ = web_app::ForInstallableSite::kNo;
    OnIconsDownloaded(true, std::map<GURL, std::vector<SkBitmap>>());
  }
}

void BookmarkAppHelper::OnDidPerformInstallableCheck(
    const InstallableData& data) {
  DCHECK(data.manifest_url.is_valid() || data.manifest->IsEmpty());

  if (contents_->IsBeingDestroyed())
    return;

  if (require_manifest_ && !data.valid_manifest) {
    LOG(WARNING) << "Did not install " << web_app_info_.app_url.spec()
                 << " because it didn't have a manifest";
    callback_.Run(nullptr, web_app_info_);
    return;
  }

  for_installable_site_ =
      data.error_code == NO_ERROR_DETECTED && !shortcut_app_requested_
          ? web_app::ForInstallableSite::kYes
          : web_app::ForInstallableSite::kNo;

  web_app::UpdateWebAppInfoFromManifest(*data.manifest, &web_app_info_,
                                        for_installable_site_);

  // TODO(mgiuca): Web Share Target should have its own flag, rather than using
  // the experimental-web-platform-features flag. https://crbug.com/736178.
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          ::switches::kEnableExperimentalWebPlatformFeatures)) {
    UpdateShareTargetInPrefs(data.manifest_url, *data.manifest,
                             profile_->GetPrefs());
  }

  const std::vector<GURL> web_app_info_icon_urls =
      web_app::GetValidIconUrlsToDownload(data, web_app_info_);

  web_app::MergeInstallableDataIcon(data, &web_app_info_);

  web_app_icon_downloader_.reset(new web_app::WebAppIconDownloader(
      contents_, web_app_info_icon_urls,
      "Extensions.BookmarkApp.Icon.HttpStatusCodeClassOnCreate",
      base::BindOnce(&BookmarkAppHelper::OnIconsDownloaded,
                     weak_factory_.GetWeakPtr())));

  // If the manifest specified icons, or this is a System App, don't use the
  // page icons.
  if (!data.manifest->icons.empty() ||
      contents_->GetVisibleURL().SchemeIs(content::kChromeUIScheme))
    web_app_icon_downloader_->SkipPageFavicons();

  web_app_icon_downloader_->Start();
}

void BookmarkAppHelper::OnIconsDownloaded(
    bool success,
    const std::map<GURL, std::vector<SkBitmap>>& bitmaps) {
  // The tab has navigated away during the icon download. Cancel the bookmark
  // app creation.
  if (!success) {
    web_app_icon_downloader_.reset();
    callback_.Run(nullptr, web_app_info_);
    return;
  }

  std::vector<web_app::BitmapAndSource> downloaded_icons =
      web_app::FilterSquareIcons(bitmaps, web_app_info_);
  web_app::ResizeDownloadedIconsGenerateMissing(std::move(downloaded_icons),
                                                &web_app_info_);

  web_app_icon_downloader_.reset();

  if (!contents_) {
    // The web contents can be null in tests.
    OnBubbleCompleted(true, web_app_info_);
    return;
  }

  Browser* browser = chrome::FindBrowserWithWebContents(contents_);
  if (!browser) {
    // The browser can be null in tests.
    OnBubbleCompleted(true, web_app_info_);
    return;
  }

  if (base::FeatureList::IsEnabled(::features::kDesktopPWAWindowing) &&
      for_installable_site_ == web_app::ForInstallableSite::kYes) {
    web_app_info_.open_as_window = true;
    chrome::ShowPWAInstallDialog(
        contents_, web_app_info_,
        base::BindOnce(&BookmarkAppHelper::OnBubbleCompleted,
                       weak_factory_.GetWeakPtr()));
  } else {
    chrome::ShowBookmarkAppDialog(
        contents_, web_app_info_,
        base::BindOnce(&BookmarkAppHelper::OnBubbleCompleted,
                       weak_factory_.GetWeakPtr()));
  }
}

void BookmarkAppHelper::OnBubbleCompleted(
    bool user_accepted,
    const WebApplicationInfo& web_app_info) {
  if (user_accepted) {
    web_app_info_ = web_app_info;

    if (is_policy_installed_app_)
      crx_installer_->set_install_source(Manifest::EXTERNAL_POLICY_DOWNLOAD);

    if (is_default_app_) {
      crx_installer_->set_install_source(Manifest::EXTERNAL_PREF_DOWNLOAD);
      // InstallWebApp will OR the creation flags with FROM_BOOKMARK.
      crx_installer_->set_creation_flags(Extension::WAS_INSTALLED_BY_DEFAULT);
    }

    if (is_system_app_) {
      // System Apps are considered EXTERNAL_COMPONENT as they are downloaded
      // from the WebUI they point to. COMPONENT seems like the more correct
      // value, but usages (icon loading, filesystem cleanup), are tightly
      // coupled to this value, making it unsuitable.
      crx_installer_->set_install_source(Manifest::EXTERNAL_COMPONENT);
      // InstallWebApp will OR the creation flags with FROM_BOOKMARK.
      crx_installer_->set_creation_flags(Extension::WAS_INSTALLED_BY_DEFAULT);
    }

    if (is_no_network_install_) {
      // Ensure that this app is not synced. A no-network install means we have
      // all data locally, so assume that there is some mechanism to propagate
      // the local source of data in place of usual extension sync.
      crx_installer_->set_install_source(Manifest::EXTERNAL_PREF_DOWNLOAD);
    }

    crx_installer_->InstallWebApp(web_app_info_);

    if (InstallableMetrics::IsReportableInstallSource(install_source_) &&
        for_installable_site_ == web_app::ForInstallableSite::kYes) {
      InstallableMetrics::TrackInstallEvent(install_source_);
    }
  } else {
    callback_.Run(nullptr, web_app_info_);
  }
}

void BookmarkAppHelper::FinishInstallation(const Extension* extension) {
  // Set the default 'open as' preference for use next time the dialog is
  // shown.
  LaunchType launch_type =
      web_app_info_.open_as_window ? LAUNCH_TYPE_WINDOW : LAUNCH_TYPE_REGULAR;

  profile_->GetPrefs()->SetInteger(pref_names::kBookmarkAppCreationLaunchType,
                                   launch_type);

  if (forced_launch_type_)
    launch_type = forced_launch_type_.value();

  // Set the launcher type for the app.
  SetLaunchType(profile_, extension->id(), launch_type);

  // Set this app to be locally installed, as it was installed from this
  // machine.
  SetBookmarkAppIsLocallyInstalled(profile_, extension, true);

  if (!contents_) {
    // The web contents can be null in tests.
    callback_.Run(extension, web_app_info_);
    return;
  }

  // Record an app banner added to homescreen event to ensure banners are not
  // shown for this app.
  AppBannerSettingsHelper::RecordBannerEvent(
      contents_, web_app_info_.app_url, web_app_info_.app_url.spec(),
      AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
      base::Time::Now());

#if !defined(OS_CHROMEOS)
  // Pin the app to the relevant launcher depending on the OS.
  Profile* current_profile = profile_->GetOriginalProfile();
#endif  // !defined(OS_CHROMEOS)

  // If there is no browser, it means that the app is being installed in the
  // background. We skip some steps in this case.
  const bool silent_install =
      (chrome::FindBrowserWithWebContents(contents_) == nullptr);

  if (create_shortcuts_) {
#if !defined(OS_CHROMEOS)
    web_app::ShortcutLocations creation_locations;
#if defined(OS_LINUX) || defined(OS_WIN)
    creation_locations.on_desktop = true;
#else
    creation_locations.on_desktop = false;
#endif
    creation_locations.applications_menu_location =
        web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
    creation_locations.in_quick_launch_bar = false;

    web_app::CreateShortcuts(web_app::SHORTCUT_CREATION_BY_USER,
                             creation_locations, current_profile, extension);
#else
    // ChromeLauncherController does not exist in unit tests.
    if (ChromeLauncherController::instance()) {
      ChromeLauncherController::instance()->shelf_model()->PinAppWithID(
          extension->id());
    }
#endif  // !defined(OS_CHROMEOS)
  }

  if (!silent_install) {
#if defined(OS_MACOSX)
    // TODO(https://crbug.com/915571): Reparent the tab on Mac just like the
    // other platforms.
    if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
            ::switches::kDisableHostedAppShimCreation)) {
      web_app::RevealAppShimInFinderForApp(current_profile, extension);
    }
#else
    // Reparent the tab into an app window immediately when opening as a window.
    if (base::FeatureList::IsEnabled(::features::kDesktopPWAWindowing) &&
        launch_type == LAUNCH_TYPE_WINDOW && !profile_->IsOffTheRecord()) {
      ReparentWebContentsIntoAppBrowser(contents_, extension);
    }
#endif  // !defined(OS_MACOSX)
  }

  callback_.Run(extension, web_app_info_);
}

void BookmarkAppHelper::Observe(int type,
                                const content::NotificationSource& source,
                                const content::NotificationDetails& details) {
  // TODO(dominickn): bookmark app creation fails when extensions cannot be
  // created (e.g. due to management policies). Add to shelf visibility should
  // be gated on whether extensions can be created - see crbug.com/545541.
  switch (type) {
    case NOTIFICATION_CRX_INSTALLER_DONE: {
      const Extension* extension =
          content::Details<const Extension>(details).ptr();
      if (extension) {
        DCHECK_EQ(AppLaunchInfo::GetLaunchWebURL(extension),
                  web_app_info_.app_url);
        FinishInstallation(extension);
      } else {
        callback_.Run(nullptr, web_app_info_);
      }
      break;
    }
    case NOTIFICATION_EXTENSION_INSTALL_ERROR:
      callback_.Run(nullptr, web_app_info_);
      break;
    default:
      NOTREACHED();
      break;
  }
}

void CreateOrUpdateBookmarkApp(extensions::ExtensionService* service,
                               WebApplicationInfo* web_app_info,
                               bool is_locally_installed) {
  scoped_refptr<BookmarkAppInstaller> installer(
      new BookmarkAppInstaller(service, *web_app_info, is_locally_installed));
  installer->Run();
}

bool IsValidBookmarkAppUrl(const GURL& url) {
  URLPattern origin_only_pattern(Extension::kValidBookmarkAppSchemes);
  origin_only_pattern.SetMatchAllURLs(true);
  return url.is_valid() && origin_only_pattern.MatchesURL(url);
}

}  // namespace extensions