Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / base / test_completion_callback.h
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_BASE_TEST_COMPLETION_CALLBACK_H_
6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_
7
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/tuple.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/net_errors.h"
13
14 //-----------------------------------------------------------------------------
15 // completion callback helper
16
17 // A helper class for completion callbacks, designed to make it easy to run
18 // tests involving asynchronous operations.  Just call WaitForResult to wait
19 // for the asynchronous operation to complete.
20 //
21 // NOTE: Since this runs a message loop to wait for the completion callback,
22 // there could be other side-effects resulting from WaitForResult.  For this
23 // reason, this class is probably not ideal for a general application.
24 //
25
26 namespace net {
27
28 class IOBuffer;
29
30 namespace internal {
31
32 class TestCompletionCallbackBaseInternal {
33  public:
34   bool have_result() const { return have_result_; }
35
36  protected:
37   TestCompletionCallbackBaseInternal();
38   virtual ~TestCompletionCallbackBaseInternal();
39
40   void DidSetResult();
41   void WaitForResult();
42
43   bool have_result_;
44   bool waiting_for_result_;
45
46  private:
47   DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal);
48 };
49
50 template <typename R>
51 class TestCompletionCallbackTemplate
52     : public TestCompletionCallbackBaseInternal {
53  public:
54   virtual ~TestCompletionCallbackTemplate() override {}
55
56   R WaitForResult() {
57     TestCompletionCallbackBaseInternal::WaitForResult();
58     return result_;
59   }
60
61   R GetResult(R result) {
62     if (net::ERR_IO_PENDING != result)
63       return result;
64     return WaitForResult();
65   }
66
67  protected:
68   // Override this method to gain control as the callback is running.
69   virtual void SetResult(R result) {
70     result_ = result;
71     DidSetResult();
72   }
73
74   TestCompletionCallbackTemplate() : result_(R()) {}
75   R result_;
76
77  private:
78   DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate);
79 };
80
81 }  // namespace internal
82
83 class TestClosure
84     : public internal::TestCompletionCallbackBaseInternal {
85  public:
86   using internal::TestCompletionCallbackBaseInternal::WaitForResult;
87
88   TestClosure();
89   virtual ~TestClosure() override;
90
91   const base::Closure& closure() const { return closure_; }
92
93  private:
94   const base::Closure closure_;
95
96   DISALLOW_COPY_AND_ASSIGN(TestClosure);
97 };
98
99 // Base class overridden by custom implementations of TestCompletionCallback.
100 typedef internal::TestCompletionCallbackTemplate<int>
101     TestCompletionCallbackBase;
102
103 typedef internal::TestCompletionCallbackTemplate<int64>
104     TestInt64CompletionCallbackBase;
105
106 class TestCompletionCallback : public TestCompletionCallbackBase {
107  public:
108   TestCompletionCallback();
109   virtual ~TestCompletionCallback() override;
110
111   const CompletionCallback& callback() const { return callback_; }
112
113  private:
114   const CompletionCallback callback_;
115
116   DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
117 };
118
119 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase {
120  public:
121   TestInt64CompletionCallback();
122   virtual ~TestInt64CompletionCallback() override;
123
124   const Int64CompletionCallback& callback() const { return callback_; }
125
126  private:
127   const Int64CompletionCallback callback_;
128
129   DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback);
130 };
131
132 // Makes sure that the buffer is not referenced when the callback runs.
133 class ReleaseBufferCompletionCallback: public TestCompletionCallback {
134  public:
135   explicit ReleaseBufferCompletionCallback(IOBuffer* buffer);
136   virtual ~ReleaseBufferCompletionCallback() override;
137
138  private:
139   void SetResult(int result) override;
140
141   IOBuffer* buffer_;
142   DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback);
143 };
144
145 }  // namespace net
146
147 #endif  // NET_BASE_TEST_COMPLETION_CALLBACK_H_