Avi Drissman | 60039d4 | 2022-09-13 21:49:05 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 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 "extensions/browser/mock_external_provider.h" |
| 6 | |
Devlin Cronin | d4c2a8f3 | 2017-09-29 17:08:30 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 9 | #include "base/version.h" |
| 10 | #include "extensions/browser/external_install_info.h" |
| 11 | #include "extensions/common/extension.h" |
| 12 | |
| 13 | namespace extensions { |
| 14 | |
| 15 | MockExternalProvider::MockExternalProvider(VisitorInterface* visitor, |
Gyuyoung Kim | 2e954c4 | 2021-03-19 14:06:29 | [diff] [blame] | 16 | mojom::ManifestLocation location) |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 17 | : location_(location), visitor_(visitor), visit_count_(0) {} |
| 18 | |
| 19 | MockExternalProvider::~MockExternalProvider() {} |
| 20 | |
| 21 | void MockExternalProvider::UpdateOrAddExtension(const ExtensionId& id, |
lazyboy | 8a08c9d | 2017-04-11 19:53:22 | [diff] [blame] | 22 | const std::string& version_str, |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 23 | const base::FilePath& path) { |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 24 | auto info = std::make_unique<ExternalInstallInfoFile>( |
Devlin Cronin | d4c2a8f3 | 2017-09-29 17:08:30 | [diff] [blame] | 25 | id, base::Version(version_str), path, location_, Extension::NO_FLAGS, |
| 26 | false, false); |
Minh X. Nguyen | 5c8322610 | 2018-04-19 16:10:25 | [diff] [blame] | 27 | UpdateOrAddExtension(std::move(info)); |
lazyboy | 8a08c9d | 2017-04-11 19:53:22 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void MockExternalProvider::UpdateOrAddExtension( |
| 31 | std::unique_ptr<ExternalInstallInfoFile> info) { |
Minh X. Nguyen | 5c8322610 | 2018-04-19 16:10:25 | [diff] [blame] | 32 | const std::string& id = info->extension_id; |
| 33 | CHECK(url_extension_map_.find(id) == url_extension_map_.end()); |
| 34 | file_extension_map_[id] = std::move(info); |
| 35 | } |
| 36 | |
| 37 | void MockExternalProvider::UpdateOrAddExtension( |
| 38 | std::unique_ptr<ExternalInstallInfoUpdateUrl> info) { |
| 39 | const std::string& id = info->extension_id; |
| 40 | CHECK(file_extension_map_.find(id) == file_extension_map_.end()); |
| 41 | url_extension_map_[id] = std::move(info); |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void MockExternalProvider::RemoveExtension(const ExtensionId& id) { |
Minh X. Nguyen | 5c8322610 | 2018-04-19 16:10:25 | [diff] [blame] | 45 | file_extension_map_.erase(id); |
| 46 | url_extension_map_.erase(id); |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void MockExternalProvider::VisitRegisteredExtension() { |
| 50 | visit_count_++; |
Minh X. Nguyen | 5c8322610 | 2018-04-19 16:10:25 | [diff] [blame] | 51 | for (const auto& extension_kv : file_extension_map_) |
lazyboy | 8a08c9d | 2017-04-11 19:53:22 | [diff] [blame] | 52 | visitor_->OnExternalExtensionFileFound(*extension_kv.second); |
Minh X. Nguyen | 5c8322610 | 2018-04-19 16:10:25 | [diff] [blame] | 53 | for (const auto& extension_kv : url_extension_map_) |
| 54 | visitor_->OnExternalExtensionUpdateUrlFound(*extension_kv.second, |
Maria Petrisor | bb5226b | 2022-06-02 09:10:51 | [diff] [blame] | 55 | true /* force_update */); |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 56 | visitor_->OnExternalProviderReady(this); |
| 57 | } |
| 58 | |
Maria Petrisor | dba7ac4 | 2021-10-06 09:01:30 | [diff] [blame] | 59 | void MockExternalProvider::TriggerOnExternalExtensionFound() { |
| 60 | for (const auto& extension_kv : file_extension_map_) |
| 61 | visitor_->OnExternalExtensionFileFound(*extension_kv.second); |
| 62 | for (const auto& extension_kv : url_extension_map_) |
| 63 | visitor_->OnExternalExtensionUpdateUrlFound(*extension_kv.second, |
Maria Petrisor | bb5226b | 2022-06-02 09:10:51 | [diff] [blame] | 64 | false /* force_update */); |
Maria Petrisor | dba7ac4 | 2021-10-06 09:01:30 | [diff] [blame] | 65 | } |
| 66 | |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 67 | bool MockExternalProvider::HasExtension(const std::string& id) const { |
Minh X. Nguyen | 5c8322610 | 2018-04-19 16:10:25 | [diff] [blame] | 68 | return file_extension_map_.find(id) != file_extension_map_.end() || |
| 69 | url_extension_map_.find(id) != url_extension_map_.end(); |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool MockExternalProvider::GetExtensionDetails( |
| 73 | const std::string& id, |
Gyuyoung Kim | 2e954c4 | 2021-03-19 14:06:29 | [diff] [blame] | 74 | mojom::ManifestLocation* location, |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 75 | std::unique_ptr<base::Version>* version) const { |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 76 | auto it1 = file_extension_map_.find(id); |
| 77 | auto it2 = url_extension_map_.find(id); |
Minh X. Nguyen | 5c8322610 | 2018-04-19 16:10:25 | [diff] [blame] | 78 | |
| 79 | // |id| can't be on both |file_extension_map_| and |url_extension_map_|. |
| 80 | if (it1 == file_extension_map_.end() && it2 == url_extension_map_.end()) |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 81 | return false; |
| 82 | |
Minh X. Nguyen | 5c8322610 | 2018-04-19 16:10:25 | [diff] [blame] | 83 | // Only ExternalInstallInfoFile has version. |
| 84 | if (version && it1 != file_extension_map_.end()) |
Peter Boström | 5effa32 | 2021-04-02 22:39:39 | [diff] [blame] | 85 | *version = std::make_unique<base::Version>(it1->second->version); |
lazyboy | a00eafc | 2017-04-08 00:57:19 | [diff] [blame] | 86 | |
| 87 | if (location) |
| 88 | *location = location_; |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool MockExternalProvider::IsReady() const { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | } // namespace extensions |