summaryrefslogtreecommitdiffstats
path: root/net/cookies/cookie_store_test_callbacks.h
blob: 86e11f8b2f8298449076fb1a5b4c30bda70924da (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2012 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.

#ifndef NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_
#define NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_

#include <string>
#include <vector>

#include "net/cookies/cookie_store.h"

class MessageLoop;

namespace base {
class Thread;
}

namespace net {

// Defines common behaviour for the callbacks from GetCookies, SetCookies, etc.
// Asserts that the current thread is the expected invocation thread, sends a
// quit to the thread in which it was constructed.
class CookieCallback {
 public:
  // Indicates whether the callback has been called.
  bool did_run() { return did_run_; }

 protected:
  // Constructs a callback that expects to be called in the given thread and
  // will, upon execution, send a QUIT to the constructing thread.
  explicit CookieCallback(base::Thread* run_in_thread);

  // Constructs a callback that expects to be called in current thread and will
  // send a QUIT to the constructing thread.
  CookieCallback();

  // Tests whether the current thread was the caller's thread.
  // Sends a QUIT to the constructing thread.
  void CallbackEpilogue();

 private:
  bool did_run_;
  base::Thread* run_in_thread_;
  MessageLoop* run_in_loop_;
  MessageLoop* parent_loop_;
  MessageLoop* loop_to_quit_;
};

// Callback implementations for the asynchronous CookieStore methods.

class SetCookieCallback : public CookieCallback {
 public:
  SetCookieCallback();
  explicit SetCookieCallback(base::Thread* run_in_thread);

  void Run(bool result) {
    result_ = result;
    CallbackEpilogue();
  }

  bool result() { return result_; }

 private:
  bool result_;
};

class GetCookieStringCallback : public CookieCallback {
 public:
  GetCookieStringCallback();
  explicit GetCookieStringCallback(base::Thread* run_in_thread);

  void Run(const std::string& cookie) {
    cookie_ = cookie;
    CallbackEpilogue();
  }

  const std::string& cookie() { return cookie_; }

 private:
  std::string cookie_;
};

class GetCookiesWithInfoCallback : public CookieCallback {
 public:
  GetCookiesWithInfoCallback();
  explicit GetCookiesWithInfoCallback(base::Thread* run_in_thread);
  ~GetCookiesWithInfoCallback();

  void Run(
      const std::string& cookie_line,
      const std::vector<CookieStore::CookieInfo>& cookie_info) {
    cookie_line_ = cookie_line;
    cookie_info_ = cookie_info;
    CallbackEpilogue();
  }

  const std::string& cookie_line() { return cookie_line_; }
  const std::vector<CookieStore::CookieInfo>& cookie_info() {
    return cookie_info_;
  }

 private:
  std::string cookie_line_;
  std::vector<CookieStore::CookieInfo> cookie_info_;
};

class DeleteCallback : public CookieCallback {
 public:
  DeleteCallback();
  explicit DeleteCallback(base::Thread* run_in_thread);

  void Run(int num_deleted) {
    num_deleted_ = num_deleted;
    CallbackEpilogue();
  }

  int num_deleted() { return num_deleted_; }

 private:
  int num_deleted_;
};

class DeleteCookieCallback : public CookieCallback {
 public:
  DeleteCookieCallback();
  explicit DeleteCookieCallback(base::Thread* run_in_thread);

  void Run() {
    CallbackEpilogue();
  }
};

}  // namespace net

#endif  // NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_