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