Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: extensions/browser/api/declarative_net_request/matcher_util.cc

Issue 2881453002: DNR Prototype: With flatbuffers
Patch Set: -- Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "extensions/browser/api/declarative_net_request/matcher_util.h"
6
7 #include "base/files/file_util.h"
8 #include "base/files/memory_mapped_file.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/threading/thread_restrictions.h"
12 #include "components/subresource_filter/core/common/first_party_origin.h"
13 #include "components/subresource_filter/core/common/flat/extension_metadata_gene rated.h"
14 #include "content/public/browser/resource_request_info.h"
15 #include "extensions/browser/info_map.h"
16 #include "net/url_request/url_request.h"
17
18 namespace extensions {
19 namespace declarative_net_request {
20
21 namespace {
22
23 ElementType ToElementType(content::ResourceType type) {
24 switch (type) {
25 case content::RESOURCE_TYPE_LAST_TYPE:
26 case content::RESOURCE_TYPE_PREFETCH:
27 case content::RESOURCE_TYPE_CSP_REPORT: // TODO
28 case content::RESOURCE_TYPE_SUB_RESOURCE: // TODO
29 return ElementType::ElementType_OTHER;
30 case content::RESOURCE_TYPE_SCRIPT:
31 case content::RESOURCE_TYPE_WORKER:
32 case content::RESOURCE_TYPE_SHARED_WORKER:
33 case content::RESOURCE_TYPE_SERVICE_WORKER:
34 return ElementType::ElementType_SCRIPT;
35 case content::RESOURCE_TYPE_IMAGE:
36 case content::RESOURCE_TYPE_FAVICON:
37 return ElementType::ElementType_IMAGE;
38 case content::RESOURCE_TYPE_STYLESHEET:
39 return ElementType::ElementType_STYLESHEET;
40 case content::RESOURCE_TYPE_OBJECT:
41 case content::RESOURCE_TYPE_PLUGIN_RESOURCE: // TODO
42 return ElementType::ElementType_OBJECT;
43 case content::RESOURCE_TYPE_XHR:
44 return ElementType::ElementType_XMLHTTPREQUEST;
45 case content::RESOURCE_TYPE_MAIN_FRAME: // TODO
46 case content::RESOURCE_TYPE_SUB_FRAME:
47 return ElementType::ElementType_SUBDOCUMENT;
48 case content::RESOURCE_TYPE_PING:
49 return ElementType::ElementType_PING;
50 case content::RESOURCE_TYPE_MEDIA:
51 return ElementType::ElementType_MEDIA;
52 case content::RESOURCE_TYPE_FONT_RESOURCE:
53 return ElementType::ElementType_FONT;
54 }
55 NOTREACHED();
56 return ElementType::ElementType_OTHER;
57 }
58
59 subresource_filter::proto::ElementType ToProtoElementType(ElementType type) {
60 switch (type) {
61 case ElementType::ElementType_OTHER:
62 return subresource_filter::proto::ELEMENT_TYPE_OTHER;
63 case ElementType::ElementType_SCRIPT:
64 return subresource_filter::proto::ELEMENT_TYPE_SCRIPT;
65 case ElementType::ElementType_IMAGE:
66 return subresource_filter::proto::ELEMENT_TYPE_IMAGE;
67 case ElementType::ElementType_STYLESHEET:
68 return subresource_filter::proto::ELEMENT_TYPE_STYLESHEET;
69 case ElementType::ElementType_OBJECT:
70 return subresource_filter::proto::ELEMENT_TYPE_OBJECT;
71 case ElementType::ElementType_XMLHTTPREQUEST:
72 return subresource_filter::proto::ELEMENT_TYPE_XMLHTTPREQUEST;
73 case ElementType::ElementType_OBJECT_SUBREQUEST:
74 return subresource_filter::proto::ELEMENT_TYPE_OBJECT_SUBREQUEST;
75 case ElementType::ElementType_SUBDOCUMENT:
76 return subresource_filter::proto::ELEMENT_TYPE_SUBDOCUMENT;
77 case ElementType::ElementType_PING:
78 return subresource_filter::proto::ELEMENT_TYPE_PING;
79 case ElementType::ElementType_MEDIA:
80 return subresource_filter::proto::ELEMENT_TYPE_MEDIA;
81 case ElementType::ElementType_FONT:
82 return subresource_filter::proto::ELEMENT_TYPE_FONT;
83 case ElementType::ElementType_POPUP:
84 return subresource_filter::proto::ELEMENT_TYPE_POPUP;
85 case ElementType::ElementType_WEBSOCKET:
86 return subresource_filter::proto::ELEMENT_TYPE_WEBSOCKET;
87 default:
88 NOTREACHED();
89 return subresource_filter::proto::ELEMENT_TYPE_UNSPECIFIED;
90 }
91 }
92
93 ElementType GetElementType(const net::URLRequest* request) {
94 DCHECK(request);
95 if (request->url().SchemeIsWSOrWSS())
96 return ElementType::ElementType_WEBSOCKET;
97 const auto* info = content::ResourceRequestInfo::ForRequest(request);
98 return info ? ToElementType(info->GetResourceType())
99 : ElementType::ElementType_OTHER;
100 }
101
102 } // namespace
103
104 ExtensionIndexedRulesetMatcher::ExtensionIndexedRulesetMatcher(
105 std::unique_ptr<base::MemoryMappedFile,
106 content::BrowserThread::DeleteOnFileThread> ruleset_file)
107 : ruleset_file_(std::move(ruleset_file)),
108 root_(flat::GetExtensionIndexedRuleset(ruleset_file_->data())),
109 blacklist_matcher_(root_->blacklist_index()),
110 whitelist_matcher_(root_->whitelist_index()),
111 redirect_matcher_(root_->redirect_index()),
112 extension_metdata_(root_->extension_metdata()) {}
113
114 bool ExtensionIndexedRulesetMatcher::ShouldBlockRequest(
115 const GURL& url,
116 const url::Origin& first_party_origin,
117 ElementType element_type,
118 bool is_third_party) {
119 return !!blacklist_matcher_.FindMatch(
120 url, first_party_origin, ToProtoElementType(element_type),
121 subresource_filter::proto::ACTIVATION_TYPE_UNSPECIFIED,
122 is_third_party, false) &&
123 !whitelist_matcher_.FindMatch(
124 url, first_party_origin, ToProtoElementType(element_type),
125 subresource_filter::proto::ACTIVATION_TYPE_UNSPECIFIED,
126 is_third_party, false);
127 }
128
129 bool ShouldBlockRequest(void* BrowserContext,
130 const InfoMap* info_map,
131 net::URLRequest* request) {
132 SCOPED_UMA_HISTOGRAM_TIMER("DNR.ShouldBlockRequest");
133 const GURL& url = request->url();
134 const base::Optional<url::Origin> initiator = request->initiator();
135 const url::Origin first_party_origin =
136 initiator ? initiator.value() : url::Origin();
137 ElementType element_type = GetElementType(request);
138 const bool is_third_party =
139 subresource_filter::FirstPartyOrigin::IsThirdParty(url,
140 first_party_origin);
141
142 for (const auto& pair : *info_map->ruleset_manager()->rules_map()) {
143 // const string& extension_id = pair.first.id;
144
145 if (pair.second->ShouldBlockRequest(url, first_party_origin, element_type,
146 is_third_party))
147 return true;
148 }
149 return false;
150 }
151
152 std::unique_ptr<ExtensionIndexedRulesetMatcher>
153 CreateVerifiedExtensionIndexedRulesetMatcher(
154 const base::FilePath& indexed_ruleset_path) {
155 SCOPED_UMA_HISTOGRAM_TIMER("DNR.LoadRulesetMatcher");
156 base::ThreadRestrictions::AssertIOAllowed();
157
158 // TODO log error uma.
159 if (!base::PathExists(indexed_ruleset_path)) {
160 NOTREACHED() << "Ruleset file does not exist";
161 return nullptr;
162 }
163
164 std::unique_ptr<base::MemoryMappedFile,
165 content::BrowserThread::DeleteOnFileThread>
166 ruleset_file(new base::MemoryMappedFile());
167 LOG(ERROR) << "--------indexed_ruleset_path " << indexed_ruleset_path.value()
168 << "\n";
169 if (!ruleset_file->Initialize(indexed_ruleset_path,
170 base::MemoryMappedFile::READ_ONLY)) {
171 NOTREACHED() << "Error reading indexed ruleset";
172 return nullptr;
173 }
174
175 LOG(ERROR) << "--------initialized ruleset\n";
176
177 // TODO we should also verify file checksum.
178 flatbuffers::Verifier verifier(ruleset_file->data(), ruleset_file->length());
179 if (!flat::VerifyExtensionIndexedRulesetBuffer(verifier)) {
180 NOTREACHED() << "Buffer could not be verified";
181 }
182
183 return base::MakeUnique<ExtensionIndexedRulesetMatcher>(
184 std::move(ruleset_file));
185 }
186
187 } // namespace declarative_net_request
188 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698