| 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/blocked_response.h" | |
| 6 | |
| 7 #include "app/l10n_util.h" | |
| 8 #include "app/resource_bundle.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/privacy_blacklist/blacklist.h" | |
| 13 #include "chrome/common/jstemplate_builder.h" | |
| 14 #include "chrome/common/url_constants.h" | |
| 15 #include "grit/browser_resources.h" | |
| 16 #include "grit/generated_resources.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 unsigned long Hash(std::set<std::string>::iterator i) { | |
| 21 return (unsigned long)(i.operator->()); | |
| 22 } | |
| 23 | |
| 24 std::string Dehash(unsigned long l) { | |
| 25 return *(std::string*)l; | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 namespace chrome { | |
| 31 | |
| 32 const char kUnblockScheme[] = "chrome-unblock"; | |
| 33 | |
| 34 const char kBlockScheme[] = "chrome-block"; | |
| 35 | |
| 36 std::string BlockedResponse::GetHTML(const std::string& url, | |
| 37 const Blacklist::Match* match) { | |
| 38 DictionaryValue strings; | |
| 39 strings.SetString(L"title", l10n_util::GetString(IDS_BLACKLIST_TITLE)); | |
| 40 strings.SetString(L"message", l10n_util::GetString(IDS_BLACKLIST_MESSAGE)); | |
| 41 strings.SetString(L"unblock", l10n_util::GetString(IDS_BLACKLIST_UNBLOCK)); | |
| 42 | |
| 43 // If kBlockAll is specified, assign blame to such an entry. | |
| 44 // Otherwise pick the first one. | |
| 45 const Blacklist::Entry* entry = NULL; | |
| 46 if (match->attributes() & Blacklist::kBlockAll) { | |
| 47 for (std::vector<const Blacklist::Entry*>::const_iterator i = | |
| 48 match->entries().begin(); i != match->entries().end(); ++i) { | |
| 49 if ((*i)->attributes() == Blacklist::kBlockAll) { | |
| 50 entry = *i; | |
| 51 break; | |
| 52 } | |
| 53 } | |
| 54 } else { | |
| 55 entry = match->entries().front(); | |
| 56 } | |
| 57 DCHECK(entry); | |
| 58 | |
| 59 strings.SetString(L"name", entry->provider()->name()); | |
| 60 strings.SetString(L"url", entry->provider()->url()); | |
| 61 strings.SetString(L"bypass", GetUnblockedURL(url)); | |
| 62 | |
| 63 const base::StringPiece html = | |
| 64 ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_BLACKLIST_HTML); | |
| 65 return jstemplate_builder::GetI18nTemplateHtml(html, &strings); | |
| 66 } | |
| 67 | |
| 68 std::string BlockedResponse::GetImage(const Blacklist::Match*) { | |
| 69 return ResourceBundle::GetSharedInstance(). | |
| 70 GetDataResource(IDR_BLACKLIST_IMAGE); | |
| 71 } | |
| 72 | |
| 73 std::string BlockedResponse::GetHeaders(const std::string& url) { | |
| 74 return | |
| 75 "HTTP/1.1 200 OK\nContent-Type: text/html\nlocation: " | |
| 76 + GetBlockedURL(url) + "\n" + "Cache-Control: no-store"; | |
| 77 } | |
| 78 | |
| 79 std::string BlockedResponse::GetBlockedURL(const std::string& url) { | |
| 80 return std::string(kBlockScheme) + "://" + url; | |
| 81 } | |
| 82 | |
| 83 std::string BlockedResponse::GetUnblockedURL(const std::string& url) { | |
| 84 std::set<std::string>::iterator i = blocked_.insert(blocked_.end(), url); | |
| 85 | |
| 86 char buf[64]; | |
| 87 base::snprintf(buf, 64, "%s://%lX", kUnblockScheme, Hash(i)); | |
| 88 return buf; | |
| 89 } | |
| 90 | |
| 91 std::string BlockedResponse::GetOriginalURL(const std::string& url) { | |
| 92 unsigned long l = 0; | |
| 93 | |
| 94 // Read the address of the url. | |
| 95 if (sscanf(url.c_str() + sizeof(kUnblockScheme) + 2, "%lX", &l)) { | |
| 96 std::size_t p = url.find('/', sizeof(kUnblockScheme) + 2); | |
| 97 if (p != std::string::npos) | |
| 98 return Dehash(l) + url.substr(p+1); | |
| 99 return Dehash(l); | |
| 100 } | |
| 101 return chrome::kAboutBlankURL; | |
| 102 } | |
| 103 | |
| 104 } // namespace chrome | |
| OLD | NEW |