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 <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
13 #include "base/move.h" | |
14 #include "third_party/WebKit/public/platform/WebCallbacks.h" | 14 #include "third_party/WebKit/public/platform/WebCallbacks.h" |
15 | 15 |
16 // A ScopedWebCallbacks is a move-only scoper which helps manage the lifetime of | 16 // A ScopedWebCallbacks is a move-only scoper which helps manage the lifetime of |
17 // a blink::WebCallbacks object. This is particularly useful when you're | 17 // a blink::WebCallbacks object. This is particularly useful when you're |
18 // simultaneously dealing with the following two conditions: | 18 // simultaneously dealing with the following two conditions: |
19 // | 19 // |
20 // 1. Your WebCallbacks implementation requires either onSuccess or onError to | 20 // 1. Your WebCallbacks implementation requires either onSuccess or onError to |
21 // be called before it's destroyed. This is the case with | 21 // be called before it's destroyed. This is the case with |
22 // CallbackPromiseAdapter for example, because its underlying | 22 // CallbackPromiseAdapter for example, because its underlying |
23 // ScriptPromiseResolver must be resolved or rejected before destruction. | 23 // ScriptPromiseResolver must be resolved or rejected before destruction. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // | 60 // |
61 // If the bound RespondWithSuccess callback actually runs, PassCallbacks() will | 61 // If the bound RespondWithSuccess callback actually runs, PassCallbacks() will |
62 // reliquish ownership of the WebCallbacks object to a temporary scoped_ptr | 62 // reliquish ownership of the WebCallbacks object to a temporary scoped_ptr |
63 // which will be destroyed immediately after onSuccess is called. | 63 // which will be destroyed immediately after onSuccess is called. |
64 // | 64 // |
65 // If the bound RespondWithSuccess callback is instead destroyed first, | 65 // If the bound RespondWithSuccess callback is instead destroyed first, |
66 // the ScopedWebCallbacks destructor will invoke OnCallbacksDropped, executing | 66 // the ScopedWebCallbacks destructor will invoke OnCallbacksDropped, executing |
67 // our desired default behavior before deleting the WebCallbacks. | 67 // our desired default behavior before deleting the WebCallbacks. |
68 template <typename CallbacksType> | 68 template <typename CallbacksType> |
69 class ScopedWebCallbacks { | 69 class ScopedWebCallbacks { |
70 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedWebCallbacks); | |
71 | |
72 public: | 70 public: |
73 using DestructionCallback = | 71 using DestructionCallback = |
74 base::Callback<void(std::unique_ptr<CallbacksType> callbacks)>; | 72 base::Callback<void(std::unique_ptr<CallbacksType> callbacks)>; |
75 | 73 |
76 ScopedWebCallbacks(std::unique_ptr<CallbacksType> callbacks, | 74 ScopedWebCallbacks(std::unique_ptr<CallbacksType> callbacks, |
77 const DestructionCallback& destruction_callback) | 75 const DestructionCallback& destruction_callback) |
78 : callbacks_(std::move(callbacks)), | 76 : callbacks_(std::move(callbacks)), |
79 destruction_callback_(destruction_callback) {} | 77 destruction_callback_(destruction_callback) {} |
80 | 78 |
81 ~ScopedWebCallbacks() { | 79 ~ScopedWebCallbacks() { |
82 if (callbacks_) | 80 if (callbacks_) |
83 destruction_callback_.Run(std::move(callbacks_)); | 81 destruction_callback_.Run(std::move(callbacks_)); |
84 } | 82 } |
85 | 83 |
86 ScopedWebCallbacks(ScopedWebCallbacks&& other) { *this = std::move(other); } | 84 ScopedWebCallbacks(ScopedWebCallbacks&& other) { *this = std::move(other); } |
87 | 85 |
88 ScopedWebCallbacks& operator=(ScopedWebCallbacks&& other) { | 86 ScopedWebCallbacks& operator=(ScopedWebCallbacks&& other) { |
89 callbacks_ = std::move(other.callbacks_); | 87 callbacks_ = std::move(other.callbacks_); |
90 destruction_callback_ = other.destruction_callback_; | 88 destruction_callback_ = other.destruction_callback_; |
91 return *this; | 89 return *this; |
92 } | 90 } |
93 | 91 |
94 std::unique_ptr<CallbacksType> PassCallbacks() { | 92 std::unique_ptr<CallbacksType> PassCallbacks() { |
95 return std::move(callbacks_); | 93 return std::move(callbacks_); |
96 } | 94 } |
97 | 95 |
98 private: | 96 private: |
99 std::unique_ptr<CallbacksType> callbacks_; | 97 std::unique_ptr<CallbacksType> callbacks_; |
100 DestructionCallback destruction_callback_; | 98 DestructionCallback destruction_callback_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(ScopedWebCallbacks); |
101 }; | 101 }; |
102 | 102 |
103 template <typename CallbacksType> | 103 template <typename CallbacksType> |
104 ScopedWebCallbacks<CallbacksType> make_scoped_web_callbacks( | 104 ScopedWebCallbacks<CallbacksType> make_scoped_web_callbacks( |
105 CallbacksType* callbacks, | 105 CallbacksType* callbacks, |
106 const typename ScopedWebCallbacks<CallbacksType>::DestructionCallback& | 106 const typename ScopedWebCallbacks<CallbacksType>::DestructionCallback& |
107 destruction_callback) { | 107 destruction_callback) { |
108 return ScopedWebCallbacks<CallbacksType>(base::WrapUnique(callbacks), | 108 return ScopedWebCallbacks<CallbacksType>(base::WrapUnique(callbacks), |
109 destruction_callback); | 109 destruction_callback); |
110 } | 110 } |
111 | 111 |
112 #endif // CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ | 112 #endif // CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ |
OLD | NEW |