NPVariantData.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "config.h"
  26. #include "NPVariantData.h"
  27. #if ENABLE(PLUGIN_PROCESS)
  28. #include "ArgumentDecoder.h"
  29. #include "ArgumentEncoder.h"
  30. #include "WebCoreArgumentCoders.h"
  31. namespace WebKit {
  32. NPVariantData::NPVariantData()
  33. : m_type(NPVariantData::Void)
  34. , m_boolValue(false)
  35. , m_int32Value(0)
  36. , m_doubleValue(0)
  37. , m_localNPObjectIDValue(0)
  38. , m_remoteNPObjectIDValue(0)
  39. {
  40. }
  41. NPVariantData NPVariantData::makeVoid()
  42. {
  43. return NPVariantData();
  44. }
  45. NPVariantData NPVariantData::makeNull()
  46. {
  47. NPVariantData npVariantData;
  48. npVariantData.m_type = NPVariantData::Null;
  49. return npVariantData;
  50. }
  51. NPVariantData NPVariantData::makeBool(bool value)
  52. {
  53. NPVariantData npVariantData;
  54. npVariantData.m_type = NPVariantData::Bool;
  55. npVariantData.m_boolValue = value;
  56. return npVariantData;
  57. }
  58. NPVariantData NPVariantData::makeInt32(int32_t value)
  59. {
  60. NPVariantData npVariantData;
  61. npVariantData.m_type = NPVariantData::Int32;
  62. npVariantData.m_int32Value = value;
  63. return npVariantData;
  64. }
  65. NPVariantData NPVariantData::makeDouble(double value)
  66. {
  67. NPVariantData npVariantData;
  68. npVariantData.m_type = NPVariantData::Double;
  69. npVariantData.m_doubleValue = value;
  70. return npVariantData;
  71. }
  72. NPVariantData NPVariantData::makeString(const char* string, unsigned length)
  73. {
  74. NPVariantData npVariantData;
  75. npVariantData.m_type = NPVariantData::String;
  76. npVariantData.m_stringValue = CString(string, length);
  77. return npVariantData;
  78. }
  79. NPVariantData NPVariantData::makeLocalNPObjectID(uint64_t value)
  80. {
  81. NPVariantData npVariantData;
  82. npVariantData.m_type = NPVariantData::LocalNPObjectID;
  83. npVariantData.m_localNPObjectIDValue = value;
  84. return npVariantData;
  85. }
  86. NPVariantData NPVariantData::makeRemoteNPObjectID(uint64_t value)
  87. {
  88. NPVariantData npVariantData;
  89. npVariantData.m_type = NPVariantData::RemoteNPObjectID;
  90. npVariantData.m_remoteNPObjectIDValue = value;
  91. return npVariantData;
  92. }
  93. void NPVariantData::encode(CoreIPC::ArgumentEncoder& encoder) const
  94. {
  95. encoder << m_type;
  96. switch (type()) {
  97. case NPVariantData::Void:
  98. case NPVariantData::Null:
  99. break;
  100. case NPVariantData::Bool:
  101. encoder << boolValue();
  102. break;
  103. case NPVariantData::Int32:
  104. encoder << int32Value();
  105. break;
  106. case NPVariantData::Double:
  107. encoder << doubleValue();
  108. break;
  109. case NPVariantData::String:
  110. encoder << stringValue();
  111. break;
  112. case NPVariantData::LocalNPObjectID:
  113. encoder << localNPObjectIDValue();
  114. break;
  115. case NPVariantData::RemoteNPObjectID:
  116. encoder << remoteNPObjectIDValue();
  117. break;
  118. }
  119. }
  120. bool NPVariantData::decode(CoreIPC::ArgumentDecoder& decoder, NPVariantData& result)
  121. {
  122. uint32_t type;
  123. if (!decoder.decode(type))
  124. return false;
  125. // We special-case LocalNPObjectID and RemoteNPObjectID here so a LocalNPObjectID is
  126. // decoded as a RemoteNPObjectID and vice versa.
  127. // This is done because the type is from the perspective of the other connection, and
  128. // thus we have to adjust it to match our own perspective.
  129. if (type == NPVariantData::LocalNPObjectID)
  130. type = NPVariantData::RemoteNPObjectID;
  131. else if (type == NPVariantData::RemoteNPObjectID)
  132. type = NPVariantData::LocalNPObjectID;
  133. result.m_type = type;
  134. switch (result.m_type) {
  135. case NPVariantData::Void:
  136. case NPVariantData::Null:
  137. return true;
  138. case NPVariantData::Bool:
  139. return decoder.decode(result.m_boolValue);
  140. case NPVariantData::Int32:
  141. return decoder.decode(result.m_int32Value);
  142. case NPVariantData::Double:
  143. return decoder.decode(result.m_doubleValue);
  144. case NPVariantData::String:
  145. return decoder.decode(result.m_stringValue);
  146. case NPVariantData::LocalNPObjectID:
  147. return decoder.decode(result.m_localNPObjectIDValue);
  148. case NPVariantData::RemoteNPObjectID:
  149. return decoder.decode(result.m_remoteNPObjectIDValue);
  150. }
  151. return false;
  152. }
  153. } // namespace WebKit
  154. #endif // ENABLE(PLUGIN_PROCESS)