SandboxExtension.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef SandboxExtension_h
  26. #define SandboxExtension_h
  27. #include <wtf/Forward.h>
  28. #include <wtf/Noncopyable.h>
  29. #include <wtf/PassRefPtr.h>
  30. #include <wtf/RefCounted.h>
  31. #include <wtf/text/WTFString.h>
  32. #if ENABLE(WEB_PROCESS_SANDBOX)
  33. typedef struct __WKSandboxExtension* WKSandboxExtensionRef;
  34. #endif
  35. namespace CoreIPC {
  36. class ArgumentEncoder;
  37. class ArgumentDecoder;
  38. }
  39. namespace WebKit {
  40. class SandboxExtension : public RefCounted<SandboxExtension> {
  41. public:
  42. enum Type {
  43. ReadOnly,
  44. ReadWrite
  45. };
  46. class Handle {
  47. WTF_MAKE_NONCOPYABLE(Handle);
  48. public:
  49. Handle();
  50. ~Handle();
  51. void encode(CoreIPC::ArgumentEncoder&) const;
  52. static bool decode(CoreIPC::ArgumentDecoder&, Handle&);
  53. private:
  54. friend class SandboxExtension;
  55. #if ENABLE(WEB_PROCESS_SANDBOX)
  56. mutable WKSandboxExtensionRef m_sandboxExtension;
  57. #endif
  58. };
  59. class HandleArray {
  60. WTF_MAKE_NONCOPYABLE(HandleArray);
  61. public:
  62. HandleArray();
  63. ~HandleArray();
  64. void allocate(size_t);
  65. Handle& operator[](size_t i);
  66. const Handle& operator[](size_t i) const;
  67. size_t size() const;
  68. void encode(CoreIPC::ArgumentEncoder&) const;
  69. static bool decode(CoreIPC::ArgumentDecoder&, HandleArray&);
  70. private:
  71. #if ENABLE(WEB_PROCESS_SANDBOX)
  72. Handle* m_data;
  73. size_t m_size;
  74. #else
  75. Handle m_emptyHandle;
  76. #endif
  77. };
  78. static PassRefPtr<SandboxExtension> create(const Handle&);
  79. static void createHandle(const String& path, Type type, Handle&);
  80. static void createHandleForReadWriteDirectory(const String& path, Handle&); // Will attempt to create the directory.
  81. static String createHandleForTemporaryFile(const String& prefix, Type type, Handle&);
  82. ~SandboxExtension();
  83. bool consume();
  84. bool revoke();
  85. bool consumePermanently();
  86. static bool consumePermanently(const Handle&);
  87. private:
  88. explicit SandboxExtension(const Handle&);
  89. #if ENABLE(WEB_PROCESS_SANDBOX)
  90. mutable WKSandboxExtensionRef m_sandboxExtension;
  91. size_t m_useCount;
  92. #endif
  93. };
  94. #if !ENABLE(WEB_PROCESS_SANDBOX)
  95. inline SandboxExtension::Handle::Handle() { }
  96. inline SandboxExtension::Handle::~Handle() { }
  97. inline void SandboxExtension::Handle::encode(CoreIPC::ArgumentEncoder&) const { }
  98. inline bool SandboxExtension::Handle::decode(CoreIPC::ArgumentDecoder&, Handle&) { return true; }
  99. inline SandboxExtension::HandleArray::HandleArray() { }
  100. inline SandboxExtension::HandleArray::~HandleArray() { }
  101. inline void SandboxExtension::HandleArray::allocate(size_t) { }
  102. inline size_t SandboxExtension::HandleArray::size() const { return 0; }
  103. inline const SandboxExtension::Handle& SandboxExtension::HandleArray::operator[](size_t) const { return m_emptyHandle; }
  104. inline SandboxExtension::Handle& SandboxExtension::HandleArray::operator[](size_t) { return m_emptyHandle; }
  105. inline void SandboxExtension::HandleArray::encode(CoreIPC::ArgumentEncoder&) const { }
  106. inline bool SandboxExtension::HandleArray::decode(CoreIPC::ArgumentDecoder&, HandleArray&) { return true; }
  107. inline PassRefPtr<SandboxExtension> SandboxExtension::create(const Handle&) { return 0; }
  108. inline void SandboxExtension::createHandle(const String&, Type, Handle&) { }
  109. inline void SandboxExtension::createHandleForReadWriteDirectory(const String&, Handle&) { }
  110. inline String SandboxExtension::createHandleForTemporaryFile(const String& /*prefix*/, Type, Handle&) {return String();}
  111. inline SandboxExtension::~SandboxExtension() { }
  112. inline bool SandboxExtension::revoke() { return true; }
  113. inline bool SandboxExtension::consume() { return true; }
  114. inline bool SandboxExtension::consumePermanently() { return true; }
  115. inline bool SandboxExtension::consumePermanently(const Handle&) { return true; }
  116. #endif
  117. } // namespace WebKit
  118. #endif // SandboxExtension_h