| 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" | |
| 13 #include "base/memory/ptr_util.h" | 12 #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 |
| 70 public: | 72 public: |
| 71 using DestructionCallback = | 73 using DestructionCallback = |
| 72 base::Callback<void(std::unique_ptr<CallbacksType> callbacks)>; | 74 base::Callback<void(std::unique_ptr<CallbacksType> callbacks)>; |
| 73 | 75 |
| 74 ScopedWebCallbacks(std::unique_ptr<CallbacksType> callbacks, | 76 ScopedWebCallbacks(std::unique_ptr<CallbacksType> callbacks, |
| 75 const DestructionCallback& destruction_callback) | 77 const DestructionCallback& destruction_callback) |
| 76 : callbacks_(std::move(callbacks)), | 78 : callbacks_(std::move(callbacks)), |
| 77 destruction_callback_(destruction_callback) {} | 79 destruction_callback_(destruction_callback) {} |
| 78 | 80 |
| 79 ~ScopedWebCallbacks() { | 81 ~ScopedWebCallbacks() { |
| 80 if (callbacks_) | 82 if (callbacks_) |
| 81 destruction_callback_.Run(std::move(callbacks_)); | 83 destruction_callback_.Run(std::move(callbacks_)); |
| 82 } | 84 } |
| 83 | 85 |
| 84 ScopedWebCallbacks(ScopedWebCallbacks&& other) { *this = std::move(other); } | 86 ScopedWebCallbacks(ScopedWebCallbacks&& other) { *this = std::move(other); } |
| 85 | 87 |
| 86 ScopedWebCallbacks& operator=(ScopedWebCallbacks&& other) { | 88 ScopedWebCallbacks& operator=(ScopedWebCallbacks&& other) { |
| 87 callbacks_ = std::move(other.callbacks_); | 89 callbacks_ = std::move(other.callbacks_); |
| 88 destruction_callback_ = other.destruction_callback_; | 90 destruction_callback_ = other.destruction_callback_; |
| 89 return *this; | 91 return *this; |
| 90 } | 92 } |
| 91 | 93 |
| 92 std::unique_ptr<CallbacksType> PassCallbacks() { | 94 std::unique_ptr<CallbacksType> PassCallbacks() { |
| 93 return std::move(callbacks_); | 95 return std::move(callbacks_); |
| 94 } | 96 } |
| 95 | 97 |
| 96 private: | 98 private: |
| 97 std::unique_ptr<CallbacksType> callbacks_; | 99 std::unique_ptr<CallbacksType> callbacks_; |
| 98 DestructionCallback destruction_callback_; | 100 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 |