diff options
Diffstat (limited to 'chrome/browser/automation')
-rw-r--r-- | chrome/browser/automation/automation_util.cc | 248 | ||||
-rw-r--r-- | chrome/browser/automation/automation_util.h | 72 | ||||
-rw-r--r-- | chrome/browser/automation/testing_automation_provider.cc | 195 |
3 files changed, 333 insertions, 182 deletions
diff --git a/chrome/browser/automation/automation_util.cc b/chrome/browser/automation/automation_util.cc new file mode 100644 index 0000000..10f5da4 --- /dev/null +++ b/chrome/browser/automation/automation_util.cc @@ -0,0 +1,248 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/automation/automation_util.h" + +#include <string> + +#include "base/values.h" +#include "chrome/browser/automation/automation_provider_json.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/renderer_host/browser_render_process_host.h" +#include "chrome/common/net/url_request_context_getter.h" +#include "chrome/browser/ui/browser.h" +#include "content/browser/browser_thread.h" +#include "content/browser/renderer_host/render_view_host.h" +#include "net/base/cookie_store.h" +#include "net/url_request/url_request_context.h" + +namespace { + +void GetCookiesOnIOThread( + const GURL& url, + const scoped_refptr<URLRequestContextGetter>& context_getter, + base::WaitableEvent* event, + std::string* cookies) { + *cookies = context_getter->GetCookieStore()->GetCookies(url); + event->Signal(); +} + +void SetCookieOnIOThread( + const GURL& url, + const std::string& value, + const scoped_refptr<URLRequestContextGetter>& context_getter, + base::WaitableEvent* event, + bool* success) { + *success = context_getter->GetCookieStore()->SetCookie(url, value); + event->Signal(); +} + +void DeleteCookieOnIOThread( + const GURL& url, + const std::string& name, + const scoped_refptr<URLRequestContextGetter>& context_getter, + base::WaitableEvent* event) { + context_getter->GetCookieStore()->DeleteCookie(url, name); + event->Signal(); +} + +} // namespace + +namespace automation_util { + +void GetCookies(const GURL& url, + TabContents* contents, + int* value_size, + std::string* value) { + *value_size = -1; + if (url.is_valid() && contents) { + // Since we may be on the UI thread don't call GetURLRequestContext(). + // Get the request context specific to the current TabContents and app. + const Extension* installed_app = static_cast<BrowserRenderProcessHost*>( + contents->render_view_host()->process())->installed_app(); + scoped_refptr<URLRequestContextGetter> context_getter = + contents->profile()->GetRequestContextForPossibleApp(installed_app); + + base::WaitableEvent event(true /* manual reset */, + false /* not initially signaled */); + CHECK(BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + NewRunnableFunction(&GetCookiesOnIOThread, + url, context_getter, &event, value))); + event.Wait(); + + *value_size = static_cast<int>(value->size()); + } +} + +void SetCookie(const GURL& url, + const std::string value, + TabContents* contents, + int* response_value) { + *response_value = -1; + + if (url.is_valid() && contents) { + // Since we may be on the UI thread don't call GetURLRequestContext(). + // Get the request context specific to the current TabContents and app. + const Extension* installed_app = static_cast<BrowserRenderProcessHost*>( + contents->render_view_host()->process())->installed_app(); + scoped_refptr<URLRequestContextGetter> context_getter = + contents->profile()->GetRequestContextForPossibleApp(installed_app); + + base::WaitableEvent event(true /* manual reset */, + false /* not initially signaled */); + bool success = false; + CHECK(BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + NewRunnableFunction(&SetCookieOnIOThread, + url, value, context_getter, &event, + &success))); + event.Wait(); + if (success) + *response_value = 1; + } +} + +void DeleteCookie(const GURL& url, + const std::string& cookie_name, + TabContents* contents, + bool* success) { + *success = false; + if (url.is_valid() && contents) { + // Since we may be on the UI thread don't call GetURLRequestContext(). + // Get the request context specific to the current TabContents and app. + const Extension* installed_app = static_cast<BrowserRenderProcessHost*>( + contents->render_view_host()->process())->installed_app(); + scoped_refptr<URLRequestContextGetter> context_getter = + contents->profile()->GetRequestContextForPossibleApp(installed_app); + + base::WaitableEvent event(true /* manual reset */, + false /* not initially signaled */); + CHECK(BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + NewRunnableFunction(&DeleteCookieOnIOThread, + url, cookie_name, context_getter, &event))); + event.Wait(); + *success = true; + } +} + +void GetCookiesJSON(AutomationProvider* provider, + DictionaryValue* args, + IPC::Message* reply_message) { + AutomationJSONReply reply(provider, reply_message); + Browser* browser; + std::string error; + if (!GetBrowserFromJSONArgs(args, &browser, &error)) { + reply.SendError(error); + return; + } + std::string url; + if (!args->GetString("url", &url)) { + reply.SendError("'url' missing or invalid"); + return; + } + + // Since we may be on the UI thread don't call GetURLRequestContext(). + scoped_refptr<URLRequestContextGetter> context_getter = + browser->profile()->GetRequestContext(); + + std::string cookies; + base::WaitableEvent event(true /* manual reset */, + false /* not initially signaled */); + Task* task = NewRunnableFunction( + &GetCookiesOnIOThread, + GURL(url), context_getter, &event, &cookies); + if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { + reply.SendError("Couldn't post task to get the cookies"); + return; + } + event.Wait(); + + DictionaryValue dict; + dict.SetString("cookies", cookies); + reply.SendSuccess(&dict); +} + +void DeleteCookieJSON(AutomationProvider* provider, + DictionaryValue* args, + IPC::Message* reply_message) { + AutomationJSONReply reply(provider, reply_message); + Browser* browser; + std::string error; + if (!GetBrowserFromJSONArgs(args, &browser, &error)) { + reply.SendError(error); + return; + } + std::string url, name; + if (!args->GetString("url", &url)) { + reply.SendError("'url' missing or invalid"); + return; + } + if (!args->GetString("name", &name)) { + reply.SendError("'name' missing or invalid"); + return; + } + + // Since we may be on the UI thread don't call GetURLRequestContext(). + scoped_refptr<URLRequestContextGetter> context_getter = + browser->profile()->GetRequestContext(); + + base::WaitableEvent event(true /* manual reset */, + false /* not initially signaled */); + Task* task = NewRunnableFunction( + &DeleteCookieOnIOThread, + GURL(url), name, context_getter, &event); + if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { + reply.SendError("Couldn't post task to delete the cookie"); + return; + } + event.Wait(); + reply.SendSuccess(NULL); +} + +void SetCookieJSON(AutomationProvider* provider, + DictionaryValue* args, + IPC::Message* reply_message) { + AutomationJSONReply reply(provider, reply_message); + Browser* browser; + std::string error; + if (!GetBrowserFromJSONArgs(args, &browser, &error)) { + reply.SendError(error); + return; + } + std::string url, cookie; + if (!args->GetString("url", &url)) { + reply.SendError("'url' missing or invalid"); + return; + } + if (!args->GetString("cookie", &cookie)) { + reply.SendError("'cookie' missing or invalid"); + return; + } + + // Since we may be on the UI thread don't call GetURLRequestContext(). + scoped_refptr<URLRequestContextGetter> context_getter = + browser->profile()->GetRequestContext(); + + base::WaitableEvent event(true /* manual reset */, + false /* not initially signaled */); + bool success = false; + Task* task = NewRunnableFunction( + &SetCookieOnIOThread, + GURL(url), cookie, context_getter, &event, &success); + if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { + reply.SendError("Couldn't post task to set the cookie"); + return; + } + event.Wait(); + + if (!success) { + reply.SendError("Could not set the cookie"); + return; + } + reply.SendSuccess(NULL); +} + +} // namespace automation_util diff --git a/chrome/browser/automation/automation_util.h b/chrome/browser/automation/automation_util.h new file mode 100644 index 0000000..bb38344 --- /dev/null +++ b/chrome/browser/automation/automation_util.h @@ -0,0 +1,72 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_UTIL_H_ +#define CHROME_BROWSER_AUTOMATION_AUTOMATION_UTIL_H_ +#pragma once + +#include "base/basictypes.h" +#include "chrome/browser/automation/automation_provider.h" +#include "content/browser/tab_contents/tab_contents.h" + +class DictionaryValue; + +// This file contains automation utility functions. + +namespace automation_util { + +// Gets the size and value of the cookie string for |url| in the given tab. +// Can be called from any thread. +void GetCookies(const GURL& url, + TabContents* contents, + int* value_size, + std::string* value); + +// Sets a cookie for |url| in the given tab. Can be called from any thread. +void SetCookie(const GURL& url, + const std::string value, + TabContents* contents, + int* response_value); + +// Deletes a cookie for |url| in the given tab. Can be called from any thread. +void DeleteCookie(const GURL& url, + const std::string& cookie_name, + TabContents* contents, + bool* success); + +// Gets the cookies for the given URL. Uses the JSON interface. +// Example: +// input: { "windex": 1, "tab_index": 1, "url": "http://www.google.com" } +// output: { "cookies": "PREF=12012" } +void GetCookiesJSON(AutomationProvider* provider, + DictionaryValue* args, + IPC::Message* reply_message); + +// Deletes the cookie with the given name for the URL. Uses the JSON interface. +// Example: +// input: { "windex": 1, +// "tab_index": 1, +// "url": "http://www.google.com", +// "name": "my_cookie" +// } +// output: none +void DeleteCookieJSON(AutomationProvider* provider, + DictionaryValue* args, + IPC::Message* reply_message); + +// Sets a cookie for the given URL. Uses the JSON interface. +// Example: +// input: { "windex": 1, +// "tab_index": 1, +// "url": "http://www.google.com", +// "cookie": "PREF=21321" +// } +// output: none +void SetCookieJSON(AutomationProvider* provider, + DictionaryValue* args, + IPC::Message* reply_message); + +} // namespace automation_util + +#endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_UTIL_H_ diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc index a2385b0..476f0ee 100644 --- a/chrome/browser/automation/testing_automation_provider.cc +++ b/chrome/browser/automation/testing_automation_provider.cc @@ -30,6 +30,7 @@ #include "chrome/browser/automation/automation_provider_list.h" #include "chrome/browser/automation/automation_provider_observers.h" #include "chrome/browser/automation/automation_tab_tracker.h" +#include "chrome/browser/automation/automation_util.h" #include "chrome/browser/automation/automation_window_tracker.h" #include "chrome/browser/automation/ui_controls.h" #include "chrome/browser/blocked_content_container.h" @@ -72,7 +73,6 @@ #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" -#include "chrome/common/net/url_request_context_getter.h" #include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" #include "content/browser/renderer_host/render_process_host.h" @@ -81,7 +81,6 @@ #include "content/common/common_param_traits.h" #include "content/common/notification_service.h" #include "net/base/cookie_store.h" -#include "net/url_request/url_request_context.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" #include "ui/base/events.h" #include "ui/base/message_box_flags.h" @@ -89,34 +88,6 @@ namespace { -void GetCookiesOnIOThread( - const GURL& url, - const scoped_refptr<URLRequestContextGetter>& context_getter, - base::WaitableEvent* event, - std::string* cookies) { - *cookies = context_getter->GetCookieStore()->GetCookies(url); - event->Signal(); -} - -void SetCookieOnIOThread( - const GURL& url, - const std::string& value, - const scoped_refptr<URLRequestContextGetter>& context_getter, - base::WaitableEvent* event, - bool* success) { - *success = context_getter->GetCookieStore()->SetCookie(url, value); - event->Signal(); -} - -void DeleteCookieOnIOThread( - const GURL& url, - const std::string& name, - const scoped_refptr<URLRequestContextGetter>& context_getter, - base::WaitableEvent* event) { - context_getter->GetCookieStore()->DeleteCookie(url, name); - event->Signal(); -} - void SendMouseClick(int flags) { ui_controls::MouseButton button = ui_controls::LEFT; if ((flags & ui::EF_LEFT_BUTTON_DOWN) == @@ -508,67 +479,26 @@ void TestingAutomationProvider::CloseTab(int tab_handle, void TestingAutomationProvider::GetCookies(const GURL& url, int handle, int* value_size, std::string* value) { - *value_size = -1; - if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) { - // Since we are running on the UI thread don't call GetURLRequestContext(). - scoped_refptr<URLRequestContextGetter> context_getter = - tab_tracker_->GetResource(handle)->profile()->GetRequestContext(); - - base::WaitableEvent event(true /* manual reset */, - false /* not initially signaled */); - CHECK(BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableFunction(&GetCookiesOnIOThread, - url, context_getter, &event, value))); - event.Wait(); - - *value_size = static_cast<int>(value->size()); - } + TabContents *contents = tab_tracker_->ContainsHandle(handle) ? + tab_tracker_->GetResource(handle)->tab_contents() : NULL; + automation_util::GetCookies(url, contents, value_size, value); } void TestingAutomationProvider::SetCookie(const GURL& url, const std::string value, int handle, int* response_value) { - *response_value = -1; - - if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) { - // Since we are running on the UI thread don't call GetURLRequestContext(). - scoped_refptr<URLRequestContextGetter> context_getter = - tab_tracker_->GetResource(handle)->profile()->GetRequestContext(); - - base::WaitableEvent event(true /* manual reset */, - false /* not initially signaled */); - bool success = false; - CHECK(BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableFunction(&SetCookieOnIOThread, - url, value, context_getter, &event, - &success))); - event.Wait(); - if (success) - *response_value = 1; - } + TabContents *contents = tab_tracker_->ContainsHandle(handle) ? + tab_tracker_->GetResource(handle)->tab_contents() : NULL; + automation_util::SetCookie(url, value, contents, response_value); } void TestingAutomationProvider::DeleteCookie(const GURL& url, const std::string& cookie_name, int handle, bool* success) { - *success = false; - if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) { - // Since we are running on the UI thread don't call GetURLRequestContext(). - scoped_refptr<URLRequestContextGetter> context_getter = - tab_tracker_->GetResource(handle)->profile()->GetRequestContext(); - - base::WaitableEvent event(true /* manual reset */, - false /* not initially signaled */); - CHECK(BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableFunction(&DeleteCookieOnIOThread, - url, cookie_name, context_getter, &event))); - event.Wait(); - *success = true; - } + TabContents *contents = tab_tracker_->ContainsHandle(handle) ? + tab_tracker_->GetResource(handle)->tab_contents() : NULL; + automation_util::DeleteCookie(url, cookie_name, contents, success); } void TestingAutomationProvider::ShowCollectedCookiesDialog( @@ -5024,116 +4954,17 @@ void TestingAutomationProvider::GetTabTitleJSON( void TestingAutomationProvider::GetCookiesJSON( DictionaryValue* args, IPC::Message* reply_message) { - AutomationJSONReply reply(this, reply_message); - Browser* browser; - std::string error; - if (!GetBrowserFromJSONArgs(args, &browser, &error)) { - reply.SendError(error); - return; - } - std::string url; - if (!args->GetString("url", &url)) { - reply.SendError("'url' missing or invalid"); - return; - } - - // Since we are running on the UI thread don't call GetURLRequestContext(). - scoped_refptr<URLRequestContextGetter> context_getter = - browser->profile()->GetRequestContext(); - - std::string cookies; - base::WaitableEvent event(true /* manual reset */, - false /* not initially signaled */); - Task* task = NewRunnableFunction( - &GetCookiesOnIOThread, - GURL(url), context_getter, &event, &cookies); - if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { - reply.SendError("Couldn't post task to get the cookies"); - return; - } - event.Wait(); - - DictionaryValue dict; - dict.SetString("cookies", cookies); - reply.SendSuccess(&dict); + automation_util::GetCookiesJSON(this, args, reply_message); } void TestingAutomationProvider::DeleteCookieJSON( DictionaryValue* args, IPC::Message* reply_message) { - AutomationJSONReply reply(this, reply_message); - Browser* browser; - std::string error; - if (!GetBrowserFromJSONArgs(args, &browser, &error)) { - reply.SendError(error); - return; - } - std::string url, name; - if (!args->GetString("url", &url)) { - reply.SendError("'url' missing or invalid"); - return; - } - if (!args->GetString("name", &name)) { - reply.SendError("'name' missing or invalid"); - return; - } - - // Since we are running on the UI thread don't call GetURLRequestContext(). - scoped_refptr<URLRequestContextGetter> context_getter = - browser->profile()->GetRequestContext(); - - base::WaitableEvent event(true /* manual reset */, - false /* not initially signaled */); - Task* task = NewRunnableFunction( - &DeleteCookieOnIOThread, - GURL(url), name, context_getter, &event); - if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { - reply.SendError("Couldn't post task to delete the cookie"); - return; - } - event.Wait(); - reply.SendSuccess(NULL); + automation_util::DeleteCookieJSON(this, args, reply_message); } void TestingAutomationProvider::SetCookieJSON( DictionaryValue* args, IPC::Message* reply_message) { - AutomationJSONReply reply(this, reply_message); - Browser* browser; - std::string error; - if (!GetBrowserFromJSONArgs(args, &browser, &error)) { - reply.SendError(error); - return; - } - std::string url, cookie; - if (!args->GetString("url", &url)) { - reply.SendError("'url' missing or invalid"); - return; - } - if (!args->GetString("cookie", &cookie)) { - reply.SendError("'cookie' missing or invalid"); - return; - } - - // Since we are running on the UI thread don't call GetURLRequestContext(). - scoped_refptr<URLRequestContextGetter> context_getter = - browser->profile()->GetRequestContext(); - - base::WaitableEvent event(true /* manual reset */, - false /* not initially signaled */); - bool success = false; - Task* task = NewRunnableFunction( - &SetCookieOnIOThread, - GURL(url), cookie, context_getter, &event, &success); - if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) { - reply.SendError("Couldn't post task to set the cookie"); - return; - } - event.Wait(); - - if (!success) { - reply.SendError("Could not set the cookie"); - return; - } - reply.SendSuccess(NULL); + automation_util::SetCookieJSON(this, args, reply_message); } void TestingAutomationProvider::GetTabIds( |