OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/automation/automation_util.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/automation/automation_provider_json.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/net/url_request_context_getter.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "content/browser/browser_thread.h" |
| 15 #include "content/browser/renderer_host/render_view_host.h" |
| 16 #include "net/base/cookie_store.h" |
| 17 #include "net/url_request/url_request_context.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 void GetCookiesOnIOThread( |
| 22 const GURL& url, |
| 23 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 24 base::WaitableEvent* event, |
| 25 std::string* cookies) { |
| 26 *cookies = context_getter->GetCookieStore()->GetCookies(url); |
| 27 event->Signal(); |
| 28 } |
| 29 |
| 30 void SetCookieOnIOThread( |
| 31 const GURL& url, |
| 32 const std::string& value, |
| 33 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 34 base::WaitableEvent* event, |
| 35 bool* success) { |
| 36 *success = context_getter->GetCookieStore()->SetCookie(url, value); |
| 37 event->Signal(); |
| 38 } |
| 39 |
| 40 void DeleteCookieOnIOThread( |
| 41 const GURL& url, |
| 42 const std::string& name, |
| 43 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 44 base::WaitableEvent* event) { |
| 45 context_getter->GetCookieStore()->DeleteCookie(url, name); |
| 46 event->Signal(); |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 namespace automation_util { |
| 52 |
| 53 void GetCookies(const GURL& url, |
| 54 TabContents* contents, |
| 55 int* value_size, |
| 56 std::string* value) { |
| 57 *value_size = -1; |
| 58 if (url.is_valid() && contents) { |
| 59 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 60 // Get the request context specific to the current TabContents and app. |
| 61 const Extension* installed_app = |
| 62 contents->render_view_host()->installed_app(); |
| 63 scoped_refptr<URLRequestContextGetter> context_getter = |
| 64 contents->profile()->GetRequestContextForPossibleApp(installed_app); |
| 65 |
| 66 base::WaitableEvent event(true /* manual reset */, |
| 67 false /* not initially signaled */); |
| 68 CHECK(BrowserThread::PostTask( |
| 69 BrowserThread::IO, FROM_HERE, |
| 70 NewRunnableFunction(&GetCookiesOnIOThread, |
| 71 url, context_getter, &event, value))); |
| 72 event.Wait(); |
| 73 |
| 74 *value_size = static_cast<int>(value->size()); |
| 75 } |
| 76 } |
| 77 |
| 78 void SetCookie(const GURL& url, |
| 79 const std::string value, |
| 80 TabContents* contents, |
| 81 int* response_value) { |
| 82 *response_value = -1; |
| 83 |
| 84 if (url.is_valid() && contents) { |
| 85 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 86 // Get the request context specific to the current TabContents and app. |
| 87 const Extension* installed_app = |
| 88 contents->render_view_host()->installed_app(); |
| 89 scoped_refptr<URLRequestContextGetter> context_getter = |
| 90 contents->profile()->GetRequestContextForPossibleApp(installed_app); |
| 91 |
| 92 base::WaitableEvent event(true /* manual reset */, |
| 93 false /* not initially signaled */); |
| 94 bool success = false; |
| 95 CHECK(BrowserThread::PostTask( |
| 96 BrowserThread::IO, FROM_HERE, |
| 97 NewRunnableFunction(&SetCookieOnIOThread, |
| 98 url, value, context_getter, &event, |
| 99 &success))); |
| 100 event.Wait(); |
| 101 if (success) |
| 102 *response_value = 1; |
| 103 } |
| 104 } |
| 105 |
| 106 void DeleteCookie(const GURL& url, |
| 107 const std::string& cookie_name, |
| 108 TabContents* contents, |
| 109 bool* success) { |
| 110 *success = false; |
| 111 if (url.is_valid() && contents) { |
| 112 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 113 // Get the request context specific to the current TabContents and app. |
| 114 const Extension* installed_app = |
| 115 contents->render_view_host()->installed_app(); |
| 116 scoped_refptr<URLRequestContextGetter> context_getter = |
| 117 contents->profile()->GetRequestContextForPossibleApp(installed_app); |
| 118 |
| 119 base::WaitableEvent event(true /* manual reset */, |
| 120 false /* not initially signaled */); |
| 121 CHECK(BrowserThread::PostTask( |
| 122 BrowserThread::IO, FROM_HERE, |
| 123 NewRunnableFunction(&DeleteCookieOnIOThread, |
| 124 url, cookie_name, context_getter, &event))); |
| 125 event.Wait(); |
| 126 *success = true; |
| 127 } |
| 128 } |
| 129 |
| 130 void GetCookiesJSON(AutomationProvider* provider, |
| 131 DictionaryValue* args, |
| 132 IPC::Message* reply_message) { |
| 133 AutomationJSONReply reply(provider, reply_message); |
| 134 Browser* browser; |
| 135 std::string error; |
| 136 if (!GetBrowserFromJSONArgs(args, &browser, &error)) { |
| 137 reply.SendError(error); |
| 138 return; |
| 139 } |
| 140 std::string url; |
| 141 if (!args->GetString("url", &url)) { |
| 142 reply.SendError("'url' missing or invalid"); |
| 143 return; |
| 144 } |
| 145 |
| 146 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 147 scoped_refptr<URLRequestContextGetter> context_getter = |
| 148 browser->profile()->GetRequestContext(); |
| 149 |
| 150 std::string cookies; |
| 151 base::WaitableEvent event(true /* manual reset */, |
| 152 false /* not initially signaled */); |
| 153 Task* task = NewRunnableFunction( |
| 154 &GetCookiesOnIOThread, |
| 155 GURL(url), context_getter, &event, &cookies); |
| 156 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { |
| 157 reply.SendError("Couldn't post task to get the cookies"); |
| 158 return; |
| 159 } |
| 160 event.Wait(); |
| 161 |
| 162 DictionaryValue dict; |
| 163 dict.SetString("cookies", cookies); |
| 164 reply.SendSuccess(&dict); |
| 165 } |
| 166 |
| 167 void DeleteCookieJSON(AutomationProvider* provider, |
| 168 DictionaryValue* args, |
| 169 IPC::Message* reply_message) { |
| 170 AutomationJSONReply reply(provider, reply_message); |
| 171 Browser* browser; |
| 172 std::string error; |
| 173 if (!GetBrowserFromJSONArgs(args, &browser, &error)) { |
| 174 reply.SendError(error); |
| 175 return; |
| 176 } |
| 177 std::string url, name; |
| 178 if (!args->GetString("url", &url)) { |
| 179 reply.SendError("'url' missing or invalid"); |
| 180 return; |
| 181 } |
| 182 if (!args->GetString("name", &name)) { |
| 183 reply.SendError("'name' missing or invalid"); |
| 184 return; |
| 185 } |
| 186 |
| 187 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 188 scoped_refptr<URLRequestContextGetter> context_getter = |
| 189 browser->profile()->GetRequestContext(); |
| 190 |
| 191 base::WaitableEvent event(true /* manual reset */, |
| 192 false /* not initially signaled */); |
| 193 Task* task = NewRunnableFunction( |
| 194 &DeleteCookieOnIOThread, |
| 195 GURL(url), name, context_getter, &event); |
| 196 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { |
| 197 reply.SendError("Couldn't post task to delete the cookie"); |
| 198 return; |
| 199 } |
| 200 event.Wait(); |
| 201 reply.SendSuccess(NULL); |
| 202 } |
| 203 |
| 204 void SetCookieJSON(AutomationProvider* provider, |
| 205 DictionaryValue* args, |
| 206 IPC::Message* reply_message) { |
| 207 AutomationJSONReply reply(provider, reply_message); |
| 208 Browser* browser; |
| 209 std::string error; |
| 210 if (!GetBrowserFromJSONArgs(args, &browser, &error)) { |
| 211 reply.SendError(error); |
| 212 return; |
| 213 } |
| 214 std::string url, cookie; |
| 215 if (!args->GetString("url", &url)) { |
| 216 reply.SendError("'url' missing or invalid"); |
| 217 return; |
| 218 } |
| 219 if (!args->GetString("cookie", &cookie)) { |
| 220 reply.SendError("'cookie' missing or invalid"); |
| 221 return; |
| 222 } |
| 223 |
| 224 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 225 scoped_refptr<URLRequestContextGetter> context_getter = |
| 226 browser->profile()->GetRequestContext(); |
| 227 |
| 228 base::WaitableEvent event(true /* manual reset */, |
| 229 false /* not initially signaled */); |
| 230 bool success = false; |
| 231 Task* task = NewRunnableFunction( |
| 232 &SetCookieOnIOThread, |
| 233 GURL(url), cookie, context_getter, &event, &success); |
| 234 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { |
| 235 reply.SendError("Couldn't post task to set the cookie"); |
| 236 return; |
| 237 } |
| 238 event.Wait(); |
| 239 |
| 240 if (!success) { |
| 241 reply.SendError("Could not set the cookie"); |
| 242 return; |
| 243 } |
| 244 reply.SendSuccess(NULL); |
| 245 } |
| 246 |
| 247 } // namespace automation_util |
OLD | NEW |