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