Chromium Code Reviews| Index: extensions/browser/mojo_extension_function.h |
| diff --git a/extensions/browser/mojo_extension_function.h b/extensions/browser/mojo_extension_function.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bee959172be51648dfcc35ff5313a73921682913 |
| --- /dev/null |
| +++ b/extensions/browser/mojo_extension_function.h |
| @@ -0,0 +1,123 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef EXTENSIONS_BROWSER_MOJO_EXTENSION_FUNCTION_H_ |
| +#define EXTENSIONS_BROWSER_MOJO_EXTENSION_FUNCTION_H_ |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "mojo/public/cpp/bindings/callback.h" |
| + |
| +namespace content { |
| +class BrowserContext; |
| +} |
| + |
| +namespace extensions { |
| + |
| +class MojoExtensionFunctionBase |
|
Ken Rockot(use gerrit already)
2016/06/20 17:19:24
I'm not sure you really want to do this. We should
Devlin
2016/06/20 17:49:23
See other comment.
|
| + : public base::RefCountedThreadSafe<MojoExtensionFunctionBase> { |
| + public: |
| + enum class RunResult { |
| + RESPOND_NOW, |
| + RESPOND_LATER, |
| + ERROR, |
| + }; |
| + |
| + MojoExtensionFunctionBase(); |
| + |
| + bool user_gesture() const { return user_gesture_; } |
| + void set_user_gesture(bool user_gesture) { user_gesture_ = user_gesture; } |
| + content::BrowserContext* browser_context() { return browser_context_; } |
| + const content::BrowserContext* browser_context() const { |
| + return browser_context_; |
| + } |
| + void set_browser_context(content::BrowserContext* browser_context) { |
| + browser_context_ = browser_context; |
| + } |
| + |
| + protected: |
| + virtual ~MojoExtensionFunctionBase(); |
| + |
| + void PreRun(); |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<MojoExtensionFunctionBase>; |
| + |
| + bool user_gesture_; |
| + content::BrowserContext* browser_context_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MojoExtensionFunctionBase); |
| +}; |
| + |
| +template <typename Response, typename... Args> |
| +class MojoExtensionFunction : public MojoExtensionFunctionBase { |
| + public: |
| + MojoExtensionFunction(const mojo::Callback<void(Response)>& response_callback) |
| + : response_callback_(response_callback) {} |
| + |
| + virtual RunResult Run(Args... arguments) = 0; |
| + |
| + void RunImpl(Args... arguments) { |
| + PreRun(); |
| + Run(arguments...); |
| + } |
| + |
| + protected: |
| + RunResult Respond(Response response) { |
| + LOG(WARNING) << "Resonding"; |
| + mojo::Callback<void(Response)> copy(response_callback_); |
| + response_callback_.reset(); |
| + copy.Run(std::move(response)); |
| + return RunResult::RESPOND_NOW; |
| + } |
| + |
| + virtual ~MojoExtensionFunction() {} |
| + |
| + private: |
| + mojo::Callback<void(Response)> response_callback_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MojoExtensionFunction); |
| +}; |
| + |
| +template <typename... Args> |
| +class MojoExtensionFunction<void, Args...> : public MojoExtensionFunctionBase { |
| + public: |
| + MojoExtensionFunction(const mojo::Callback<void()>& response_callback) |
| + : response_callback_(response_callback) {} |
| + |
| + virtual RunResult Run(Args... arguments) = 0; |
| + |
| + RunResult RunImpl(Args... arguments) { |
| + PreRun(); |
| + return Run(arguments...); |
| + } |
| + |
| + protected: |
| + RunResult Respond() { |
| + LOG(WARNING) << "Resonding"; |
| + mojo::Callback<void()> copy(response_callback_); |
| + response_callback_.reset(); |
| + copy.Run(); |
| + return RunResult::RESPOND_NOW; |
| + } |
| + |
| + RunResult Error(const std::string& error) { |
| + LOG(WARNING) << "Resonding with error: " << error; |
| + mojo::Callback<void()> copy(response_callback_); |
| + response_callback_.reset(); |
| + copy.Run(); |
| + return RunResult::ERROR; |
| + } |
| + |
| + virtual ~MojoExtensionFunction() {} |
| + |
| + private: |
| + mojo::Callback<void()> response_callback_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MojoExtensionFunction); |
| +}; |
| + |
| +} // namespace extensions |
| + |
| +#endif // EXTENSIONS_BROWSER_MOJO_EXTENSION_FUNCTION_H_ |