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