OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 return base::Bind(&SaveValue, last_value_seen, closure); | 44 return base::Bind(&SaveValue, last_value_seen, closure); |
45 } | 45 } |
46 | 46 |
47 // An implementation of sample::Provider used on the server side. | 47 // An implementation of sample::Provider used on the server side. |
48 // It only implements one of the methods: EchoInt(). | 48 // It only implements one of the methods: EchoInt(). |
49 // All it does is save the values and Callbacks it sees. | 49 // All it does is save the values and Callbacks it sees. |
50 class InterfaceImpl : public sample::Provider { | 50 class InterfaceImpl : public sample::Provider { |
51 public: | 51 public: |
52 InterfaceImpl() | 52 InterfaceImpl() |
53 : last_server_value_seen_(0), | 53 : last_server_value_seen_(0), |
54 callback_saved_(new EchoIntCallback) {} | 54 callback_saved_(new Callback<void(int32_t)>()) {} |
55 | 55 |
56 ~InterfaceImpl() override { | 56 ~InterfaceImpl() override { |
57 if (callback_saved_) { | 57 if (callback_saved_) { |
58 delete callback_saved_; | 58 delete callback_saved_; |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 // Run's the callback previously saved from the last invocation | 62 // Run's the callback previously saved from the last invocation |
63 // of |EchoInt()|. | 63 // of |EchoInt()|. |
64 bool RunCallback() { | 64 bool RunCallback() { |
65 if (callback_saved_) { | 65 if (callback_saved_) { |
66 callback_saved_->Run(last_server_value_seen_); | 66 callback_saved_->Run(last_server_value_seen_); |
67 return true; | 67 return true; |
68 } | 68 } |
69 return false; | 69 return false; |
70 } | 70 } |
71 | 71 |
72 // Delete's the previously saved callback. | 72 // Delete's the previously saved callback. |
73 void DeleteCallback() { | 73 void DeleteCallback() { |
74 delete callback_saved_; | 74 delete callback_saved_; |
75 callback_saved_ = nullptr; | 75 callback_saved_ = nullptr; |
76 } | 76 } |
77 | 77 |
78 // sample::Provider implementation | 78 // sample::Provider implementation |
79 | 79 |
80 // Saves its two input values in member variables and does nothing else. | 80 // Saves its two input values in member variables and does nothing else. |
81 void EchoInt(int32_t x, const EchoIntCallback& callback) override { | 81 void EchoInt(int32_t x, const Callback<void(int32_t)>& callback) override { |
82 last_server_value_seen_ = x; | 82 last_server_value_seen_ = x; |
83 *callback_saved_ = callback; | 83 *callback_saved_ = callback; |
84 if (!closure_.is_null()) { | 84 if (!closure_.is_null()) { |
85 closure_.Run(); | 85 closure_.Run(); |
86 closure_.Reset(); | 86 closure_.Reset(); |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 void EchoString(const String& a, | 90 void EchoString(const String& a, |
91 const EchoStringCallback& callback) override { | 91 const Callback<void(String)>& callback) override { |
92 CHECK(false) << "Not implemented."; | 92 CHECK(false) << "Not implemented."; |
93 } | 93 } |
94 | 94 |
95 void EchoStrings(const String& a, | 95 void EchoStrings(const String& a, |
96 const String& b, | 96 const String& b, |
97 const EchoStringsCallback& callback) override { | 97 const Callback<void(String, String)>& callback) override { |
98 CHECK(false) << "Not implemented."; | 98 CHECK(false) << "Not implemented."; |
99 } | 99 } |
100 | 100 |
101 void EchoMessagePipeHandle( | 101 void EchoMessagePipeHandle( |
102 ScopedMessagePipeHandle a, | 102 ScopedMessagePipeHandle a, |
103 const EchoMessagePipeHandleCallback& callback) override { | 103 const Callback<void(ScopedMessagePipeHandle)>& callback) override { |
104 CHECK(false) << "Not implemented."; | 104 CHECK(false) << "Not implemented."; |
105 } | 105 } |
106 | 106 |
107 void EchoEnum(sample::Enum a, const EchoEnumCallback& callback) override { | 107 void EchoEnum(sample::Enum a, |
| 108 const Callback<void(sample::Enum)>& callback) override { |
108 CHECK(false) << "Not implemented."; | 109 CHECK(false) << "Not implemented."; |
109 } | 110 } |
110 | 111 |
111 void resetLastServerValueSeen() { last_server_value_seen_ = 0; } | 112 void resetLastServerValueSeen() { last_server_value_seen_ = 0; } |
112 | 113 |
113 int32_t last_server_value_seen() const { return last_server_value_seen_; } | 114 int32_t last_server_value_seen() const { return last_server_value_seen_; } |
114 | 115 |
115 void set_closure(const base::Closure& closure) { closure_ = closure; } | 116 void set_closure(const base::Closure& closure) { closure_ = closure; } |
116 | 117 |
117 private: | 118 private: |
118 int32_t last_server_value_seen_; | 119 int32_t last_server_value_seen_; |
119 EchoIntCallback* callback_saved_; | 120 Callback<void(int32_t)>* callback_saved_; |
120 base::Closure closure_; | 121 base::Closure closure_; |
121 }; | 122 }; |
122 | 123 |
123 class BindingCallbackTest : public testing::Test { | 124 class BindingCallbackTest : public testing::Test { |
124 public: | 125 public: |
125 BindingCallbackTest() {} | 126 BindingCallbackTest() {} |
126 ~BindingCallbackTest() override {} | 127 ~BindingCallbackTest() override {} |
127 | 128 |
128 protected: | 129 protected: |
129 int32_t last_client_callback_value_seen_; | 130 int32_t last_client_callback_value_seen_; |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 regex.clear(); | 338 regex.clear(); |
338 #endif // OS_WIN | 339 #endif // OS_WIN |
339 EXPECT_DEATH_IF_SUPPORTED(server_impl.DeleteCallback(), regex.c_str()); | 340 EXPECT_DEATH_IF_SUPPORTED(server_impl.DeleteCallback(), regex.c_str()); |
340 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 341 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
341 // !defined(OS_ANDROID) | 342 // !defined(OS_ANDROID) |
342 } | 343 } |
343 | 344 |
344 } // namespace | 345 } // namespace |
345 } // namespace test | 346 } // namespace test |
346 } // namespace mojo | 347 } // namespace mojo |
OLD | NEW |