api_last_error.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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
  3. // found in the LICENSE file.
  4. #ifndef EXTENSIONS_RENDERER_BINDINGS_API_LAST_ERROR_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_API_LAST_ERROR_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "extensions/renderer/bindings/api_binding_types.h"
  9. #include "v8/include/v8.h"
  10. namespace extensions {
  11. // Handles creating and clearing a 'lastError' property to hold the last error
  12. // set by an extension API function.
  13. class APILastError {
  14. public:
  15. // Returns the object the 'lastError' property should be exposed on for the
  16. // given context. Also allows for the population of a |secondary_parent|; if
  17. // populated, this object will also have a lastError property, but it will be
  18. // a simple object without getters/setters. This is to accommodate the
  19. // legacy chrome.extension.lastError property.
  20. // Note: |secondary_parent| may be null.
  21. using GetParent = base::RepeatingCallback<v8::Local<v8::Object>(
  22. v8::Local<v8::Context>,
  23. v8::Local<v8::Object>* secondary_parent)>;
  24. APILastError(GetParent get_parent,
  25. binding::AddConsoleError add_console_error);
  26. APILastError(APILastError&& other);
  27. APILastError(const APILastError&) = delete;
  28. APILastError& operator=(const APILastError&) = delete;
  29. ~APILastError();
  30. // Sets the last error for the given |context| to |error|.
  31. void SetError(v8::Local<v8::Context> context, const std::string& error);
  32. // 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.
  34. void ClearError(v8::Local<v8::Context> context, bool report_if_unchecked);
  35. // Returns true if the given context has an active error.
  36. bool HasError(v8::Local<v8::Context> context);
  37. // Reports an unchecked error by logging it to the console. This is used when
  38. // an error occurs, and there is no way it could be checked.
  39. void ReportUncheckedError(v8::Local<v8::Context> context,
  40. const std::string& error);
  41. private:
  42. // Sets the lastError property on the primary parent object (in practice, this
  43. // is chrome.runtime.lastError);
  44. void SetErrorOnPrimaryParent(v8::Local<v8::Context> context,
  45. v8::Local<v8::Object> parent,
  46. const std::string& error);
  47. // Sets the lastError property on the secondary parent object (in practice,
  48. // this is chrome.extension.lastError).
  49. void SetErrorOnSecondaryParent(v8::Local<v8::Context> context,
  50. v8::Local<v8::Object> parent,
  51. const std::string& error);
  52. GetParent get_parent_;
  53. binding::AddConsoleError add_console_error_;
  54. };
  55. } // namespace extensions
  56. #endif // EXTENSIONS_RENDERER_BINDINGS_API_LAST_ERROR_H_