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

Side by Side Diff: chrome/browser/automation/automation_provider.cc

Issue 2756003: Make CookieMonster NonThreadSafe. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Address eroman's and cindylau's comments. Created 10 years, 6 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
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_cookies_api.h » ('j') | 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #include "chrome/browser/automation/automation_provider.h" 5 #include "chrome/browser/automation/automation_provider.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "app/l10n_util.h" 9 #include "app/l10n_util.h"
10 #include "app/message_box_flags.h" 10 #include "app/message_box_flags.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/file_version_info.h" 12 #include "base/file_version_info.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/keyboard_codes.h" 15 #include "base/keyboard_codes.h"
16 #include "base/message_loop.h" 16 #include "base/message_loop.h"
17 #include "base/path_service.h" 17 #include "base/path_service.h"
18 #include "base/process_util.h" 18 #include "base/process_util.h"
19 #include "base/stl_util-inl.h" 19 #include "base/stl_util-inl.h"
20 #include "base/string_util.h" 20 #include "base/string_util.h"
21 #include "base/task.h"
21 #include "base/thread.h" 22 #include "base/thread.h"
22 #include "base/utf_string_conversions.h" 23 #include "base/utf_string_conversions.h"
23 #include "base/values.h" 24 #include "base/values.h"
25 #include "base/waitable_event.h"
24 #include "chrome/app/chrome_dll_resource.h" 26 #include "chrome/app/chrome_dll_resource.h"
25 #include "chrome/app/chrome_version_info.h" 27 #include "chrome/app/chrome_version_info.h"
26 #include "chrome/browser/app_modal_dialog.h" 28 #include "chrome/browser/app_modal_dialog.h"
27 #include "chrome/browser/app_modal_dialog_queue.h" 29 #include "chrome/browser/app_modal_dialog_queue.h"
28 #include "chrome/browser/automation/automation_extension_function.h" 30 #include "chrome/browser/automation/automation_extension_function.h"
29 #include "chrome/browser/automation/automation_extension_tracker.h" 31 #include "chrome/browser/automation/automation_extension_tracker.h"
30 #include "chrome/browser/automation/automation_provider_list.h" 32 #include "chrome/browser/automation/automation_provider_list.h"
31 #include "chrome/browser/automation/automation_provider_observers.h" 33 #include "chrome/browser/automation/automation_provider_observers.h"
32 #include "chrome/browser/automation/extension_automation_constants.h" 34 #include "chrome/browser/automation/extension_automation_constants.h"
33 #include "chrome/browser/automation/extension_port_container.h" 35 #include "chrome/browser/automation/extension_port_container.h"
(...skipping 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after
1122 Browser* AutomationProvider::FindAndActivateTab( 1124 Browser* AutomationProvider::FindAndActivateTab(
1123 NavigationController* controller) { 1125 NavigationController* controller) {
1124 int tab_index; 1126 int tab_index;
1125 Browser* browser = Browser::GetBrowserForController(controller, &tab_index); 1127 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1126 if (browser) 1128 if (browser)
1127 browser->SelectTabContentsAt(tab_index, true); 1129 browser->SelectTabContentsAt(tab_index, true);
1128 1130
1129 return browser; 1131 return browser;
1130 } 1132 }
1131 1133
1134 namespace {
1135
1136 class GetCookiesTask : public Task {
1137 public:
1138 GetCookiesTask(const GURL& url,
1139 URLRequestContextGetter* context_getter,
1140 base::WaitableEvent* event,
1141 std::string* cookies)
1142 : url_(url),
1143 context_getter_(context_getter),
1144 event_(event),
1145 cookies_(cookies) {}
1146
1147 virtual void Run() {
1148 *cookies_ = context_getter_->GetCookieStore()->GetCookies(url_);
1149 event_->Signal();
1150 }
1151
1152 private:
1153 const GURL& url_;
1154 URLRequestContextGetter* const context_getter_;
1155 base::WaitableEvent* const event_;
1156 std::string* const cookies_;
1157
1158 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
1159 };
1160
1161 std::string GetCookiesForURL(
1162 const GURL& url,
1163 URLRequestContextGetter* context_getter) {
1164 std::string cookies;
1165 base::WaitableEvent event(true /* manual reset */,
1166 false /* not initially signaled */);
1167 CHECK(ChromeThread::PostTask(
1168 ChromeThread::IO, FROM_HERE,
1169 new GetCookiesTask(url, context_getter, &event, &cookies)));
1170 event.Wait();
1171 return cookies;
1172 }
1173
1174 class SetCookieTask : public Task {
1175 public:
1176 SetCookieTask(const GURL& url,
1177 const std::string& value,
1178 URLRequestContextGetter* context_getter,
1179 base::WaitableEvent* event,
1180 bool* rv)
1181 : url_(url),
1182 value_(value),
1183 context_getter_(context_getter),
1184 event_(event),
1185 rv_(rv) {}
1186
1187 virtual void Run() {
1188 *rv_ = context_getter_->GetCookieStore()->SetCookie(url_, value_);
1189 event_->Signal();
1190 }
1191
1192 private:
1193 const GURL& url_;
1194 const std::string& value_;
1195 URLRequestContextGetter* const context_getter_;
1196 base::WaitableEvent* const event_;
1197 bool* const rv_;
1198
1199 DISALLOW_COPY_AND_ASSIGN(SetCookieTask);
1200 };
1201
1202 bool SetCookieForURL(
1203 const GURL& url,
1204 const std::string& value,
1205 URLRequestContextGetter* context_getter) {
1206 base::WaitableEvent event(true /* manual reset */,
1207 false /* not initially signaled */);
1208 bool rv = false;
1209 CHECK(ChromeThread::PostTask(
1210 ChromeThread::IO, FROM_HERE,
1211 new SetCookieTask(url, value, context_getter, &event, &rv)));
1212 event.Wait();
1213 return rv;
1214 }
1215
1216 class DeleteCookieTask : public Task {
1217 public:
1218 DeleteCookieTask(const GURL& url,
1219 const std::string& name,
1220 const scoped_refptr<URLRequestContextGetter>& context_getter)
1221 : url_(url),
1222 name_(name),
1223 context_getter_(context_getter) {}
1224
1225 virtual void Run() {
1226 net::CookieStore* cookie_store = context_getter_->GetCookieStore();
1227 cookie_store->DeleteCookie(url_, name_);
1228 }
1229
1230 private:
1231 const GURL url_;
1232 const std::string name_;
1233 const scoped_refptr<URLRequestContextGetter> context_getter_;
1234
1235 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
1236 };
1237
1238 } // namespace
1239
1132 void AutomationProvider::GetCookies(const GURL& url, int handle, 1240 void AutomationProvider::GetCookies(const GURL& url, int handle,
1133 int* value_size, 1241 int* value_size,
1134 std::string* value) { 1242 std::string* value) {
1135 *value_size = -1; 1243 *value_size = -1;
1136 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) { 1244 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1137 NavigationController* tab = tab_tracker_->GetResource(handle); 1245 NavigationController* tab = tab_tracker_->GetResource(handle);
1138 1246
1139 // Since we are running on the UI thread don't call GetURLRequestContext(). 1247 // Since we are running on the UI thread don't call GetURLRequestContext().
1140 scoped_refptr<URLRequestContextGetter> request_context = 1248 scoped_refptr<URLRequestContextGetter> request_context =
1141 tab->tab_contents()->request_context(); 1249 tab->tab_contents()->request_context();
1142 if (!request_context.get()) 1250 if (!request_context.get())
1143 request_context = tab->profile()->GetRequestContext(); 1251 request_context = tab->profile()->GetRequestContext();
1144 1252
1145 net::CookieStore* cookie_store = request_context->GetCookieStore(); 1253 *value = GetCookiesForURL(url, request_context.get());
1146
1147 *value = cookie_store->GetCookies(url);
1148 *value_size = static_cast<int>(value->size()); 1254 *value_size = static_cast<int>(value->size());
1149 } 1255 }
1150 } 1256 }
1151 1257
1152 void AutomationProvider::SetCookie(const GURL& url, 1258 void AutomationProvider::SetCookie(const GURL& url,
1153 const std::string value, 1259 const std::string value,
1154 int handle, 1260 int handle,
1155 int* response_value) { 1261 int* response_value) {
1156 *response_value = -1; 1262 *response_value = -1;
1157 1263
1158 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) { 1264 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1159 NavigationController* tab = tab_tracker_->GetResource(handle); 1265 NavigationController* tab = tab_tracker_->GetResource(handle);
1160 1266
1161 scoped_refptr<URLRequestContextGetter> request_context = 1267 scoped_refptr<URLRequestContextGetter> request_context =
1162 tab->tab_contents()->request_context(); 1268 tab->tab_contents()->request_context();
1163 if (!request_context.get()) 1269 if (!request_context.get())
1164 request_context = tab->profile()->GetRequestContext(); 1270 request_context = tab->profile()->GetRequestContext();
1165 1271
1166 // Since we are running on the UI thread don't call GetURLRequestContext(). 1272 if (SetCookieForURL(url, value, request_context.get()))
1167 scoped_refptr<net::CookieStore> cookie_store =
1168 request_context->GetCookieStore();
1169
1170 if (cookie_store->SetCookie(url, value))
1171 *response_value = 1; 1273 *response_value = 1;
1172 } 1274 }
1173 } 1275 }
1174 1276
1175 void AutomationProvider::DeleteCookie(const GURL& url, 1277 void AutomationProvider::DeleteCookie(const GURL& url,
1176 const std::string& cookie_name, 1278 const std::string& cookie_name,
1177 int handle, bool* success) { 1279 int handle, bool* success) {
1178 *success = false; 1280 *success = false;
1179 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) { 1281 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1180 NavigationController* tab = tab_tracker_->GetResource(handle); 1282 NavigationController* tab = tab_tracker_->GetResource(handle);
1181 net::CookieStore* cookie_store = 1283 ChromeThread::PostTask(
1182 tab->profile()->GetRequestContext()->GetCookieStore(); 1284 ChromeThread::IO, FROM_HERE,
1183 cookie_store->DeleteCookie(url, cookie_name); 1285 new DeleteCookieTask(url, cookie_name,
1286 tab->profile()->GetRequestContext()));
1184 *success = true; 1287 *success = true;
1185 } 1288 }
1186 } 1289 }
1187 1290
1188 void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) { 1291 void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1189 *success = false; 1292 *success = false;
1190 if (tab_tracker_->ContainsHandle(handle)) { 1293 if (tab_tracker_->ContainsHandle(handle)) {
1191 NavigationController* tab = tab_tracker_->GetResource(handle); 1294 NavigationController* tab = tab_tracker_->GetResource(handle);
1192 // Return what the user would see in the location bar. 1295 // Return what the user would see in the location bar.
1193 *url = tab->GetActiveEntry()->virtual_url(); 1296 *url = tab->GetActiveEntry()->virtual_url();
(...skipping 2339 matching lines...) Expand 10 before | Expand all | Expand 10 after
3533 } 3636 }
3534 3637
3535 void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) { 3638 void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3536 NOTIMPLEMENTED(); 3639 NOTIMPLEMENTED();
3537 } 3640 }
3538 #endif // !defined(TOOLKIT_VIEWS) 3641 #endif // !defined(TOOLKIT_VIEWS)
3539 3642
3540 void AutomationProvider::ResetToDefaultTheme() { 3643 void AutomationProvider::ResetToDefaultTheme() {
3541 profile_->ClearTheme(); 3644 profile_->ClearTheme();
3542 } 3645 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_cookies_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698