api_binding_util.h 2.6 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_BINDING_UTIL_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_API_BINDING_UTIL_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/auto_reset.h"
  9. #include "base/callback.h"
  10. #include "v8/include/v8.h"
  11. namespace extensions {
  12. namespace binding {
  13. class ContextInvalidationData;
  14. // Returns true if the given |context| is considered valid. Contexts can be
  15. // invalidated if various objects or scripts hold onto references after when
  16. // blink releases the context, but we don't want to handle interactions past
  17. // this point. Additionally, simply checking if gin::PerContextData exists is
  18. // insufficient, because gin::PerContextData is released after the notifications
  19. // for releasing the script context, and author script can run between those
  20. // points. See https://crbug.com/772071.
  21. bool IsContextValid(v8::Local<v8::Context> context);
  22. // Same as above, but throws an exception in the |context| if it is invalid.
  23. bool IsContextValidOrThrowError(v8::Local<v8::Context> context);
  24. // Marks the given |context| as invalid.
  25. void InvalidateContext(v8::Local<v8::Context> context);
  26. // A helper class to watch for context invalidation. If the context is
  27. // invalidated before this object is destroyed, the passed in closure will be
  28. // called.
  29. class ContextInvalidationListener {
  30. public:
  31. ContextInvalidationListener(v8::Local<v8::Context> context,
  32. base::OnceClosure on_invalidated);
  33. ContextInvalidationListener(const ContextInvalidationListener&) = delete;
  34. ContextInvalidationListener& operator=(const ContextInvalidationListener&) =
  35. delete;
  36. ~ContextInvalidationListener();
  37. void OnInvalidated();
  38. private:
  39. base::OnceClosure on_invalidated_;
  40. ContextInvalidationData* context_invalidation_data_ = nullptr;
  41. };
  42. // Returns the string version of the current platform, one of "chromeos",
  43. // "linux", "win", or "mac".
  44. std::string GetPlatformString();
  45. // Returns true if response validation is enabled, and the bindings system
  46. // should check the values returned by the browser against the expected results
  47. // defined in the schemas. By default, this is corresponds to whether DCHECK is
  48. // enabled.
  49. bool IsResponseValidationEnabled();
  50. // Override response validation for testing purposes.
  51. std::unique_ptr<base::AutoReset<bool>> SetResponseValidationEnabledForTesting(
  52. bool is_enabled);
  53. } // namespace binding
  54. } // namespace extensions
  55. #endif // EXTENSIONS_RENDERER_BINDINGS_API_BINDING_UTIL_H_