Chromium Code Reviews| 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 const Extension* installed_app = NULL; | |
|
Charlie Reis
2011/04/21 00:25:12
Don't need this line.
jam
2011/04/21 06:52:19
Done.
| |
| 111 // Since we may be on the UI thread don't call GetURLRequestContext(). | |
| 112 // Get the request context specific to the current TabContents and app. | |
| 113 return contents->profile()->GetRequestContextForPossibleApp( | |
| 114 contents->render_view_host()->process()->id()); | |
| 115 } | |
| 116 | |
| 108 void GetCookies(const GURL& url, | 117 void GetCookies(const GURL& url, |
| 109 TabContents* contents, | 118 TabContents* contents, |
| 110 int* value_size, | 119 int* value_size, |
| 111 std::string* value) { | 120 std::string* value) { |
| 112 *value_size = -1; | 121 *value_size = -1; |
| 113 if (url.is_valid() && contents) { | 122 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 = | 123 scoped_refptr<net::URLRequestContextGetter> context_getter = |
| 119 contents->profile()->GetRequestContextForPossibleApp(installed_app); | 124 GetRequestContext(contents); |
| 120 | |
| 121 base::WaitableEvent event(true /* manual reset */, | 125 base::WaitableEvent event(true /* manual reset */, |
| 122 false /* not initially signaled */); | 126 false /* not initially signaled */); |
| 123 CHECK(BrowserThread::PostTask( | 127 CHECK(BrowserThread::PostTask( |
| 124 BrowserThread::IO, FROM_HERE, | 128 BrowserThread::IO, FROM_HERE, |
| 125 NewRunnableFunction(&GetCookiesOnIOThread, | 129 NewRunnableFunction(&GetCookiesOnIOThread, |
| 126 url, context_getter, &event, value))); | 130 url, context_getter, &event, value))); |
| 127 event.Wait(); | 131 event.Wait(); |
| 128 | 132 |
| 129 *value_size = static_cast<int>(value->size()); | 133 *value_size = static_cast<int>(value->size()); |
| 130 } | 134 } |
| 131 } | 135 } |
| 132 | 136 |
| 133 void SetCookie(const GURL& url, | 137 void SetCookie(const GURL& url, |
| 134 const std::string& value, | 138 const std::string& value, |
| 135 TabContents* contents, | 139 TabContents* contents, |
| 136 int* response_value) { | 140 int* response_value) { |
| 137 *response_value = -1; | 141 *response_value = -1; |
| 138 | 142 |
| 139 if (url.is_valid() && contents) { | 143 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 = | 144 scoped_refptr<net::URLRequestContextGetter> context_getter = |
| 145 contents->profile()->GetRequestContextForPossibleApp(installed_app); | 145 GetRequestContext(contents); |
| 146 | |
| 147 base::WaitableEvent event(true /* manual reset */, | 146 base::WaitableEvent event(true /* manual reset */, |
| 148 false /* not initially signaled */); | 147 false /* not initially signaled */); |
| 149 bool success = false; | 148 bool success = false; |
| 150 CHECK(BrowserThread::PostTask( | 149 CHECK(BrowserThread::PostTask( |
| 151 BrowserThread::IO, FROM_HERE, | 150 BrowserThread::IO, FROM_HERE, |
| 152 NewRunnableFunction(&SetCookieOnIOThread, | 151 NewRunnableFunction(&SetCookieOnIOThread, |
| 153 url, value, context_getter, &event, | 152 url, value, context_getter, &event, |
| 154 &success))); | 153 &success))); |
| 155 event.Wait(); | 154 event.Wait(); |
| 156 if (success) | 155 if (success) |
| 157 *response_value = 1; | 156 *response_value = 1; |
| 158 } | 157 } |
| 159 } | 158 } |
| 160 | 159 |
| 161 void DeleteCookie(const GURL& url, | 160 void DeleteCookie(const GURL& url, |
| 162 const std::string& cookie_name, | 161 const std::string& cookie_name, |
| 163 TabContents* contents, | 162 TabContents* contents, |
| 164 bool* success) { | 163 bool* success) { |
| 165 *success = false; | 164 *success = false; |
| 166 if (url.is_valid() && contents) { | 165 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 = | 166 scoped_refptr<net::URLRequestContextGetter> context_getter = |
| 172 contents->profile()->GetRequestContextForPossibleApp(installed_app); | 167 GetRequestContext(contents); |
| 173 | |
| 174 base::WaitableEvent event(true /* manual reset */, | 168 base::WaitableEvent event(true /* manual reset */, |
| 175 false /* not initially signaled */); | 169 false /* not initially signaled */); |
| 176 CHECK(BrowserThread::PostTask( | 170 CHECK(BrowserThread::PostTask( |
| 177 BrowserThread::IO, FROM_HERE, | 171 BrowserThread::IO, FROM_HERE, |
| 178 NewRunnableFunction(&DeleteCookieOnIOThread, | 172 NewRunnableFunction(&DeleteCookieOnIOThread, |
| 179 url, cookie_name, context_getter, &event))); | 173 url, cookie_name, context_getter, &event))); |
| 180 event.Wait(); | 174 event.Wait(); |
| 181 *success = true; | 175 *success = true; |
| 182 } | 176 } |
| 183 } | 177 } |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 event.Wait(); | 336 event.Wait(); |
| 343 | 337 |
| 344 if (!success) { | 338 if (!success) { |
| 345 reply.SendError("Could not set the cookie"); | 339 reply.SendError("Could not set the cookie"); |
| 346 return; | 340 return; |
| 347 } | 341 } |
| 348 reply.SendSuccess(NULL); | 342 reply.SendSuccess(NULL); |
| 349 } | 343 } |
| 350 | 344 |
| 351 } // namespace automation_util | 345 } // namespace automation_util |
| OLD | NEW |