Chromium Code Reviews| 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> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/time.h" | |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" | 17 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" |
| 17 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" | 18 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" |
| 18 #include "chrome/browser/extensions/event_router.h" | 19 #include "chrome/browser/extensions/event_router.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_list.h" | 22 #include "chrome/browser/ui/browser_list.h" |
| 22 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| 23 #include "chrome/common/extensions/api/cookies.h" | 24 #include "chrome/common/extensions/api/cookies.h" |
| 24 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 55 registrar_.Add(this, | 56 registrar_.Add(this, |
| 56 chrome::NOTIFICATION_COOKIE_CHANGED, | 57 chrome::NOTIFICATION_COOKIE_CHANGED, |
| 57 content::NotificationService::AllBrowserContextsAndSources()); | 58 content::NotificationService::AllBrowserContextsAndSources()); |
| 58 } | 59 } |
| 59 | 60 |
| 60 void ExtensionCookiesEventRouter::Observe( | 61 void ExtensionCookiesEventRouter::Observe( |
| 61 int type, | 62 int type, |
| 62 const content::NotificationSource& source, | 63 const content::NotificationSource& source, |
| 63 const content::NotificationDetails& details) { | 64 const content::NotificationDetails& details) { |
| 64 Profile* profile = content::Source<Profile>(source).ptr(); | 65 Profile* profile = content::Source<Profile>(source).ptr(); |
| 65 if (!profile_->IsSameProfile(profile)) { | 66 if (!profile_->IsSameProfile(profile)) |
|
jochen (gone - plz use gerrit)
2012/09/10 08:31:27
The incognito profile and the regular profile are
Lei Zhang
2012/09/10 17:55:12
Done.
| |
| 66 return; | 67 return; |
| 67 } | 68 |
| 68 switch (type) { | 69 switch (type) { |
| 69 case chrome::NOTIFICATION_COOKIE_CHANGED: | 70 case chrome::NOTIFICATION_COOKIE_CHANGED: |
| 70 CookieChanged( | 71 CookieChanged(content::Details<ChromeCookieDetails>(details).ptr()); |
| 71 profile, | |
| 72 content::Details<ChromeCookieDetails>(details).ptr()); | |
| 73 break; | 72 break; |
| 74 | 73 |
| 75 default: | 74 default: |
| 76 NOTREACHED(); | 75 NOTREACHED(); |
| 77 } | 76 } |
| 78 } | 77 } |
| 79 | 78 |
| 80 void ExtensionCookiesEventRouter::CookieChanged( | 79 void ExtensionCookiesEventRouter::CookieChanged(ChromeCookieDetails* details) { |
| 81 Profile* profile, | |
| 82 ChromeCookieDetails* details) { | |
| 83 scoped_ptr<ListValue> args(new ListValue()); | 80 scoped_ptr<ListValue> args(new ListValue()); |
| 84 DictionaryValue* dict = new DictionaryValue(); | 81 DictionaryValue* dict = new DictionaryValue(); |
| 85 dict->SetBoolean(keys::kRemovedKey, details->removed); | 82 dict->SetBoolean(keys::kRemovedKey, details->removed); |
| 86 | 83 |
| 87 scoped_ptr<Cookie> cookie( | 84 scoped_ptr<Cookie> cookie( |
| 88 cookies_helpers::CreateCookie(*details->cookie, | 85 cookies_helpers::CreateCookie(*details->cookie, |
| 89 cookies_helpers::GetStoreIdFromProfile(profile))); | 86 cookies_helpers::GetStoreIdFromProfile(profile_))); |
| 90 dict->Set(keys::kCookieKey, cookie->ToValue().release()); | 87 dict->Set(keys::kCookieKey, cookie->ToValue().release()); |
| 91 | 88 |
| 92 // Map the interal cause to an external string. | 89 // Map the internal cause to an external string. |
| 93 std::string cause; | 90 std::string cause; |
| 94 switch (details->cause) { | 91 switch (details->cause) { |
| 95 case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT: | 92 case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT: |
| 96 cause = keys::kExplicitChangeCause; | 93 cause = keys::kExplicitChangeCause; |
| 97 break; | 94 break; |
| 98 | 95 |
| 99 case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE: | 96 case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE: |
| 100 cause = keys::kOverwriteChangeCause; | 97 cause = keys::kOverwriteChangeCause; |
| 101 break; | 98 break; |
| 102 | 99 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 114 | 111 |
| 115 default: | 112 default: |
| 116 NOTREACHED(); | 113 NOTREACHED(); |
| 117 } | 114 } |
| 118 dict->SetString(keys::kCauseKey, cause); | 115 dict->SetString(keys::kCauseKey, cause); |
| 119 | 116 |
| 120 args->Append(dict); | 117 args->Append(dict); |
| 121 | 118 |
| 122 GURL cookie_domain = | 119 GURL cookie_domain = |
| 123 cookies_helpers::GetURLFromCanonicalCookie(*details->cookie); | 120 cookies_helpers::GetURLFromCanonicalCookie(*details->cookie); |
| 124 DispatchEvent(profile, keys::kOnChanged, args.Pass(), cookie_domain); | 121 DispatchEvent(keys::kOnChanged, args.Pass(), cookie_domain); |
| 125 } | 122 } |
| 126 | 123 |
| 127 void ExtensionCookiesEventRouter::DispatchEvent( | 124 void ExtensionCookiesEventRouter::DispatchEvent( |
| 128 Profile* profile, const char* event_name, scoped_ptr<ListValue> event_args, | 125 const std::string& event_name, |
| 126 scoped_ptr<ListValue> event_args, | |
| 129 GURL& cookie_domain) { | 127 GURL& cookie_domain) { |
| 130 if (profile && profile->GetExtensionEventRouter()) { | 128 EventRouter* router = profile_ ? profile_->GetExtensionEventRouter() : NULL; |
| 131 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 129 if (!router) |
| 132 event_name, event_args.Pass(), profile, cookie_domain, | 130 return; |
| 133 EventFilteringInfo()); | 131 router->DispatchEventToRenderers(event_name, event_args.Pass(), profile_, |
| 134 } | 132 cookie_domain); |
| 135 } | 133 } |
| 136 | 134 |
| 137 bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url, | 135 bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url, |
| 138 bool check_host_permissions) { | 136 bool check_host_permissions) { |
| 139 *url = GURL(url_string); | 137 *url = GURL(url_string); |
| 140 if (!url->is_valid()) { | 138 if (!url->is_valid()) { |
| 141 error_ = ExtensionErrorUtils::FormatErrorMessage( | 139 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 142 keys::kInvalidUrlError, url_string); | 140 keys::kInvalidUrlError, url_string); |
| 143 return false; | 141 return false; |
| 144 } | 142 } |
| 145 // Check against host permissions if needed. | 143 // Check against host permissions if needed. |
| 146 if (check_host_permissions && | 144 if (check_host_permissions && !GetExtension()->HasHostPermission(*url)) { |
| 147 !GetExtension()->HasHostPermission(*url)) { | |
| 148 error_ = ExtensionErrorUtils::FormatErrorMessage( | 145 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 149 keys::kNoHostPermissionsError, url->spec()); | 146 keys::kNoHostPermissionsError, url->spec()); |
| 150 return false; | 147 return false; |
| 151 } | 148 } |
| 152 return true; | 149 return true; |
| 153 } | 150 } |
| 154 | 151 |
| 155 bool CookiesFunction::ParseStoreContext( | 152 bool CookiesFunction::ParseStoreContext( |
| 156 std::string* store_id, | 153 std::string* store_id, |
| 157 net::URLRequestContextGetter** context) { | 154 net::URLRequestContextGetter** context) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 GetAllCookiesFunction::GetAllCookiesFunction() { | 257 GetAllCookiesFunction::GetAllCookiesFunction() { |
| 261 } | 258 } |
| 262 | 259 |
| 263 GetAllCookiesFunction::~GetAllCookiesFunction() { | 260 GetAllCookiesFunction::~GetAllCookiesFunction() { |
| 264 } | 261 } |
| 265 | 262 |
| 266 bool GetAllCookiesFunction::RunImpl() { | 263 bool GetAllCookiesFunction::RunImpl() { |
| 267 parsed_args_ = GetAll::Params::Create(*args_); | 264 parsed_args_ = GetAll::Params::Create(*args_); |
| 268 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 265 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
| 269 | 266 |
| 270 if (parsed_args_->details.url.get()) { | 267 if (parsed_args_->details.url.get() && |
| 271 if (!ParseUrl(*parsed_args_->details.url, &url_, false)) | 268 !ParseUrl(*parsed_args_->details.url, &url_, false)) { |
| 272 return false; | 269 return false; |
| 273 } | 270 } |
| 274 | 271 |
| 275 std::string store_id = parsed_args_->details.store_id.get() ? | 272 std::string store_id = parsed_args_->details.store_id.get() ? |
| 276 *parsed_args_->details.store_id : ""; | 273 *parsed_args_->details.store_id : ""; |
| 277 net::URLRequestContextGetter* store_context = NULL; | 274 net::URLRequestContextGetter* store_context = NULL; |
| 278 if (!ParseStoreContext(&store_id, &store_context)) | 275 if (!ParseStoreContext(&store_id, &store_context)) |
| 279 return false; | 276 return false; |
| 280 store_context_ = store_context; | 277 store_context_ = store_context; |
| 281 if (!parsed_args_->details.store_id.get()) | 278 if (!parsed_args_->details.store_id.get()) |
| 282 parsed_args_->details.store_id.reset(new std::string(store_id)); | 279 parsed_args_->details.store_id.reset(new std::string(store_id)); |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 533 } | 530 } |
| 534 results_ = GetAllCookieStores::Results::Create(cookie_stores); | 531 results_ = GetAllCookieStores::Results::Create(cookie_stores); |
| 535 return true; | 532 return true; |
| 536 } | 533 } |
| 537 | 534 |
| 538 void GetAllCookieStoresFunction::Run() { | 535 void GetAllCookieStoresFunction::Run() { |
| 539 SendResponse(RunImpl()); | 536 SendResponse(RunImpl()); |
| 540 } | 537 } |
| 541 | 538 |
| 542 } // namespace extensions | 539 } // namespace extensions |
| OLD | NEW |