| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/cookies/cookies_helpers.h" | 7 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" |
| 8 | 8 |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/linked_ptr.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/values.h" | 16 #include "base/values.h" |
| 19 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" | 17 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" |
| 20 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/common/extensions/api/cookies.h" | 22 #include "chrome/common/extensions/api/cookies.h" |
| 25 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 return profile->GetOffTheRecordProfile(); | 56 return profile->GetOffTheRecordProfile(); |
| 59 return NULL; | 57 return NULL; |
| 60 } | 58 } |
| 61 | 59 |
| 62 const char* GetStoreIdFromProfile(Profile* profile) { | 60 const char* GetStoreIdFromProfile(Profile* profile) { |
| 63 DCHECK(profile); | 61 DCHECK(profile); |
| 64 return profile->IsOffTheRecord() ? | 62 return profile->IsOffTheRecord() ? |
| 65 kOffTheRecordProfileStoreId : kOriginalProfileStoreId; | 63 kOffTheRecordProfileStoreId : kOriginalProfileStoreId; |
| 66 } | 64 } |
| 67 | 65 |
| 68 scoped_ptr<Cookie> CreateCookie( | 66 Cookie CreateCookie(const net::CanonicalCookie& canonical_cookie, |
| 69 const net::CanonicalCookie& canonical_cookie, | 67 const std::string& store_id) { |
| 70 const std::string& store_id) { | 68 Cookie cookie; |
| 71 scoped_ptr<Cookie> cookie(new Cookie()); | |
| 72 | 69 |
| 73 // A cookie is a raw byte sequence. By explicitly parsing it as UTF-8, we | 70 // A cookie is a raw byte sequence. By explicitly parsing it as UTF-8, we |
| 74 // apply error correction, so the string can be safely passed to the renderer. | 71 // apply error correction, so the string can be safely passed to the renderer. |
| 75 cookie->name = base::UTF16ToUTF8(base::UTF8ToUTF16(canonical_cookie.Name())); | 72 cookie.name = base::UTF16ToUTF8(base::UTF8ToUTF16(canonical_cookie.Name())); |
| 76 cookie->value = | 73 cookie.value = base::UTF16ToUTF8(base::UTF8ToUTF16(canonical_cookie.Value())); |
| 77 base::UTF16ToUTF8(base::UTF8ToUTF16(canonical_cookie.Value())); | 74 cookie.domain = canonical_cookie.Domain(); |
| 78 cookie->domain = canonical_cookie.Domain(); | 75 cookie.host_only = |
| 79 cookie->host_only = net::cookie_util::DomainIsHostOnly( | 76 net::cookie_util::DomainIsHostOnly(canonical_cookie.Domain()); |
| 80 canonical_cookie.Domain()); | |
| 81 // A non-UTF8 path is invalid, so we just replace it with an empty string. | 77 // A non-UTF8 path is invalid, so we just replace it with an empty string. |
| 82 cookie->path = base::IsStringUTF8(canonical_cookie.Path()) ? | 78 cookie.path = base::IsStringUTF8(canonical_cookie.Path()) |
| 83 canonical_cookie.Path() : std::string(); | 79 ? canonical_cookie.Path() |
| 84 cookie->secure = canonical_cookie.IsSecure(); | 80 : std::string(); |
| 85 cookie->http_only = canonical_cookie.IsHttpOnly(); | 81 cookie.secure = canonical_cookie.IsSecure(); |
| 82 cookie.http_only = canonical_cookie.IsHttpOnly(); |
| 86 | 83 |
| 87 switch (canonical_cookie.SameSite()) { | 84 switch (canonical_cookie.SameSite()) { |
| 88 case net::CookieSameSite::DEFAULT_MODE: | 85 case net::CookieSameSite::DEFAULT_MODE: |
| 89 cookie->same_site = api::cookies::SAME_SITE_STATUS_NO_RESTRICTION; | 86 cookie.same_site = api::cookies::SAME_SITE_STATUS_NO_RESTRICTION; |
| 90 break; | 87 break; |
| 91 case net::CookieSameSite::LAX_MODE: | 88 case net::CookieSameSite::LAX_MODE: |
| 92 cookie->same_site = api::cookies::SAME_SITE_STATUS_LAX; | 89 cookie.same_site = api::cookies::SAME_SITE_STATUS_LAX; |
| 93 break; | 90 break; |
| 94 case net::CookieSameSite::STRICT_MODE: | 91 case net::CookieSameSite::STRICT_MODE: |
| 95 cookie->same_site = api::cookies::SAME_SITE_STATUS_STRICT; | 92 cookie.same_site = api::cookies::SAME_SITE_STATUS_STRICT; |
| 96 break; | 93 break; |
| 97 } | 94 } |
| 98 | 95 |
| 99 cookie->session = !canonical_cookie.IsPersistent(); | 96 cookie.session = !canonical_cookie.IsPersistent(); |
| 100 if (canonical_cookie.IsPersistent()) { | 97 if (canonical_cookie.IsPersistent()) { |
| 101 cookie->expiration_date.reset( | 98 cookie.expiration_date.reset( |
| 102 new double(canonical_cookie.ExpiryDate().ToDoubleT())); | 99 new double(canonical_cookie.ExpiryDate().ToDoubleT())); |
| 103 } | 100 } |
| 104 cookie->store_id = store_id; | 101 cookie.store_id = store_id; |
| 105 | 102 |
| 106 return cookie; | 103 return cookie; |
| 107 } | 104 } |
| 108 | 105 |
| 109 scoped_ptr<CookieStore> CreateCookieStore(Profile* profile, | 106 CookieStore CreateCookieStore(Profile* profile, base::ListValue* tab_ids) { |
| 110 base::ListValue* tab_ids) { | |
| 111 DCHECK(profile); | 107 DCHECK(profile); |
| 112 DCHECK(tab_ids); | 108 DCHECK(tab_ids); |
| 113 base::DictionaryValue dict; | 109 base::DictionaryValue dict; |
| 114 dict.SetString(keys::kIdKey, GetStoreIdFromProfile(profile)); | 110 dict.SetString(keys::kIdKey, GetStoreIdFromProfile(profile)); |
| 115 dict.Set(keys::kTabIdsKey, tab_ids); | 111 dict.Set(keys::kTabIdsKey, tab_ids); |
| 116 | 112 |
| 117 CookieStore* cookie_store = new CookieStore(); | 113 CookieStore cookie_store; |
| 118 bool rv = CookieStore::Populate(dict, cookie_store); | 114 bool rv = CookieStore::Populate(dict, &cookie_store); |
| 119 CHECK(rv); | 115 CHECK(rv); |
| 120 return scoped_ptr<CookieStore>(cookie_store); | 116 return cookie_store; |
| 121 } | 117 } |
| 122 | 118 |
| 123 void GetCookieListFromStore( | 119 void GetCookieListFromStore( |
| 124 net::CookieStore* cookie_store, const GURL& url, | 120 net::CookieStore* cookie_store, |
| 121 const GURL& url, |
| 125 const net::CookieMonster::GetCookieListCallback& callback) { | 122 const net::CookieMonster::GetCookieListCallback& callback) { |
| 126 DCHECK(cookie_store); | 123 DCHECK(cookie_store); |
| 127 if (!url.is_empty()) { | 124 if (!url.is_empty()) { |
| 128 DCHECK(url.is_valid()); | 125 DCHECK(url.is_valid()); |
| 129 cookie_store->GetAllCookiesForURLAsync(url, callback); | 126 cookie_store->GetAllCookiesForURLAsync(url, callback); |
| 130 } else { | 127 } else { |
| 131 cookie_store->GetAllCookiesAsync(callback); | 128 cookie_store->GetAllCookiesAsync(callback); |
| 132 } | 129 } |
| 133 } | 130 } |
| 134 | 131 |
| 135 GURL GetURLFromCanonicalCookie(const net::CanonicalCookie& cookie) { | 132 GURL GetURLFromCanonicalCookie(const net::CanonicalCookie& cookie) { |
| 136 const std::string& domain_key = cookie.Domain(); | 133 const std::string& domain_key = cookie.Domain(); |
| 137 const std::string scheme = | 134 const std::string scheme = |
| 138 cookie.IsSecure() ? url::kHttpsScheme : url::kHttpScheme; | 135 cookie.IsSecure() ? url::kHttpsScheme : url::kHttpScheme; |
| 139 const std::string host = | 136 const std::string host = |
| 140 domain_key.find('.') != 0 ? domain_key : domain_key.substr(1); | 137 domain_key.find('.') != 0 ? domain_key : domain_key.substr(1); |
| 141 return GURL(scheme + url::kStandardSchemeSeparator + host + "/"); | 138 return GURL(scheme + url::kStandardSchemeSeparator + host + "/"); |
| 142 } | 139 } |
| 143 | 140 |
| 144 void AppendMatchingCookiesToVector(const net::CookieList& all_cookies, | 141 void AppendMatchingCookiesToVector(const net::CookieList& all_cookies, |
| 145 const GURL& url, | 142 const GURL& url, |
| 146 const GetAll::Params::Details* details, | 143 const GetAll::Params::Details* details, |
| 147 const Extension* extension, | 144 const Extension* extension, |
| 148 LinkedCookieVec* match_vector) { | 145 std::vector<Cookie>* match_vector) { |
| 149 net::CookieList::const_iterator it; | 146 for (const net::CanonicalCookie& cookie : all_cookies) { |
| 150 for (it = all_cookies.begin(); it != all_cookies.end(); ++it) { | |
| 151 // Ignore any cookie whose domain doesn't match the extension's | 147 // Ignore any cookie whose domain doesn't match the extension's |
| 152 // host permissions. | 148 // host permissions. |
| 153 GURL cookie_domain_url = GetURLFromCanonicalCookie(*it); | 149 GURL cookie_domain_url = GetURLFromCanonicalCookie(cookie); |
| 154 if (!extension->permissions_data()->HasHostPermission(cookie_domain_url)) | 150 if (!extension->permissions_data()->HasHostPermission(cookie_domain_url)) |
| 155 continue; | 151 continue; |
| 156 // Filter the cookie using the match filter. | 152 // Filter the cookie using the match filter. |
| 157 cookies_helpers::MatchFilter filter(details); | 153 cookies_helpers::MatchFilter filter(details); |
| 158 if (filter.MatchesCookie(*it)) { | 154 if (filter.MatchesCookie(cookie)) |
| 159 match_vector->push_back(make_linked_ptr( | 155 match_vector->push_back(CreateCookie(cookie, *details->store_id)); |
| 160 CreateCookie(*it, *details->store_id).release())); | |
| 161 } | |
| 162 } | 156 } |
| 163 } | 157 } |
| 164 | 158 |
| 165 void AppendToTabIdList(Browser* browser, base::ListValue* tab_ids) { | 159 void AppendToTabIdList(Browser* browser, base::ListValue* tab_ids) { |
| 166 DCHECK(browser); | 160 DCHECK(browser); |
| 167 DCHECK(tab_ids); | 161 DCHECK(tab_ids); |
| 168 TabStripModel* tab_strip = browser->tab_strip_model(); | 162 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 169 for (int i = 0; i < tab_strip->count(); ++i) { | 163 for (int i = 0; i < tab_strip->count(); ++i) { |
| 170 tab_ids->Append(new base::FundamentalValue( | 164 tab_ids->Append(new base::FundamentalValue( |
| 171 ExtensionTabUtil::GetTabId(tab_strip->GetWebContentsAt(i)))); | 165 ExtensionTabUtil::GetTabId(tab_strip->GetWebContentsAt(i)))); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 if (sub_domain == *details_->domain) | 210 if (sub_domain == *details_->domain) |
| 217 return true; | 211 return true; |
| 218 const size_t next_dot = sub_domain.find('.', 1); // Skip over leading dot. | 212 const size_t next_dot = sub_domain.find('.', 1); // Skip over leading dot. |
| 219 sub_domain.erase(0, next_dot); | 213 sub_domain.erase(0, next_dot); |
| 220 } | 214 } |
| 221 return false; | 215 return false; |
| 222 } | 216 } |
| 223 | 217 |
| 224 } // namespace cookies_helpers | 218 } // namespace cookies_helpers |
| 225 } // namespace extensions | 219 } // namespace extensions |
| OLD | NEW |