Infobar material design refresh: bg color
[chromium-blink-merge.git] / content / child / scoped_web_callbacks.h
blob7f578dda93f41a2eb9d6f7918694241ad5aa4d7d
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_
6 #define CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/move.h"
11 #include "third_party/WebKit/public/platform/WebCallbacks.h"
13 // 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
15 // simultaneously dealing with the following two conditions:
17 // 1. Your WebCallbacks implementation requires either onSuccess or onError to
18 // be called before it's destroyed. This is the case with
19 // CallbackPromiseAdapter for example, because its underlying
20 // ScriptPromiseResolver must be resolved or rejected before destruction.
22 // 2. You are passing ownership of the WebCallbacks to code which may
23 // silenty drop it. A common way for this to happen is to bind the
24 // WebCallbacks as an argument to a base::Callback which gets destroyed
25 // before it can run.
27 // While it's possible to individually track the lifetime of pending
28 // WebCallbacks, this becomes cumbersome when dealing with many different
29 // callbacks types. ScopedWebCallbacks provides a generic and relatively
30 // lightweight solution to this problem.
32 // Example usage:
34 // using FooCallbacks = blink::WebCallbacks<const Foo&, const FooError&>;
36 // void RespondWithSuccess(ScopedWebCallbacks<FooCallbacks> callbacks) {
37 // callbacks.PassCallbacks()->onSuccess(Foo("everything is great"));
38 // }
40 // void OnCallbacksDropped(scoped_ptr<FooCallbacks> callbacks) {
41 // // Ownership of the FooCallbacks is passed to this function if
42 // // ScopedWebCallbacks::PassCallbacks isn't called before the
43 // // ScopedWebCallbacks is destroyed.
44 // callbacks->onError(FooError("everything is terrible"));
45 // }
47 // // Blink client implementation
48 // void FooClientImpl::doMagic(FooCallbacks* callbacks) {
49 // auto scoped_callbacks = make_scoped_web_callbacks(
50 // callbacks, base::Bind(&OnCallbacksDropped));
52 // // Call to some lower-level service which may never run the callback we
53 // // give it.
54 // foo_service_->DoMagic(base::Bind(&RespondWithSuccess,
55 // base::Passed(&scoped_callbacks)));
56 // }
58 // If the bound RespondWithSuccess callback actually runs, PassCallbacks() will
59 // reliquish ownership of the WebCallbacks object to a temporary scoped_ptr
60 // which will be destroyed immediately after onSuccess is called.
62 // If the bound RespondWithSuccess callback is instead destroyed first,
63 // the ScopedWebCallbacks destructor will invoke OnCallbacksDropped, executing
64 // our desired default behavior before deleting the WebCallbacks.
65 template <typename CallbacksType>
66 class ScopedWebCallbacks {
67 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedWebCallbacks, RValue);
69 public:
70 using DestructionCallback =
71 base::Callback<void(scoped_ptr<CallbacksType> callbacks)>;
73 ScopedWebCallbacks(scoped_ptr<CallbacksType> callbacks,
74 const DestructionCallback& destruction_callback)
75 : callbacks_(callbacks.Pass()),
76 destruction_callback_(destruction_callback) {}
78 ~ScopedWebCallbacks() {
79 if (callbacks_)
80 destruction_callback_.Run(callbacks_.Pass());
83 ScopedWebCallbacks(RValue other) { *this = other; }
85 ScopedWebCallbacks& operator=(RValue other) {
86 callbacks_ = other.object->callbacks_.Pass();
87 destruction_callback_ = other.object->destruction_callback_;
88 return *this;
91 scoped_ptr<CallbacksType> PassCallbacks() { return callbacks_.Pass(); }
93 private:
94 scoped_ptr<CallbacksType> callbacks_;
95 DestructionCallback destruction_callback_;
98 template <typename CallbacksType>
99 ScopedWebCallbacks<CallbacksType> make_scoped_web_callbacks(
100 CallbacksType* callbacks,
101 const typename ScopedWebCallbacks<CallbacksType>::DestructionCallback&
102 destruction_callback) {
103 return ScopedWebCallbacks<CallbacksType>(make_scoped_ptr(callbacks),
104 destruction_callback);
107 #endif // CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_