| Index: chrome/browser/automation/automation_cookie_util.cc
|
| diff --git a/chrome/browser/automation/automation_cookie_util.cc b/chrome/browser/automation/automation_cookie_util.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..dca98ac6857855ad93198b4552e5169885afea5b
|
| --- /dev/null
|
| +++ b/chrome/browser/automation/automation_cookie_util.cc
|
| @@ -0,0 +1,122 @@
|
| +// 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_cookie_util.h"
|
| +
|
| +#include <string>
|
| +
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/common/net/url_request_context_getter.h"
|
| +#include "content/browser/browser_thread.h"
|
| +#include "content/browser/renderer_host/render_view_host.h"
|
| +#include "net/base/cookie_store.h"
|
| +
|
| +namespace automation_cookie_util {
|
| +
|
| +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 GetCookies(const GURL& url,
|
| + TabContents* contents,
|
| + int* value_size,
|
| + std::string* value) {
|
| + *value_size = -1;
|
| + if (url.is_valid() && contents) {
|
| + // Since we are on the UI thread don't call GetURLRequestContext().
|
| + // Get the request context specific to the current TabContents and app.
|
| + const Extension* installed_app =
|
| + contents->render_view_host()->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 are running on the UI thread don't call GetURLRequestContext().
|
| + // Get the request context specific to the current TabContents and app.
|
| + const Extension* installed_app =
|
| + contents->render_view_host()->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 are running on the UI thread don't call GetURLRequestContext().
|
| + // Get the request context specific to the current TabContents and app.
|
| + const Extension* installed_app =
|
| + contents->render_view_host()->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;
|
| + }
|
| +}
|
| +
|
| +} // namespace automation_cookie_util
|
|
|