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