Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/browser/extensions/api/cookies/cookies_api.cc

Issue 10915153: Cleanup: Simplify some extension API code. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/extensions/api/cookies/cookies_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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))
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(
71 profile, 72 profile,
72 content::Details<ChromeCookieDetails>(details).ptr()); 73 content::Details<ChromeCookieDetails>(details).ptr());
73 break; 74 break;
74 75
75 default: 76 default:
76 NOTREACHED(); 77 NOTREACHED();
77 } 78 }
78 } 79 }
79 80
80 void ExtensionCookiesEventRouter::CookieChanged( 81 void ExtensionCookiesEventRouter::CookieChanged(
81 Profile* profile, 82 Profile* profile,
82 ChromeCookieDetails* details) { 83 ChromeCookieDetails* details) {
83 scoped_ptr<ListValue> args(new ListValue()); 84 scoped_ptr<ListValue> args(new ListValue());
84 DictionaryValue* dict = new DictionaryValue(); 85 DictionaryValue* dict = new DictionaryValue();
85 dict->SetBoolean(keys::kRemovedKey, details->removed); 86 dict->SetBoolean(keys::kRemovedKey, details->removed);
86 87
87 scoped_ptr<Cookie> cookie( 88 scoped_ptr<Cookie> cookie(
88 cookies_helpers::CreateCookie(*details->cookie, 89 cookies_helpers::CreateCookie(*details->cookie,
89 cookies_helpers::GetStoreIdFromProfile(profile))); 90 cookies_helpers::GetStoreIdFromProfile(profile_)));
90 dict->Set(keys::kCookieKey, cookie->ToValue().release()); 91 dict->Set(keys::kCookieKey, cookie->ToValue().release());
91 92
92 // Map the interal cause to an external string. 93 // Map the internal cause to an external string.
93 std::string cause; 94 std::string cause;
94 switch (details->cause) { 95 switch (details->cause) {
95 case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT: 96 case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT:
96 cause = keys::kExplicitChangeCause; 97 cause = keys::kExplicitChangeCause;
97 break; 98 break;
98 99
99 case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE: 100 case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE:
100 cause = keys::kOverwriteChangeCause; 101 cause = keys::kOverwriteChangeCause;
101 break; 102 break;
102 103
(...skipping 15 matching lines...) Expand all
118 dict->SetString(keys::kCauseKey, cause); 119 dict->SetString(keys::kCauseKey, cause);
119 120
120 args->Append(dict); 121 args->Append(dict);
121 122
122 GURL cookie_domain = 123 GURL cookie_domain =
123 cookies_helpers::GetURLFromCanonicalCookie(*details->cookie); 124 cookies_helpers::GetURLFromCanonicalCookie(*details->cookie);
124 DispatchEvent(profile, keys::kOnChanged, args.Pass(), cookie_domain); 125 DispatchEvent(profile, keys::kOnChanged, args.Pass(), cookie_domain);
125 } 126 }
126 127
127 void ExtensionCookiesEventRouter::DispatchEvent( 128 void ExtensionCookiesEventRouter::DispatchEvent(
128 Profile* profile, const char* event_name, scoped_ptr<ListValue> event_args, 129 Profile* profile,
130 const std::string& event_name,
131 scoped_ptr<ListValue> event_args,
129 GURL& cookie_domain) { 132 GURL& cookie_domain) {
130 if (profile && profile->GetExtensionEventRouter()) { 133 EventRouter* router = profile ? profile->GetExtensionEventRouter() : NULL;
131 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 134 if (!router)
132 event_name, event_args.Pass(), profile, cookie_domain, 135 return;
133 EventFilteringInfo()); 136 router->DispatchEventToRenderers(event_name, event_args.Pass(), profile,
134 } 137 cookie_domain);
135 } 138 }
136 139
137 bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url, 140 bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url,
138 bool check_host_permissions) { 141 bool check_host_permissions) {
139 *url = GURL(url_string); 142 *url = GURL(url_string);
140 if (!url->is_valid()) { 143 if (!url->is_valid()) {
141 error_ = ExtensionErrorUtils::FormatErrorMessage( 144 error_ = ExtensionErrorUtils::FormatErrorMessage(
142 keys::kInvalidUrlError, url_string); 145 keys::kInvalidUrlError, url_string);
143 return false; 146 return false;
144 } 147 }
145 // Check against host permissions if needed. 148 // Check against host permissions if needed.
146 if (check_host_permissions && 149 if (check_host_permissions && !GetExtension()->HasHostPermission(*url)) {
147 !GetExtension()->HasHostPermission(*url)) {
148 error_ = ExtensionErrorUtils::FormatErrorMessage( 150 error_ = ExtensionErrorUtils::FormatErrorMessage(
149 keys::kNoHostPermissionsError, url->spec()); 151 keys::kNoHostPermissionsError, url->spec());
150 return false; 152 return false;
151 } 153 }
152 return true; 154 return true;
153 } 155 }
154 156
155 bool CookiesFunction::ParseStoreContext( 157 bool CookiesFunction::ParseStoreContext(
156 std::string* store_id, 158 std::string* store_id,
157 net::URLRequestContextGetter** context) { 159 net::URLRequestContextGetter** context) {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 GetAllCookiesFunction::GetAllCookiesFunction() { 262 GetAllCookiesFunction::GetAllCookiesFunction() {
261 } 263 }
262 264
263 GetAllCookiesFunction::~GetAllCookiesFunction() { 265 GetAllCookiesFunction::~GetAllCookiesFunction() {
264 } 266 }
265 267
266 bool GetAllCookiesFunction::RunImpl() { 268 bool GetAllCookiesFunction::RunImpl() {
267 parsed_args_ = GetAll::Params::Create(*args_); 269 parsed_args_ = GetAll::Params::Create(*args_);
268 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); 270 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get());
269 271
270 if (parsed_args_->details.url.get()) { 272 if (parsed_args_->details.url.get() &&
271 if (!ParseUrl(*parsed_args_->details.url, &url_, false)) 273 !ParseUrl(*parsed_args_->details.url, &url_, false)) {
272 return false; 274 return false;
273 } 275 }
274 276
275 std::string store_id = parsed_args_->details.store_id.get() ? 277 std::string store_id = parsed_args_->details.store_id.get() ?
276 *parsed_args_->details.store_id : ""; 278 *parsed_args_->details.store_id : "";
277 net::URLRequestContextGetter* store_context = NULL; 279 net::URLRequestContextGetter* store_context = NULL;
278 if (!ParseStoreContext(&store_id, &store_context)) 280 if (!ParseStoreContext(&store_id, &store_context))
279 return false; 281 return false;
280 store_context_ = store_context; 282 store_context_ = store_context;
281 if (!parsed_args_->details.store_id.get()) 283 if (!parsed_args_->details.store_id.get())
282 parsed_args_->details.store_id.reset(new std::string(store_id)); 284 parsed_args_->details.store_id.reset(new std::string(store_id));
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 } 535 }
534 results_ = GetAllCookieStores::Results::Create(cookie_stores); 536 results_ = GetAllCookieStores::Results::Create(cookie_stores);
535 return true; 537 return true;
536 } 538 }
537 539
538 void GetAllCookieStoresFunction::Run() { 540 void GetAllCookieStoresFunction::Run() {
539 SendResponse(RunImpl()); 541 SendResponse(RunImpl());
540 } 542 }
541 543
542 } // namespace extensions 544 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/cookies/cookies_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698