OLD | NEW |
1 // Copyright (c) 2010 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/plugin_var_serialization_rules.h" | 5 #include "ppapi/proxy/plugin_var_serialization_rules.h" |
6 | 6 |
| 7 #include "ppapi/proxy/plugin_dispatcher.h" |
7 #include "ppapi/proxy/plugin_var_tracker.h" | 8 #include "ppapi/proxy/plugin_var_tracker.h" |
8 | 9 |
9 namespace pp { | 10 namespace pp { |
10 namespace proxy { | 11 namespace proxy { |
11 | 12 |
12 PluginVarSerializationRules::PluginVarSerializationRules( | 13 PluginVarSerializationRules::PluginVarSerializationRules() |
13 PluginVarTracker* var_tracker) | 14 : var_tracker_(PluginVarTracker::GetInstance()) { |
14 : var_tracker_(var_tracker) { | |
15 } | 15 } |
16 | 16 |
17 PluginVarSerializationRules::~PluginVarSerializationRules() { | 17 PluginVarSerializationRules::~PluginVarSerializationRules() { |
18 } | 18 } |
19 | 19 |
20 void PluginVarSerializationRules::SendCallerOwned(const PP_Var& var, | 20 PP_Var PluginVarSerializationRules::SendCallerOwned(const PP_Var& var, |
21 std::string* str_val) { | 21 std::string* str_val) { |
| 22 // Objects need special translations to get the IDs valid in the host. |
| 23 if (var.type == PP_VARTYPE_OBJECT) |
| 24 return var_tracker_->GetHostObject(var); |
| 25 |
22 // Nothing to do since we manage the refcount, other than retrieve the string | 26 // Nothing to do since we manage the refcount, other than retrieve the string |
23 // to use for IPC. | 27 // to use for IPC. |
24 if (var.type == PP_VARTYPE_STRING) | 28 if (var.type == PP_VARTYPE_STRING) |
25 *str_val = var_tracker_->GetString(var); | 29 *str_val = var_tracker_->GetString(var); |
| 30 return var; |
26 } | 31 } |
27 | 32 |
28 PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned( | 33 PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned( |
29 const PP_Var& var, | 34 const PP_Var& var, |
30 const std::string* str_val) { | 35 const std::string* str_val, |
| 36 Dispatcher* dispatcher) { |
31 if (var.type == PP_VARTYPE_STRING) { | 37 if (var.type == PP_VARTYPE_STRING) { |
32 // Convert the string to the context of the current process. | 38 // Convert the string to the context of the current process. |
33 PP_Var ret; | 39 PP_Var ret; |
34 ret.type = PP_VARTYPE_STRING; | 40 ret.type = PP_VARTYPE_STRING; |
35 ret.value.as_id = var_tracker_->MakeString(*str_val); | 41 ret.value.as_id = var_tracker_->MakeString(*str_val); |
36 return ret; | 42 return ret; |
37 } | 43 } |
38 | 44 |
| 45 if (var.type == PP_VARTYPE_OBJECT) |
| 46 return var_tracker_->TrackObjectWithNoReference(var, dispatcher); |
| 47 |
39 return var; | 48 return var; |
40 } | 49 } |
41 | 50 |
42 void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { | 51 void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { |
43 if (var.type == PP_VARTYPE_STRING) { | 52 if (var.type == PP_VARTYPE_STRING) { |
44 // Destroy the string BeginReceiveCallerOwned created above. | 53 // Destroy the string BeginReceiveCallerOwned created above. |
45 var_tracker_->Release(var); | 54 var_tracker_->Release(var); |
| 55 } else if (var.type == PP_VARTYPE_OBJECT) { |
| 56 var_tracker_->StopTrackingObjectWithNoReference(var); |
46 } | 57 } |
47 } | 58 } |
48 | 59 |
49 PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var, | 60 PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var, |
50 const std::string& str_val) { | 61 const std::string& str_val, |
| 62 Dispatcher* dispatcher) { |
51 if (var.type == PP_VARTYPE_STRING) { | 63 if (var.type == PP_VARTYPE_STRING) { |
52 // Convert the string to the context of the current process. | 64 // Convert the string to the context of the current process. |
53 PP_Var ret; | 65 PP_Var ret; |
54 ret.type = PP_VARTYPE_STRING; | 66 ret.type = PP_VARTYPE_STRING; |
55 ret.value.as_id = var_tracker_->MakeString(str_val); | 67 ret.value.as_id = var_tracker_->MakeString(str_val); |
56 return ret; | 68 return ret; |
57 } | 69 } |
58 | 70 |
59 // Overview of sending an object with "pass ref" from the browser to the | 71 // Overview of sending an object with "pass ref" from the browser to the |
60 // plugin: | 72 // plugin: |
61 // Example 1 Example 2 | 73 // Example 1 Example 2 |
62 // Plugin Browser Plugin Browser | 74 // Plugin Browser Plugin Browser |
63 // Before send 3 2 0 1 | 75 // Before send 3 2 0 1 |
64 // Browser calls BeginSendPassRef 3 2 0 1 | 76 // Browser calls BeginSendPassRef 3 2 0 1 |
65 // Plugin calls ReceivePassRef 4 1 1 1 | 77 // Plugin calls ReceivePassRef 4 1 1 1 |
66 // Browser calls EndSendPassRef 4 1 1 1 | 78 // Browser calls EndSendPassRef 4 1 1 1 |
67 // | 79 // |
68 // In example 1 before the send, the plugin has 3 refs which are represented | 80 // In example 1 before the send, the plugin has 3 refs which are represented |
69 // as one ref in the browser (since the plugin only tells the browser when | 81 // as one ref in the browser (since the plugin only tells the browser when |
70 // it's refcount goes from 1 -> 0). The initial state is that the browser | 82 // it's refcount goes from 1 -> 0). The initial state is that the browser |
71 // plugin code started to return a value, which means it gets another ref | 83 // plugin code started to return a value, which means it gets another ref |
72 // on behalf of the caller. This needs to be transferred to the plugin and | 84 // on behalf of the caller. This needs to be transferred to the plugin and |
73 // folded in to its set of refs it maintains (with one ref representing all | 85 // folded in to its set of refs it maintains (with one ref representing all |
74 // fo them in the browser). | 86 // fo them in the browser). |
75 if (var.type == PP_VARTYPE_OBJECT) | 87 if (var.type == PP_VARTYPE_OBJECT) { |
76 var_tracker_->ReceiveObjectPassRef(var); | 88 return var_tracker_->ReceiveObjectPassRef(var, dispatcher); |
| 89 } |
| 90 |
| 91 // Other types are unchanged. |
77 return var; | 92 return var; |
78 } | 93 } |
79 | 94 |
80 void PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var, | 95 PP_Var PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var, |
81 std::string* str_val) { | 96 std::string* str_val) { |
82 // Overview of sending an object with "pass ref" from the plugin to the | 97 // Overview of sending an object with "pass ref" from the plugin to the |
83 // browser: | 98 // browser: |
84 // Example 1 Example 2 | 99 // Example 1 Example 2 |
85 // Plugin Browser Plugin Browser | 100 // Plugin Browser Plugin Browser |
86 // Before send 3 1 1 1 | 101 // Before send 3 1 1 1 |
87 // Plugin calls BeginSendPassRef 3 1 1 1 | 102 // Plugin calls BeginSendPassRef 3 1 1 1 |
88 // Browser calls ReceivePassRef 3 2 1 2 | 103 // Browser calls ReceivePassRef 3 2 1 2 |
89 // Plugin calls EndSendPassRef 2 2 0 1 | 104 // Plugin calls EndSendPassRef 2 2 0 1 |
90 // | 105 // |
91 // The plugin maintains one ref count in the browser on behalf of the | 106 // The plugin maintains one ref count in the browser on behalf of the |
92 // entire ref count in the plugin. When the plugin refcount goes to 0, it | 107 // entire ref count in the plugin. When the plugin refcount goes to 0, it |
93 // will call the browser to deref the object. This is why in example 2 | 108 // will call the browser to deref the object. This is why in example 2 |
94 // transferring the object ref to the browser involves no net change in the | 109 // transferring the object ref to the browser involves no net change in the |
95 // browser's refcount. | 110 // browser's refcount. |
96 | 111 |
| 112 // Objects need special translations to get the IDs valid in the host. |
| 113 if (var.type == PP_VARTYPE_OBJECT) |
| 114 return var_tracker_->GetHostObject(var); |
| 115 |
97 if (var.type == PP_VARTYPE_STRING) | 116 if (var.type == PP_VARTYPE_STRING) |
98 *str_val = var_tracker_->GetString(var); | 117 *str_val = var_tracker_->GetString(var); |
| 118 return var; |
99 } | 119 } |
100 | 120 |
101 void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var) { | 121 void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var) { |
102 // See BeginSendPassRef for an example of why we release our ref here. | 122 // See BeginSendPassRef for an example of why we release our ref here. |
103 if (var.type == PP_VARTYPE_OBJECT) | 123 if (var.type == PP_VARTYPE_OBJECT) |
104 var_tracker_->Release(var); | 124 var_tracker_->Release(var); |
105 } | 125 } |
106 | 126 |
107 void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { | 127 void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { |
108 var_tracker_->Release(var); | 128 var_tracker_->Release(var); |
109 } | 129 } |
110 | 130 |
111 } // namespace proxy | 131 } // namespace proxy |
112 } // namespace pp | 132 } // namespace pp |
OLD | NEW |