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_cookie_util.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/net/url_request_context_getter.h" |
| 11 #include "content/browser/browser_thread.h" |
| 12 #include "content/browser/renderer_host/render_view_host.h" |
| 13 #include "net/base/cookie_store.h" |
| 14 |
| 15 namespace automation_cookie_util { |
| 16 |
| 17 void GetCookiesOnIOThread( |
| 18 const GURL& url, |
| 19 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 20 base::WaitableEvent* event, |
| 21 std::string* cookies) { |
| 22 *cookies = context_getter->GetCookieStore()->GetCookies(url); |
| 23 event->Signal(); |
| 24 } |
| 25 |
| 26 void SetCookieOnIOThread( |
| 27 const GURL& url, |
| 28 const std::string& value, |
| 29 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 30 base::WaitableEvent* event, |
| 31 bool* success) { |
| 32 *success = context_getter->GetCookieStore()->SetCookie(url, value); |
| 33 event->Signal(); |
| 34 } |
| 35 |
| 36 void DeleteCookieOnIOThread( |
| 37 const GURL& url, |
| 38 const std::string& name, |
| 39 const scoped_refptr<URLRequestContextGetter>& context_getter, |
| 40 base::WaitableEvent* event) { |
| 41 context_getter->GetCookieStore()->DeleteCookie(url, name); |
| 42 event->Signal(); |
| 43 } |
| 44 |
| 45 void GetCookies(const GURL& url, |
| 46 TabContents* contents, |
| 47 int* value_size, |
| 48 std::string* value) { |
| 49 *value_size = -1; |
| 50 if (url.is_valid() && contents) { |
| 51 // Since we are on the UI thread don't call GetURLRequestContext(). |
| 52 // Get the request context specific to the current TabContents and app. |
| 53 const Extension* installed_app = |
| 54 contents->render_view_host()->installed_app(); |
| 55 scoped_refptr<URLRequestContextGetter> context_getter = |
| 56 contents->profile()->GetRequestContextForPossibleApp(installed_app); |
| 57 |
| 58 base::WaitableEvent event(true /* manual reset */, |
| 59 false /* not initially signaled */); |
| 60 CHECK(BrowserThread::PostTask( |
| 61 BrowserThread::IO, FROM_HERE, |
| 62 NewRunnableFunction(&GetCookiesOnIOThread, |
| 63 url, context_getter, &event, value))); |
| 64 event.Wait(); |
| 65 |
| 66 *value_size = static_cast<int>(value->size()); |
| 67 } |
| 68 } |
| 69 |
| 70 void SetCookie(const GURL& url, |
| 71 const std::string value, |
| 72 TabContents* contents, |
| 73 int* response_value) { |
| 74 *response_value = -1; |
| 75 |
| 76 if (url.is_valid() && contents) { |
| 77 // Since we are running on the UI thread don't call GetURLRequestContext(). |
| 78 // Get the request context specific to the current TabContents and app. |
| 79 const Extension* installed_app = |
| 80 contents->render_view_host()->installed_app(); |
| 81 scoped_refptr<URLRequestContextGetter> context_getter = |
| 82 contents->profile()->GetRequestContextForPossibleApp(installed_app); |
| 83 |
| 84 base::WaitableEvent event(true /* manual reset */, |
| 85 false /* not initially signaled */); |
| 86 bool success = false; |
| 87 CHECK(BrowserThread::PostTask( |
| 88 BrowserThread::IO, FROM_HERE, |
| 89 NewRunnableFunction(&SetCookieOnIOThread, |
| 90 url, value, context_getter, &event, |
| 91 &success))); |
| 92 event.Wait(); |
| 93 if (success) |
| 94 *response_value = 1; |
| 95 } |
| 96 } |
| 97 |
| 98 void DeleteCookie(const GURL& url, |
| 99 const std::string& cookie_name, |
| 100 TabContents* contents, |
| 101 bool* success) { |
| 102 *success = false; |
| 103 if (url.is_valid() && contents) { |
| 104 // Since we are running on the UI thread don't call GetURLRequestContext(). |
| 105 // Get the request context specific to the current TabContents and app. |
| 106 const Extension* installed_app = |
| 107 contents->render_view_host()->installed_app(); |
| 108 scoped_refptr<URLRequestContextGetter> context_getter = |
| 109 contents->profile()->GetRequestContextForPossibleApp(installed_app); |
| 110 |
| 111 base::WaitableEvent event(true /* manual reset */, |
| 112 false /* not initially signaled */); |
| 113 CHECK(BrowserThread::PostTask( |
| 114 BrowserThread::IO, FROM_HERE, |
| 115 NewRunnableFunction(&DeleteCookieOnIOThread, |
| 116 url, cookie_name, context_getter, &event))); |
| 117 event.Wait(); |
| 118 *success = true; |
| 119 } |
| 120 } |
| 121 |
| 122 } // namespace automation_cookie_util |
OLD | NEW |