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 #ifndef CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ | 5 #ifndef CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ |
6 #define CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ | 6 #define CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/move.h" | 10 #include "base/move.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 // | 57 // |
58 // If the bound RespondWithSuccess callback actually runs, PassCallbacks() will | 58 // If the bound RespondWithSuccess callback actually runs, PassCallbacks() will |
59 // reliquish ownership of the WebCallbacks object to a temporary scoped_ptr | 59 // reliquish ownership of the WebCallbacks object to a temporary scoped_ptr |
60 // which will be destroyed immediately after onSuccess is called. | 60 // which will be destroyed immediately after onSuccess is called. |
61 // | 61 // |
62 // If the bound RespondWithSuccess callback is instead destroyed first, | 62 // If the bound RespondWithSuccess callback is instead destroyed first, |
63 // the ScopedWebCallbacks destructor will invoke OnCallbacksDropped, executing | 63 // the ScopedWebCallbacks destructor will invoke OnCallbacksDropped, executing |
64 // our desired default behavior before deleting the WebCallbacks. | 64 // our desired default behavior before deleting the WebCallbacks. |
65 template <typename CallbacksType> | 65 template <typename CallbacksType> |
66 class ScopedWebCallbacks { | 66 class ScopedWebCallbacks { |
67 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedWebCallbacks, RValue); | 67 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedWebCallbacks); |
68 | 68 |
69 public: | 69 public: |
70 using DestructionCallback = | 70 using DestructionCallback = |
71 base::Callback<void(scoped_ptr<CallbacksType> callbacks)>; | 71 base::Callback<void(scoped_ptr<CallbacksType> callbacks)>; |
72 | 72 |
73 ScopedWebCallbacks(scoped_ptr<CallbacksType> callbacks, | 73 ScopedWebCallbacks(scoped_ptr<CallbacksType> callbacks, |
74 const DestructionCallback& destruction_callback) | 74 const DestructionCallback& destruction_callback) |
75 : callbacks_(callbacks.Pass()), | 75 : callbacks_(callbacks.Pass()), |
76 destruction_callback_(destruction_callback) {} | 76 destruction_callback_(destruction_callback) {} |
77 | 77 |
78 ~ScopedWebCallbacks() { | 78 ~ScopedWebCallbacks() { |
79 if (callbacks_) | 79 if (callbacks_) |
80 destruction_callback_.Run(callbacks_.Pass()); | 80 destruction_callback_.Run(callbacks_.Pass()); |
81 } | 81 } |
82 | 82 |
83 ScopedWebCallbacks(RValue other) { *this = other; } | 83 ScopedWebCallbacks(ScopedWebCallbacks&& other) { *this = other.Pass(); } |
Nico
2015/11/26 03:06:10
consider std::move() instead of Pass()
dcheng
2015/11/26 03:10:41
Done here and elsewhere (I'll upload this as soon
| |
84 | 84 |
85 ScopedWebCallbacks& operator=(RValue other) { | 85 ScopedWebCallbacks& operator=(ScopedWebCallbacks&& other) { |
86 callbacks_ = other.object->callbacks_.Pass(); | 86 callbacks_ = other.callbacks_.Pass(); |
87 destruction_callback_ = other.object->destruction_callback_; | 87 destruction_callback_ = other.destruction_callback_; |
88 return *this; | 88 return *this; |
89 } | 89 } |
90 | 90 |
91 scoped_ptr<CallbacksType> PassCallbacks() { return callbacks_.Pass(); } | 91 scoped_ptr<CallbacksType> PassCallbacks() { return callbacks_.Pass(); } |
92 | 92 |
93 private: | 93 private: |
94 scoped_ptr<CallbacksType> callbacks_; | 94 scoped_ptr<CallbacksType> callbacks_; |
95 DestructionCallback destruction_callback_; | 95 DestructionCallback destruction_callback_; |
96 }; | 96 }; |
97 | 97 |
98 template <typename CallbacksType> | 98 template <typename CallbacksType> |
99 ScopedWebCallbacks<CallbacksType> make_scoped_web_callbacks( | 99 ScopedWebCallbacks<CallbacksType> make_scoped_web_callbacks( |
100 CallbacksType* callbacks, | 100 CallbacksType* callbacks, |
101 const typename ScopedWebCallbacks<CallbacksType>::DestructionCallback& | 101 const typename ScopedWebCallbacks<CallbacksType>::DestructionCallback& |
102 destruction_callback) { | 102 destruction_callback) { |
103 return ScopedWebCallbacks<CallbacksType>(make_scoped_ptr(callbacks), | 103 return ScopedWebCallbacks<CallbacksType>(make_scoped_ptr(callbacks), |
104 destruction_callback); | 104 destruction_callback); |
105 } | 105 } |
106 | 106 |
107 #endif // CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ | 107 #endif // CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ |
OLD | NEW |