blob: f772554b4ac55299c6061311c734f42b9f48e08d [file] [log] [blame]
[email protected]73097562012-01-12 19:38:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]66085312010-11-05 22:14:252// 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]465faa22011-02-08 16:31:467#include "base/logging.h"
[email protected]4614f192011-01-21 00:26:438#include "ppapi/proxy/plugin_dispatcher.h"
[email protected]794d83cd2011-10-20 19:09:209#include "ppapi/proxy/plugin_globals.h"
[email protected]2bbd2c672011-08-09 23:14:1310#include "ppapi/proxy/plugin_resource_tracker.h"
[email protected]66085312010-11-05 22:14:2511#include "ppapi/proxy/plugin_var_tracker.h"
[email protected]794d83cd2011-10-20 19:09:2012#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]2bbd2c672011-08-09 23:14:1313#include "ppapi/shared_impl/var.h"
14
[email protected]4d2efd22011-08-18 21:58:0215namespace ppapi {
[email protected]66085312010-11-05 22:14:2516namespace proxy {
17
[email protected]67600b92012-03-10 06:51:4818PluginVarSerializationRules::PluginVarSerializationRules(
19 const base::WeakPtr<PluginDispatcher>& dispatcher)
20 : var_tracker_(PluginGlobals::Get()->plugin_var_tracker()),
21 dispatcher_(dispatcher) {
[email protected]66085312010-11-05 22:14:2522}
23
24PluginVarSerializationRules::~PluginVarSerializationRules() {
25}
26
[email protected]2d449b32012-02-07 05:38:0027PP_Var PluginVarSerializationRules::SendCallerOwned(const PP_Var& var) {
[email protected]4614f192011-01-21 00:26:4328 // 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]4614f192011-01-21 00:26:4331 return var;
[email protected]66085312010-11-05 22:14:2532}
33
[email protected]67600b92012-03-10 06:51:4834PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned(const PP_Var& var) {
[email protected]465faa22011-02-08 16:31:4635 if (var.type == PP_VARTYPE_OBJECT) {
[email protected]7ffde472013-06-04 06:42:1936 return dispatcher_.get() ? var_tracker_->TrackObjectWithNoReference(
37 var, dispatcher_.get())
38 : PP_MakeUndefined();
[email protected]465faa22011-02-08 16:31:4639 }
[email protected]67600b92012-03-10 06:51:4840
[email protected]66085312010-11-05 22:14:2541 return var;
42}
43
44void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) {
[email protected]10305deb2012-02-16 19:39:2245 if (var.type == PP_VARTYPE_OBJECT) {
[email protected]4614f192011-01-21 00:26:4346 var_tracker_->StopTrackingObjectWithNoReference(var);
[email protected]10305deb2012-02-16 19:39:2247 } else if (var.type >= PP_VARTYPE_STRING) {
48 // Release our reference to the local Var.
49 var_tracker_->ReleaseVar(var);
[email protected]66085312010-11-05 22:14:2550 }
51}
52
[email protected]67600b92012-03-10 06:51:4853PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var) {
[email protected]66085312010-11-05 22:14:2554 // 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]73097562012-01-12 19:38:5569 // of them in the browser).
[email protected]4614f192011-01-21 00:26:4370 if (var.type == PP_VARTYPE_OBJECT) {
[email protected]7ffde472013-06-04 06:42:1971 return dispatcher_.get()
72 ? var_tracker_->ReceiveObjectPassRef(var, dispatcher_.get())
73 : PP_MakeUndefined();
[email protected]4614f192011-01-21 00:26:4374 }
75
76 // Other types are unchanged.
[email protected]66085312010-11-05 22:14:2577 return var;
78}
79
[email protected]2d449b32012-02-07 05:38:0080PP_Var PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var) {
[email protected]66085312010-11-05 22:14:2581 // 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]4614f192011-01-21 00:26:4396 // 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]4614f192011-01-21 00:26:4399 return var;
[email protected]66085312010-11-05 22:14:25100}
101
[email protected]67600b92012-03-10 06:51:48102void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var) {
[email protected]66085312010-11-05 22:14:25103 // See BeginSendPassRef for an example of why we release our ref here.
[email protected]f24448db2011-01-27 20:40:39104 // 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]465faa22011-02-08 16:31:46107 if (var.type == PP_VARTYPE_OBJECT) {
[email protected]7ffde472013-06-04 06:42:19108 if (dispatcher_.get())
109 var_tracker_->ReleaseHostObject(dispatcher_.get(), var);
[email protected]10305deb2012-02-16 19:39:22110 } else if (var.type >= PP_VARTYPE_STRING) {
[email protected]a732cec2011-12-22 08:35:52111 var_tracker_->ReleaseVar(var);
[email protected]465faa22011-02-08 16:31:46112 }
[email protected]66085312010-11-05 22:14:25113}
114
115void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) {
[email protected]2bbd2c672011-08-09 23:14:13116 var_tracker_->ReleaseVar(var);
[email protected]66085312010-11-05 22:14:25117}
118
119} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02120} // namespace ppapi