blob: 27185f85410b31b364495d2dc538b5e31f94d585 [file] [log] [blame]
Avi Drissmandb497b32022-09-15 19:47:281// Copyright 2012 The Chromium Authors
[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]4614f192011-01-21 00:26:437#include "ppapi/proxy/plugin_dispatcher.h"
[email protected]794d83cd2011-10-20 19:09:208#include "ppapi/proxy/plugin_globals.h"
[email protected]2bbd2c672011-08-09 23:14:139#include "ppapi/proxy/plugin_resource_tracker.h"
[email protected]66085312010-11-05 22:14:2510#include "ppapi/proxy/plugin_var_tracker.h"
[email protected]794d83cd2011-10-20 19:09:2011#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]2bbd2c672011-08-09 23:14:1312#include "ppapi/shared_impl/var.h"
13
[email protected]4d2efd22011-08-18 21:58:0214namespace ppapi {
[email protected]66085312010-11-05 22:14:2515namespace proxy {
16
[email protected]67600b92012-03-10 06:51:4817PluginVarSerializationRules::PluginVarSerializationRules(
18 const base::WeakPtr<PluginDispatcher>& dispatcher)
19 : var_tracker_(PluginGlobals::Get()->plugin_var_tracker()),
20 dispatcher_(dispatcher) {
[email protected]66085312010-11-05 22:14:2521}
22
23PluginVarSerializationRules::~PluginVarSerializationRules() {
24}
25
[email protected]2d449b32012-02-07 05:38:0026PP_Var PluginVarSerializationRules::SendCallerOwned(const PP_Var& var) {
[email protected]4614f192011-01-21 00:26:4327 // 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]4614f192011-01-21 00:26:4330 return var;
[email protected]66085312010-11-05 22:14:2531}
32
[email protected]67600b92012-03-10 06:51:4833PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned(const PP_Var& var) {
[email protected]465faa22011-02-08 16:31:4634 if (var.type == PP_VARTYPE_OBJECT) {
[email protected]7ffde472013-06-04 06:42:1935 return dispatcher_.get() ? var_tracker_->TrackObjectWithNoReference(
36 var, dispatcher_.get())
37 : PP_MakeUndefined();
[email protected]465faa22011-02-08 16:31:4638 }
[email protected]67600b92012-03-10 06:51:4839
[email protected]66085312010-11-05 22:14:2540 return var;
41}
42
43void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) {
[email protected]10305deb2012-02-16 19:39:2244 if (var.type == PP_VARTYPE_OBJECT) {
[email protected]4614f192011-01-21 00:26:4345 var_tracker_->StopTrackingObjectWithNoReference(var);
[email protected]10305deb2012-02-16 19:39:2246 } else if (var.type >= PP_VARTYPE_STRING) {
47 // Release our reference to the local Var.
48 var_tracker_->ReleaseVar(var);
[email protected]66085312010-11-05 22:14:2549 }
50}
51
[email protected]67600b92012-03-10 06:51:4852PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var) {
[email protected]66085312010-11-05 22:14:2553 // 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]73097562012-01-12 19:38:5568 // of them in the browser).
[email protected]4614f192011-01-21 00:26:4369 if (var.type == PP_VARTYPE_OBJECT) {
[email protected]7ffde472013-06-04 06:42:1970 return dispatcher_.get()
71 ? var_tracker_->ReceiveObjectPassRef(var, dispatcher_.get())
72 : PP_MakeUndefined();
[email protected]4614f192011-01-21 00:26:4373 }
74
75 // Other types are unchanged.
[email protected]66085312010-11-05 22:14:2576 return var;
77}
78
[email protected]2d449b32012-02-07 05:38:0079PP_Var PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var) {
[email protected]66085312010-11-05 22:14:2580 // 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]4614f192011-01-21 00:26:4395 // 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]4614f192011-01-21 00:26:4398 return var;
[email protected]66085312010-11-05 22:14:2599}
100
[email protected]67600b92012-03-10 06:51:48101void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var) {
[email protected]66085312010-11-05 22:14:25102 // See BeginSendPassRef for an example of why we release our ref here.
[email protected]f24448db2011-01-27 20:40:39103 // 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]465faa22011-02-08 16:31:46106 if (var.type == PP_VARTYPE_OBJECT) {
[email protected]7ffde472013-06-04 06:42:19107 if (dispatcher_.get())
108 var_tracker_->ReleaseHostObject(dispatcher_.get(), var);
[email protected]10305deb2012-02-16 19:39:22109 } else if (var.type >= PP_VARTYPE_STRING) {
[email protected]a732cec2011-12-22 08:35:52110 var_tracker_->ReleaseVar(var);
[email protected]465faa22011-02-08 16:31:46111 }
[email protected]66085312010-11-05 22:14:25112}
113
114void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) {
[email protected]2bbd2c672011-08-09 23:14:13115 var_tracker_->ReleaseVar(var);
[email protected]66085312010-11-05 22:14:25116}
117
118} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02119} // namespace ppapi