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