File: mock_external_provider.cc

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 (119 lines) | stat: -rw-r--r-- 3,961 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/mock_external_provider.h"

#include <memory>

#include "base/containers/contains.h"
#include "base/version.h"
#include "extensions/browser/external_install_info.h"
#include "extensions/common/extension.h"

namespace extensions {

MockExternalProvider::MockExternalProvider(VisitorInterface* visitor,
                                           mojom::ManifestLocation location)
    : location_(location), visitor_(visitor), visit_count_(0) {}

MockExternalProvider::~MockExternalProvider() = default;

void MockExternalProvider::UpdateOrAddExtension(const ExtensionId& id,
                                                const std::string& version_str,
                                                const base::FilePath& path) {
  auto info = std::make_unique<ExternalInstallInfoFile>(
      id, base::Version(version_str), path, location_, Extension::NO_FLAGS,
      false, false);
  UpdateOrAddExtension(std::move(info));
}

void MockExternalProvider::UpdateOrAddExtension(
    std::unique_ptr<ExternalInstallInfoFile> info) {
  const std::string& id = info->extension_id;
  CHECK(!base::Contains(url_extension_map_, id));
  file_extension_map_[id] = std::move(info);
}

void MockExternalProvider::UpdateOrAddExtension(
    std::unique_ptr<ExternalInstallInfoUpdateUrl> info) {
  const std::string& id = info->extension_id;
  CHECK(!base::Contains(file_extension_map_, id));
  url_extension_map_[id] = std::move(info);
}

void MockExternalProvider::RemoveExtension(const ExtensionId& id) {
  file_extension_map_.erase(id);
  url_extension_map_.erase(id);
}

void MockExternalProvider::VisitRegisteredExtension() {
  visit_count_++;
  for (const auto& extension_kv : file_extension_map_)
    visitor_->OnExternalExtensionFileFound(*extension_kv.second);
  for (const auto& extension_kv : url_extension_map_)
    visitor_->OnExternalExtensionUpdateUrlFound(*extension_kv.second,
                                                true /* force_update */);
  visitor_->OnExternalProviderReady(this);
}

void MockExternalProvider::TriggerOnExternalExtensionFound() {
  for (const auto& extension_kv : file_extension_map_)
    visitor_->OnExternalExtensionFileFound(*extension_kv.second);
  for (const auto& extension_kv : url_extension_map_)
    visitor_->OnExternalExtensionUpdateUrlFound(*extension_kv.second,
                                                false /* force_update */);
}

bool MockExternalProvider::HasExtension(const std::string& id) const {
  return base::Contains(file_extension_map_, id) ||
         base::Contains(url_extension_map_, id);
}

bool MockExternalProvider::HasExtensionWithLocation(
    const std::string& id,
    mojom::ManifestLocation location) const {
  if (auto it = file_extension_map_.find(id); it != file_extension_map_.end()) {
    if (it->second->crx_location == location) {
      return true;
    }
  }

  if (auto it = url_extension_map_.find(id); it != url_extension_map_.end()) {
    if (it->second->download_location == location) {
      return true;
    }
  }

  return false;
}

bool MockExternalProvider::GetExtensionDetails(
    const std::string& id,
    mojom::ManifestLocation* location,
    std::unique_ptr<base::Version>* version) const {
  auto it1 = file_extension_map_.find(id);
  auto it2 = url_extension_map_.find(id);

  // |id| can't be on both |file_extension_map_| and |url_extension_map_|.
  if (it1 == file_extension_map_.end() && it2 == url_extension_map_.end()) {
    return false;
  }

  // Only ExternalInstallInfoFile has version.
  if (version && it1 != file_extension_map_.end()) {
    *version = std::make_unique<base::Version>(it1->second->version);
  }

  if (location) {
    *location = location_;
  }

  return true;
}

bool MockExternalProvider::IsReady() const {
  return true;
}

}  // namespace extensions