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

Side by Side Diff: chrome/browser/privacy_blacklist/blacklist_interceptor.cc

Issue 501082: Implement delaying resource requests until privacy blacklists are ready. (Closed)
Patch Set: don't get stuck on errors Created 10 years, 11 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 (c) 2009 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/privacy_blacklist/blacklist_interceptor.h"
6
7 #include "app/l10n_util.h"
8 #include "app/resource_bundle.h"
9 #include "base/string_util.h"
10 #include "base/values.h"
11 #include "chrome/browser/privacy_blacklist/blacklist.h"
12 #include "chrome/browser/privacy_blacklist/blacklist_manager.h"
13 #include "chrome/browser/privacy_blacklist/blacklist_request_info.h"
14 #include "chrome/common/jstemplate_builder.h"
15 #include "net/url_request/url_request_simple_job.h"
16 #include "grit/browser_resources.h"
17 #include "grit/generated_resources.h"
18
19 namespace {
20
21 class URLRequestBlacklistJob : public URLRequestSimpleJob {
22 public:
23 URLRequestBlacklistJob(URLRequest* request,
24 const BlacklistRequestInfo& request_info)
25 : URLRequestSimpleJob(request),
26 request_info_(request_info) {
27 }
28
29 virtual bool GetData(std::string* mime_type,
30 std::string* charset,
31 std::string* data) const {
32 if (ResourceType::IsFrame(request_info_.resource_type())) {
33 *mime_type = "text/html";
34 *charset = "utf-8";
35 *data = GetHTMLResponse();
36 } else {
37 // TODO(phajdan.jr): For some resources (like script) we may want to
38 // simulate an error instead or return other MIME type.
39 *mime_type = "image/png";
40 *data = GetImageResponse();
41 }
42 return true;
43 }
44
45 private:
46 std::string GetHTMLResponse() const {
47 return "HTTP/1.1 200 OK\r\n"
48 "Content-Type: text/html;charset=utf-8\r\n"
49 "Cache-Control: no-store\r\n\r\n" + GetHTML();
50 }
51
52 static std::string GetImageResponse() {
53 return "HTTP/1.1 200 OK\r\n"
54 "Content-Type: image/png\r\n"
55 "Cache-Control: no-store\r\n\r\n" + GetImage();
56 }
57
58 static std::string GetImage() {
59 return ResourceBundle::GetSharedInstance().
60 GetDataResource(IDR_BLACKLIST_IMAGE);
61 }
62
63 std::string GetHTML() const {
64 DictionaryValue strings;
65 strings.SetString(L"title", l10n_util::GetString(IDS_BLACKLIST_TITLE));
66 strings.SetString(L"message", l10n_util::GetString(IDS_BLACKLIST_MESSAGE));
67
68 const Blacklist::Provider* provider = GetBestMatchingEntryProvider();
69 strings.SetString(L"name", provider->name());
70 strings.SetString(L"url", provider->url());
71
72 const base::StringPiece html =
73 ResourceBundle::GetSharedInstance().GetRawDataResource(
74 IDR_BLACKLIST_HTML);
75 return jstemplate_builder::GetI18nTemplateHtml(html, &strings);
76 }
77
78 const Blacklist::Provider* GetBestMatchingEntryProvider() const {
79 // If kBlockAll is specified, assign blame to such an entry.
80 // Otherwise pick the first one.
81 const BlacklistManager* blacklist_manager =
82 request_info_.GetBlacklistManager();
83 const Blacklist* blacklist = blacklist_manager->GetCompiledBlacklist();
84 scoped_ptr<Blacklist::Match> match(blacklist->findMatch(request_->url()));
85 const Blacklist::Entry* entry = NULL;
86 if (match->attributes() & Blacklist::kBlockAll) {
87 for (std::vector<const Blacklist::Entry*>::const_iterator i =
88 match->entries().begin(); i != match->entries().end(); ++i) {
89 if ((*i)->attributes() == Blacklist::kBlockAll) {
90 entry = *i;
91 break;
92 }
93 }
94 } else {
95 entry = match->entries().front();
96 }
97 return entry->provider();
98 }
99
100 const BlacklistRequestInfo& request_info_;
101
102 DISALLOW_COPY_AND_ASSIGN(URLRequestBlacklistJob);
103 };
104
105 } // namespace
106
107 BlacklistInterceptor::BlacklistInterceptor() {
108 URLRequest::RegisterRequestInterceptor(this);
109 }
110
111 BlacklistInterceptor::~BlacklistInterceptor() {
112 URLRequest::UnregisterRequestInterceptor(this);
113 }
114
115 URLRequestJob* BlacklistInterceptor::MaybeIntercept(URLRequest* request) {
116 BlacklistRequestInfo* request_info =
117 BlacklistRequestInfo::FromURLRequest(request);
118 if (!request_info) {
119 // Not all requests have privacy blacklist data, for example downloads.
120 return NULL;
121 }
122
123 const BlacklistManager* blacklist_manager =
124 request_info->GetBlacklistManager();
125 const Blacklist* blacklist = blacklist_manager->GetCompiledBlacklist();
126 scoped_ptr<Blacklist::Match> match(blacklist->findMatch(request->url()));
127
128 if (!match.get()) {
129 // Nothing is blacklisted for this request. Do not intercept.
130 return NULL;
131 }
132
133 // TODO(phajdan.jr): Should we have some UI to notify about blocked referrer?
134 if (match->attributes() & Blacklist::kDontSendReferrer)
135 request->set_referrer(std::string());
136
137 if (match->IsBlocked(request->url()))
138 return new URLRequestBlacklistJob(request, *request_info);
139
140 return NULL;
141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698