blob: 2a1d0ee6328b45dd29b81cc235437ad66f9ca5d7 [file] [log] [blame]
[email protected]66085312010-11-05 22:14:251// 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
10namespace pp {
11namespace proxy {
12
13HostVarSerializationRules::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
20HostVarSerializationRules::~HostVarSerializationRules() {
21}
22
[email protected]4614f192011-01-21 00:26:4323PP_Var HostVarSerializationRules::SendCallerOwned(const PP_Var& var,
24 std::string* str_val) {
[email protected]66085312010-11-05 22:14:2525 if (var.type == PP_VARTYPE_STRING)
26 VarToString(var, str_val);
[email protected]4614f192011-01-21 00:26:4327 return var;
[email protected]66085312010-11-05 22:14:2528}
29
30PP_Var HostVarSerializationRules::BeginReceiveCallerOwned(
31 const PP_Var& var,
[email protected]4614f192011-01-21 00:26:4332 const std::string* str_val,
33 Dispatcher* /* dispatcher */) {
[email protected]66085312010-11-05 22:14:2534 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
42void 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
49PP_Var HostVarSerializationRules::ReceivePassRef(const PP_Var& var,
[email protected]4614f192011-01-21 00:26:4350 const std::string& str_val,
51 Dispatcher* /* dispatcher */) {
[email protected]66085312010-11-05 22:14:2552 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]4614f192011-01-21 00:26:4364PP_Var HostVarSerializationRules::BeginSendPassRef(const PP_Var& var,
[email protected]f24448db2011-01-27 20:40:3965 std::string* str_val) {
[email protected]66085312010-11-05 22:14:2566 // 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]4614f192011-01-21 00:26:4370 return var;
[email protected]66085312010-11-05 22:14:2571}
72
[email protected]f24448db2011-01-27 20:40:3973void HostVarSerializationRules::EndSendPassRef(const PP_Var& /* var */,
74 Dispatcher* /* dispatcher */) {
[email protected]66085312010-11-05 22:14:2575 // See PluginVarSerialization::ReceivePassRef for an example. We don't need
76 // to do anything here.
77}
78
79void 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
91void HostVarSerializationRules::ReleaseObjectRef(const PP_Var& var) {
92 var_interface_->Release(var);
93}
94
95} // namespace proxy
96} // namespace pp