forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension_set.cc
171 lines (133 loc) · 4.62 KB
/
extension_set.cc
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
// Copyright 2013 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 "extensions/common/extension_set.h"
#include "extensions/common/constants.h"
#include "extensions/common/url_pattern_set.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace extensions {
namespace {
ExtensionId GetExtensionIdByURL(const GURL& url) {
if (url.SchemeIs(kExtensionScheme))
return url.host();
// Trying url::Origin is important to properly handle extension schemes inside
// blob: and filesystem: URLs, which won't match the extension scheme check
// above.
url::Origin origin = url::Origin::Create(url);
if (origin.scheme() == kExtensionScheme)
return origin.host();
return ExtensionId();
}
} // namespace
ExtensionSet::const_iterator::const_iterator() {}
ExtensionSet::const_iterator::const_iterator(const const_iterator& other)
: it_(other.it_) {
}
ExtensionSet::const_iterator::const_iterator(ExtensionMap::const_iterator it)
: it_(it) {
}
ExtensionSet::const_iterator::~const_iterator() {}
ExtensionSet::ExtensionSet() {
}
ExtensionSet::~ExtensionSet() {
}
size_t ExtensionSet::size() const {
return extensions_.size();
}
bool ExtensionSet::is_empty() const {
return extensions_.empty();
}
bool ExtensionSet::Contains(const ExtensionId& extension_id) const {
return extensions_.find(extension_id) != extensions_.end();
}
bool ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) {
auto iter = extensions_.find(extension->id());
if (iter != extensions_.end()) {
iter->second = extension;
return false; // Had a previous entry.
}
extensions_.emplace(extension->id(), extension);
return true; // New entry added.
}
bool ExtensionSet::InsertAll(const ExtensionSet& extensions) {
size_t before = size();
for (ExtensionSet::const_iterator iter = extensions.begin();
iter != extensions.end(); ++iter) {
Insert(*iter);
}
return size() != before;
}
bool ExtensionSet::Remove(const ExtensionId& id) {
return extensions_.erase(id) > 0;
}
void ExtensionSet::Clear() {
extensions_.clear();
}
ExtensionId ExtensionSet::GetExtensionOrAppIDByURL(const GURL& url) const {
ExtensionId extension_id = GetExtensionIdByURL(url);
if (!extension_id.empty())
return extension_id;
// GetHostedAppByURL already supports filesystem: URLs (via MatchesURL).
// TODO(crbug/852162): Add support for blob: URLs in MatchesURL.
const Extension* extension = GetHostedAppByURL(url);
if (!extension)
return ExtensionId();
return extension->id();
}
const Extension* ExtensionSet::GetExtensionOrAppByURL(const GURL& url) const {
ExtensionId extension_id = GetExtensionIdByURL(url);
if (!extension_id.empty())
return GetByID(extension_id);
// GetHostedAppByURL already supports filesystem: URLs (via MatchesURL).
// TODO(crbug/852162): Add support for blob: URLs in MatchesURL.
return GetHostedAppByURL(url);
}
const Extension* ExtensionSet::GetAppByURL(const GURL& url) const {
const Extension* extension = GetExtensionOrAppByURL(url);
return (extension && extension->is_app()) ? extension : NULL;
}
const Extension* ExtensionSet::GetHostedAppByURL(const GURL& url) const {
for (auto iter = extensions_.cbegin(); iter != extensions_.cend(); ++iter) {
if (iter->second->web_extent().MatchesURL(url))
return iter->second.get();
}
return NULL;
}
const Extension* ExtensionSet::GetHostedAppByOverlappingWebExtent(
const URLPatternSet& extent) const {
for (auto iter = extensions_.cbegin(); iter != extensions_.cend(); ++iter) {
if (iter->second->web_extent().OverlapsWith(extent))
return iter->second.get();
}
return NULL;
}
bool ExtensionSet::InSameExtent(const GURL& old_url,
const GURL& new_url) const {
return GetExtensionOrAppByURL(old_url) ==
GetExtensionOrAppByURL(new_url);
}
const Extension* ExtensionSet::GetByID(const ExtensionId& id) const {
auto i = extensions_.find(id);
if (i != extensions_.end())
return i->second.get();
return nullptr;
}
ExtensionIdSet ExtensionSet::GetIDs() const {
ExtensionIdSet ids;
for (auto it = extensions_.cbegin(); it != extensions_.cend(); ++it) {
ids.insert(it->first);
}
return ids;
}
bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const {
if (url.SchemeIs(kExtensionScheme))
return true;
for (auto it = extensions_.cbegin(); it != extensions_.cend(); ++it) {
if (it->second->location() == Manifest::COMPONENT &&
it->second->web_extent().MatchesURL(url))
return true;
}
return false;
}
} // namespace extensions