1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2012
2 // Google Inc. All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // This defines helpful methods for dealing with Callbacks. Because Callbacks
32 // are implemented using templates, with a class per callback signature, adding
33 // methods to Callback<> itself is unattractive (lots of extra code gets
34 // generated). Instead, consider adding methods here.
36 // ResetAndReturn(&cb) is like cb.Reset() but allows executing a callback (via a
37 // copy) after the original callback is Reset(). This can be handy if Run()
38 // reads/writes the variable holding the Callback.
40 #ifndef CEF_INCLUDE_BASE_CEF_CALLBACK_HELPERS_H_
41 #define CEF_INCLUDE_BASE_CEF_CALLBACK_HELPERS_H_
44 #if defined(BASE_CALLBACK_HELPERS_H_)
45 // Do nothing if the Chromium header has already been included.
46 // This can happen in cases where Chromium code is used directly by the
47 // client application. When using Chromium code directly always include
48 // the Chromium header first to avoid type conflicts.
49 #elif defined(BUILDING_CEF_SHARED)
50 // When building CEF include the Chromium header directly.
51 #include "base/callback_helpers.h"
52 #else // !BUILDING_CEF_SHARED
53 // The following is substantially similar to the Chromium implementation.
54 // If the Chromium implementation diverges the below implementation should be
57 #include "include/base/cef_basictypes.h"
58 #include "include/base/cef_build.h"
59 #include "include/base/cef_callback.h"
60 #include "include/base/cef_macros.h"
64 template <typename Sig>
65 base::Callback<Sig> ResetAndReturn(base::Callback<Sig>* cb) {
66 base::Callback<Sig> ret(*cb);
71 // ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the
72 // Closure is executed and deleted no matter how the current scope exits.
73 class ScopedClosureRunner {
75 ScopedClosureRunner();
76 explicit ScopedClosureRunner(const Closure& closure);
77 ~ScopedClosureRunner();
80 void Reset(const Closure& closure);
81 Closure Release() WARN_UNUSED_RESULT;
86 DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner);
91 #endif // !BUILDING_CEF_SHARED
93 #endif // CEF_INCLUDE_BASE_CEF_CALLBACK_HELPERS_H_