| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 EXTENSIONS_RENDERER_API_LAST_ERROR_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_LAST_ERROR_H_ |
| 6 #define EXTENSIONS_RENDERER_API_LAST_ERROR_H_ | 6 #define EXTENSIONS_RENDERER_API_LAST_ERROR_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
| 13 | 13 |
| 14 namespace extensions { | 14 namespace extensions { |
| 15 | 15 |
| 16 // Handles creating and clearing a 'lastError' property to hold the last error | 16 // Handles creating and clearing a 'lastError' property to hold the last error |
| 17 // set by an extension API function. | 17 // set by an extension API function. |
| 18 class APILastError { | 18 class APILastError { |
| 19 public: | 19 public: |
| 20 // Returns the object the 'lastError' property should be exposed on for the | 20 // Returns the object the 'lastError' property should be exposed on for the |
| 21 // given context. | 21 // given context. |
| 22 using GetParent = | 22 using GetParent = |
| 23 base::Callback<v8::Local<v8::Object>(v8::Local<v8::Context>)>; | 23 base::Callback<v8::Local<v8::Object>(v8::Local<v8::Context>)>; |
| 24 // Adds an error message to the context's console. |
| 25 using AddConsoleError = |
| 26 base::Callback<void(v8::Local<v8::Context>, const std::string& error)>; |
| 24 | 27 |
| 25 explicit APILastError(const GetParent& get_parent); | 28 APILastError(const GetParent& get_parent, |
| 29 const AddConsoleError& add_console_error); |
| 26 APILastError(APILastError&& other); | 30 APILastError(APILastError&& other); |
| 27 ~APILastError(); | 31 ~APILastError(); |
| 28 | 32 |
| 29 // Sets the last error for the given |context| to |error|. | 33 // Sets the last error for the given |context| to |error|. |
| 30 void SetError(v8::Local<v8::Context> context, const std::string& error); | 34 void SetError(v8::Local<v8::Context> context, const std::string& error); |
| 31 | 35 |
| 32 // Clears the last error in the given |context|. If |report_if_unchecked| is | 36 // Clears the last error in the given |context|. If |report_if_unchecked| is |
| 33 // true and the developer didn't check the error, this throws an exception. | 37 // true and the developer didn't check the error, this throws an exception. |
| 34 void ClearError(v8::Local<v8::Context> context, bool report_if_unchecked); | 38 void ClearError(v8::Local<v8::Context> context, bool report_if_unchecked); |
| 35 | 39 |
| 36 // Returns true if the given context has an active error. | 40 // Returns true if the given context has an active error. |
| 37 bool HasError(v8::Local<v8::Context> context); | 41 bool HasError(v8::Local<v8::Context> context); |
| 38 | 42 |
| 39 private: | 43 private: |
| 40 GetParent get_parent_; | 44 GetParent get_parent_; |
| 41 | 45 |
| 46 AddConsoleError add_console_error_; |
| 47 |
| 42 DISALLOW_COPY_AND_ASSIGN(APILastError); | 48 DISALLOW_COPY_AND_ASSIGN(APILastError); |
| 43 }; | 49 }; |
| 44 | 50 |
| 45 } // namespace extensions | 51 } // namespace extensions |
| 46 | 52 |
| 47 #endif // EXTENSIONS_RENDERER_API_LAST_ERROR_H_ | 53 #endif // EXTENSIONS_RENDERER_API_LAST_ERROR_H_ |
| OLD | NEW |