binding_access_checker.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_BINDING_ACCESS_CHECKER_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_BINDING_ACCESS_CHECKER_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "v8/include/v8.h"
  9. namespace extensions {
  10. // A helper class to handle access-checking API features.
  11. class BindingAccessChecker {
  12. public:
  13. // The callback for determining if a given API feature (specified by |name|)
  14. // is available in the given context.
  15. using APIAvailabilityCallback =
  16. base::RepeatingCallback<bool(v8::Local<v8::Context>,
  17. const std::string& name)>;
  18. // The callback for determining if a given context is allowed to use promises
  19. // with API calls.
  20. using PromiseAvailabilityCallback =
  21. base::RepeatingCallback<bool(v8::Local<v8::Context>)>;
  22. BindingAccessChecker(APIAvailabilityCallback api_available,
  23. PromiseAvailabilityCallback promises_available);
  24. BindingAccessChecker(const BindingAccessChecker&) = delete;
  25. BindingAccessChecker& operator=(const BindingAccessChecker&) = delete;
  26. ~BindingAccessChecker();
  27. // Returns true if the feature specified by |full_name| is available to the
  28. // given |context|.
  29. bool HasAccess(v8::Local<v8::Context> context,
  30. const std::string& full_name) const;
  31. // Same as HasAccess(), but throws an exception in the |context| if it doesn't
  32. // have access.
  33. bool HasAccessOrThrowError(v8::Local<v8::Context> context,
  34. const std::string& full_name) const;
  35. // Returns true if the given |context| is allowed to use promise-based APIs.
  36. bool HasPromiseAccess(v8::Local<v8::Context> context) const;
  37. private:
  38. APIAvailabilityCallback api_available_;
  39. PromiseAvailabilityCallback promises_available_;
  40. };
  41. } // namespace extensions
  42. #endif // EXTENSIONS_RENDERER_BINDINGS_BINDING_ACCESS_CHECKER_H_