binding_access_checker.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #include "extensions/renderer/bindings/binding_access_checker.h"
  5. #include "base/strings/stringprintf.h"
  6. #include "gin/converter.h"
  7. namespace extensions {
  8. BindingAccessChecker::BindingAccessChecker(
  9. APIAvailabilityCallback api_available,
  10. PromiseAvailabilityCallback promises_available)
  11. : api_available_(std::move(api_available)),
  12. promises_available_(std::move(promises_available)) {}
  13. BindingAccessChecker::~BindingAccessChecker() = default;
  14. // TODO(tjudkins): Now that this also handles some promise checking, these two
  15. // methods and the class should probably be renamed.
  16. bool BindingAccessChecker::HasAccess(v8::Local<v8::Context> context,
  17. const std::string& full_name) const {
  18. return api_available_.Run(context, full_name);
  19. }
  20. bool BindingAccessChecker::HasAccessOrThrowError(
  21. v8::Local<v8::Context> context,
  22. const std::string& full_name) const {
  23. if (!HasAccess(context, full_name)) {
  24. context->GetIsolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
  25. context->GetIsolate(),
  26. base::StringPrintf("'%s' is not available in this context.",
  27. full_name.c_str()))));
  28. return false;
  29. }
  30. return true;
  31. }
  32. bool BindingAccessChecker::HasPromiseAccess(
  33. v8::Local<v8::Context> context) const {
  34. return promises_available_.Run(context);
  35. }
  36. } // namespace extensions