1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/web_applications/components/install_finalizer.h"
6 
7 #include <utility>
8 
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/logging.h"
12 #include "base/optional.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "chrome/browser/web_applications/components/app_registrar.h"
15 #include "chrome/browser/web_applications/components/os_integration_manager.h"
16 #include "chrome/browser/web_applications/components/web_app_constants.h"
17 #include "chrome/browser/web_applications/components/web_app_ui_manager.h"
18 
19 namespace web_app {
20 
21 InstallFinalizer::FinalizeOptions::FinalizeOptions() = default;
22 
23 InstallFinalizer::FinalizeOptions::~FinalizeOptions() = default;
24 
25 InstallFinalizer::FinalizeOptions::FinalizeOptions(const FinalizeOptions&) =
26     default;
27 
UninstallExternalWebAppByUrl(const GURL & app_url,ExternalInstallSource external_install_source,UninstallWebAppCallback callback)28 void InstallFinalizer::UninstallExternalWebAppByUrl(
29     const GURL& app_url,
30     ExternalInstallSource external_install_source,
31     UninstallWebAppCallback callback) {
32   base::Optional<AppId> app_id = registrar().LookupExternalAppId(app_url);
33   if (!app_id.has_value()) {
34     LOG(WARNING) << "Couldn't uninstall web app with url " << app_url
35                  << "; No corresponding web app for url.";
36     base::ThreadTaskRunnerHandle::Get()->PostTask(
37         FROM_HERE, base::BindOnce(std::move(callback), /*uninstalled=*/false));
38     return;
39   }
40 
41   UninstallExternalWebApp(app_id.value(), external_install_source,
42                           std::move(callback));
43 }
44 
SetSubsystems(AppRegistrar * registrar,WebAppUiManager * ui_manager,AppRegistryController * registry_controller,OsIntegrationManager * os_integration_manager)45 void InstallFinalizer::SetSubsystems(
46     AppRegistrar* registrar,
47     WebAppUiManager* ui_manager,
48     AppRegistryController* registry_controller,
49     OsIntegrationManager* os_integration_manager) {
50   registrar_ = registrar;
51   ui_manager_ = ui_manager;
52   registry_controller_ = registry_controller;
53   os_integration_manager_ = os_integration_manager;
54 }
55 
CanReparentTab(const AppId & app_id,bool shortcut_created) const56 bool InstallFinalizer::CanReparentTab(const AppId& app_id,
57                                       bool shortcut_created) const {
58   // Reparent the web contents into its own window only if that is the
59   // app's launch type.
60   DCHECK(registrar_);
61   if (registrar_->GetAppUserDisplayMode(app_id) != DisplayMode::kStandalone)
62     return false;
63 
64   return ui_manager().CanReparentAppTabToWindow(app_id, shortcut_created);
65 }
66 
ReparentTab(const AppId & app_id,bool shortcut_created,content::WebContents * web_contents)67 void InstallFinalizer::ReparentTab(const AppId& app_id,
68                                    bool shortcut_created,
69                                    content::WebContents* web_contents) {
70   DCHECK(web_contents);
71   return ui_manager().ReparentAppTabToWindow(web_contents, app_id,
72                                              shortcut_created);
73 }
74 
legacy_finalizer_for_testing()75 InstallFinalizer* InstallFinalizer::legacy_finalizer_for_testing() {
76   return nullptr;
77 }
78 
registrar() const79 AppRegistrar& InstallFinalizer::registrar() const {
80   DCHECK(!is_legacy_finalizer());
81   return *registrar_;
82 }
83 
84 }  // namespace web_app
85