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#ifndef NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_
6#define NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_
7
8#include <string>
9#include <vector>
10
11#include "net/cookies/cookie_store.h"
12
13namespace base {
14class MessageLoop;
15class Thread;
16}
17
18namespace net {
19
20// Defines common behaviour for the callbacks from GetCookies, SetCookies, etc.
21// Asserts that the current thread is the expected invocation thread, sends a
22// quit to the thread in which it was constructed.
23class CookieCallback {
24 public:
25  // Indicates whether the callback has been called.
26  bool did_run() { return did_run_; }
27
28 protected:
29  // Constructs a callback that expects to be called in the given thread and
30  // will, upon execution, send a QUIT to the constructing thread.
31  explicit CookieCallback(base::Thread* run_in_thread);
32
33  // Constructs a callback that expects to be called in current thread and will
34  // send a QUIT to the constructing thread.
35  CookieCallback();
36
37  // Tests whether the current thread was the caller's thread.
38  // Sends a QUIT to the constructing thread.
39  void CallbackEpilogue();
40
41 private:
42  bool did_run_;
43  base::Thread* run_in_thread_;
44  base::MessageLoop* run_in_loop_;
45  base::MessageLoop* parent_loop_;
46  base::MessageLoop* loop_to_quit_;
47};
48
49// Callback implementations for the asynchronous CookieStore methods.
50
51template <typename T>
52class ResultSavingCookieCallback : public CookieCallback {
53 public:
54  ResultSavingCookieCallback() {
55  }
56  explicit ResultSavingCookieCallback(base::Thread* run_in_thread)
57      : CookieCallback(run_in_thread) {
58  }
59
60  void Run(T result) {
61    result_ = result;
62    CallbackEpilogue();
63  }
64
65  const T& result() { return result_; }
66
67 private:
68  T result_;
69};
70
71class StringResultCookieCallback : public CookieCallback {
72 public:
73  StringResultCookieCallback();
74  explicit StringResultCookieCallback(base::Thread* run_in_thread);
75
76  void Run(const std::string& result) {
77    result_ = result;
78    CallbackEpilogue();
79  }
80
81  const std::string& result() { return result_; }
82
83 private:
84  std::string result_;
85};
86
87class NoResultCookieCallback : public CookieCallback {
88 public:
89  NoResultCookieCallback();
90  explicit NoResultCookieCallback(base::Thread* run_in_thread);
91
92  void Run() {
93    CallbackEpilogue();
94  }
95};
96
97}  // namespace net
98
99#endif  // NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_
100