1// Copyright (c) 2012 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 "chrome/browser/browsing_data/mock_browsing_data_cookie_helper.h"
6
7#include "base/logging.h"
8#include "net/cookies/canonical_cookie.h"
9#include "net/cookies/parsed_cookie.h"
10
11MockBrowsingDataCookieHelper::MockBrowsingDataCookieHelper(
12    net::URLRequestContextGetter* request_context_getter)
13    : BrowsingDataCookieHelper(request_context_getter) {
14}
15
16MockBrowsingDataCookieHelper::~MockBrowsingDataCookieHelper() {
17}
18
19void MockBrowsingDataCookieHelper::StartFetching(
20    const net::CookieMonster::GetCookieListCallback &callback) {
21  callback_ = callback;
22}
23
24void MockBrowsingDataCookieHelper::DeleteCookie(
25    const net::CanonicalCookie& cookie) {
26  std::string key = cookie.Name() + "=" + cookie.Value();
27  CHECK(cookies_.find(key) != cookies_.end());
28  cookies_[key] = false;
29}
30
31void MockBrowsingDataCookieHelper::AddCookieSamples(
32    const GURL& url, const std::string& cookie_line) {
33  typedef net::CookieList::const_iterator cookie_iterator;
34  net::ParsedCookie pc(cookie_line);
35  scoped_ptr<net::CanonicalCookie> cc(new net::CanonicalCookie(url, pc));
36
37  if (cc.get()) {
38    for (cookie_iterator cookie = cookie_list_.begin();
39        cookie != cookie_list_.end(); ++cookie) {
40      if (cookie->Name() == cc->Name() &&
41          cookie->Domain() == cc->Domain()&&
42          cookie->Path() == cc->Path()) {
43        return;
44      }
45    }
46    cookie_list_.push_back(*cc);
47    cookies_[cookie_line] = true;
48  }
49}
50
51void MockBrowsingDataCookieHelper::Notify() {
52  if (!callback_.is_null())
53    callback_.Run(cookie_list_);
54}
55
56void MockBrowsingDataCookieHelper::Reset() {
57  for (std::map<const std::string, bool>::iterator i = cookies_.begin();
58       i != cookies_.end(); ++i)
59    i->second = true;
60}
61
62bool MockBrowsingDataCookieHelper::AllDeleted() {
63  for (std::map<const std::string, bool>::const_iterator i = cookies_.begin();
64       i != cookies_.end(); ++i)
65    if (i->second)
66      return false;
67  return true;
68}
69