1 // Copyright 2018 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/test/test_pending_app_manager.h"
6 
7 #include <string>
8 #include <utility>
9 
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/test/bind.h"
14 #include "base/threading/sequenced_task_runner_handle.h"
15 #include "chrome/browser/web_applications/components/web_app_constants.h"
16 #include "chrome/browser/web_applications/test/test_app_registrar.h"
17 #include "chrome/browser/web_applications/test/test_install_finalizer.h"
18 #include "url/gurl.h"
19 
20 namespace web_app {
21 
TestPendingAppManager(TestAppRegistrar * registrar)22 TestPendingAppManager::TestPendingAppManager(TestAppRegistrar* registrar)
23     : deduped_install_count_(0),
24       deduped_uninstall_count_(0),
25       registrar_(registrar) {
26   // TODO(crbug.com/973324): Wire this up to a TestInstallFinalizer.
27   SetSubsystems(registrar, nullptr, nullptr, nullptr, nullptr);
28 }
29 
30 TestPendingAppManager::~TestPendingAppManager() = default;
31 
SimulatePreviouslyInstalledApp(const GURL & url,ExternalInstallSource install_source)32 void TestPendingAppManager::SimulatePreviouslyInstalledApp(
33     const GURL& url,
34     ExternalInstallSource install_source) {
35   registrar_->AddExternalApp(TestInstallFinalizer::GetAppIdForUrl(url),
36                              {url, install_source});
37 }
38 
SetInstallResultCode(InstallResultCode result_code)39 void TestPendingAppManager::SetInstallResultCode(
40     InstallResultCode result_code) {
41   install_result_code_ = result_code;
42 }
43 
Install(ExternalInstallOptions install_options,OnceInstallCallback callback)44 void TestPendingAppManager::Install(ExternalInstallOptions install_options,
45                                     OnceInstallCallback callback) {
46   // TODO(nigeltao): Add error simulation when error codes are added to the API.
47   auto weak_ptr = weak_ptr_factory_.GetWeakPtr();
48   auto result_code = install_result_code_;
49 
50   base::SequencedTaskRunnerHandle::Get()->PostTask(
51       FROM_HERE,
52       base::BindLambdaForTesting([this, weak_ptr, install_options, result_code,
53                                   callback = std::move(callback)]() mutable {
54         const GURL& url = install_options.install_url;
55         // Use a WeakPtr to be able to simulate the Install callback running
56         // after PendingAppManager gets deleted.
57         if (weak_ptr) {
58           if (!registrar_->LookupExternalAppId(url)) {
59             registrar_->AddExternalApp(
60                 TestInstallFinalizer::GetAppIdForUrl(url),
61                 {url, install_options.install_source});
62             deduped_install_count_++;
63           }
64           install_requests_.push_back(install_options);
65         }
66         std::move(std::move(callback)).Run(url, result_code);
67       }));
68 }
69 
InstallApps(std::vector<ExternalInstallOptions> install_options_list,const RepeatingInstallCallback & callback)70 void TestPendingAppManager::InstallApps(
71     std::vector<ExternalInstallOptions> install_options_list,
72     const RepeatingInstallCallback& callback) {
73   for (auto& install_options : install_options_list)
74     Install(std::move(install_options), callback);
75 }
76 
UninstallApps(std::vector<GURL> uninstall_urls,ExternalInstallSource install_source,const UninstallCallback & callback)77 void TestPendingAppManager::UninstallApps(std::vector<GURL> uninstall_urls,
78                                           ExternalInstallSource install_source,
79                                           const UninstallCallback& callback) {
80   auto weak_ptr = weak_ptr_factory_.GetWeakPtr();
81   for (const auto& url : uninstall_urls) {
82     base::SequencedTaskRunnerHandle::Get()->PostTask(
83         FROM_HERE,
84         base::BindLambdaForTesting([this, weak_ptr, url, callback]() {
85           if (weak_ptr) {
86             base::Optional<AppId> app_id = registrar_->LookupExternalAppId(url);
87             if (app_id) {
88               registrar_->RemoveExternalApp(*app_id);
89               deduped_uninstall_count_++;
90             }
91             uninstall_requests_.push_back(url);
92           }
93           callback.Run(url, true /* succeeded */);
94         }));
95   }
96 }
97 
98 }  // namespace web_app
99