safe_builtins.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2014 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_SAFE_BUILTINS_H_
  5. #define EXTENSIONS_RENDERER_SAFE_BUILTINS_H_
  6. #include <memory>
  7. #include "v8/include/v8-forward.h"
  8. namespace extensions {
  9. class ScriptContext;
  10. // A collection of safe builtin objects, in that they won't be tainted by
  11. // extensions overriding methods on them.
  12. class SafeBuiltins {
  13. public:
  14. // Creates the v8::Extension which manages SafeBuiltins instances.
  15. static std::unique_ptr<v8::Extension> CreateV8Extension();
  16. explicit SafeBuiltins(ScriptContext* context);
  17. SafeBuiltins(const SafeBuiltins&) = delete;
  18. SafeBuiltins& operator=(const SafeBuiltins&) = delete;
  19. ~SafeBuiltins();
  20. // Each method returns an object with methods taken from their respective
  21. // builtin object's prototype, adapted to automatically call() themselves.
  22. //
  23. // Examples:
  24. // Array.prototype.forEach.call(...) becomes Array.forEach(...)
  25. // Object.prototype.toString.call(...) becomes Object.toString(...)
  26. // Object.keys.call(...) becomes Object.keys(...)
  27. v8::Local<v8::Object> GetArray() const;
  28. v8::Local<v8::Object> GetFunction() const;
  29. v8::Local<v8::Object> GetJSON() const;
  30. // NOTE(kalman): VS2010 won't compile "GetObject", it mysteriously renames it
  31. // to "GetObjectW" - hence GetObjekt. Sorry.
  32. v8::Local<v8::Object> GetObjekt() const;
  33. v8::Local<v8::Object> GetRegExp() const;
  34. v8::Local<v8::Object> GetString() const;
  35. v8::Local<v8::Object> GetError() const;
  36. v8::Local<v8::Object> GetPromise() const;
  37. private:
  38. ScriptContext* context_;
  39. };
  40. } // namespace extensions
  41. #endif // EXTENSIONS_RENDERER_SAFE_BUILTINS_H_