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 const base::WeakPtr<PluginDispatcher>& dispatcher) |
| 20 : var_tracker_(PluginGlobals::Get()->plugin_var_tracker()), |
| 21 dispatcher_(dispatcher) { |
20 } | 22 } |
21 | 23 |
22 PluginVarSerializationRules::~PluginVarSerializationRules() { | 24 PluginVarSerializationRules::~PluginVarSerializationRules() { |
23 } | 25 } |
24 | 26 |
25 PP_Var PluginVarSerializationRules::SendCallerOwned(const PP_Var& var) { | 27 PP_Var PluginVarSerializationRules::SendCallerOwned(const PP_Var& var) { |
26 // Objects need special translations to get the IDs valid in the host. | 28 // Objects need special translations to get the IDs valid in the host. |
27 if (var.type == PP_VARTYPE_OBJECT) | 29 if (var.type == PP_VARTYPE_OBJECT) |
28 return var_tracker_->GetHostObject(var); | 30 return var_tracker_->GetHostObject(var); |
29 return var; | 31 return var; |
30 } | 32 } |
31 | 33 |
32 PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned( | 34 PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned(const PP_Var& var) { |
33 const PP_Var& var, | |
34 Dispatcher* dispatcher) { | |
35 if (var.type == PP_VARTYPE_OBJECT) { | 35 if (var.type == PP_VARTYPE_OBJECT) { |
36 DCHECK(dispatcher->IsPlugin()); | 36 return dispatcher_ ? |
37 return var_tracker_->TrackObjectWithNoReference( | 37 var_tracker_->TrackObjectWithNoReference(var, dispatcher_) : |
38 var, static_cast<PluginDispatcher*>(dispatcher)); | 38 PP_MakeUndefined(); |
39 } | 39 } |
| 40 |
40 return var; | 41 return var; |
41 } | 42 } |
42 | 43 |
43 void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { | 44 void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { |
44 if (var.type == PP_VARTYPE_OBJECT) { | 45 if (var.type == PP_VARTYPE_OBJECT) { |
45 var_tracker_->StopTrackingObjectWithNoReference(var); | 46 var_tracker_->StopTrackingObjectWithNoReference(var); |
46 } else if (var.type >= PP_VARTYPE_STRING) { | 47 } else if (var.type >= PP_VARTYPE_STRING) { |
47 // Release our reference to the local Var. | 48 // Release our reference to the local Var. |
48 var_tracker_->ReleaseVar(var); | 49 var_tracker_->ReleaseVar(var); |
49 } | 50 } |
50 } | 51 } |
51 | 52 |
52 PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var, | 53 PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var) { |
53 Dispatcher* dispatcher) { | |
54 // 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 |
55 // plugin: | 55 // plugin: |
56 // Example 1 Example 2 | 56 // Example 1 Example 2 |
57 // Plugin Browser Plugin Browser | 57 // Plugin Browser Plugin Browser |
58 // Before send 3 2 0 1 | 58 // Before send 3 2 0 1 |
59 // Browser calls BeginSendPassRef 3 2 0 1 | 59 // Browser calls BeginSendPassRef 3 2 0 1 |
60 // Plugin calls ReceivePassRef 4 1 1 1 | 60 // Plugin calls ReceivePassRef 4 1 1 1 |
61 // Browser calls EndSendPassRef 4 1 1 1 | 61 // Browser calls EndSendPassRef 4 1 1 1 |
62 // | 62 // |
63 // 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 |
64 // 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 |
65 // 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 |
66 // 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 |
67 // 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 |
68 // 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 |
69 // of them in the browser). | 69 // of them in the browser). |
70 if (var.type == PP_VARTYPE_OBJECT) { | 70 if (var.type == PP_VARTYPE_OBJECT) { |
71 DCHECK(dispatcher->IsPlugin()); | 71 return dispatcher_ ? |
72 return var_tracker_->ReceiveObjectPassRef( | 72 var_tracker_->ReceiveObjectPassRef(var, dispatcher_) : |
73 var, static_cast<PluginDispatcher*>(dispatcher)); | 73 PP_MakeUndefined(); |
74 } | 74 } |
75 | 75 |
76 // Other types are unchanged. | 76 // Other types are unchanged. |
77 return var; | 77 return var; |
78 } | 78 } |
79 | 79 |
80 PP_Var PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var) { | 80 PP_Var PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var) { |
81 // 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 |
82 // browser: | 82 // browser: |
83 // Example 1 Example 2 | 83 // Example 1 Example 2 |
84 // Plugin Browser Plugin Browser | 84 // Plugin Browser Plugin Browser |
85 // Before send 3 1 1 1 | 85 // Before send 3 1 1 1 |
86 // Plugin calls BeginSendPassRef 3 1 1 1 | 86 // Plugin calls BeginSendPassRef 3 1 1 1 |
87 // Browser calls ReceivePassRef 3 2 1 2 | 87 // Browser calls ReceivePassRef 3 2 1 2 |
88 // Plugin calls EndSendPassRef 2 2 0 1 | 88 // Plugin calls EndSendPassRef 2 2 0 1 |
89 // | 89 // |
90 // 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 |
91 // 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 |
92 // 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 |
93 // 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 |
94 // browser's refcount. | 94 // browser's refcount. |
95 | 95 |
96 // 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. |
97 if (var.type == PP_VARTYPE_OBJECT) | 97 if (var.type == PP_VARTYPE_OBJECT) |
98 return var_tracker_->GetHostObject(var); | 98 return var_tracker_->GetHostObject(var); |
99 return var; | 99 return var; |
100 } | 100 } |
101 | 101 |
102 void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var, | 102 void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var) { |
103 Dispatcher* dispatcher) { | |
104 // See BeginSendPassRef for an example of why we release our ref here. | 103 // See BeginSendPassRef for an example of why we release our ref here. |
105 // The var we have in our inner class has been converted to a host object | 104 // The var we have in our inner class has been converted to a host object |
106 // by BeginSendPassRef. This means it's not a normal var valid in the plugin, | 105 // by BeginSendPassRef. This means it's not a normal var valid in the plugin, |
107 // so we need to use the special ReleaseHostObject. | 106 // so we need to use the special ReleaseHostObject. |
108 if (var.type == PP_VARTYPE_OBJECT) { | 107 if (var.type == PP_VARTYPE_OBJECT) { |
109 var_tracker_->ReleaseHostObject( | 108 if (dispatcher_) |
110 static_cast<PluginDispatcher*>(dispatcher), var); | 109 var_tracker_->ReleaseHostObject(dispatcher_, var); |
111 } else if (var.type >= PP_VARTYPE_STRING) { | 110 } else if (var.type >= PP_VARTYPE_STRING) { |
112 var_tracker_->ReleaseVar(var); | 111 var_tracker_->ReleaseVar(var); |
113 } | 112 } |
114 } | 113 } |
115 | 114 |
116 void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { | 115 void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { |
117 var_tracker_->ReleaseVar(var); | 116 var_tracker_->ReleaseVar(var); |
118 } | 117 } |
119 | 118 |
120 } // namespace proxy | 119 } // namespace proxy |
121 } // namespace ppapi | 120 } // namespace ppapi |
OLD | NEW |