1// Copyright 2014 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 "mojo/services/html_viewer/webcookiejar_impl.h"
6
7#include "base/bind.h"
8#include "third_party/WebKit/public/platform/WebURL.h"
9
10namespace mojo {
11namespace {
12
13void CopyBool(bool* output, bool input) {
14  *output = input;
15}
16
17void CopyString(String* output, const String& input) {
18  *output = input;
19}
20
21}  // namespace
22
23WebCookieJarImpl::WebCookieJarImpl(CookieStorePtr store)
24    : store_(store.Pass()) {
25}
26
27WebCookieJarImpl::~WebCookieJarImpl() {
28}
29
30void WebCookieJarImpl::setCookie(const blink::WebURL& url,
31                                 const blink::WebURL& first_party_for_cookies,
32                                 const blink::WebString& cookie) {
33  bool success;
34  store_->Set(url.string().utf8(), cookie.utf8(),
35              base::Bind(&CopyBool, &success));
36
37  // Wait to ensure the cookie was set before advancing. That way any
38  // subsequent URL request will see the changes to the cookie store.
39  //
40  // TODO(darin): Consider using associated message pipes for the CookieStore
41  // and URLLoader, so that we could let this method call run asynchronously
42  // without suffering an ordering problem. See crbug/386825.
43  //
44  store_.WaitForIncomingMethodCall();
45}
46
47blink::WebString WebCookieJarImpl::cookies(
48    const blink::WebURL& url,
49    const blink::WebURL& first_party_for_cookies) {
50  String result;
51  store_->Get(url.string().utf8(), base::Bind(&CopyString, &result));
52
53  // Wait for the result. Since every outbound request we make to the cookie
54  // store is followed up with WaitForIncomingMethodCall, we can be sure that
55  // the next incoming method call will be the response to our request.
56  store_.WaitForIncomingMethodCall();
57  if (!result)
58    return blink::WebString();
59
60  return blink::WebString::fromUTF8(result);
61}
62
63blink::WebString WebCookieJarImpl::cookieRequestHeaderFieldValue(
64    const blink::WebURL& url,
65    const blink::WebURL& first_party_for_cookies) {
66  return cookies(url, first_party_for_cookies);
67}
68
69}  // namespace mojo
70