[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 5 | // Implements the Chrome Extensions Cookies API. |
| 6 | |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 7 | #include "chrome/browser/extensions/api/cookies/cookies_api.h" |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 8 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 12 | #include "base/json/json_writer.h" |
[email protected] | 1794e6a | 2013-01-03 02:22:02 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 14 | #include "base/memory/linked_ptr.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 16 | #include "base/time.h" |
[email protected] | 3bb8499 | 2010-08-26 17:23:46 | [diff] [blame] | 17 | #include "base/values.h" |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 18 | #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" |
| 19 | #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 20 | #include "chrome/browser/extensions/event_router.h" |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 22 | #include "chrome/browser/profiles/profile.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser.h" |
[email protected] | 71b73f0 | 2011-04-06 15:57:29 | [diff] [blame] | 24 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 25 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 26 | #include "chrome/common/extensions/api/cookies.h" |
[email protected] | d83a560 | 2010-09-16 00:22:48 | [diff] [blame] | 27 | #include "chrome/common/extensions/extension.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 28 | #include "content/public/browser/browser_thread.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 29 | #include "content/public/browser/notification_service.h" |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 30 | #include "extensions/common/error_utils.h" |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 31 | #include "net/cookies/canonical_cookie.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 32 | #include "net/cookies/cookie_monster.h" |
[email protected] | 277ec26 | 2011-03-30 21:09:40 | [diff] [blame] | 33 | #include "net/url_request/url_request_context.h" |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 34 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 35 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 36 | using content::BrowserThread; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 37 | using extensions::api::cookies::Cookie; |
| 38 | using extensions::api::cookies::CookieStore; |
| 39 | |
| 40 | namespace Get = extensions::api::cookies::Get; |
| 41 | namespace GetAll = extensions::api::cookies::GetAll; |
| 42 | namespace GetAllCookieStores = extensions::api::cookies::GetAllCookieStores; |
| 43 | namespace Remove = extensions::api::cookies::Remove; |
| 44 | namespace Set = extensions::api::cookies::Set; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 45 | |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 46 | namespace extensions { |
| 47 | namespace keys = cookies_api_constants; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 48 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 49 | CookiesEventRouter::CookiesEventRouter(Profile* profile) |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 50 | : profile_(profile) { |
[email protected] | f825df2 | 2011-06-28 17:36:10 | [diff] [blame] | 51 | CHECK(registrar_.IsEmpty()); |
| 52 | registrar_.Add(this, |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 53 | chrome::NOTIFICATION_COOKIE_CHANGED, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 54 | content::NotificationService::AllBrowserContextsAndSources()); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 57 | CookiesEventRouter::~CookiesEventRouter() { |
[email protected] | ab9356c | 2012-11-05 23:13:26 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 60 | void CookiesEventRouter::Observe( |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 61 | int type, |
| 62 | const content::NotificationSource& source, |
| 63 | const content::NotificationDetails& details) { |
| 64 | Profile* profile = content::Source<Profile>(source).ptr(); |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 65 | if (!profile_->IsSameProfile(profile)) |
[email protected] | f825df2 | 2011-06-28 17:36:10 | [diff] [blame] | 66 | return; |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 67 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 68 | switch (type) { |
| 69 | case chrome::NOTIFICATION_COOKIE_CHANGED: |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 70 | CookieChanged( |
[email protected] | f825df2 | 2011-06-28 17:36:10 | [diff] [blame] | 71 | profile, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 72 | content::Details<ChromeCookieDetails>(details).ptr()); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 73 | break; |
| 74 | |
| 75 | default: |
| 76 | NOTREACHED(); |
| 77 | } |
| 78 | } |
| 79 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 80 | void CookiesEventRouter::CookieChanged( |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 81 | Profile* profile, |
| 82 | ChromeCookieDetails* details) { |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 83 | scoped_ptr<ListValue> args(new ListValue()); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 84 | DictionaryValue* dict = new DictionaryValue(); |
| 85 | dict->SetBoolean(keys::kRemovedKey, details->removed); |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 86 | |
| 87 | scoped_ptr<Cookie> cookie( |
| 88 | cookies_helpers::CreateCookie(*details->cookie, |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 89 | cookies_helpers::GetStoreIdFromProfile(profile_))); |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 90 | dict->Set(keys::kCookieKey, cookie->ToValue().release()); |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 91 | |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 92 | // Map the internal cause to an external string. |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 93 | std::string cause; |
| 94 | switch (details->cause) { |
| 95 | case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT: |
| 96 | cause = keys::kExplicitChangeCause; |
| 97 | break; |
| 98 | |
| 99 | case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE: |
| 100 | cause = keys::kOverwriteChangeCause; |
| 101 | break; |
| 102 | |
| 103 | case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED: |
| 104 | cause = keys::kExpiredChangeCause; |
| 105 | break; |
| 106 | |
| 107 | case net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED: |
| 108 | cause = keys::kEvictedChangeCause; |
| 109 | break; |
| 110 | |
[email protected] | e7c590e5 | 2011-03-30 08:33:55 | [diff] [blame] | 111 | case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE: |
| 112 | cause = keys::kExpiredOverwriteChangeCause; |
| 113 | break; |
| 114 | |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 115 | default: |
| 116 | NOTREACHED(); |
| 117 | } |
| 118 | dict->SetString(keys::kCauseKey, cause); |
| 119 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 120 | args->Append(dict); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 121 | |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 122 | GURL cookie_domain = |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 123 | cookies_helpers::GetURLFromCanonicalCookie(*details->cookie); |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 124 | DispatchEvent(profile, keys::kOnChanged, args.Pass(), cookie_domain); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 127 | void CookiesEventRouter::DispatchEvent( |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 128 | Profile* profile, |
| 129 | const std::string& event_name, |
| 130 | scoped_ptr<ListValue> event_args, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 131 | GURL& cookie_domain) { |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 132 | EventRouter* router = profile ? |
| 133 | extensions::ExtensionSystem::Get(profile)->event_router() : NULL; |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 134 | if (!router) |
| 135 | return; |
[email protected] | 01f7a804 | 2012-12-07 07:48:02 | [diff] [blame] | 136 | scoped_ptr<Event> event(new Event(event_name, event_args.Pass())); |
| 137 | event->restrict_to_profile = profile; |
| 138 | event->event_url = cookie_domain; |
| 139 | router->BroadcastEvent(event.Pass()); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 142 | bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url, |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 143 | bool check_host_permissions) { |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 144 | *url = GURL(url_string); |
| 145 | if (!url->is_valid()) { |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 146 | error_ = ErrorUtils::FormatErrorMessage( |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 147 | keys::kInvalidUrlError, url_string); |
| 148 | return false; |
| 149 | } |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 150 | // Check against host permissions if needed. |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 151 | if (check_host_permissions && !GetExtension()->HasHostPermission(*url)) { |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 152 | error_ = ErrorUtils::FormatErrorMessage( |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 153 | keys::kNoHostPermissionsError, url->spec()); |
| 154 | return false; |
| 155 | } |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 159 | bool CookiesFunction::ParseStoreContext( |
| 160 | std::string* store_id, |
| 161 | net::URLRequestContextGetter** context) { |
| 162 | DCHECK((context || store_id->empty())); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 163 | Profile* store_profile = NULL; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 164 | if (!store_id->empty()) { |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 165 | store_profile = cookies_helpers::ChooseProfileFromStoreId( |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 166 | *store_id, profile(), include_incognito()); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 167 | if (!store_profile) { |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 168 | error_ = ErrorUtils::FormatErrorMessage( |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 169 | keys::kInvalidStoreIdError, *store_id); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | } else { |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 173 | // The store ID was not specified; use the current execution context's |
| 174 | // cookie store by default. |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 175 | // GetCurrentBrowser() already takes into account incognito settings. |
| 176 | Browser* current_browser = GetCurrentBrowser(); |
| 177 | if (!current_browser) { |
| 178 | error_ = keys::kNoCookieStoreFoundError; |
| 179 | return false; |
| 180 | } |
| 181 | store_profile = current_browser->profile(); |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 182 | *store_id = cookies_helpers::GetStoreIdFromProfile(store_profile); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 183 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 184 | |
| 185 | if (context) |
| 186 | *context = store_profile->GetRequestContext(); |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 187 | DCHECK(context); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 188 | |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 192 | GetCookieFunction::GetCookieFunction() { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 193 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 194 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 195 | GetCookieFunction::~GetCookieFunction() { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 196 | } |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 197 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 198 | bool GetCookieFunction::RunImpl() { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 199 | parsed_args_ = Get::Params::Create(*args_); |
| 200 | EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 201 | |
| 202 | // Read/validate input parameters. |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 203 | if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 204 | return false; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 205 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 206 | std::string store_id = parsed_args_->details.store_id.get() ? |
| 207 | *parsed_args_->details.store_id : ""; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 208 | net::URLRequestContextGetter* store_context = NULL; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 209 | if (!ParseStoreContext(&store_id, &store_context)) |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 210 | return false; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 211 | store_context_ = store_context; |
| 212 | if (!parsed_args_->details.store_id.get()) |
| 213 | parsed_args_->details.store_id.reset(new std::string(store_id)); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 214 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 215 | store_context_ = store_context; |
| 216 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 217 | bool rv = BrowserThread::PostTask( |
| 218 | BrowserThread::IO, FROM_HERE, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 219 | base::Bind(&GetCookieFunction::GetCookieOnIOThread, this)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 220 | DCHECK(rv); |
| 221 | |
| 222 | // Will finish asynchronously. |
| 223 | return true; |
| 224 | } |
| 225 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 226 | void GetCookieFunction::GetCookieOnIOThread() { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 227 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 277ec26 | 2011-03-30 21:09:40 | [diff] [blame] | 228 | net::CookieStore* cookie_store = |
| 229 | store_context_->GetURLRequestContext()->cookie_store(); |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 230 | cookies_helpers::GetCookieListFromStore( |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 231 | cookie_store, url_, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 232 | base::Bind(&GetCookieFunction::GetCookieCallback, this)); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 235 | void GetCookieFunction::GetCookieCallback(const net::CookieList& cookie_list) { |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 236 | net::CookieList::const_iterator it; |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 237 | for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 238 | // Return the first matching cookie. Relies on the fact that the |
[email protected] | c5803020 | 2010-08-27 17:25:38 | [diff] [blame] | 239 | // CookieMonster returns them in canonical order (longest path, then |
| 240 | // earliest creation time). |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 241 | if (it->Name() == parsed_args_->details.name) { |
| 242 | scoped_ptr<Cookie> cookie( |
| 243 | cookies_helpers::CreateCookie(*it, *parsed_args_->details.store_id)); |
| 244 | results_ = Get::Results::Create(*cookie); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 245 | break; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 246 | } |
| 247 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 248 | |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 249 | // The cookie doesn't exist; return null. |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 250 | if (it == cookie_list.end()) |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 251 | SetResult(Value::CreateNullValue()); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 252 | |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 253 | bool rv = BrowserThread::PostTask( |
| 254 | BrowserThread::UI, FROM_HERE, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 255 | base::Bind(&GetCookieFunction::RespondOnUIThread, this)); |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 256 | DCHECK(rv); |
| 257 | } |
| 258 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 259 | void GetCookieFunction::RespondOnUIThread() { |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 260 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 261 | SendResponse(true); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 264 | GetAllCookiesFunction::GetAllCookiesFunction() { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 265 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 266 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 267 | GetAllCookiesFunction::~GetAllCookiesFunction() { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 268 | } |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 269 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 270 | bool GetAllCookiesFunction::RunImpl() { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 271 | parsed_args_ = GetAll::Params::Create(*args_); |
| 272 | EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
[email protected] | e2d4bf0 | 2010-06-18 23:26:17 | [diff] [blame] | 273 | |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 274 | if (parsed_args_->details.url.get() && |
| 275 | !ParseUrl(*parsed_args_->details.url, &url_, false)) { |
| 276 | return false; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 277 | } |
[email protected] | 34d18e4 | 2010-06-21 16:04:50 | [diff] [blame] | 278 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 279 | std::string store_id = parsed_args_->details.store_id.get() ? |
| 280 | *parsed_args_->details.store_id : ""; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 281 | net::URLRequestContextGetter* store_context = NULL; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 282 | if (!ParseStoreContext(&store_id, &store_context)) |
[email protected] | 34d18e4 | 2010-06-21 16:04:50 | [diff] [blame] | 283 | return false; |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 284 | store_context_ = store_context; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 285 | if (!parsed_args_->details.store_id.get()) |
| 286 | parsed_args_->details.store_id.reset(new std::string(store_id)); |
[email protected] | 34d18e4 | 2010-06-21 16:04:50 | [diff] [blame] | 287 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 288 | bool rv = BrowserThread::PostTask( |
| 289 | BrowserThread::IO, FROM_HERE, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 290 | base::Bind(&GetAllCookiesFunction::GetAllCookiesOnIOThread, this)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 291 | DCHECK(rv); |
| 292 | |
| 293 | // Will finish asynchronously. |
[email protected] | 34d18e4 | 2010-06-21 16:04:50 | [diff] [blame] | 294 | return true; |
[email protected] | e2d4bf0 | 2010-06-18 23:26:17 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 297 | void GetAllCookiesFunction::GetAllCookiesOnIOThread() { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 298 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 277ec26 | 2011-03-30 21:09:40 | [diff] [blame] | 299 | net::CookieStore* cookie_store = |
| 300 | store_context_->GetURLRequestContext()->cookie_store(); |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 301 | cookies_helpers::GetCookieListFromStore( |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 302 | cookie_store, url_, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 303 | base::Bind(&GetAllCookiesFunction::GetAllCookiesCallback, this)); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 304 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 305 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 306 | void GetAllCookiesFunction::GetAllCookiesCallback( |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 307 | const net::CookieList& cookie_list) { |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 308 | const extensions::Extension* extension = GetExtension(); |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 309 | if (extension) { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 310 | std::vector<linked_ptr<Cookie> > match_vector; |
| 311 | cookies_helpers::AppendMatchingCookiesToVector( |
| 312 | cookie_list, url_, &parsed_args_->details, |
| 313 | GetExtension(), &match_vector); |
| 314 | |
| 315 | results_ = GetAll::Results::Create(match_vector); |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 316 | } |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 317 | bool rv = BrowserThread::PostTask( |
| 318 | BrowserThread::UI, FROM_HERE, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 319 | base::Bind(&GetAllCookiesFunction::RespondOnUIThread, this)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 320 | DCHECK(rv); |
| 321 | } |
| 322 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 323 | void GetAllCookiesFunction::RespondOnUIThread() { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 324 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 325 | SendResponse(true); |
| 326 | } |
| 327 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 328 | SetCookieFunction::SetCookieFunction() : success_(false) { |
[email protected] | a6c8e7250 | 2010-07-24 09:18:25 | [diff] [blame] | 329 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 330 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 331 | SetCookieFunction::~SetCookieFunction() { |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 334 | bool SetCookieFunction::RunImpl() { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 335 | parsed_args_ = Set::Params::Create(*args_); |
| 336 | EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 337 | |
| 338 | // Read/validate input parameters. |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 339 | if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 340 | return false; |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 341 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 342 | std::string store_id = parsed_args_->details.store_id.get() ? |
| 343 | *parsed_args_->details.store_id : ""; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 344 | net::URLRequestContextGetter* store_context = NULL; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 345 | if (!ParseStoreContext(&store_id, &store_context)) |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 346 | return false; |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 347 | store_context_ = store_context; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 348 | if (!parsed_args_->details.store_id.get()) |
| 349 | parsed_args_->details.store_id.reset(new std::string(store_id)); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 350 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 351 | bool rv = BrowserThread::PostTask( |
| 352 | BrowserThread::IO, FROM_HERE, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 353 | base::Bind(&SetCookieFunction::SetCookieOnIOThread, this)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 354 | DCHECK(rv); |
| 355 | |
| 356 | // Will finish asynchronously. |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 357 | return true; |
| 358 | } |
| 359 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 360 | void SetCookieFunction::SetCookieOnIOThread() { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 361 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 362 | net::CookieMonster* cookie_monster = |
[email protected] | 277ec26 | 2011-03-30 21:09:40 | [diff] [blame] | 363 | store_context_->GetURLRequestContext()->cookie_store()-> |
| 364 | GetCookieMonster(); |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 365 | |
| 366 | base::Time expiration_time; |
| 367 | if (parsed_args_->details.expiration_date.get()) { |
| 368 | // Time::FromDoubleT converts double time 0 to empty Time object. So we need |
| 369 | // to do special handling here. |
| 370 | expiration_time = (*parsed_args_->details.expiration_date == 0) ? |
| 371 | base::Time::UnixEpoch() : |
| 372 | base::Time::FromDoubleT(*parsed_args_->details.expiration_date); |
| 373 | } |
| 374 | |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 375 | cookie_monster->SetCookieWithDetailsAsync( |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 376 | url_, |
| 377 | parsed_args_->details.name.get() ? *parsed_args_->details.name : "", |
| 378 | parsed_args_->details.value.get() ? *parsed_args_->details.value : "", |
| 379 | parsed_args_->details.domain.get() ? *parsed_args_->details.domain : "", |
| 380 | parsed_args_->details.path.get() ? *parsed_args_->details.path : "", |
| 381 | expiration_time, |
| 382 | parsed_args_->details.secure.get() ? |
| 383 | *parsed_args_->details.secure.get() : |
| 384 | false, |
| 385 | parsed_args_->details.http_only.get() ? |
| 386 | *parsed_args_->details.http_only : |
| 387 | false, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 388 | base::Bind(&SetCookieFunction::PullCookie, this)); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 389 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 390 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 391 | void SetCookieFunction::PullCookie(bool set_cookie_result) { |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 392 | // Pull the newly set cookie. |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 393 | net::CookieMonster* cookie_monster = |
| 394 | store_context_->GetURLRequestContext()->cookie_store()-> |
| 395 | GetCookieMonster(); |
| 396 | success_ = set_cookie_result; |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 397 | cookies_helpers::GetCookieListFromStore( |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 398 | cookie_monster, url_, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 399 | base::Bind(&SetCookieFunction::PullCookieCallback, this)); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 400 | } |
| 401 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 402 | void SetCookieFunction::PullCookieCallback(const net::CookieList& cookie_list) { |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 403 | net::CookieList::const_iterator it; |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 404 | for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
| 405 | // Return the first matching cookie. Relies on the fact that the |
| 406 | // CookieMonster returns them in canonical order (longest path, then |
| 407 | // earliest creation time). |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 408 | std::string name = parsed_args_->details.name.get() ? |
| 409 | *parsed_args_->details.name : ""; |
| 410 | if (it->Name() == name) { |
| 411 | scoped_ptr<Cookie> cookie( |
| 412 | cookies_helpers::CreateCookie(*it, *parsed_args_->details.store_id)); |
| 413 | results_ = Set::Results::Create(*cookie); |
[email protected] | 71c94e1 | 2011-02-17 09:46:07 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 418 | bool rv = BrowserThread::PostTask( |
| 419 | BrowserThread::UI, FROM_HERE, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 420 | base::Bind(&SetCookieFunction::RespondOnUIThread, this)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 421 | DCHECK(rv); |
| 422 | } |
| 423 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 424 | void SetCookieFunction::RespondOnUIThread() { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 425 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 426 | if (!success_) { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 427 | std::string name = parsed_args_->details.name.get() ? |
| 428 | *parsed_args_->details.name : ""; |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 429 | error_ = ErrorUtils::FormatErrorMessage( |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 430 | keys::kCookieSetFailedError, name); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 431 | } |
| 432 | SendResponse(success_); |
| 433 | } |
| 434 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 435 | RemoveCookieFunction::RemoveCookieFunction() { |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 436 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 437 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 438 | RemoveCookieFunction::~RemoveCookieFunction() { |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 439 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 440 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 441 | bool RemoveCookieFunction::RunImpl() { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 442 | parsed_args_ = Remove::Params::Create(*args_); |
| 443 | EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 444 | |
| 445 | // Read/validate input parameters. |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 446 | if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 447 | return false; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 448 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 449 | std::string store_id = parsed_args_->details.store_id.get() ? |
| 450 | *parsed_args_->details.store_id : ""; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 451 | net::URLRequestContextGetter* store_context = NULL; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 452 | if (!ParseStoreContext(&store_id, &store_context)) |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 453 | return false; |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 454 | store_context_ = store_context; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 455 | if (!parsed_args_->details.store_id.get()) |
| 456 | parsed_args_->details.store_id.reset(new std::string(store_id)); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 457 | |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 458 | // Pass the work off to the IO thread. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 459 | bool rv = BrowserThread::PostTask( |
| 460 | BrowserThread::IO, FROM_HERE, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 461 | base::Bind(&RemoveCookieFunction::RemoveCookieOnIOThread, this)); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 462 | DCHECK(rv); |
| 463 | |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 464 | // Will return asynchronously. |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 465 | return true; |
| 466 | } |
| 467 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 468 | void RemoveCookieFunction::RemoveCookieOnIOThread() { |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 469 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 470 | |
| 471 | // Remove the cookie |
[email protected] | 277ec26 | 2011-03-30 21:09:40 | [diff] [blame] | 472 | net::CookieStore* cookie_store = |
| 473 | store_context_->GetURLRequestContext()->cookie_store(); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 474 | cookie_store->DeleteCookieAsync( |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 475 | url_, parsed_args_->details.name, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 476 | base::Bind(&RemoveCookieFunction::RemoveCookieCallback, this)); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 477 | } |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 478 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 479 | void RemoveCookieFunction::RemoveCookieCallback() { |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 480 | // Build the callback result |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 481 | Remove::Results::Details details; |
| 482 | details.name = parsed_args_->details.name; |
| 483 | details.url = url_.spec(); |
| 484 | details.store_id = *parsed_args_->details.store_id; |
| 485 | results_ = Remove::Results::Create(details); |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 486 | |
| 487 | // Return to UI thread |
| 488 | bool rv = BrowserThread::PostTask( |
| 489 | BrowserThread::UI, FROM_HERE, |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 490 | base::Bind(&RemoveCookieFunction::RespondOnUIThread, this)); |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 491 | DCHECK(rv); |
| 492 | } |
| 493 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 494 | void RemoveCookieFunction::RespondOnUIThread() { |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 495 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 496 | SendResponse(true); |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 497 | } |
| 498 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 499 | bool GetAllCookieStoresFunction::RunImpl() { |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 500 | Profile* original_profile = profile(); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 501 | DCHECK(original_profile); |
| 502 | scoped_ptr<ListValue> original_tab_ids(new ListValue()); |
| 503 | Profile* incognito_profile = NULL; |
| 504 | scoped_ptr<ListValue> incognito_tab_ids; |
[email protected] | 6df23c4 | 2010-12-15 08:52:57 | [diff] [blame] | 505 | if (include_incognito() && profile()->HasOffTheRecordProfile()) { |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 506 | incognito_profile = profile()->GetOffTheRecordProfile(); |
| 507 | if (incognito_profile) |
| 508 | incognito_tab_ids.reset(new ListValue()); |
| 509 | } |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 510 | DCHECK(original_profile != incognito_profile); |
| 511 | |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 512 | // Iterate through all browser instances, and for each browser, |
| 513 | // add its tab IDs to either the regular or incognito tab ID list depending |
| 514 | // whether the browser is regular or incognito. |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 515 | for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 516 | iter != BrowserList::end(); ++iter) { |
| 517 | Browser* browser = *iter; |
| 518 | if (browser->profile() == original_profile) { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 519 | cookies_helpers::AppendToTabIdList(browser, original_tab_ids.get()); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 520 | } else if (incognito_tab_ids.get() && |
| 521 | browser->profile() == incognito_profile) { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 522 | cookies_helpers::AppendToTabIdList(browser, incognito_tab_ids.get()); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 523 | } |
| 524 | } |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 525 | // Return a list of all cookie stores with at least one open tab. |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 526 | std::vector<linked_ptr<CookieStore> > cookie_stores; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 527 | if (original_tab_ids->GetSize() > 0) { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 528 | cookie_stores.push_back(make_linked_ptr( |
| 529 | cookies_helpers::CreateCookieStore( |
| 530 | original_profile, original_tab_ids.release()).release())); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 531 | } |
[email protected] | f0a151f | 2011-07-08 22:26:15 | [diff] [blame] | 532 | if (incognito_tab_ids.get() && incognito_tab_ids->GetSize() > 0 && |
| 533 | incognito_profile) { |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 534 | cookie_stores.push_back(make_linked_ptr( |
| 535 | cookies_helpers::CreateCookieStore( |
| 536 | incognito_profile, incognito_tab_ids.release()).release())); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 537 | } |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 538 | results_ = GetAllCookieStores::Results::Create(cookie_stores); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 539 | return true; |
| 540 | } |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 541 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 542 | void GetAllCookieStoresFunction::Run() { |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 543 | SendResponse(RunImpl()); |
| 544 | } |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 545 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 546 | CookiesAPI::CookiesAPI(Profile* profile) |
| 547 | : profile_(profile) { |
| 548 | ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 549 | this, keys::kOnChanged); |
| 550 | } |
| 551 | |
| 552 | CookiesAPI::~CookiesAPI() { |
| 553 | } |
| 554 | |
| 555 | void CookiesAPI::Shutdown() { |
| 556 | ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 557 | } |
| 558 | |
[email protected] | c39df1a | 2013-01-08 18:22:07 | [diff] [blame] | 559 | static base::LazyInstance<ProfileKeyedAPIFactory<CookiesAPI> > |
| 560 | g_factory = LAZY_INSTANCE_INITIALIZER; |
| 561 | |
| 562 | // static |
| 563 | ProfileKeyedAPIFactory<CookiesAPI>* CookiesAPI::GetFactoryInstance() { |
| 564 | return &g_factory.Get(); |
| 565 | } |
| 566 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 567 | void CookiesAPI::OnListenerAdded( |
| 568 | const extensions::EventListenerInfo& details) { |
| 569 | cookies_event_router_.reset(new CookiesEventRouter(profile_)); |
| 570 | ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 571 | } |
| 572 | |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 573 | } // namespace extensions |