| 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_BINDINGS_API_LAST_ERROR_H_ | 5 #ifndef EXTENSIONS_RENDERER_BINDINGS_API_LAST_ERROR_H_ |
| 6 #define EXTENSIONS_RENDERER_BINDINGS_API_LAST_ERROR_H_ | 6 #define EXTENSIONS_RENDERER_BINDINGS_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 "extensions/renderer/bindings/api_binding_types.h" |
| 12 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 13 | 14 |
| 14 namespace extensions { | 15 namespace extensions { |
| 15 | 16 |
| 16 // Handles creating and clearing a 'lastError' property to hold the last error | 17 // Handles creating and clearing a 'lastError' property to hold the last error |
| 17 // set by an extension API function. | 18 // set by an extension API function. |
| 18 class APILastError { | 19 class APILastError { |
| 19 public: | 20 public: |
| 20 // Returns the object the 'lastError' property should be exposed on for the | 21 // Returns the object the 'lastError' property should be exposed on for the |
| 21 // given context. Also allows for the population of a |secondary_parent|; if | 22 // given context. Also allows for the population of a |secondary_parent|; if |
| 22 // populated, this object will also have a lastError property, but it will be | 23 // populated, this object will also have a lastError property, but it will be |
| 23 // a simple object without getters/setters. This is to accommodate the | 24 // a simple object without getters/setters. This is to accommodate the |
| 24 // legacy chrome.extension.lastError property. | 25 // legacy chrome.extension.lastError property. |
| 25 // Note: |secondary_parent| may be null. | 26 // Note: |secondary_parent| may be null. |
| 26 using GetParent = base::Callback<v8::Local<v8::Object>( | 27 using GetParent = base::Callback<v8::Local<v8::Object>( |
| 27 v8::Local<v8::Context>, | 28 v8::Local<v8::Context>, |
| 28 v8::Local<v8::Object>* secondary_parent)>; | 29 v8::Local<v8::Object>* secondary_parent)>; |
| 29 // Adds an error message to the context's console. | |
| 30 using AddConsoleError = | |
| 31 base::Callback<void(v8::Local<v8::Context>, const std::string& error)>; | |
| 32 | 30 |
| 33 APILastError(const GetParent& get_parent, | 31 APILastError(const GetParent& get_parent, |
| 34 const AddConsoleError& add_console_error); | 32 const binding::AddConsoleError& add_console_error); |
| 35 APILastError(APILastError&& other); | 33 APILastError(APILastError&& other); |
| 36 ~APILastError(); | 34 ~APILastError(); |
| 37 | 35 |
| 38 // Sets the last error for the given |context| to |error|. | 36 // Sets the last error for the given |context| to |error|. |
| 39 void SetError(v8::Local<v8::Context> context, const std::string& error); | 37 void SetError(v8::Local<v8::Context> context, const std::string& error); |
| 40 | 38 |
| 41 // Clears the last error in the given |context|. If |report_if_unchecked| is | 39 // Clears the last error in the given |context|. If |report_if_unchecked| is |
| 42 // true and the developer didn't check the error, this throws an exception. | 40 // true and the developer didn't check the error, this throws an exception. |
| 43 void ClearError(v8::Local<v8::Context> context, bool report_if_unchecked); | 41 void ClearError(v8::Local<v8::Context> context, bool report_if_unchecked); |
| 44 | 42 |
| 45 // Returns true if the given context has an active error. | 43 // Returns true if the given context has an active error. |
| 46 bool HasError(v8::Local<v8::Context> context); | 44 bool HasError(v8::Local<v8::Context> context); |
| 47 | 45 |
| 48 private: | 46 private: |
| 49 // Sets the lastError property on the primary parent object (in practice, this | 47 // Sets the lastError property on the primary parent object (in practice, this |
| 50 // is chrome.runtime.lastError); | 48 // is chrome.runtime.lastError); |
| 51 void SetErrorOnPrimaryParent(v8::Local<v8::Context> context, | 49 void SetErrorOnPrimaryParent(v8::Local<v8::Context> context, |
| 52 v8::Local<v8::Object> parent, | 50 v8::Local<v8::Object> parent, |
| 53 const std::string& error); | 51 const std::string& error); |
| 54 | 52 |
| 55 // Sets the lastError property on the secondary parent object (in practice, | 53 // Sets the lastError property on the secondary parent object (in practice, |
| 56 // this is chrome.extension.lastError). | 54 // this is chrome.extension.lastError). |
| 57 void SetErrorOnSecondaryParent(v8::Local<v8::Context> context, | 55 void SetErrorOnSecondaryParent(v8::Local<v8::Context> context, |
| 58 v8::Local<v8::Object> parent, | 56 v8::Local<v8::Object> parent, |
| 59 const std::string& error); | 57 const std::string& error); |
| 60 | 58 |
| 61 GetParent get_parent_; | 59 GetParent get_parent_; |
| 62 | 60 |
| 63 AddConsoleError add_console_error_; | 61 binding::AddConsoleError add_console_error_; |
| 64 | 62 |
| 65 DISALLOW_COPY_AND_ASSIGN(APILastError); | 63 DISALLOW_COPY_AND_ASSIGN(APILastError); |
| 66 }; | 64 }; |
| 67 | 65 |
| 68 } // namespace extensions | 66 } // namespace extensions |
| 69 | 67 |
| 70 #endif // EXTENSIONS_RENDERER_BINDINGS_API_LAST_ERROR_H_ | 68 #endif // EXTENSIONS_RENDERER_BINDINGS_API_LAST_ERROR_H_ |
| OLD | NEW |