summaryrefslogtreecommitdiffstats
path: root/mojo/services/network/cookie_store_impl.cc
blob: 05963aa3de4e1be66e3d8fb73d72e0506c18c3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2014 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 "mojo/services/network/cookie_store_impl.h"

#include "mojo/common/url_type_converters.h"
#include "mojo/services/network/network_context.h"
#include "net/cookies/cookie_store.h"
#include "net/url_request/url_request_context.h"

namespace mojo {
namespace {

void AdaptGetCookiesCallback(const Callback<void(String)>& callback,
                             const std::string& cookies) {
  callback.Run(cookies);
}

void AdaptSetCookieCallback(const Callback<void(bool)>& callback,
                            bool success) {
  callback.Run(success);
}

}  // namespace

CookieStoreImpl::CookieStoreImpl(
    NetworkContext* context,
    const GURL& origin,
    scoped_ptr<mojo::AppRefCount> app_refcount)
    : context_(context),
      origin_(origin),
      app_refcount_(app_refcount.Pass()) {
}

CookieStoreImpl::~CookieStoreImpl() {
}

void CookieStoreImpl::Get(const String& url,
                          const Callback<void(String)>& callback) {
  // TODO(darin): Perform origin restriction.
  net::CookieStore* store = context_->url_request_context()->cookie_store();
  if (!store) {
    callback.Run(String());
    return;
  }
  store->GetCookiesWithOptionsAsync(
      url.To<GURL>(),
      net::CookieOptions(),
      base::Bind(&AdaptGetCookiesCallback, callback));
}

void CookieStoreImpl::Set(const String& url,
                          const String& cookie,
                          const Callback<void(bool)>& callback) {
  // TODO(darin): Perform origin restriction.
  net::CookieStore* store = context_->url_request_context()->cookie_store();
  if (!store) {
    callback.Run(false);
    return;
  }
  store->SetCookieWithOptionsAsync(
      url.To<GURL>(),
      cookie,
      net::CookieOptions(),
      base::Bind(&AdaptSetCookieCallback, callback));
}

}  // namespace mojo