Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #ifndef EXTENSIONS_BROWSER_MOJO_EXTENSION_FUNCTION_H_ | |
| 6 #define EXTENSIONS_BROWSER_MOJO_EXTENSION_FUNCTION_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "mojo/public/cpp/bindings/callback.h" | |
| 11 | |
| 12 namespace content { | |
| 13 class BrowserContext; | |
| 14 } | |
| 15 | |
| 16 namespace extensions { | |
| 17 | |
| 18 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.
| |
| 19 : public base::RefCountedThreadSafe<MojoExtensionFunctionBase> { | |
| 20 public: | |
| 21 enum class RunResult { | |
| 22 RESPOND_NOW, | |
| 23 RESPOND_LATER, | |
| 24 ERROR, | |
| 25 }; | |
| 26 | |
| 27 MojoExtensionFunctionBase(); | |
| 28 | |
| 29 bool user_gesture() const { return user_gesture_; } | |
| 30 void set_user_gesture(bool user_gesture) { user_gesture_ = user_gesture; } | |
| 31 content::BrowserContext* browser_context() { return browser_context_; } | |
| 32 const content::BrowserContext* browser_context() const { | |
| 33 return browser_context_; | |
| 34 } | |
| 35 void set_browser_context(content::BrowserContext* browser_context) { | |
| 36 browser_context_ = browser_context; | |
| 37 } | |
| 38 | |
| 39 protected: | |
| 40 virtual ~MojoExtensionFunctionBase(); | |
| 41 | |
| 42 void PreRun(); | |
| 43 | |
| 44 private: | |
| 45 friend class base::RefCountedThreadSafe<MojoExtensionFunctionBase>; | |
| 46 | |
| 47 bool user_gesture_; | |
| 48 content::BrowserContext* browser_context_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(MojoExtensionFunctionBase); | |
| 51 }; | |
| 52 | |
| 53 template <typename Response, typename... Args> | |
| 54 class MojoExtensionFunction : public MojoExtensionFunctionBase { | |
| 55 public: | |
| 56 MojoExtensionFunction(const mojo::Callback<void(Response)>& response_callback) | |
| 57 : response_callback_(response_callback) {} | |
| 58 | |
| 59 virtual RunResult Run(Args... arguments) = 0; | |
| 60 | |
| 61 void RunImpl(Args... arguments) { | |
| 62 PreRun(); | |
| 63 Run(arguments...); | |
| 64 } | |
| 65 | |
| 66 protected: | |
| 67 RunResult Respond(Response response) { | |
| 68 LOG(WARNING) << "Resonding"; | |
| 69 mojo::Callback<void(Response)> copy(response_callback_); | |
| 70 response_callback_.reset(); | |
| 71 copy.Run(std::move(response)); | |
| 72 return RunResult::RESPOND_NOW; | |
| 73 } | |
| 74 | |
| 75 virtual ~MojoExtensionFunction() {} | |
| 76 | |
| 77 private: | |
| 78 mojo::Callback<void(Response)> response_callback_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(MojoExtensionFunction); | |
| 81 }; | |
| 82 | |
| 83 template <typename... Args> | |
| 84 class MojoExtensionFunction<void, Args...> : public MojoExtensionFunctionBase { | |
| 85 public: | |
| 86 MojoExtensionFunction(const mojo::Callback<void()>& response_callback) | |
| 87 : response_callback_(response_callback) {} | |
| 88 | |
| 89 virtual RunResult Run(Args... arguments) = 0; | |
| 90 | |
| 91 RunResult RunImpl(Args... arguments) { | |
| 92 PreRun(); | |
| 93 return Run(arguments...); | |
| 94 } | |
| 95 | |
| 96 protected: | |
| 97 RunResult Respond() { | |
| 98 LOG(WARNING) << "Resonding"; | |
| 99 mojo::Callback<void()> copy(response_callback_); | |
| 100 response_callback_.reset(); | |
| 101 copy.Run(); | |
| 102 return RunResult::RESPOND_NOW; | |
| 103 } | |
| 104 | |
| 105 RunResult Error(const std::string& error) { | |
| 106 LOG(WARNING) << "Resonding with error: " << error; | |
| 107 mojo::Callback<void()> copy(response_callback_); | |
| 108 response_callback_.reset(); | |
| 109 copy.Run(); | |
| 110 return RunResult::ERROR; | |
| 111 } | |
| 112 | |
| 113 virtual ~MojoExtensionFunction() {} | |
| 114 | |
| 115 private: | |
| 116 mojo::Callback<void()> response_callback_; | |
| 117 | |
| 118 DISALLOW_COPY_AND_ASSIGN(MojoExtensionFunction); | |
| 119 }; | |
| 120 | |
| 121 } // namespace extensions | |
| 122 | |
| 123 #endif // EXTENSIONS_BROWSER_MOJO_EXTENSION_FUNCTION_H_ | |
| OLD | NEW |