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