Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 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/plugin_var_serialization_rules.h" | ||||
6 | |||||
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 7 | #include "ppapi/proxy/plugin_dispatcher.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 8 | #include "ppapi/proxy/plugin_globals.h" |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 9 | #include "ppapi/proxy/plugin_resource_tracker.h" |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 10 | #include "ppapi/proxy/plugin_var_tracker.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 11 | #include "ppapi/shared_impl/ppapi_globals.h" |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 12 | #include "ppapi/shared_impl/var.h" |
13 | |||||
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 14 | namespace ppapi { |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 15 | namespace proxy { |
16 | |||||
[email protected] | 67600b9 | 2012-03-10 06:51:48 | [diff] [blame] | 17 | PluginVarSerializationRules::PluginVarSerializationRules( |
18 | const base::WeakPtr<PluginDispatcher>& dispatcher) | ||||
19 | : var_tracker_(PluginGlobals::Get()->plugin_var_tracker()), | ||||
20 | dispatcher_(dispatcher) { | ||||
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 21 | } |
22 | |||||
23 | PluginVarSerializationRules::~PluginVarSerializationRules() { | ||||
24 | } | ||||
25 | |||||
[email protected] | 2d449b3 | 2012-02-07 05:38:00 | [diff] [blame] | 26 | PP_Var PluginVarSerializationRules::SendCallerOwned(const PP_Var& var) { |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 27 | // Objects need special translations to get the IDs valid in the host. |
28 | if (var.type == PP_VARTYPE_OBJECT) | ||||
29 | return var_tracker_->GetHostObject(var); | ||||
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 30 | return var; |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 31 | } |
32 | |||||
[email protected] | 67600b9 | 2012-03-10 06:51:48 | [diff] [blame] | 33 | PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned(const PP_Var& var) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 34 | if (var.type == PP_VARTYPE_OBJECT) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 35 | return dispatcher_.get() ? var_tracker_->TrackObjectWithNoReference( |
36 | var, dispatcher_.get()) | ||||
37 | : PP_MakeUndefined(); | ||||
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 38 | } |
[email protected] | 67600b9 | 2012-03-10 06:51:48 | [diff] [blame] | 39 | |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 40 | return var; |
41 | } | ||||
42 | |||||
43 | void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) { | ||||
[email protected] | 10305deb | 2012-02-16 19:39:22 | [diff] [blame] | 44 | if (var.type == PP_VARTYPE_OBJECT) { |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 45 | var_tracker_->StopTrackingObjectWithNoReference(var); |
[email protected] | 10305deb | 2012-02-16 19:39:22 | [diff] [blame] | 46 | } else if (var.type >= PP_VARTYPE_STRING) { |
47 | // Release our reference to the local Var. | ||||
48 | var_tracker_->ReleaseVar(var); | ||||
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 49 | } |
50 | } | ||||
51 | |||||
[email protected] | 67600b9 | 2012-03-10 06:51:48 | [diff] [blame] | 52 | PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var) { |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 53 | // Overview of sending an object with "pass ref" from the browser to the |
54 | // plugin: | ||||
55 | // Example 1 Example 2 | ||||
56 | // Plugin Browser Plugin Browser | ||||
57 | // Before send 3 2 0 1 | ||||
58 | // Browser calls BeginSendPassRef 3 2 0 1 | ||||
59 | // Plugin calls ReceivePassRef 4 1 1 1 | ||||
60 | // Browser calls EndSendPassRef 4 1 1 1 | ||||
61 | // | ||||
62 | // In example 1 before the send, the plugin has 3 refs which are represented | ||||
63 | // as one ref in the browser (since the plugin only tells the browser when | ||||
64 | // it's refcount goes from 1 -> 0). The initial state is that the browser | ||||
65 | // plugin code started to return a value, which means it gets another ref | ||||
66 | // on behalf of the caller. This needs to be transferred to the plugin and | ||||
67 | // folded in to its set of refs it maintains (with one ref representing all | ||||
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 68 | // of them in the browser). |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 69 | if (var.type == PP_VARTYPE_OBJECT) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 70 | return dispatcher_.get() |
71 | ? var_tracker_->ReceiveObjectPassRef(var, dispatcher_.get()) | ||||
72 | : PP_MakeUndefined(); | ||||
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 73 | } |
74 | |||||
75 | // Other types are unchanged. | ||||
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 76 | return var; |
77 | } | ||||
78 | |||||
[email protected] | 2d449b3 | 2012-02-07 05:38:00 | [diff] [blame] | 79 | PP_Var PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var) { |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 80 | // Overview of sending an object with "pass ref" from the plugin to the |
81 | // browser: | ||||
82 | // Example 1 Example 2 | ||||
83 | // Plugin Browser Plugin Browser | ||||
84 | // Before send 3 1 1 1 | ||||
85 | // Plugin calls BeginSendPassRef 3 1 1 1 | ||||
86 | // Browser calls ReceivePassRef 3 2 1 2 | ||||
87 | // Plugin calls EndSendPassRef 2 2 0 1 | ||||
88 | // | ||||
89 | // The plugin maintains one ref count in the browser on behalf of the | ||||
90 | // entire ref count in the plugin. When the plugin refcount goes to 0, it | ||||
91 | // will call the browser to deref the object. This is why in example 2 | ||||
92 | // transferring the object ref to the browser involves no net change in the | ||||
93 | // browser's refcount. | ||||
94 | |||||
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 95 | // Objects need special translations to get the IDs valid in the host. |
96 | if (var.type == PP_VARTYPE_OBJECT) | ||||
97 | return var_tracker_->GetHostObject(var); | ||||
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 98 | return var; |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 99 | } |
100 | |||||
[email protected] | 67600b9 | 2012-03-10 06:51:48 | [diff] [blame] | 101 | void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var) { |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 102 | // See BeginSendPassRef for an example of why we release our ref here. |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 103 | // The var we have in our inner class has been converted to a host object |
104 | // by BeginSendPassRef. This means it's not a normal var valid in the plugin, | ||||
105 | // so we need to use the special ReleaseHostObject. | ||||
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 106 | if (var.type == PP_VARTYPE_OBJECT) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 107 | if (dispatcher_.get()) |
108 | var_tracker_->ReleaseHostObject(dispatcher_.get(), var); | ||||
[email protected] | 10305deb | 2012-02-16 19:39:22 | [diff] [blame] | 109 | } else if (var.type >= PP_VARTYPE_STRING) { |
[email protected] | a732cec | 2011-12-22 08:35:52 | [diff] [blame] | 110 | var_tracker_->ReleaseVar(var); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 111 | } |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 112 | } |
113 | |||||
114 | void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) { | ||||
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 115 | var_tracker_->ReleaseVar(var); |
[email protected] | 6608531 | 2010-11-05 22:14:25 | [diff] [blame] | 116 | } |
117 | |||||
118 | } // namespace proxy | ||||
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 119 | } // namespace ppapi |