| 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 the Chrome Extensions Cookies API. | 5 // Implements the Chrome Extensions Cookies API. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/cookies/cookies_api.h" | 7 #include "chrome/browser/extensions/api/cookies/cookies_api.h" |
| 8 | 8 |
| 9 #include <vector> |
| 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/values.h" | 15 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" | 16 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" |
| 13 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" | 17 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" |
| 14 #include "chrome/browser/extensions/extension_event_router.h" | 18 #include "chrome/browser/extensions/extension_event_router.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_list.h" | 21 #include "chrome/browser/ui/browser_list.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
| 23 #include "chrome/common/extensions/api/cookies.h" |
| 19 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/extension_error_utils.h" | 25 #include "chrome/common/extensions/extension_error_utils.h" |
| 21 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
| 23 #include "net/cookies/canonical_cookie.h" | 28 #include "net/cookies/canonical_cookie.h" |
| 24 #include "net/cookies/cookie_monster.h" | 29 #include "net/cookies/cookie_monster.h" |
| 25 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
| 26 #include "net/url_request/url_request_context_getter.h" | 31 #include "net/url_request/url_request_context_getter.h" |
| 27 | 32 |
| 28 using content::BrowserThread; | 33 using content::BrowserThread; |
| 34 using extensions::api::cookies::Cookie; |
| 35 using extensions::api::cookies::CookieStore; |
| 36 |
| 37 namespace Get = extensions::api::cookies::Get; |
| 38 namespace GetAll = extensions::api::cookies::GetAll; |
| 39 namespace GetAllCookieStores = extensions::api::cookies::GetAllCookieStores; |
| 40 namespace Remove = extensions::api::cookies::Remove; |
| 41 namespace Set = extensions::api::cookies::Set; |
| 29 | 42 |
| 30 namespace extensions { | 43 namespace extensions { |
| 31 namespace keys = cookies_api_constants; | 44 namespace keys = cookies_api_constants; |
| 32 | 45 |
| 33 ExtensionCookiesEventRouter::ExtensionCookiesEventRouter(Profile* profile) | 46 ExtensionCookiesEventRouter::ExtensionCookiesEventRouter(Profile* profile) |
| 34 : profile_(profile) {} | 47 : profile_(profile) { |
| 48 } |
| 35 | 49 |
| 36 ExtensionCookiesEventRouter::~ExtensionCookiesEventRouter() {} | 50 ExtensionCookiesEventRouter::~ExtensionCookiesEventRouter() { |
| 51 } |
| 37 | 52 |
| 38 void ExtensionCookiesEventRouter::Init() { | 53 void ExtensionCookiesEventRouter::Init() { |
| 39 CHECK(registrar_.IsEmpty()); | 54 CHECK(registrar_.IsEmpty()); |
| 40 registrar_.Add(this, | 55 registrar_.Add(this, |
| 41 chrome::NOTIFICATION_COOKIE_CHANGED, | 56 chrome::NOTIFICATION_COOKIE_CHANGED, |
| 42 content::NotificationService::AllBrowserContextsAndSources()); | 57 content::NotificationService::AllBrowserContextsAndSources()); |
| 43 } | 58 } |
| 44 | 59 |
| 45 void ExtensionCookiesEventRouter::Observe( | 60 void ExtensionCookiesEventRouter::Observe( |
| 46 int type, | 61 int type, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 61 NOTREACHED(); | 76 NOTREACHED(); |
| 62 } | 77 } |
| 63 } | 78 } |
| 64 | 79 |
| 65 void ExtensionCookiesEventRouter::CookieChanged( | 80 void ExtensionCookiesEventRouter::CookieChanged( |
| 66 Profile* profile, | 81 Profile* profile, |
| 67 ChromeCookieDetails* details) { | 82 ChromeCookieDetails* details) { |
| 68 ListValue args; | 83 ListValue args; |
| 69 DictionaryValue* dict = new DictionaryValue(); | 84 DictionaryValue* dict = new DictionaryValue(); |
| 70 dict->SetBoolean(keys::kRemovedKey, details->removed); | 85 dict->SetBoolean(keys::kRemovedKey, details->removed); |
| 71 dict->Set( | 86 |
| 72 keys::kCookieKey, | 87 scoped_ptr<Cookie> cookie( |
| 73 cookies_helpers::CreateCookieValue(*details->cookie, | 88 cookies_helpers::CreateCookie(*details->cookie, |
| 74 cookies_helpers::GetStoreIdFromProfile(profile))); | 89 cookies_helpers::GetStoreIdFromProfile(profile))); |
| 90 dict->Set(keys::kCookieKey, cookie->ToValue().release()); |
| 75 | 91 |
| 76 // Map the interal cause to an external string. | 92 // Map the interal cause to an external string. |
| 77 std::string cause; | 93 std::string cause; |
| 78 switch (details->cause) { | 94 switch (details->cause) { |
| 79 case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT: | 95 case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT: |
| 80 cause = keys::kExplicitChangeCause; | 96 cause = keys::kExplicitChangeCause; |
| 81 break; | 97 break; |
| 82 | 98 |
| 83 case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE: | 99 case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE: |
| 84 cause = keys::kOverwriteChangeCause; | 100 cause = keys::kOverwriteChangeCause; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 113 void ExtensionCookiesEventRouter::DispatchEvent(Profile* profile, | 129 void ExtensionCookiesEventRouter::DispatchEvent(Profile* profile, |
| 114 const char* event_name, | 130 const char* event_name, |
| 115 const std::string& json_args, | 131 const std::string& json_args, |
| 116 GURL& cookie_domain) { | 132 GURL& cookie_domain) { |
| 117 if (profile && profile->GetExtensionEventRouter()) { | 133 if (profile && profile->GetExtensionEventRouter()) { |
| 118 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 134 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 119 event_name, json_args, profile, cookie_domain, EventFilteringInfo()); | 135 event_name, json_args, profile, cookie_domain, EventFilteringInfo()); |
| 120 } | 136 } |
| 121 } | 137 } |
| 122 | 138 |
| 123 bool CookiesFunction::ParseUrl(const DictionaryValue* details, GURL* url, | 139 bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url, |
| 124 bool check_host_permissions) { | 140 bool check_host_permissions) { |
| 125 DCHECK(details && url); | |
| 126 std::string url_string; | |
| 127 // Get the URL string or return false. | |
| 128 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kUrlKey, &url_string)); | |
| 129 *url = GURL(url_string); | 141 *url = GURL(url_string); |
| 130 if (!url->is_valid()) { | 142 if (!url->is_valid()) { |
| 131 error_ = ExtensionErrorUtils::FormatErrorMessage( | 143 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 132 keys::kInvalidUrlError, url_string); | 144 keys::kInvalidUrlError, url_string); |
| 133 return false; | 145 return false; |
| 134 } | 146 } |
| 135 // Check against host permissions if needed. | 147 // Check against host permissions if needed. |
| 136 if (check_host_permissions && | 148 if (check_host_permissions && |
| 137 !GetExtension()->HasHostPermission(*url)) { | 149 !GetExtension()->HasHostPermission(*url)) { |
| 138 error_ = ExtensionErrorUtils::FormatErrorMessage( | 150 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 139 keys::kNoHostPermissionsError, url->spec()); | 151 keys::kNoHostPermissionsError, url->spec()); |
| 140 return false; | 152 return false; |
| 141 } | 153 } |
| 142 return true; | 154 return true; |
| 143 } | 155 } |
| 144 | 156 |
| 145 bool CookiesFunction::ParseStoreContext(const DictionaryValue* details, | 157 bool CookiesFunction::ParseStoreContext( |
| 146 net::URLRequestContextGetter** context, | 158 std::string* store_id, |
| 147 std::string* store_id) { | 159 net::URLRequestContextGetter** context) { |
| 148 DCHECK(details && (context || store_id)); | 160 DCHECK((context || store_id->empty())); |
| 149 Profile* store_profile = NULL; | 161 Profile* store_profile = NULL; |
| 150 if (details->HasKey(keys::kStoreIdKey)) { | 162 if (!store_id->empty()) { |
| 151 // The store ID was explicitly specified in the details dictionary. | |
| 152 // Retrieve its corresponding cookie store. | |
| 153 std::string store_id_value; | |
| 154 // Get the store ID string or return false. | |
| 155 EXTENSION_FUNCTION_VALIDATE( | |
| 156 details->GetString(keys::kStoreIdKey, &store_id_value)); | |
| 157 store_profile = cookies_helpers::ChooseProfileFromStoreId( | 163 store_profile = cookies_helpers::ChooseProfileFromStoreId( |
| 158 store_id_value, profile(), include_incognito()); | 164 *store_id, profile(), include_incognito()); |
| 159 if (!store_profile) { | 165 if (!store_profile) { |
| 160 error_ = ExtensionErrorUtils::FormatErrorMessage( | 166 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 161 keys::kInvalidStoreIdError, store_id_value); | 167 keys::kInvalidStoreIdError, *store_id); |
| 162 return false; | 168 return false; |
| 163 } | 169 } |
| 164 } else { | 170 } else { |
| 165 // The store ID was not specified; use the current execution context's | 171 // The store ID was not specified; use the current execution context's |
| 166 // cookie store by default. | 172 // cookie store by default. |
| 167 // GetCurrentBrowser() already takes into account incognito settings. | 173 // GetCurrentBrowser() already takes into account incognito settings. |
| 168 Browser* current_browser = GetCurrentBrowser(); | 174 Browser* current_browser = GetCurrentBrowser(); |
| 169 if (!current_browser) { | 175 if (!current_browser) { |
| 170 error_ = keys::kNoCookieStoreFoundError; | 176 error_ = keys::kNoCookieStoreFoundError; |
| 171 return false; | 177 return false; |
| 172 } | 178 } |
| 173 store_profile = current_browser->profile(); | 179 store_profile = current_browser->profile(); |
| 180 *store_id = cookies_helpers::GetStoreIdFromProfile(store_profile); |
| 174 } | 181 } |
| 175 DCHECK(store_profile); | |
| 176 | 182 |
| 177 if (context) | 183 if (context) |
| 178 *context = store_profile->GetRequestContext(); | 184 *context = store_profile->GetRequestContext(); |
| 179 if (store_id) | 185 DCHECK(context); |
| 180 *store_id = cookies_helpers::GetStoreIdFromProfile(store_profile); | |
| 181 | 186 |
| 182 return true; | 187 return true; |
| 183 } | 188 } |
| 184 | 189 |
| 185 GetCookieFunction::GetCookieFunction() {} | 190 GetCookieFunction::GetCookieFunction() { |
| 191 } |
| 186 | 192 |
| 187 GetCookieFunction::~GetCookieFunction() {} | 193 GetCookieFunction::~GetCookieFunction() { |
| 194 } |
| 188 | 195 |
| 189 bool GetCookieFunction::RunImpl() { | 196 bool GetCookieFunction::RunImpl() { |
| 190 // Return false if the arguments are malformed. | 197 parsed_args_ = Get::Params::Create(*args_); |
| 191 DictionaryValue* details; | 198 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 192 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
| 193 DCHECK(details); | |
| 194 | 199 |
| 195 // Read/validate input parameters. | 200 // Read/validate input parameters. |
| 196 if (!ParseUrl(details, &url_, true)) | 201 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
| 197 return false; | 202 return false; |
| 198 | 203 |
| 199 // Get the cookie name string or return false. | 204 std::string store_id = parsed_args_->details.store_id.get() ? |
| 200 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name_)); | 205 *parsed_args_->details.store_id : ""; |
| 206 net::URLRequestContextGetter* store_context = NULL; |
| 207 if (!ParseStoreContext(&store_id, &store_context)) |
| 208 return false; |
| 209 store_context_ = store_context; |
| 210 if (!parsed_args_->details.store_id.get()) |
| 211 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| 201 | 212 |
| 202 net::URLRequestContextGetter* store_context = NULL; | |
| 203 if (!ParseStoreContext(details, &store_context, &store_id_)) | |
| 204 return false; | |
| 205 | |
| 206 DCHECK(store_context && !store_id_.empty()); | |
| 207 store_context_ = store_context; | 213 store_context_ = store_context; |
| 208 | 214 |
| 209 bool rv = BrowserThread::PostTask( | 215 bool rv = BrowserThread::PostTask( |
| 210 BrowserThread::IO, FROM_HERE, | 216 BrowserThread::IO, FROM_HERE, |
| 211 base::Bind(&GetCookieFunction::GetCookieOnIOThread, this)); | 217 base::Bind(&GetCookieFunction::GetCookieOnIOThread, this)); |
| 212 DCHECK(rv); | 218 DCHECK(rv); |
| 213 | 219 |
| 214 // Will finish asynchronously. | 220 // Will finish asynchronously. |
| 215 return true; | 221 return true; |
| 216 } | 222 } |
| 217 | 223 |
| 218 void GetCookieFunction::GetCookieOnIOThread() { | 224 void GetCookieFunction::GetCookieOnIOThread() { |
| 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 220 net::CookieStore* cookie_store = | 226 net::CookieStore* cookie_store = |
| 221 store_context_->GetURLRequestContext()->cookie_store(); | 227 store_context_->GetURLRequestContext()->cookie_store(); |
| 222 cookies_helpers::GetCookieListFromStore( | 228 cookies_helpers::GetCookieListFromStore( |
| 223 cookie_store, url_, | 229 cookie_store, url_, |
| 224 base::Bind(&GetCookieFunction::GetCookieCallback, this)); | 230 base::Bind(&GetCookieFunction::GetCookieCallback, this)); |
| 225 } | 231 } |
| 226 | 232 |
| 227 void GetCookieFunction::GetCookieCallback(const net::CookieList& cookie_list) { | 233 void GetCookieFunction::GetCookieCallback(const net::CookieList& cookie_list) { |
| 228 net::CookieList::const_iterator it; | 234 net::CookieList::const_iterator it; |
| 229 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { | 235 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
| 230 // Return the first matching cookie. Relies on the fact that the | 236 // Return the first matching cookie. Relies on the fact that the |
| 231 // CookieMonster returns them in canonical order (longest path, then | 237 // CookieMonster returns them in canonical order (longest path, then |
| 232 // earliest creation time). | 238 // earliest creation time). |
| 233 if (it->Name() == name_) { | 239 if (it->Name() == parsed_args_->details.name) { |
| 234 SetResult(cookies_helpers::CreateCookieValue(*it, store_id_)); | 240 scoped_ptr<Cookie> cookie( |
| 241 cookies_helpers::CreateCookie(*it, *parsed_args_->details.store_id)); |
| 242 results_ = Get::Results::Create(*cookie); |
| 235 break; | 243 break; |
| 236 } | 244 } |
| 237 } | 245 } |
| 238 | 246 |
| 239 // The cookie doesn't exist; return null. | 247 // The cookie doesn't exist; return null. |
| 240 if (it == cookie_list.end()) | 248 if (it == cookie_list.end()) |
| 241 SetResult(Value::CreateNullValue()); | 249 SetResult(Value::CreateNullValue()); |
| 242 | 250 |
| 243 bool rv = BrowserThread::PostTask( | 251 bool rv = BrowserThread::PostTask( |
| 244 BrowserThread::UI, FROM_HERE, | 252 BrowserThread::UI, FROM_HERE, |
| 245 base::Bind(&GetCookieFunction::RespondOnUIThread, this)); | 253 base::Bind(&GetCookieFunction::RespondOnUIThread, this)); |
| 246 DCHECK(rv); | 254 DCHECK(rv); |
| 247 } | 255 } |
| 248 | 256 |
| 249 void GetCookieFunction::RespondOnUIThread() { | 257 void GetCookieFunction::RespondOnUIThread() { |
| 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 251 SendResponse(true); | 259 SendResponse(true); |
| 252 } | 260 } |
| 253 | 261 |
| 254 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {} | 262 GetAllCookiesFunction::GetAllCookiesFunction() { |
| 263 } |
| 255 | 264 |
| 256 GetAllCookiesFunction::~GetAllCookiesFunction() {} | 265 GetAllCookiesFunction::~GetAllCookiesFunction() { |
| 266 } |
| 257 | 267 |
| 258 bool GetAllCookiesFunction::RunImpl() { | 268 bool GetAllCookiesFunction::RunImpl() { |
| 259 // Return false if the arguments are malformed. | 269 parsed_args_ = GetAll::Params::Create(*args_); |
| 260 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_)); | 270 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 261 DCHECK(details_); | |
| 262 | 271 |
| 263 // Read/validate input parameters. | 272 if (parsed_args_->details.url.get()) { |
| 264 if (details_->HasKey(keys::kUrlKey) && !ParseUrl(details_, &url_, false)) | 273 if (!ParseUrl(*parsed_args_->details.url, &url_, false)) |
| 274 return false; |
| 275 } |
| 276 |
| 277 std::string store_id = parsed_args_->details.store_id.get() ? |
| 278 *parsed_args_->details.store_id : ""; |
| 279 net::URLRequestContextGetter* store_context = NULL; |
| 280 if (!ParseStoreContext(&store_id, &store_context)) |
| 265 return false; | 281 return false; |
| 266 | |
| 267 net::URLRequestContextGetter* store_context = NULL; | |
| 268 if (!ParseStoreContext(details_, &store_context, &store_id_)) | |
| 269 return false; | |
| 270 DCHECK(store_context); | |
| 271 store_context_ = store_context; | 282 store_context_ = store_context; |
| 283 if (!parsed_args_->details.store_id.get()) |
| 284 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| 272 | 285 |
| 273 bool rv = BrowserThread::PostTask( | 286 bool rv = BrowserThread::PostTask( |
| 274 BrowserThread::IO, FROM_HERE, | 287 BrowserThread::IO, FROM_HERE, |
| 275 base::Bind(&GetAllCookiesFunction::GetAllCookiesOnIOThread, this)); | 288 base::Bind(&GetAllCookiesFunction::GetAllCookiesOnIOThread, this)); |
| 276 DCHECK(rv); | 289 DCHECK(rv); |
| 277 | 290 |
| 278 // Will finish asynchronously. | 291 // Will finish asynchronously. |
| 279 return true; | 292 return true; |
| 280 } | 293 } |
| 281 | 294 |
| 282 void GetAllCookiesFunction::GetAllCookiesOnIOThread() { | 295 void GetAllCookiesFunction::GetAllCookiesOnIOThread() { |
| 283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 284 net::CookieStore* cookie_store = | 297 net::CookieStore* cookie_store = |
| 285 store_context_->GetURLRequestContext()->cookie_store(); | 298 store_context_->GetURLRequestContext()->cookie_store(); |
| 286 cookies_helpers::GetCookieListFromStore( | 299 cookies_helpers::GetCookieListFromStore( |
| 287 cookie_store, url_, | 300 cookie_store, url_, |
| 288 base::Bind(&GetAllCookiesFunction::GetAllCookiesCallback, this)); | 301 base::Bind(&GetAllCookiesFunction::GetAllCookiesCallback, this)); |
| 289 } | 302 } |
| 290 | 303 |
| 291 void GetAllCookiesFunction::GetAllCookiesCallback( | 304 void GetAllCookiesFunction::GetAllCookiesCallback( |
| 292 const net::CookieList& cookie_list) { | 305 const net::CookieList& cookie_list) { |
| 293 const extensions::Extension* extension = GetExtension(); | 306 const extensions::Extension* extension = GetExtension(); |
| 294 if (extension) { | 307 if (extension) { |
| 295 ListValue* matching_list = new ListValue(); | 308 std::vector<linked_ptr<Cookie> > match_vector; |
| 296 cookies_helpers::AppendMatchingCookiesToList( | 309 cookies_helpers::AppendMatchingCookiesToVector( |
| 297 cookie_list, store_id_, url_, details_, | 310 cookie_list, url_, &parsed_args_->details, |
| 298 GetExtension(), matching_list); | 311 GetExtension(), &match_vector); |
| 299 SetResult(matching_list); | 312 |
| 313 results_ = GetAll::Results::Create(match_vector); |
| 300 } | 314 } |
| 301 bool rv = BrowserThread::PostTask( | 315 bool rv = BrowserThread::PostTask( |
| 302 BrowserThread::UI, FROM_HERE, | 316 BrowserThread::UI, FROM_HERE, |
| 303 base::Bind(&GetAllCookiesFunction::RespondOnUIThread, this)); | 317 base::Bind(&GetAllCookiesFunction::RespondOnUIThread, this)); |
| 304 DCHECK(rv); | 318 DCHECK(rv); |
| 305 } | 319 } |
| 306 | 320 |
| 307 void GetAllCookiesFunction::RespondOnUIThread() { | 321 void GetAllCookiesFunction::RespondOnUIThread() { |
| 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 309 SendResponse(true); | 323 SendResponse(true); |
| 310 } | 324 } |
| 311 | 325 |
| 312 SetCookieFunction::SetCookieFunction() | 326 SetCookieFunction::SetCookieFunction() { |
| 313 : secure_(false), | |
| 314 http_only_(false), | |
| 315 success_(false) { | |
| 316 } | 327 } |
| 317 | 328 |
| 318 SetCookieFunction::~SetCookieFunction() { | 329 SetCookieFunction::~SetCookieFunction() { |
| 319 } | 330 } |
| 320 | 331 |
| 321 bool SetCookieFunction::RunImpl() { | 332 bool SetCookieFunction::RunImpl() { |
| 322 // Return false if the arguments are malformed. | 333 parsed_args_ = Set::Params::Create(*args_); |
| 323 DictionaryValue* details; | 334 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 324 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
| 325 DCHECK(details); | |
| 326 | 335 |
| 327 // Read/validate input parameters. | 336 // Read/validate input parameters. |
| 328 if (!ParseUrl(details, &url_, true)) | 337 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
| 329 return false; | 338 return false; |
| 330 // The macros below return false if argument types are not as expected. | |
| 331 if (details->HasKey(keys::kNameKey)) | |
| 332 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name_)); | |
| 333 if (details->HasKey(keys::kValueKey)) | |
| 334 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kValueKey, &value_)); | |
| 335 if (details->HasKey(keys::kDomainKey)) | |
| 336 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kDomainKey, &domain_)); | |
| 337 if (details->HasKey(keys::kPathKey)) | |
| 338 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kPathKey, &path_)); | |
| 339 | 339 |
| 340 if (details->HasKey(keys::kSecureKey)) { | 340 std::string store_id = parsed_args_->details.store_id.get() ? |
| 341 EXTENSION_FUNCTION_VALIDATE( | 341 *parsed_args_->details.store_id : ""; |
| 342 details->GetBoolean(keys::kSecureKey, &secure_)); | |
| 343 } | |
| 344 if (details->HasKey(keys::kHttpOnlyKey)) { | |
| 345 EXTENSION_FUNCTION_VALIDATE( | |
| 346 details->GetBoolean(keys::kHttpOnlyKey, &http_only_)); | |
| 347 } | |
| 348 if (details->HasKey(keys::kExpirationDateKey)) { | |
| 349 Value* expiration_date_value; | |
| 350 EXTENSION_FUNCTION_VALIDATE(details->Get(keys::kExpirationDateKey, | |
| 351 &expiration_date_value)); | |
| 352 double expiration_date; | |
| 353 if (expiration_date_value->IsType(Value::TYPE_INTEGER)) { | |
| 354 int expiration_date_int; | |
| 355 EXTENSION_FUNCTION_VALIDATE( | |
| 356 expiration_date_value->GetAsInteger(&expiration_date_int)); | |
| 357 expiration_date = static_cast<double>(expiration_date_int); | |
| 358 } else { | |
| 359 EXTENSION_FUNCTION_VALIDATE( | |
| 360 expiration_date_value->GetAsDouble(&expiration_date)); | |
| 361 } | |
| 362 // Time::FromDoubleT converts double time 0 to empty Time object. So we need | |
| 363 // to do special handling here. | |
| 364 expiration_time_ = (expiration_date == 0) ? | |
| 365 base::Time::UnixEpoch() : base::Time::FromDoubleT(expiration_date); | |
| 366 } | |
| 367 | |
| 368 net::URLRequestContextGetter* store_context = NULL; | 342 net::URLRequestContextGetter* store_context = NULL; |
| 369 if (!ParseStoreContext(details, &store_context, NULL)) | 343 if (!ParseStoreContext(&store_id, &store_context)) |
| 370 return false; | 344 return false; |
| 371 DCHECK(store_context); | |
| 372 store_context_ = store_context; | 345 store_context_ = store_context; |
| 346 if (!parsed_args_->details.store_id.get()) |
| 347 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| 373 | 348 |
| 374 bool rv = BrowserThread::PostTask( | 349 bool rv = BrowserThread::PostTask( |
| 375 BrowserThread::IO, FROM_HERE, | 350 BrowserThread::IO, FROM_HERE, |
| 376 base::Bind(&SetCookieFunction::SetCookieOnIOThread, this)); | 351 base::Bind(&SetCookieFunction::SetCookieOnIOThread, this)); |
| 377 DCHECK(rv); | 352 DCHECK(rv); |
| 378 | 353 |
| 379 // Will finish asynchronously. | 354 // Will finish asynchronously. |
| 380 return true; | 355 return true; |
| 381 } | 356 } |
| 382 | 357 |
| 383 void SetCookieFunction::SetCookieOnIOThread() { | 358 void SetCookieFunction::SetCookieOnIOThread() { |
| 384 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 359 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 385 net::CookieMonster* cookie_monster = | 360 net::CookieMonster* cookie_monster = |
| 386 store_context_->GetURLRequestContext()->cookie_store()-> | 361 store_context_->GetURLRequestContext()->cookie_store()-> |
| 387 GetCookieMonster(); | 362 GetCookieMonster(); |
| 363 |
| 364 base::Time expiration_time; |
| 365 if (parsed_args_->details.expiration_date.get()) { |
| 366 // Time::FromDoubleT converts double time 0 to empty Time object. So we need |
| 367 // to do special handling here. |
| 368 expiration_time = (*parsed_args_->details.expiration_date == 0) ? |
| 369 base::Time::UnixEpoch() : |
| 370 base::Time::FromDoubleT(*parsed_args_->details.expiration_date); |
| 371 } |
| 372 |
| 373 if (parsed_args_->details.name.get()) |
| 374 LOG(INFO) << "Cookie name: " << *parsed_args_->details.name; |
| 375 |
| 388 cookie_monster->SetCookieWithDetailsAsync( | 376 cookie_monster->SetCookieWithDetailsAsync( |
| 389 url_, name_, value_, domain_, path_, expiration_time_, | 377 url_, |
| 390 secure_, http_only_, base::Bind(&SetCookieFunction::PullCookie, this)); | 378 parsed_args_->details.name.get() ? *parsed_args_->details.name : "", |
| 379 parsed_args_->details.value.get() ? *parsed_args_->details.value : "", |
| 380 parsed_args_->details.domain.get() ? *parsed_args_->details.domain : "", |
| 381 parsed_args_->details.path.get() ? *parsed_args_->details.path : "", |
| 382 expiration_time, |
| 383 parsed_args_->details.secure.get() ? |
| 384 *parsed_args_->details.secure.get() : |
| 385 false, |
| 386 parsed_args_->details.http_only.get() ? |
| 387 *parsed_args_->details.http_only : |
| 388 false, |
| 389 base::Bind(&SetCookieFunction::PullCookie, this)); |
| 391 } | 390 } |
| 392 | 391 |
| 393 void SetCookieFunction::PullCookie(bool set_cookie_result) { | 392 void SetCookieFunction::PullCookie(bool set_cookie_result) { |
| 394 // Pull the newly set cookie. | 393 // Pull the newly set cookie. |
| 395 net::CookieMonster* cookie_monster = | 394 net::CookieMonster* cookie_monster = |
| 396 store_context_->GetURLRequestContext()->cookie_store()-> | 395 store_context_->GetURLRequestContext()->cookie_store()-> |
| 397 GetCookieMonster(); | 396 GetCookieMonster(); |
| 398 success_ = set_cookie_result; | 397 success_ = set_cookie_result; |
| 399 cookies_helpers::GetCookieListFromStore( | 398 cookies_helpers::GetCookieListFromStore( |
| 400 cookie_monster, url_, | 399 cookie_monster, url_, |
| 401 base::Bind(&SetCookieFunction::PullCookieCallback, this)); | 400 base::Bind(&SetCookieFunction::PullCookieCallback, this)); |
| 402 } | 401 } |
| 403 | 402 |
| 404 void SetCookieFunction::PullCookieCallback(const net::CookieList& cookie_list) { | 403 void SetCookieFunction::PullCookieCallback(const net::CookieList& cookie_list) { |
| 405 net::CookieList::const_iterator it; | 404 net::CookieList::const_iterator it; |
| 406 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { | 405 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
| 407 // Return the first matching cookie. Relies on the fact that the | 406 // Return the first matching cookie. Relies on the fact that the |
| 408 // CookieMonster returns them in canonical order (longest path, then | 407 // CookieMonster returns them in canonical order (longest path, then |
| 409 // earliest creation time). | 408 // earliest creation time). |
| 410 if (it->Name() == name_) { | 409 std::string name = parsed_args_->details.name.get() ? |
| 411 SetResult(cookies_helpers::CreateCookieValue(*it, store_id_)); | 410 *parsed_args_->details.name : ""; |
| 411 if (it->Name() == name) { |
| 412 scoped_ptr<Cookie> cookie( |
| 413 cookies_helpers::CreateCookie(*it, *parsed_args_->details.store_id)); |
| 414 results_ = Set::Results::Create(*cookie); |
| 412 break; | 415 break; |
| 413 } | 416 } |
| 414 } | 417 } |
| 415 | 418 |
| 416 bool rv = BrowserThread::PostTask( | 419 bool rv = BrowserThread::PostTask( |
| 417 BrowserThread::UI, FROM_HERE, | 420 BrowserThread::UI, FROM_HERE, |
| 418 base::Bind(&SetCookieFunction::RespondOnUIThread, this)); | 421 base::Bind(&SetCookieFunction::RespondOnUIThread, this)); |
| 419 DCHECK(rv); | 422 DCHECK(rv); |
| 420 } | 423 } |
| 421 | 424 |
| 422 void SetCookieFunction::RespondOnUIThread() { | 425 void SetCookieFunction::RespondOnUIThread() { |
| 423 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 424 if (!success_) { | 427 if (!success_) { |
| 428 std::string name = parsed_args_->details.name.get() ? |
| 429 *parsed_args_->details.name : ""; |
| 425 error_ = ExtensionErrorUtils::FormatErrorMessage( | 430 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 426 keys::kCookieSetFailedError, name_); | 431 keys::kCookieSetFailedError, name); |
| 427 } | 432 } |
| 428 SendResponse(success_); | 433 SendResponse(success_); |
| 429 } | 434 } |
| 430 | 435 |
| 431 RemoveCookieFunction::RemoveCookieFunction() { | 436 RemoveCookieFunction::RemoveCookieFunction() { |
| 432 } | 437 } |
| 433 | 438 |
| 434 RemoveCookieFunction::~RemoveCookieFunction() { | 439 RemoveCookieFunction::~RemoveCookieFunction() { |
| 435 } | 440 } |
| 436 | 441 |
| 437 bool RemoveCookieFunction::RunImpl() { | 442 bool RemoveCookieFunction::RunImpl() { |
| 438 // Return false if the arguments are malformed. | 443 parsed_args_ = Remove::Params::Create(*args_); |
| 439 DictionaryValue* details; | 444 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 440 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
| 441 DCHECK(details); | |
| 442 | 445 |
| 443 // Read/validate input parameters. | 446 // Read/validate input parameters. |
| 444 if (!ParseUrl(details, &url_, true)) | 447 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
| 445 return false; | 448 return false; |
| 446 | 449 |
| 447 // Get the cookie name string or return false. | 450 std::string store_id = parsed_args_->details.store_id.get() ? |
| 448 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name_)); | 451 *parsed_args_->details.store_id : ""; |
| 449 | |
| 450 net::URLRequestContextGetter* store_context = NULL; | 452 net::URLRequestContextGetter* store_context = NULL; |
| 451 if (!ParseStoreContext(details, &store_context, &store_id_)) | 453 if (!ParseStoreContext(&store_id, &store_context)) |
| 452 return false; | 454 return false; |
| 453 DCHECK(store_context); | |
| 454 store_context_ = store_context; | 455 store_context_ = store_context; |
| 456 if (!parsed_args_->details.store_id.get()) |
| 457 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| 455 | 458 |
| 456 // Pass the work off to the IO thread. | 459 // Pass the work off to the IO thread. |
| 457 bool rv = BrowserThread::PostTask( | 460 bool rv = BrowserThread::PostTask( |
| 458 BrowserThread::IO, FROM_HERE, | 461 BrowserThread::IO, FROM_HERE, |
| 459 base::Bind(&RemoveCookieFunction::RemoveCookieOnIOThread, this)); | 462 base::Bind(&RemoveCookieFunction::RemoveCookieOnIOThread, this)); |
| 460 DCHECK(rv); | 463 DCHECK(rv); |
| 461 | 464 |
| 462 // Will return asynchronously. | 465 // Will return asynchronously. |
| 463 return true; | 466 return true; |
| 464 } | 467 } |
| 465 | 468 |
| 466 void RemoveCookieFunction::RemoveCookieOnIOThread() { | 469 void RemoveCookieFunction::RemoveCookieOnIOThread() { |
| 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 468 | 471 |
| 469 // Remove the cookie | 472 // Remove the cookie |
| 470 net::CookieStore* cookie_store = | 473 net::CookieStore* cookie_store = |
| 471 store_context_->GetURLRequestContext()->cookie_store(); | 474 store_context_->GetURLRequestContext()->cookie_store(); |
| 472 cookie_store->DeleteCookieAsync( | 475 cookie_store->DeleteCookieAsync( |
| 473 url_, name_, | 476 url_, parsed_args_->details.name, |
| 474 base::Bind(&RemoveCookieFunction::RemoveCookieCallback, this)); | 477 base::Bind(&RemoveCookieFunction::RemoveCookieCallback, this)); |
| 475 } | 478 } |
| 476 | 479 |
| 477 void RemoveCookieFunction::RemoveCookieCallback() { | 480 void RemoveCookieFunction::RemoveCookieCallback() { |
| 478 // Build the callback result | 481 // Build the callback result |
| 479 DictionaryValue* resultDictionary = new DictionaryValue(); | 482 Remove::Results::Details details; |
| 480 resultDictionary->SetString(keys::kNameKey, name_); | 483 details.name = parsed_args_->details.name; |
| 481 resultDictionary->SetString(keys::kUrlKey, url_.spec()); | 484 details.url = url_.spec(); |
| 482 resultDictionary->SetString(keys::kStoreIdKey, store_id_); | 485 details.store_id = *parsed_args_->details.store_id; |
| 483 SetResult(resultDictionary); | 486 results_ = Remove::Results::Create(details); |
| 484 | 487 |
| 485 // Return to UI thread | 488 // Return to UI thread |
| 486 bool rv = BrowserThread::PostTask( | 489 bool rv = BrowserThread::PostTask( |
| 487 BrowserThread::UI, FROM_HERE, | 490 BrowserThread::UI, FROM_HERE, |
| 488 base::Bind(&RemoveCookieFunction::RespondOnUIThread, this)); | 491 base::Bind(&RemoveCookieFunction::RespondOnUIThread, this)); |
| 489 DCHECK(rv); | 492 DCHECK(rv); |
| 490 } | 493 } |
| 491 | 494 |
| 492 void RemoveCookieFunction::RespondOnUIThread() { | 495 void RemoveCookieFunction::RespondOnUIThread() { |
| 493 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 507 } | 510 } |
| 508 DCHECK(original_profile != incognito_profile); | 511 DCHECK(original_profile != incognito_profile); |
| 509 | 512 |
| 510 // Iterate through all browser instances, and for each browser, | 513 // Iterate through all browser instances, and for each browser, |
| 511 // add its tab IDs to either the regular or incognito tab ID list depending | 514 // add its tab IDs to either the regular or incognito tab ID list depending |
| 512 // whether the browser is regular or incognito. | 515 // whether the browser is regular or incognito. |
| 513 for (BrowserList::const_iterator iter = BrowserList::begin(); | 516 for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 514 iter != BrowserList::end(); ++iter) { | 517 iter != BrowserList::end(); ++iter) { |
| 515 Browser* browser = *iter; | 518 Browser* browser = *iter; |
| 516 if (browser->profile() == original_profile) { | 519 if (browser->profile() == original_profile) { |
| 517 cookies_helpers::AppendToTabIdList(browser, | 520 cookies_helpers::AppendToTabIdList(browser, original_tab_ids.get()); |
| 518 original_tab_ids.get()); | |
| 519 } else if (incognito_tab_ids.get() && | 521 } else if (incognito_tab_ids.get() && |
| 520 browser->profile() == incognito_profile) { | 522 browser->profile() == incognito_profile) { |
| 521 cookies_helpers::AppendToTabIdList(browser, | 523 cookies_helpers::AppendToTabIdList(browser, incognito_tab_ids.get()); |
| 522 incognito_tab_ids.get()); | |
| 523 } | 524 } |
| 524 } | 525 } |
| 525 // Return a list of all cookie stores with at least one open tab. | 526 // Return a list of all cookie stores with at least one open tab. |
| 526 ListValue* cookie_store_list = new ListValue(); | 527 std::vector<linked_ptr<CookieStore> > cookie_stores; |
| 527 if (original_tab_ids->GetSize() > 0) { | 528 if (original_tab_ids->GetSize() > 0) { |
| 528 cookie_store_list->Append( | 529 cookie_stores.push_back(make_linked_ptr( |
| 529 cookies_helpers::CreateCookieStoreValue( | 530 cookies_helpers::CreateCookieStore( |
| 530 original_profile, original_tab_ids.release())); | 531 original_profile, original_tab_ids.release()).release())); |
| 531 } | 532 } |
| 532 if (incognito_tab_ids.get() && incognito_tab_ids->GetSize() > 0 && | 533 if (incognito_tab_ids.get() && incognito_tab_ids->GetSize() > 0 && |
| 533 incognito_profile) { | 534 incognito_profile) { |
| 534 cookie_store_list->Append( | 535 cookie_stores.push_back(make_linked_ptr( |
| 535 cookies_helpers::CreateCookieStoreValue( | 536 cookies_helpers::CreateCookieStore( |
| 536 incognito_profile, incognito_tab_ids.release())); | 537 incognito_profile, incognito_tab_ids.release()).release())); |
| 537 } | 538 } |
| 538 SetResult(cookie_store_list); | 539 results_ = GetAllCookieStores::Results::Create(cookie_stores); |
| 539 return true; | 540 return true; |
| 540 } | 541 } |
| 541 | 542 |
| 542 void GetAllCookieStoresFunction::Run() { | 543 void GetAllCookieStoresFunction::Run() { |
| 543 SendResponse(RunImpl()); | 544 SendResponse(RunImpl()); |
| 544 } | 545 } |
| 545 | 546 |
| 546 } // namespace extensions | 547 } // namespace extensions |
| OLD | NEW |