1 // Copyright (c) 2012 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 5 #include "remoting/host/plugin/host_plugin_utils.h" 6 7 namespace remoting { 8 9 NPNetscapeFuncs* g_npnetscape_funcs = NULL; 10 11 std::string StringFromNPIdentifier(NPIdentifier identifier) { 12 if (!g_npnetscape_funcs->identifierisstring(identifier)) 13 return std::string(); 14 NPUTF8* np_string = g_npnetscape_funcs->utf8fromidentifier(identifier); 15 std::string string(np_string); 16 g_npnetscape_funcs->memfree(np_string); 17 return string; 18 } 19 20 std::string StringFromNPVariant(const NPVariant& variant) { 21 if (!NPVARIANT_IS_STRING(variant)) 22 return std::string(); 23 const NPString& np_string = NPVARIANT_TO_STRING(variant); 24 return std::string(np_string.UTF8Characters, np_string.UTF8Length); 25 } 26 27 NPVariant NPVariantFromString(const std::string& val) { 28 size_t len = val.length(); 29 NPUTF8* chars = 30 reinterpret_cast<NPUTF8*>(g_npnetscape_funcs->memalloc(len + 1)); 31 strcpy(chars, val.c_str()); 32 NPVariant variant; 33 STRINGN_TO_NPVARIANT(chars, len, variant); 34 return variant; 35 } 36 37 NPObject* ObjectFromNPVariant(const NPVariant& variant) { 38 if (!NPVARIANT_IS_OBJECT(variant)) 39 return NULL; 40 return NPVARIANT_TO_OBJECT(variant); 41 } 42 43 ScopedRefNPObject::ScopedRefNPObject() : object_(NULL) { } 44 45 ScopedRefNPObject::ScopedRefNPObject(NPObject* object) 46 : object_(NULL) { 47 *this = object; 48 } 49 50 ScopedRefNPObject::ScopedRefNPObject(const ScopedRefNPObject& object) 51 : object_(NULL) { 52 *this = object; 53 } 54 55 ScopedRefNPObject::~ScopedRefNPObject() { 56 *this = NULL; 57 } 58 59 ScopedRefNPObject& ScopedRefNPObject::operator=(NPObject* object) { 60 if (object) { 61 g_npnetscape_funcs->retainobject(object); 62 } 63 if (object_) { 64 g_npnetscape_funcs->releaseobject(object_); 65 } 66 object_ = object; 67 return *this; 68 } 69 70 ScopedRefNPObject& ScopedRefNPObject::operator=( 71 const ScopedRefNPObject& object) { 72 return *this = object.get(); 73 } 74 75 } // namespace remoting 76