| OLD | NEW |
| (Empty) |
| 1 // Copyright 2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 // | |
| 16 // TODO(omaha): verify that the new operator throws on failure to prevent NULL | |
| 17 // pointer exploits. | |
| 18 | |
| 19 #include "omaha/plugins/update/npapi/variant_utils.h" | |
| 20 #include <atlstr.h> | |
| 21 #include "base/debug.h" | |
| 22 #include "base/string.h" | |
| 23 #include "omaha/plugins/update/npapi/dispatch_host.h" | |
| 24 #include "omaha/plugins/update/npapi/npfunction_host.h" | |
| 25 | |
| 26 namespace omaha { | |
| 27 | |
| 28 void NPVariantToVariant(NPP npp, | |
| 29 const NPVariant& source, | |
| 30 VARIANT* destination) { | |
| 31 ASSERT1(destination); | |
| 32 V_VT(destination) = VT_EMPTY; | |
| 33 | |
| 34 switch (source.type) { | |
| 35 case NPVariantType_Void: | |
| 36 V_VT(destination) = VT_EMPTY; | |
| 37 break; | |
| 38 case NPVariantType_Null: | |
| 39 V_VT(destination) = VT_NULL; | |
| 40 break; | |
| 41 case NPVariantType_Bool: | |
| 42 V_VT(destination) = VT_BOOL; | |
| 43 V_BOOL(destination) = source.value.boolValue ? VARIANT_TRUE | |
| 44 : VARIANT_FALSE; | |
| 45 break; | |
| 46 case NPVariantType_Int32: | |
| 47 V_VT(destination) = VT_I4; | |
| 48 V_I4(destination) = source.value.intValue; | |
| 49 break; | |
| 50 case NPVariantType_Double: | |
| 51 V_VT(destination) = VT_R8; | |
| 52 V_R8(destination) = source.value.doubleValue; | |
| 53 break; | |
| 54 case NPVariantType_String: | |
| 55 V_VT(destination) = VT_BSTR; | |
| 56 if (source.value.stringValue.UTF8Length) { | |
| 57 CString string = Utf8ToWideChar(source.value.stringValue.UTF8Characters, | |
| 58 source.value.stringValue.UTF8Length); | |
| 59 V_BSTR(destination) = string.AllocSysString(); | |
| 60 } else { | |
| 61 V_BSTR(destination) = CString().AllocSysString(); | |
| 62 } | |
| 63 break; | |
| 64 case NPVariantType_Object: | |
| 65 V_VT(destination) = VT_DISPATCH; | |
| 66 if (source.value.objectValue) { | |
| 67 NpFunctionHost::Create(npp, source.value.objectValue, | |
| 68 &V_DISPATCH(destination)); | |
| 69 } else { | |
| 70 V_DISPATCH(destination) = NULL; | |
| 71 } | |
| 72 break; | |
| 73 default: | |
| 74 ASSERT1(false); | |
| 75 break; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 void VariantToNPVariant(NPP npp, | |
| 80 const VARIANT& source, | |
| 81 NPVariant* destination) { | |
| 82 ASSERT1(destination); | |
| 83 VOID_TO_NPVARIANT(*destination); | |
| 84 | |
| 85 switch (V_VT(&source)) { | |
| 86 case VT_EMPTY: | |
| 87 VOID_TO_NPVARIANT(*destination); | |
| 88 break; | |
| 89 case VT_NULL: | |
| 90 NULL_TO_NPVARIANT(*destination); | |
| 91 break; | |
| 92 case VT_BOOL: | |
| 93 BOOLEAN_TO_NPVARIANT(V_BOOL(&source), *destination); | |
| 94 break; | |
| 95 case VT_I4: | |
| 96 INT32_TO_NPVARIANT(V_I4(&source), *destination); | |
| 97 break; | |
| 98 case VT_UI4: | |
| 99 INT32_TO_NPVARIANT(V_UI4(&source), *destination); | |
| 100 break; | |
| 101 case VT_R8: | |
| 102 DOUBLE_TO_NPVARIANT(V_R8(&source), *destination); | |
| 103 break; | |
| 104 case VT_BSTR: | |
| 105 if (V_BSTR(&source)) { | |
| 106 int source_length = ::SysStringLen(V_BSTR(&source)) + 1; | |
| 107 int buffer_length = ::WideCharToMultiByte(CP_UTF8, 0, V_BSTR(&source), | |
| 108 source_length, NULL, 0, NULL, | |
| 109 NULL); | |
| 110 char* buffer = static_cast<char*>(NPN_MemAlloc(buffer_length)); | |
| 111 VERIFY1(::WideCharToMultiByte(CP_UTF8, 0, V_BSTR(&source), | |
| 112 source_length, buffer, buffer_length, | |
| 113 NULL, NULL) > 0); | |
| 114 STRINGN_TO_NPVARIANT(buffer, buffer_length - 1, *destination); | |
| 115 } else { | |
| 116 char* buffer = static_cast<char*>(NPN_MemAlloc(1)); | |
| 117 buffer[0] = '\0'; | |
| 118 STRINGN_TO_NPVARIANT(buffer, 0, *destination); | |
| 119 } | |
| 120 break; | |
| 121 case VT_DISPATCH: | |
| 122 if (V_DISPATCH(&source)) { | |
| 123 OBJECT_TO_NPVARIANT( | |
| 124 DispatchHost::CreateInstance(npp, V_DISPATCH(&source)), | |
| 125 *destination); | |
| 126 } | |
| 127 break; | |
| 128 default: | |
| 129 ASSERT(false, (L"Unhandled variant type %d", V_VT(&source))); | |
| 130 break; | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 } // namespace omaha | |
| OLD | NEW |