| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Implements common functionality for the Chrome Extensions Cookies API. | 5 // Implements common functionality for the Chrome Extensions Cookies API. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_cookies_helpers.h" | 7 #include "chrome/browser/extensions/extension_cookies_helpers.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/extension_cookies_api_constants.h" | 13 #include "chrome/browser/extensions/extension_cookies_api_constants.h" |
| 14 #include "chrome/browser/extensions/extension_tab_util.h" | 14 #include "chrome/browser/extensions/extension_tab_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/tabs/tab_strip_model.h" | 16 #include "chrome/browser/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 19 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
| 21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "net/base/cookie_utils.h" |
| 22 | 23 |
| 23 namespace keys = extension_cookies_api_constants; | 24 namespace keys = extension_cookies_api_constants; |
| 24 | 25 |
| 25 namespace extension_cookies_helpers { | 26 namespace extension_cookies_helpers { |
| 26 | 27 |
| 27 static const char kOriginalProfileStoreId[] = "0"; | 28 static const char kOriginalProfileStoreId[] = "0"; |
| 28 static const char kOffTheRecordProfileStoreId[] = "1"; | 29 static const char kOffTheRecordProfileStoreId[] = "1"; |
| 29 | 30 |
| 30 Profile* ChooseProfileFromStoreId(const std::string& store_id, | 31 Profile* ChooseProfileFromStoreId(const std::string& store_id, |
| 31 Profile* profile, | 32 Profile* profile, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 52 const std::string& store_id) { | 53 const std::string& store_id) { |
| 53 DictionaryValue* result = new DictionaryValue(); | 54 DictionaryValue* result = new DictionaryValue(); |
| 54 | 55 |
| 55 // A cookie is a raw byte sequence. By explicitly parsing it as UTF8, we | 56 // A cookie is a raw byte sequence. By explicitly parsing it as UTF8, we |
| 56 // apply error correction, so the string can be safely passed to the | 57 // apply error correction, so the string can be safely passed to the |
| 57 // renderer. | 58 // renderer. |
| 58 result->SetString(keys::kNameKey, UTF8ToUTF16(cookie.Name())); | 59 result->SetString(keys::kNameKey, UTF8ToUTF16(cookie.Name())); |
| 59 result->SetString(keys::kValueKey, UTF8ToUTF16(cookie.Value())); | 60 result->SetString(keys::kValueKey, UTF8ToUTF16(cookie.Value())); |
| 60 result->SetString(keys::kDomainKey, cookie.Domain()); | 61 result->SetString(keys::kDomainKey, cookie.Domain()); |
| 61 result->SetBoolean(keys::kHostOnlyKey, | 62 result->SetBoolean(keys::kHostOnlyKey, |
| 62 net::CookieMonster::DomainIsHostOnly(cookie.Domain())); | 63 net::cookie_utils::DomainIsHostOnly(cookie.Domain())); |
| 63 | 64 |
| 64 // A non-UTF8 path is invalid, so we just replace it with an empty string. | 65 // A non-UTF8 path is invalid, so we just replace it with an empty string. |
| 65 result->SetString(keys::kPathKey, | 66 result->SetString(keys::kPathKey, |
| 66 IsStringUTF8(cookie.Path()) ? cookie.Path() : ""); | 67 IsStringUTF8(cookie.Path()) ? cookie.Path() : ""); |
| 67 result->SetBoolean(keys::kSecureKey, cookie.IsSecure()); | 68 result->SetBoolean(keys::kSecureKey, cookie.IsSecure()); |
| 68 result->SetBoolean(keys::kHttpOnlyKey, cookie.IsHttpOnly()); | 69 result->SetBoolean(keys::kHttpOnlyKey, cookie.IsHttpOnly()); |
| 69 result->SetBoolean(keys::kSessionKey, !cookie.DoesExpire()); | 70 result->SetBoolean(keys::kSessionKey, !cookie.DoesExpire()); |
| 70 if (cookie.DoesExpire()) { | 71 if (cookie.DoesExpire()) { |
| 71 result->SetDouble(keys::kExpirationDateKey, | 72 result->SetDouble(keys::kExpirationDateKey, |
| 72 cookie.ExpiryDate().ToDoubleT()); | 73 cookie.ExpiryDate().ToDoubleT()); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } | 172 } |
| 172 | 173 |
| 173 bool MatchFilter::MatchesDomain(const std::string& domain) { | 174 bool MatchFilter::MatchesDomain(const std::string& domain) { |
| 174 if (!details_->HasKey(keys::kDomainKey)) | 175 if (!details_->HasKey(keys::kDomainKey)) |
| 175 return true; | 176 return true; |
| 176 | 177 |
| 177 std::string filter_value; | 178 std::string filter_value; |
| 178 if (!details_->GetString(keys::kDomainKey, &filter_value)) | 179 if (!details_->GetString(keys::kDomainKey, &filter_value)) |
| 179 return false; | 180 return false; |
| 180 // Add a leading '.' character to the filter domain if it doesn't exist. | 181 // Add a leading '.' character to the filter domain if it doesn't exist. |
| 181 if (net::CookieMonster::DomainIsHostOnly(filter_value)) | 182 if (net::cookie_utils::DomainIsHostOnly(filter_value)) |
| 182 filter_value.insert(0, "."); | 183 filter_value.insert(0, "."); |
| 183 | 184 |
| 184 std::string sub_domain(domain); | 185 std::string sub_domain(domain); |
| 185 // Strip any leading '.' character from the input cookie domain. | 186 // Strip any leading '.' character from the input cookie domain. |
| 186 if (!net::CookieMonster::DomainIsHostOnly(sub_domain)) | 187 if (!net::cookie_utils::DomainIsHostOnly(sub_domain)) |
| 187 sub_domain = sub_domain.substr(1); | 188 sub_domain = sub_domain.substr(1); |
| 188 | 189 |
| 189 // Now check whether the domain argument is a subdomain of the filter domain. | 190 // Now check whether the domain argument is a subdomain of the filter domain. |
| 190 for (sub_domain.insert(0, "."); | 191 for (sub_domain.insert(0, "."); |
| 191 sub_domain.length() >= filter_value.length();) { | 192 sub_domain.length() >= filter_value.length();) { |
| 192 if (sub_domain == filter_value) | 193 if (sub_domain == filter_value) |
| 193 return true; | 194 return true; |
| 194 const size_t next_dot = sub_domain.find('.', 1); // Skip over leading dot. | 195 const size_t next_dot = sub_domain.find('.', 1); // Skip over leading dot. |
| 195 sub_domain.erase(0, next_dot); | 196 sub_domain.erase(0, next_dot); |
| 196 } | 197 } |
| 197 return false; | 198 return false; |
| 198 } | 199 } |
| 199 | 200 |
| 200 } // namespace extension_cookies_helpers | 201 } // namespace extension_cookies_helpers |
| OLD | NEW |