OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_util.h" | 5 #include "chrome/browser/automation/automation_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/automation/automation_provider.h" | 12 #include "chrome/browser/automation/automation_provider.h" |
13 #include "chrome/browser/automation/automation_provider_json.h" | 13 #include "chrome/browser/automation/automation_provider_json.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_list.h" | 17 #include "chrome/browser/ui/browser_list.h" |
17 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
18 #include "content/browser/browser_thread.h" | 19 #include "content/browser/browser_thread.h" |
19 #include "content/browser/renderer_host/browser_render_process_host.h" | 20 #include "content/browser/renderer_host/render_process_host.h" |
20 #include "content/browser/renderer_host/render_view_host.h" | 21 #include "content/browser/renderer_host/render_view_host.h" |
21 #include "content/browser/tab_contents/tab_contents.h" | 22 #include "content/browser/tab_contents/tab_contents.h" |
22 #include "net/base/cookie_monster.h" | 23 #include "net/base/cookie_monster.h" |
23 #include "net/base/cookie_store.h" | 24 #include "net/base/cookie_store.h" |
24 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
25 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 void GetCookiesOnIOThread( | 30 void GetCookiesOnIOThread( |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 99 |
99 TabContents* GetTabContentsAt(int browser_index, int tab_index) { | 100 TabContents* GetTabContentsAt(int browser_index, int tab_index) { |
100 if (tab_index < 0) | 101 if (tab_index < 0) |
101 return NULL; | 102 return NULL; |
102 Browser* browser = GetBrowserAt(browser_index); | 103 Browser* browser = GetBrowserAt(browser_index); |
103 if (!browser || tab_index >= browser->tab_count()) | 104 if (!browser || tab_index >= browser->tab_count()) |
104 return NULL; | 105 return NULL; |
105 return browser->GetTabContentsAt(tab_index); | 106 return browser->GetTabContentsAt(tab_index); |
106 } | 107 } |
107 | 108 |
| 109 net::URLRequestContextGetter* GetRequestContext(TabContents* contents) { |
| 110 // Since we may be on the UI thread don't call GetURLRequestContext(). |
| 111 // Get the request context specific to the current TabContents and app. |
| 112 return contents->profile()->GetRequestContextForRenderProcess( |
| 113 contents->render_view_host()->process()->id()); |
| 114 } |
| 115 |
108 void GetCookies(const GURL& url, | 116 void GetCookies(const GURL& url, |
109 TabContents* contents, | 117 TabContents* contents, |
110 int* value_size, | 118 int* value_size, |
111 std::string* value) { | 119 std::string* value) { |
112 *value_size = -1; | 120 *value_size = -1; |
113 if (url.is_valid() && contents) { | 121 if (url.is_valid() && contents) { |
114 // Since we may be on the UI thread don't call GetURLRequestContext(). | |
115 // Get the request context specific to the current TabContents and app. | |
116 const Extension* installed_app = static_cast<BrowserRenderProcessHost*>( | |
117 contents->render_view_host()->process())->installed_app(); | |
118 scoped_refptr<net::URLRequestContextGetter> context_getter = | 122 scoped_refptr<net::URLRequestContextGetter> context_getter = |
119 contents->profile()->GetRequestContextForPossibleApp(installed_app); | 123 GetRequestContext(contents); |
120 | |
121 base::WaitableEvent event(true /* manual reset */, | 124 base::WaitableEvent event(true /* manual reset */, |
122 false /* not initially signaled */); | 125 false /* not initially signaled */); |
123 CHECK(BrowserThread::PostTask( | 126 CHECK(BrowserThread::PostTask( |
124 BrowserThread::IO, FROM_HERE, | 127 BrowserThread::IO, FROM_HERE, |
125 NewRunnableFunction(&GetCookiesOnIOThread, | 128 NewRunnableFunction(&GetCookiesOnIOThread, |
126 url, context_getter, &event, value))); | 129 url, context_getter, &event, value))); |
127 event.Wait(); | 130 event.Wait(); |
128 | 131 |
129 *value_size = static_cast<int>(value->size()); | 132 *value_size = static_cast<int>(value->size()); |
130 } | 133 } |
131 } | 134 } |
132 | 135 |
133 void SetCookie(const GURL& url, | 136 void SetCookie(const GURL& url, |
134 const std::string& value, | 137 const std::string& value, |
135 TabContents* contents, | 138 TabContents* contents, |
136 int* response_value) { | 139 int* response_value) { |
137 *response_value = -1; | 140 *response_value = -1; |
138 | 141 |
139 if (url.is_valid() && contents) { | 142 if (url.is_valid() && contents) { |
140 // Since we may be on the UI thread don't call GetURLRequestContext(). | |
141 // Get the request context specific to the current TabContents and app. | |
142 const Extension* installed_app = static_cast<BrowserRenderProcessHost*>( | |
143 contents->render_view_host()->process())->installed_app(); | |
144 scoped_refptr<net::URLRequestContextGetter> context_getter = | 143 scoped_refptr<net::URLRequestContextGetter> context_getter = |
145 contents->profile()->GetRequestContextForPossibleApp(installed_app); | 144 GetRequestContext(contents); |
146 | |
147 base::WaitableEvent event(true /* manual reset */, | 145 base::WaitableEvent event(true /* manual reset */, |
148 false /* not initially signaled */); | 146 false /* not initially signaled */); |
149 bool success = false; | 147 bool success = false; |
150 CHECK(BrowserThread::PostTask( | 148 CHECK(BrowserThread::PostTask( |
151 BrowserThread::IO, FROM_HERE, | 149 BrowserThread::IO, FROM_HERE, |
152 NewRunnableFunction(&SetCookieOnIOThread, | 150 NewRunnableFunction(&SetCookieOnIOThread, |
153 url, value, context_getter, &event, | 151 url, value, context_getter, &event, |
154 &success))); | 152 &success))); |
155 event.Wait(); | 153 event.Wait(); |
156 if (success) | 154 if (success) |
157 *response_value = 1; | 155 *response_value = 1; |
158 } | 156 } |
159 } | 157 } |
160 | 158 |
161 void DeleteCookie(const GURL& url, | 159 void DeleteCookie(const GURL& url, |
162 const std::string& cookie_name, | 160 const std::string& cookie_name, |
163 TabContents* contents, | 161 TabContents* contents, |
164 bool* success) { | 162 bool* success) { |
165 *success = false; | 163 *success = false; |
166 if (url.is_valid() && contents) { | 164 if (url.is_valid() && contents) { |
167 // Since we may be on the UI thread don't call GetURLRequestContext(). | |
168 // Get the request context specific to the current TabContents and app. | |
169 const Extension* installed_app = static_cast<BrowserRenderProcessHost*>( | |
170 contents->render_view_host()->process())->installed_app(); | |
171 scoped_refptr<net::URLRequestContextGetter> context_getter = | 165 scoped_refptr<net::URLRequestContextGetter> context_getter = |
172 contents->profile()->GetRequestContextForPossibleApp(installed_app); | 166 GetRequestContext(contents); |
173 | |
174 base::WaitableEvent event(true /* manual reset */, | 167 base::WaitableEvent event(true /* manual reset */, |
175 false /* not initially signaled */); | 168 false /* not initially signaled */); |
176 CHECK(BrowserThread::PostTask( | 169 CHECK(BrowserThread::PostTask( |
177 BrowserThread::IO, FROM_HERE, | 170 BrowserThread::IO, FROM_HERE, |
178 NewRunnableFunction(&DeleteCookieOnIOThread, | 171 NewRunnableFunction(&DeleteCookieOnIOThread, |
179 url, cookie_name, context_getter, &event))); | 172 url, cookie_name, context_getter, &event))); |
180 event.Wait(); | 173 event.Wait(); |
181 *success = true; | 174 *success = true; |
182 } | 175 } |
183 } | 176 } |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 event.Wait(); | 335 event.Wait(); |
343 | 336 |
344 if (!success) { | 337 if (!success) { |
345 reply.SendError("Could not set the cookie"); | 338 reply.SendError("Could not set the cookie"); |
346 return; | 339 return; |
347 } | 340 } |
348 reply.SendSuccess(NULL); | 341 reply.SendSuccess(NULL); |
349 } | 342 } |
350 | 343 |
351 } // namespace automation_util | 344 } // namespace automation_util |
OLD | NEW |