OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/host_var_serialization_rules.h" | 5 #include "ppapi/proxy/host_var_serialization_rules.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ppapi/c/ppb_var.h" | 8 #include "ppapi/c/ppb_var.h" |
| 9 #include "ppapi/shared_impl/ppapi_globals.h" |
| 10 #include "ppapi/shared_impl/var.h" |
| 11 #include "ppapi/shared_impl/var_tracker.h" |
| 12 |
| 13 using ppapi::PpapiGlobals; |
| 14 using ppapi::StringVar; |
| 15 using ppapi::VarTracker; |
9 | 16 |
10 namespace ppapi { | 17 namespace ppapi { |
11 namespace proxy { | 18 namespace proxy { |
12 | 19 |
13 HostVarSerializationRules::HostVarSerializationRules( | 20 HostVarSerializationRules::HostVarSerializationRules(PP_Module pp_module) |
14 const PPB_Var* var_interface, | 21 : pp_module_(pp_module) { |
15 PP_Module pp_module) | |
16 : var_interface_(var_interface), | |
17 pp_module_(pp_module) { | |
18 } | 22 } |
19 | 23 |
20 HostVarSerializationRules::~HostVarSerializationRules() { | 24 HostVarSerializationRules::~HostVarSerializationRules() { |
21 } | 25 } |
22 | 26 |
23 PP_Var HostVarSerializationRules::SendCallerOwned(const PP_Var& var, | 27 PP_Var HostVarSerializationRules::SendCallerOwned(const PP_Var& var, |
24 std::string* str_val) { | 28 std::string* str_val) { |
25 if (var.type == PP_VARTYPE_STRING) | 29 if (var.type == PP_VARTYPE_STRING) |
26 VarToString(var, str_val); | 30 VarToString(var, str_val); |
27 return var; | 31 return var; |
28 } | 32 } |
29 | 33 |
30 PP_Var HostVarSerializationRules::BeginReceiveCallerOwned( | 34 PP_Var HostVarSerializationRules::BeginReceiveCallerOwned( |
31 const PP_Var& var, | 35 const PP_Var& var, |
32 const std::string* str_val, | 36 const std::string* str_val, |
33 Dispatcher* /* dispatcher */) { | 37 Dispatcher* /* dispatcher */) { |
34 if (var.type == PP_VARTYPE_STRING) { | 38 if (var.type == PP_VARTYPE_STRING) |
35 // Convert the string to the context of the current process. | 39 return StringVar::StringToPPVar(*str_val); |
36 return var_interface_->VarFromUtf8(str_val->c_str(), | |
37 static_cast<uint32_t>(str_val->size())); | |
38 } | |
39 return var; | 40 return var; |
40 } | 41 } |
41 | 42 |
42 void HostVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { | 43 void HostVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { |
43 if (var.type == PP_VARTYPE_STRING) { | 44 if (var.type == PP_VARTYPE_STRING) { |
44 // Destroy the string BeginReceiveCallerOwned created above. | 45 // Destroy the string BeginReceiveCallerOwned created above. |
45 var_interface_->Release(var); | 46 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var); |
46 } | 47 } |
47 } | 48 } |
48 | 49 |
49 PP_Var HostVarSerializationRules::ReceivePassRef(const PP_Var& var, | 50 PP_Var HostVarSerializationRules::ReceivePassRef(const PP_Var& var, |
50 const std::string& str_val, | 51 const std::string& str_val, |
51 Dispatcher* /* dispatcher */) { | 52 Dispatcher* /* dispatcher */) { |
52 if (var.type == PP_VARTYPE_STRING) { | 53 if (var.type == PP_VARTYPE_STRING) { |
53 // Convert the string to the context of the current process. | 54 // Convert the string to the context of the current process. |
54 return var_interface_->VarFromUtf8(str_val.c_str(), | 55 return StringVar::StringToPPVar(str_val); |
55 static_cast<uint32_t>(str_val.size())); | |
56 } | 56 } |
57 | 57 |
58 // See PluginVarSerialization::BeginSendPassRef for an example. | 58 // See PluginVarSerialization::BeginSendPassRef for an example. |
59 if (var.type == PP_VARTYPE_OBJECT) | 59 if (var.type == PP_VARTYPE_OBJECT) |
60 var_interface_->AddRef(var); | 60 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var); |
61 return var; | 61 return var; |
62 } | 62 } |
63 | 63 |
64 PP_Var HostVarSerializationRules::BeginSendPassRef(const PP_Var& var, | 64 PP_Var HostVarSerializationRules::BeginSendPassRef(const PP_Var& var, |
65 std::string* str_val) { | 65 std::string* str_val) { |
66 // See PluginVarSerialization::ReceivePassRef for an example. We don't need | 66 // See PluginVarSerialization::ReceivePassRef for an example. We don't need |
67 // to do anything here other than convert the string. | 67 // to do anything here other than convert the string. |
68 if (var.type == PP_VARTYPE_STRING) | 68 if (var.type == PP_VARTYPE_STRING) |
69 VarToString(var, str_val); | 69 VarToString(var, str_val); |
70 return var; | 70 return var; |
71 } | 71 } |
72 | 72 |
73 void HostVarSerializationRules::EndSendPassRef(const PP_Var& /* var */, | 73 void HostVarSerializationRules::EndSendPassRef(const PP_Var& /* var */, |
74 Dispatcher* /* dispatcher */) { | 74 Dispatcher* /* dispatcher */) { |
75 // See PluginVarSerialization::ReceivePassRef for an example. We don't need | 75 // See PluginVarSerialization::ReceivePassRef for an example. We don't need |
76 // to do anything here. | 76 // to do anything here. |
77 } | 77 } |
78 | 78 |
79 void HostVarSerializationRules::VarToString(const PP_Var& var, | 79 void HostVarSerializationRules::VarToString(const PP_Var& var, |
80 std::string* str) { | 80 std::string* str) { |
81 DCHECK(var.type == PP_VARTYPE_STRING); | 81 DCHECK(var.type == PP_VARTYPE_STRING); |
82 | 82 |
83 // This could be optimized to avoid an extra string copy by going to a lower | 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 | 84 // level of the browser's implementation of strings where we already have |
85 // a std::string. | 85 // a std::string. |
86 uint32_t len = 0; | 86 StringVar* string_var = StringVar::FromPPVar(var); |
87 const char* data = var_interface_->VarToUtf8(var, &len); | 87 if (string_var) |
88 str->assign(data, len); | 88 *str = string_var->value(); |
89 } | 89 } |
90 | 90 |
91 void HostVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { | 91 void HostVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { |
92 var_interface_->Release(var); | 92 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var); |
93 } | 93 } |
94 | 94 |
95 } // namespace proxy | 95 } // namespace proxy |
96 } // namespace ppapi | 96 } // namespace ppapi |
OLD | NEW |