exception_handler.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_EXCEPTION_HANDLER_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_EXCEPTION_HANDLER_H_
  6. #include <string>
  7. #include "base/memory/weak_ptr.h"
  8. #include "extensions/renderer/bindings/api_binding_types.h"
  9. #include "v8/include/v8.h"
  10. namespace extensions {
  11. // A class to handle uncaught exceptions encountered in the bindings system
  12. // while running untrusted code, such as exceptions thrown during callback
  13. // execution or event handling.
  14. class ExceptionHandler {
  15. public:
  16. ExceptionHandler(const binding::AddConsoleError& add_console_error);
  17. ExceptionHandler(const ExceptionHandler&) = delete;
  18. ExceptionHandler& operator=(const ExceptionHandler&) = delete;
  19. ~ExceptionHandler();
  20. // Returns a v8::Value wrapping a weak reference to this ExceptionHandler.
  21. v8::Local<v8::Value> GetV8Wrapper(v8::Isolate* isolate);
  22. // Returns the ExceptionHandler associated with `value`, or null if the value
  23. // doesn't refer to a valid ExceptionHandler (which could happen with a
  24. // irrelevant v8::Value or if the ExceptionHandler was destroyed).
  25. static ExceptionHandler* FromV8Wrapper(v8::Isolate* isolate,
  26. v8::Local<v8::Value> value);
  27. // Handles an exception in the given |context|. |message| is a message to
  28. // prefix the error message with, e.g. "Exception in response to foo".
  29. // The |try_catch| is the TryCatch that caught the exception.
  30. void HandleException(v8::Local<v8::Context> context,
  31. const std::string& message,
  32. v8::TryCatch* try_catch);
  33. // Same as above, but accepts a v8::Value for the exception rather than
  34. // retrieving it from a TryCatch.
  35. void HandleException(v8::Local<v8::Context> context,
  36. const std::string& full_message,
  37. v8::Local<v8::Value> exception_value);
  38. // Sets a custom handler for the given context, which will be notified of
  39. // exceptions thrown. This is only allowed to be called while the context is
  40. // valid.
  41. void SetHandlerForContext(v8::Local<v8::Context> context,
  42. v8::Local<v8::Function> handler);
  43. // Safely runs an `extension_callback` with the provided `callback_arguments`,
  44. // handling any exceptions that arise. If an exception is found, prefixes the
  45. // exception with `message`.
  46. void RunExtensionCallback(
  47. v8::Local<v8::Context> context,
  48. v8::Local<v8::Function> extension_callback,
  49. std::vector<v8::Local<v8::Value>> callback_arguments,
  50. const std::string& message);
  51. private:
  52. // Returns the custom handler for the given |context|, or an empty handle if
  53. // no custom handle exists.
  54. v8::Local<v8::Function> GetCustomHandler(v8::Local<v8::Context> context);
  55. binding::AddConsoleError add_console_error_;
  56. base::WeakPtrFactory<ExceptionHandler> weak_factory_{this};
  57. };
  58. } // namespace extensions
  59. #endif // EXTENSIONS_RENDERER_BINDINGS_EXCEPTION_HANDLER_H_