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