OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/plugin_var_serialization_rules.h" | 5 #include "ppapi/proxy/plugin_var_serialization_rules.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ppapi/proxy/plugin_dispatcher.h" | 8 #include "ppapi/proxy/plugin_dispatcher.h" |
9 #include "ppapi/proxy/plugin_globals.h" | 9 #include "ppapi/proxy/plugin_globals.h" |
10 #include "ppapi/proxy/plugin_resource_tracker.h" | 10 #include "ppapi/proxy/plugin_resource_tracker.h" |
11 #include "ppapi/proxy/plugin_var_tracker.h" | 11 #include "ppapi/proxy/plugin_var_tracker.h" |
12 #include "ppapi/shared_impl/ppapi_globals.h" | 12 #include "ppapi/shared_impl/ppapi_globals.h" |
13 #include "ppapi/shared_impl/var.h" | 13 #include "ppapi/shared_impl/var.h" |
14 | 14 |
15 namespace ppapi { | 15 namespace ppapi { |
16 namespace proxy { | 16 namespace proxy { |
17 | 17 |
18 PluginVarSerializationRules::PluginVarSerializationRules() | 18 PluginVarSerializationRules::PluginVarSerializationRules() |
19 : var_tracker_(PluginGlobals::Get()->plugin_var_tracker()) { | 19 : var_tracker_(PluginGlobals::Get()->plugin_var_tracker()) { |
20 } | 20 } |
21 | 21 |
22 PluginVarSerializationRules::~PluginVarSerializationRules() { | 22 PluginVarSerializationRules::~PluginVarSerializationRules() { |
23 } | 23 } |
24 | 24 |
25 PP_Var PluginVarSerializationRules::SendCallerOwned( | 25 PP_Var PluginVarSerializationRules::SendCallerOwned(const PP_Var& var) { |
26 const PP_Var& var, | |
27 const std::string** str_ptr_out) { | |
28 // Objects need special translations to get the IDs valid in the host. | 26 // Objects need special translations to get the IDs valid in the host. |
29 if (var.type == PP_VARTYPE_OBJECT) | 27 if (var.type == PP_VARTYPE_OBJECT) |
30 return var_tracker_->GetHostObject(var); | 28 return var_tracker_->GetHostObject(var); |
31 | |
32 // Retrieve the pointer to the string in the tracker in order to send the | |
33 // string over IPC without unnecessary copies. | |
34 if (var.type == PP_VARTYPE_STRING) { | |
35 StringVar* string_var = StringVar::FromPPVar(var); | |
36 if (string_var) | |
37 *str_ptr_out = string_var->ptr(); | |
38 else | |
39 NOTREACHED() << "Trying to send unknown string over IPC."; | |
40 } | |
41 return var; | 29 return var; |
42 } | 30 } |
43 | 31 |
44 PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned( | 32 PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned( |
45 const PP_Var& var, | 33 const PP_Var& var, |
46 scoped_ptr<std::string> str, | |
47 Dispatcher* dispatcher) { | 34 Dispatcher* dispatcher) { |
48 if (var.type == PP_VARTYPE_STRING) | |
49 return StringVar::StringToPPVar(str.Pass()); | |
50 | |
51 if (var.type == PP_VARTYPE_OBJECT) { | 35 if (var.type == PP_VARTYPE_OBJECT) { |
52 DCHECK(dispatcher->IsPlugin()); | 36 DCHECK(dispatcher->IsPlugin()); |
53 return var_tracker_->TrackObjectWithNoReference( | 37 return var_tracker_->TrackObjectWithNoReference( |
54 var, static_cast<PluginDispatcher*>(dispatcher)); | 38 var, static_cast<PluginDispatcher*>(dispatcher)); |
55 } | 39 } |
56 | |
57 return var; | 40 return var; |
58 } | 41 } |
59 | 42 |
60 void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { | 43 void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { |
61 if (var.type == PP_VARTYPE_STRING) { | 44 if (var.type == PP_VARTYPE_STRING) { |
62 // Destroy the string BeginReceiveCallerOwned created above. | 45 // Destroy the string. |
63 var_tracker_->ReleaseVar(var); | 46 var_tracker_->ReleaseVar(var); |
64 } else if (var.type == PP_VARTYPE_OBJECT) { | 47 } else if (var.type == PP_VARTYPE_OBJECT) { |
65 var_tracker_->StopTrackingObjectWithNoReference(var); | 48 var_tracker_->StopTrackingObjectWithNoReference(var); |
66 } | 49 } |
67 } | 50 } |
68 | 51 |
69 PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var, | 52 PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var, |
70 scoped_ptr<std::string> str, | |
71 Dispatcher* dispatcher) { | 53 Dispatcher* dispatcher) { |
72 if (var.type == PP_VARTYPE_STRING) | |
73 return StringVar::StringToPPVar(str.Pass()); | |
74 | |
75 // Overview of sending an object with "pass ref" from the browser to the | 54 // Overview of sending an object with "pass ref" from the browser to the |
76 // plugin: | 55 // plugin: |
77 // Example 1 Example 2 | 56 // Example 1 Example 2 |
78 // Plugin Browser Plugin Browser | 57 // Plugin Browser Plugin Browser |
79 // Before send 3 2 0 1 | 58 // Before send 3 2 0 1 |
80 // Browser calls BeginSendPassRef 3 2 0 1 | 59 // Browser calls BeginSendPassRef 3 2 0 1 |
81 // Plugin calls ReceivePassRef 4 1 1 1 | 60 // Plugin calls ReceivePassRef 4 1 1 1 |
82 // Browser calls EndSendPassRef 4 1 1 1 | 61 // Browser calls EndSendPassRef 4 1 1 1 |
83 // | 62 // |
84 // In example 1 before the send, the plugin has 3 refs which are represented | 63 // In example 1 before the send, the plugin has 3 refs which are represented |
85 // as one ref in the browser (since the plugin only tells the browser when | 64 // as one ref in the browser (since the plugin only tells the browser when |
86 // it's refcount goes from 1 -> 0). The initial state is that the browser | 65 // it's refcount goes from 1 -> 0). The initial state is that the browser |
87 // plugin code started to return a value, which means it gets another ref | 66 // plugin code started to return a value, which means it gets another ref |
88 // on behalf of the caller. This needs to be transferred to the plugin and | 67 // on behalf of the caller. This needs to be transferred to the plugin and |
89 // folded in to its set of refs it maintains (with one ref representing all | 68 // folded in to its set of refs it maintains (with one ref representing all |
90 // of them in the browser). | 69 // of them in the browser). |
91 if (var.type == PP_VARTYPE_OBJECT) { | 70 if (var.type == PP_VARTYPE_OBJECT) { |
92 DCHECK(dispatcher->IsPlugin()); | 71 DCHECK(dispatcher->IsPlugin()); |
93 return var_tracker_->ReceiveObjectPassRef( | 72 return var_tracker_->ReceiveObjectPassRef( |
94 var, static_cast<PluginDispatcher*>(dispatcher)); | 73 var, static_cast<PluginDispatcher*>(dispatcher)); |
95 } | 74 } |
96 | 75 |
97 // Other types are unchanged. | 76 // Other types are unchanged. |
98 return var; | 77 return var; |
99 } | 78 } |
100 | 79 |
101 PP_Var PluginVarSerializationRules::BeginSendPassRef( | 80 PP_Var PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var) { |
102 const PP_Var& var, | |
103 const std::string** str_ptr_out) { | |
104 // Overview of sending an object with "pass ref" from the plugin to the | 81 // Overview of sending an object with "pass ref" from the plugin to the |
105 // browser: | 82 // browser: |
106 // Example 1 Example 2 | 83 // Example 1 Example 2 |
107 // Plugin Browser Plugin Browser | 84 // Plugin Browser Plugin Browser |
108 // Before send 3 1 1 1 | 85 // Before send 3 1 1 1 |
109 // Plugin calls BeginSendPassRef 3 1 1 1 | 86 // Plugin calls BeginSendPassRef 3 1 1 1 |
110 // Browser calls ReceivePassRef 3 2 1 2 | 87 // Browser calls ReceivePassRef 3 2 1 2 |
111 // Plugin calls EndSendPassRef 2 2 0 1 | 88 // Plugin calls EndSendPassRef 2 2 0 1 |
112 // | 89 // |
113 // The plugin maintains one ref count in the browser on behalf of the | 90 // The plugin maintains one ref count in the browser on behalf of the |
114 // entire ref count in the plugin. When the plugin refcount goes to 0, it | 91 // entire ref count in the plugin. When the plugin refcount goes to 0, it |
115 // will call the browser to deref the object. This is why in example 2 | 92 // will call the browser to deref the object. This is why in example 2 |
116 // transferring the object ref to the browser involves no net change in the | 93 // transferring the object ref to the browser involves no net change in the |
117 // browser's refcount. | 94 // browser's refcount. |
118 | 95 |
119 // Objects need special translations to get the IDs valid in the host. | 96 // Objects need special translations to get the IDs valid in the host. |
120 if (var.type == PP_VARTYPE_OBJECT) | 97 if (var.type == PP_VARTYPE_OBJECT) |
121 return var_tracker_->GetHostObject(var); | 98 return var_tracker_->GetHostObject(var); |
122 | |
123 if (var.type == PP_VARTYPE_STRING) { | |
124 // Get the pointer to the string that's in the tracker and return it, so we | |
125 // can avoid an extra copy of the string when serializing over IPC. | |
126 StringVar* string_var = StringVar::FromPPVar(var); | |
127 if (string_var) | |
128 *str_ptr_out = string_var->ptr(); | |
129 else | |
130 NOTREACHED() << "Trying to send unknown string over IPC."; | |
131 } | |
132 return var; | 99 return var; |
133 } | 100 } |
134 | 101 |
135 void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var, | 102 void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var, |
136 Dispatcher* dispatcher) { | 103 Dispatcher* dispatcher) { |
137 // See BeginSendPassRef for an example of why we release our ref here. | 104 // See BeginSendPassRef for an example of why we release our ref here. |
138 // The var we have in our inner class has been converted to a host object | 105 // The var we have in our inner class has been converted to a host object |
139 // by BeginSendPassRef. This means it's not a normal var valid in the plugin, | 106 // by BeginSendPassRef. This means it's not a normal var valid in the plugin, |
140 // so we need to use the special ReleaseHostObject. | 107 // so we need to use the special ReleaseHostObject. |
141 if (var.type == PP_VARTYPE_OBJECT) { | 108 if (var.type == PP_VARTYPE_OBJECT) { |
142 var_tracker_->ReleaseHostObject( | 109 var_tracker_->ReleaseHostObject( |
143 static_cast<PluginDispatcher*>(dispatcher), var); | 110 static_cast<PluginDispatcher*>(dispatcher), var); |
144 } else if (var.type == PP_VARTYPE_STRING) { | 111 } else if (var.type == PP_VARTYPE_STRING) { |
145 var_tracker_->ReleaseVar(var); | 112 var_tracker_->ReleaseVar(var); |
146 } | 113 } |
147 } | 114 } |
148 | 115 |
149 void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { | 116 void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { |
150 var_tracker_->ReleaseVar(var); | 117 var_tracker_->ReleaseVar(var); |
151 } | 118 } |
152 | 119 |
153 } // namespace proxy | 120 } // namespace proxy |
154 } // namespace ppapi | 121 } // namespace ppapi |
OLD | NEW |