OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "ppapi/proxy/host_var_serialization_rules.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "ppapi/c/dev/ppb_var_deprecated.h" |
| 9 |
| 10 namespace pp { |
| 11 namespace proxy { |
| 12 |
| 13 HostVarSerializationRules::HostVarSerializationRules( |
| 14 const PPB_Var_Deprecated* var_interface, |
| 15 PP_Module pp_module) |
| 16 : var_interface_(var_interface), |
| 17 pp_module_(pp_module) { |
| 18 } |
| 19 |
| 20 HostVarSerializationRules::~HostVarSerializationRules() { |
| 21 } |
| 22 |
| 23 void HostVarSerializationRules::SendCallerOwned(const PP_Var& var, |
| 24 std::string* str_val) { |
| 25 if (var.type == PP_VARTYPE_STRING) |
| 26 VarToString(var, str_val); |
| 27 } |
| 28 |
| 29 PP_Var HostVarSerializationRules::BeginReceiveCallerOwned( |
| 30 const PP_Var& var, |
| 31 const std::string* str_val) { |
| 32 if (var.type == PP_VARTYPE_STRING) { |
| 33 // Convert the string to the context of the current process. |
| 34 return var_interface_->VarFromUtf8(pp_module_, str_val->c_str(), |
| 35 static_cast<uint32_t>(str_val->size())); |
| 36 } |
| 37 return var; |
| 38 } |
| 39 |
| 40 void HostVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { |
| 41 if (var.type == PP_VARTYPE_STRING) { |
| 42 // Destroy the string BeginReceiveCallerOwned created above. |
| 43 var_interface_->Release(var); |
| 44 } |
| 45 } |
| 46 |
| 47 PP_Var HostVarSerializationRules::ReceivePassRef(const PP_Var& var, |
| 48 const std::string& str_val) { |
| 49 if (var.type == PP_VARTYPE_STRING) { |
| 50 // Convert the string to the context of the current process. |
| 51 return var_interface_->VarFromUtf8(pp_module_, str_val.c_str(), |
| 52 static_cast<uint32_t>(str_val.size())); |
| 53 } |
| 54 |
| 55 // See PluginVarSerialization::BeginSendPassRef for an example. |
| 56 if (var.type == PP_VARTYPE_OBJECT) |
| 57 var_interface_->AddRef(var); |
| 58 return var; |
| 59 } |
| 60 |
| 61 void HostVarSerializationRules::BeginSendPassRef(const PP_Var& var, |
| 62 std::string* str_val) { |
| 63 // See PluginVarSerialization::ReceivePassRef for an example. We don't need |
| 64 // to do anything here other than convert the string. |
| 65 if (var.type == PP_VARTYPE_STRING) |
| 66 VarToString(var, str_val); |
| 67 } |
| 68 |
| 69 void HostVarSerializationRules::EndSendPassRef(const PP_Var& var) { |
| 70 // See PluginVarSerialization::ReceivePassRef for an example. We don't need |
| 71 // to do anything here. |
| 72 } |
| 73 |
| 74 void HostVarSerializationRules::VarToString(const PP_Var& var, |
| 75 std::string* str) { |
| 76 DCHECK(var.type == PP_VARTYPE_STRING); |
| 77 |
| 78 // This could be optimized to avoid an extra string copy by going to a lower |
| 79 // level of the browser's implementation of strings where we already have |
| 80 // a std::string. |
| 81 uint32_t len = 0; |
| 82 const char* data = var_interface_->VarToUtf8(var, &len); |
| 83 str->assign(data, len); |
| 84 } |
| 85 |
| 86 void HostVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { |
| 87 var_interface_->Release(var); |
| 88 } |
| 89 |
| 90 } // namespace proxy |
| 91 } // namespace pp |
OLD | NEW |