| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/privacy_blacklist/blocked_response.h" | 5 #include "chrome/browser/privacy_blacklist/blocked_response.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "chrome/browser/privacy_blacklist/blacklist.h" | 12 #include "chrome/browser/privacy_blacklist/blacklist.h" |
| 12 #include "chrome/common/jstemplate_builder.h" | 13 #include "chrome/common/jstemplate_builder.h" |
| 14 #include "chrome/common/url_constants.h" |
| 13 #include "grit/browser_resources.h" | 15 #include "grit/browser_resources.h" |
| 14 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 15 | 17 |
| 16 namespace BlockedResponse { | 18 namespace { |
| 17 | 19 |
| 18 std::string GetHTML(const Blacklist::Match* match) { | 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 } |
| 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) { |
| 19 DictionaryValue strings; | 38 DictionaryValue strings; |
| 20 strings.SetString(L"title", l10n_util::GetString(IDS_BLACKLIST_TITLE)); | 39 strings.SetString(L"title", l10n_util::GetString(IDS_BLACKLIST_TITLE)); |
| 21 strings.SetString(L"message", l10n_util::GetString(IDS_BLACKLIST_MESSAGE)); | 40 strings.SetString(L"message", l10n_util::GetString(IDS_BLACKLIST_MESSAGE)); |
| 22 strings.SetString(L"unblock", l10n_util::GetString(IDS_BLACKLIST_UNBLOCK)); | 41 strings.SetString(L"unblock", l10n_util::GetString(IDS_BLACKLIST_UNBLOCK)); |
| 23 | 42 |
| 24 // If kBlockAll is specified, assign blame to such an entry. | 43 // If kBlockAll is specified, assign blame to such an entry. |
| 25 // Otherwise pick the first one. | 44 // Otherwise pick the first one. |
| 26 const Blacklist::Entry* entry = NULL; | 45 const Blacklist::Entry* entry = NULL; |
| 27 if (match->attributes() & Blacklist::kBlockAll) { | 46 if (match->attributes() & Blacklist::kBlockAll) { |
| 28 for (std::vector<const Blacklist::Entry*>::const_iterator i = | 47 for (std::vector<const Blacklist::Entry*>::const_iterator i = |
| 29 match->entries().begin(); i != match->entries().end(); ++i) { | 48 match->entries().begin(); i != match->entries().end(); ++i) { |
| 30 if ((*i)->attributes() == Blacklist::kBlockAll) { | 49 if ((*i)->attributes() == Blacklist::kBlockAll) { |
| 31 entry = *i; | 50 entry = *i; |
| 32 break; | 51 break; |
| 33 } | 52 } |
| 34 } | 53 } |
| 35 } else { | 54 } else { |
| 36 entry = match->entries().front(); | 55 entry = match->entries().front(); |
| 37 } | 56 } |
| 38 DCHECK(entry); | 57 DCHECK(entry); |
| 39 | 58 |
| 40 strings.SetString(L"name", entry->provider()->name()); | 59 strings.SetString(L"name", entry->provider()->name()); |
| 41 strings.SetString(L"url", entry->provider()->url()); | 60 strings.SetString(L"url", entry->provider()->url()); |
| 61 strings.SetString(L"bypass", GetUnblockedURL(url)); |
| 42 | 62 |
| 43 const base::StringPiece html = | 63 const base::StringPiece html = |
| 44 ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_BLACKLIST_HTML); | 64 ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_BLACKLIST_HTML); |
| 45 return jstemplate_builder::GetI18nTemplateHtml(html, &strings); | 65 return jstemplate_builder::GetI18nTemplateHtml(html, &strings); |
| 46 } | 66 } |
| 47 | 67 |
| 48 std::string GetImage(const Blacklist::Match*) { | 68 std::string BlockedResponse::GetImage(const Blacklist::Match*) { |
| 49 return ResourceBundle::GetSharedInstance(). | 69 return ResourceBundle::GetSharedInstance(). |
| 50 GetDataResource(IDR_BLACKLIST_IMAGE); | 70 GetDataResource(IDR_BLACKLIST_IMAGE); |
| 51 } | 71 } |
| 52 | 72 |
| 53 } // namespace BlockedResponse | 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 } // end namespace chrome |
| OLD | NEW |