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