summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-05 22:14:25 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-05 22:14:25 +0000
commit66085319ab6fbc59916871566848ec454b35a453 (patch)
tree851b6521f75ba18f913645ccec2711a1f1c46dc7
parentf6b8a8d47aeba7ee52e1b3f1ee93f84972a0348c (diff)
downloadchromium_src-66085319ab6fbc59916871566848ec454b35a453.zip
chromium_src-66085319ab6fbc59916871566848ec454b35a453.tar.gz
chromium_src-66085319ab6fbc59916871566848ec454b35a453.tar.bz2
Var serialization-related proxy stuff. This allows vars to be sent over IPC
and received with proper refcounting semantics. The basic design is that the SerializedVar is the thing actually handled by IPC. Then you use one of the many helper classes depending on whether you are sending or receiving and what the ownership semantics are. The helper classes, along with the VarSerialization interface makes the right thing happen for string conversion & refcounting. The VarSerialization class is implemented differently on the browser and plugin side and is where the differences in refcounting & strings are handled. This allows the rest of the code to be the same on both the plugin and browser side and allows vars to be sent back and forth. This patch references some files not in it like the dispatcher and various tracker classes. This is the cleanest cut I could make for a reasonably reviewable chunk. Review URL: http://codereview.chromium.org/4096008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65262 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--ppapi/proxy/host_var_serialization_rules.cc91
-rw-r--r--ppapi/proxy/host_var_serialization_rules.h51
-rw-r--r--ppapi/proxy/plugin_var_serialization_rules.cc112
-rw-r--r--ppapi/proxy/plugin_var_serialization_rules.h44
-rw-r--r--ppapi/proxy/serialized_var.cc465
-rw-r--r--ppapi/proxy/serialized_var.h404
-rw-r--r--ppapi/proxy/var_serialization_rules.h85
7 files changed, 1252 insertions, 0 deletions
diff --git a/ppapi/proxy/host_var_serialization_rules.cc b/ppapi/proxy/host_var_serialization_rules.cc
new file mode 100644
index 0000000..9ca6a15
--- /dev/null
+++ b/ppapi/proxy/host_var_serialization_rules.cc
@@ -0,0 +1,91 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/proxy/host_var_serialization_rules.h"
+
+#include "base/logging.h"
+#include "ppapi/c/dev/ppb_var_deprecated.h"
+
+namespace pp {
+namespace proxy {
+
+HostVarSerializationRules::HostVarSerializationRules(
+ const PPB_Var_Deprecated* var_interface,
+ PP_Module pp_module)
+ : var_interface_(var_interface),
+ pp_module_(pp_module) {
+}
+
+HostVarSerializationRules::~HostVarSerializationRules() {
+}
+
+void HostVarSerializationRules::SendCallerOwned(const PP_Var& var,
+ std::string* str_val) {
+ if (var.type == PP_VARTYPE_STRING)
+ VarToString(var, str_val);
+}
+
+PP_Var HostVarSerializationRules::BeginReceiveCallerOwned(
+ const PP_Var& var,
+ const std::string* str_val) {
+ if (var.type == PP_VARTYPE_STRING) {
+ // Convert the string to the context of the current process.
+ return var_interface_->VarFromUtf8(pp_module_, str_val->c_str(),
+ static_cast<uint32_t>(str_val->size()));
+ }
+ return var;
+}
+
+void HostVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) {
+ if (var.type == PP_VARTYPE_STRING) {
+ // Destroy the string BeginReceiveCallerOwned created above.
+ var_interface_->Release(var);
+ }
+}
+
+PP_Var HostVarSerializationRules::ReceivePassRef(const PP_Var& var,
+ const std::string& str_val) {
+ if (var.type == PP_VARTYPE_STRING) {
+ // Convert the string to the context of the current process.
+ return var_interface_->VarFromUtf8(pp_module_, str_val.c_str(),
+ static_cast<uint32_t>(str_val.size()));
+ }
+
+ // See PluginVarSerialization::BeginSendPassRef for an example.
+ if (var.type == PP_VARTYPE_OBJECT)
+ var_interface_->AddRef(var);
+ return var;
+}
+
+void HostVarSerializationRules::BeginSendPassRef(const PP_Var& var,
+ std::string* str_val) {
+ // See PluginVarSerialization::ReceivePassRef for an example. We don't need
+ // to do anything here other than convert the string.
+ if (var.type == PP_VARTYPE_STRING)
+ VarToString(var, str_val);
+}
+
+void HostVarSerializationRules::EndSendPassRef(const PP_Var& var) {
+ // See PluginVarSerialization::ReceivePassRef for an example. We don't need
+ // to do anything here.
+}
+
+void HostVarSerializationRules::VarToString(const PP_Var& var,
+ std::string* str) {
+ DCHECK(var.type == PP_VARTYPE_STRING);
+
+ // This could be optimized to avoid an extra string copy by going to a lower
+ // level of the browser's implementation of strings where we already have
+ // a std::string.
+ uint32_t len = 0;
+ const char* data = var_interface_->VarToUtf8(var, &len);
+ str->assign(data, len);
+}
+
+void HostVarSerializationRules::ReleaseObjectRef(const PP_Var& var) {
+ var_interface_->Release(var);
+}
+
+} // namespace proxy
+} // namespace pp
diff --git a/ppapi/proxy/host_var_serialization_rules.h b/ppapi/proxy/host_var_serialization_rules.h
new file mode 100644
index 0000000..92e294f
--- /dev/null
+++ b/ppapi/proxy/host_var_serialization_rules.h
@@ -0,0 +1,51 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PPAPI_PROXY_HOST_VAR_SERIALIZATION_RULES_H_
+#define PPAPI_PROXY_HOST_VAR_SERIALIZATION_RULES_H_
+
+#include "base/basictypes.h"
+#include "ppapi/c/pp_module.h"
+#include "ppapi/proxy/var_serialization_rules.h"
+
+struct PPB_Var_Deprecated;
+
+namespace pp {
+namespace proxy {
+
+class VarTracker;
+
+// Implementation of the VarSerializationRules interface for the host side.
+class HostVarSerializationRules : public VarSerializationRules {
+ public:
+ HostVarSerializationRules(const PPB_Var_Deprecated* var_interface,
+ PP_Module pp_module);
+ ~HostVarSerializationRules();
+
+ // VarSerialization implementation.
+ virtual void SendCallerOwned(const PP_Var& var, std::string* str_val);
+ virtual PP_Var BeginReceiveCallerOwned(const PP_Var& var,
+ const std::string* str_val);
+ virtual void EndReceiveCallerOwned(const PP_Var& var);
+ virtual PP_Var ReceivePassRef(const PP_Var& var,
+ const std::string& str_val);
+ virtual void BeginSendPassRef(const PP_Var& var, std::string* str_val);
+ virtual void EndSendPassRef(const PP_Var& var);
+ virtual void ReleaseObjectRef(const PP_Var& var);
+
+ private:
+ // Converts the given var (which must be a VARTYPE_STRING) to the given
+ // string object.
+ void VarToString(const PP_Var& var, std::string* str);
+
+ const PPB_Var_Deprecated* var_interface_;
+ PP_Module pp_module_;
+
+ DISALLOW_COPY_AND_ASSIGN(HostVarSerializationRules);
+};
+
+} // namespace proxy
+} // namespace pp
+
+#endif // PPAPI_PROXY_HOST_VAR_SERIALIZATION_RULES_H_
diff --git a/ppapi/proxy/plugin_var_serialization_rules.cc b/ppapi/proxy/plugin_var_serialization_rules.cc
new file mode 100644
index 0000000..d01f8d5
--- /dev/null
+++ b/ppapi/proxy/plugin_var_serialization_rules.cc
@@ -0,0 +1,112 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/proxy/plugin_var_serialization_rules.h"
+
+#include "ppapi/proxy/plugin_var_tracker.h"
+
+namespace pp {
+namespace proxy {
+
+PluginVarSerializationRules::PluginVarSerializationRules(
+ PluginVarTracker* var_tracker)
+ : var_tracker_(var_tracker) {
+}
+
+PluginVarSerializationRules::~PluginVarSerializationRules() {
+}
+
+void PluginVarSerializationRules::SendCallerOwned(const PP_Var& var,
+ std::string* str_val) {
+ // Nothing to do since we manage the refcount, other than retrieve the string
+ // to use for IPC.
+ if (var.type == PP_VARTYPE_STRING)
+ *str_val = var_tracker_->GetString(var);
+}
+
+PP_Var PluginVarSerializationRules::BeginReceiveCallerOwned(
+ const PP_Var& var,
+ const std::string* str_val) {
+ if (var.type == PP_VARTYPE_STRING) {
+ // Convert the string to the context of the current process.
+ PP_Var ret;
+ ret.type = PP_VARTYPE_STRING;
+ ret.value.as_id = var_tracker_->MakeString(*str_val);
+ return ret;
+ }
+
+ return var;
+}
+
+void PluginVarSerializationRules::EndReceiveCallerOwned(const PP_Var& var) {
+ if (var.type == PP_VARTYPE_STRING) {
+ // Destroy the string BeginReceiveCallerOwned created above.
+ var_tracker_->Release(var);
+ }
+}
+
+PP_Var PluginVarSerializationRules::ReceivePassRef(const PP_Var& var,
+ const std::string& str_val) {
+ if (var.type == PP_VARTYPE_STRING) {
+ // Convert the string to the context of the current process.
+ PP_Var ret;
+ ret.type = PP_VARTYPE_STRING;
+ ret.value.as_id = var_tracker_->MakeString(str_val);
+ return ret;
+ }
+
+ // Overview of sending an object with "pass ref" from the browser to the
+ // plugin:
+ // Example 1 Example 2
+ // Plugin Browser Plugin Browser
+ // Before send 3 2 0 1
+ // Browser calls BeginSendPassRef 3 2 0 1
+ // Plugin calls ReceivePassRef 4 1 1 1
+ // Browser calls EndSendPassRef 4 1 1 1
+ //
+ // In example 1 before the send, the plugin has 3 refs which are represented
+ // as one ref in the browser (since the plugin only tells the browser when
+ // it's refcount goes from 1 -> 0). The initial state is that the browser
+ // plugin code started to return a value, which means it gets another ref
+ // on behalf of the caller. This needs to be transferred to the plugin and
+ // folded in to its set of refs it maintains (with one ref representing all
+ // fo them in the browser).
+ if (var.type == PP_VARTYPE_OBJECT)
+ var_tracker_->ReceiveObjectPassRef(var);
+ return var;
+}
+
+void PluginVarSerializationRules::BeginSendPassRef(const PP_Var& var,
+ std::string* str_val) {
+ // Overview of sending an object with "pass ref" from the plugin to the
+ // browser:
+ // Example 1 Example 2
+ // Plugin Browser Plugin Browser
+ // Before send 3 1 1 1
+ // Plugin calls BeginSendPassRef 3 1 1 1
+ // Browser calls ReceivePassRef 3 2 1 2
+ // Plugin calls EndSendPassRef 2 2 0 1
+ //
+ // The plugin maintains one ref count in the browser on behalf of the
+ // entire ref count in the plugin. When the plugin refcount goes to 0, it
+ // will call the browser to deref the object. This is why in example 2
+ // transferring the object ref to the browser involves no net change in the
+ // browser's refcount.
+
+ if (var.type == PP_VARTYPE_STRING)
+ *str_val = var_tracker_->GetString(var);
+}
+
+void PluginVarSerializationRules::EndSendPassRef(const PP_Var& var) {
+ // See BeginSendPassRef for an example of why we release our ref here.
+ if (var.type == PP_VARTYPE_OBJECT)
+ var_tracker_->Release(var);
+}
+
+void PluginVarSerializationRules::ReleaseObjectRef(const PP_Var& var) {
+ var_tracker_->Release(var);
+}
+
+} // namespace proxy
+} // namespace pp
diff --git a/ppapi/proxy/plugin_var_serialization_rules.h b/ppapi/proxy/plugin_var_serialization_rules.h
new file mode 100644
index 0000000..6f302f2
--- /dev/null
+++ b/ppapi/proxy/plugin_var_serialization_rules.h
@@ -0,0 +1,44 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PPAPI_PROXY_PLUGIN_VAR_SERIALIZATION_RULES_H_
+#define PPAPI_PROXY_PLUGIN_VAR_SERIALIZATION_RULES_H_
+
+#include "base/basictypes.h"
+#include "ppapi/proxy/var_serialization_rules.h"
+
+namespace pp {
+namespace proxy {
+
+class PluginVarTracker;
+
+// Implementation of the VarSerialization interface for the plugin.
+class PluginVarSerializationRules : public VarSerializationRules {
+ public:
+ // This class will use the given non-owning pointer to the var tracker to
+ // handle object refcounting and string conversion.
+ PluginVarSerializationRules(PluginVarTracker* tracker);
+ ~PluginVarSerializationRules();
+
+ // VarSerialization implementation.
+ virtual void SendCallerOwned(const PP_Var& var, std::string* str_val);
+ virtual PP_Var BeginReceiveCallerOwned(const PP_Var& var,
+ const std::string* str_val);
+ virtual void EndReceiveCallerOwned(const PP_Var& var);
+ virtual PP_Var ReceivePassRef(const PP_Var& var,
+ const std::string& str_val);
+ virtual void BeginSendPassRef(const PP_Var& var, std::string* str_val);
+ virtual void EndSendPassRef(const PP_Var& var);
+ virtual void ReleaseObjectRef(const PP_Var& var);
+
+ private:
+ PluginVarTracker* var_tracker_;
+
+ DISALLOW_COPY_AND_ASSIGN(PluginVarSerializationRules);
+};
+
+} // namespace proxy
+} // namespace pp
+
+#endif // PPAPI_PROXY_PLUGIN_VAR_SERIALIZATION_RULES_H_
diff --git a/ppapi/proxy/serialized_var.cc b/ppapi/proxy/serialized_var.cc
new file mode 100644
index 0000000..66a16c3
--- /dev/null
+++ b/ppapi/proxy/serialized_var.cc
@@ -0,0 +1,465 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/proxy/serialized_var.h"
+
+#include "base/logging.h"
+#include "ipc/ipc_message_utils.h"
+#include "ppapi/proxy/dispatcher.h"
+#include "ppapi/proxy/ppapi_param_traits.h"
+#include "ppapi/proxy/var_serialization_rules.h"
+
+namespace pp {
+namespace proxy {
+
+// SerializedVar::Inner --------------------------------------------------------
+
+SerializedVar::Inner::Inner()
+ : serialization_rules_(NULL),
+ var_(PP_MakeUndefined()),
+ cleanup_mode_(CLEANUP_NONE) {
+#ifndef NDEBUG
+ has_been_serialized_ = false;
+ has_been_deserialized_ = false;
+#endif
+}
+
+SerializedVar::Inner::Inner(VarSerializationRules* serialization_rules)
+ : serialization_rules_(serialization_rules),
+ var_(PP_MakeUndefined()),
+ cleanup_mode_(CLEANUP_NONE) {
+#ifndef NDEBUG
+ has_been_serialized_ = false;
+ has_been_deserialized_ = false;
+#endif
+}
+
+SerializedVar::Inner::Inner(VarSerializationRules* serialization_rules,
+ const PP_Var& var)
+ : serialization_rules_(serialization_rules),
+ var_(var),
+ cleanup_mode_(CLEANUP_NONE) {
+#ifndef NDEBUG
+ has_been_serialized_ = false;
+ has_been_deserialized_ = false;
+#endif
+}
+
+SerializedVar::Inner::~Inner() {
+ switch (cleanup_mode_) {
+ case END_SEND_PASS_REF:
+ serialization_rules_->EndSendPassRef(var_);
+ break;
+ case END_RECEIVE_CALLER_OWNED:
+ serialization_rules_->EndReceiveCallerOwned(var_);
+ break;
+ default:
+ break;
+ }
+}
+
+PP_Var SerializedVar::Inner::GetVar() const {
+ DCHECK(serialization_rules_);
+
+ // If we're a string var, we should have already converted the string value
+ // to a var ID.
+ DCHECK(var_.type != PP_VARTYPE_STRING || var_.value.as_id != 0);
+ return var_;
+}
+
+PP_Var SerializedVar::Inner::GetIncompleteVar() const {
+ DCHECK(serialization_rules_);
+ return var_;
+}
+
+void SerializedVar::Inner::SetVar(PP_Var var) {
+ // Sanity check, when updating the var we should have received a
+ // serialization rules pointer already.
+ DCHECK(serialization_rules_);
+ var_ = var;
+}
+
+const std::string& SerializedVar::Inner::GetString() const {
+ DCHECK(serialization_rules_);
+ return string_value_;
+}
+
+std::string* SerializedVar::Inner::GetStringPtr() {
+ DCHECK(serialization_rules_);
+ return &string_value_;
+}
+
+void SerializedVar::Inner::WriteToMessage(IPC::Message* m) const {
+ // When writing to the IPC messages, a serization rules handler should
+ // always have been set.
+ //
+ // When sending a message, it should be difficult to trigger this if you're
+ // using the SerializedVarSendInput class and giving a non-NULL dispatcher.
+ // Make sure you're using the proper "Send" helper class.
+ //
+ // It should be more common to see this when handling an incoming message
+ // that returns a var. This means the message handler didn't write to the
+ // output parameter, or possibly you used the wrong helper class
+ // (normally SerializedVarReturnValue).
+ DCHECK(serialization_rules_);
+
+#ifndef NDEBUG
+ // We should only be serializing something once.
+ DCHECK(!has_been_serialized_);
+ has_been_serialized_ = true;
+#endif
+
+ // If the var is not a string type, we should not have ended up with any
+ // string data.
+ DCHECK(var_.type == PP_VARTYPE_STRING || string_value_.empty());
+
+ m->WriteInt(static_cast<int>(var_.type));
+ switch (var_.type) {
+ case PP_VARTYPE_UNDEFINED:
+ case PP_VARTYPE_NULL:
+ // These don't need any data associated with them other than the type we
+ // just serialized.
+ break;
+ case PP_VARTYPE_BOOL:
+ m->WriteBool(var_.value.as_bool);
+ break;
+ case PP_VARTYPE_INT32:
+ m->WriteInt(var_.value.as_int);
+ break;
+ case PP_VARTYPE_DOUBLE:
+ IPC::ParamTraits<double>::Write(m, var_.value.as_double);
+ break;
+ case PP_VARTYPE_STRING:
+ // TODO(brettw) in the case of an invalid string ID, it would be nice
+ // to send something to the other side such that a 0 ID would be
+ // generated there. Then the function implementing the interface can
+ // handle the invalid string as if it was in process rather than seeing
+ // what looks like a valid empty string.
+ m->WriteString(string_value_);
+ break;
+ case PP_VARTYPE_OBJECT:
+ m->WriteInt64(var_.value.as_id);
+ break;
+ }
+}
+
+bool SerializedVar::Inner::ReadFromMessage(const IPC::Message* m, void** iter) {
+#ifndef NDEBUG
+ // We should only deserialize something once or will end up with leaked
+ // references.
+ //
+ // One place this has happened in the past is using
+ // std::vector<SerializedVar>.resize(). If you're doing this manually instead
+ // of using the helper classes for handling in/out vectors of vars, be
+ // sure you use the same pattern as the SerializedVarVector classes.
+ DCHECK(!has_been_deserialized_);
+ has_been_deserialized_ = true;
+#endif
+
+ // When reading, the dispatcher should be set when we get a Deserialize
+ // call (which will supply a dispatcher).
+ int type;
+ if (!m->ReadInt(iter, &type))
+ return false;
+
+ bool success = false;
+ switch (type) {
+ case PP_VARTYPE_UNDEFINED:
+ case PP_VARTYPE_NULL:
+ // These don't have any data associated with them other than the type we
+ // just serialized.
+ success = true;
+ break;
+ case PP_VARTYPE_BOOL:
+ success = m->ReadBool(iter, &var_.value.as_bool);
+ break;
+ case PP_VARTYPE_INT32:
+ success = m->ReadInt(iter, &var_.value.as_int);
+ break;
+ case PP_VARTYPE_DOUBLE:
+ success = IPC::ParamTraits<double>::Read(m, iter, &var_.value.as_double);
+ break;
+ case PP_VARTYPE_STRING:
+ success = m->ReadString(iter, &string_value_);
+ var_.value.as_id = 0;
+ break;
+ case PP_VARTYPE_OBJECT:
+ success = m->ReadInt64(iter, &var_.value.as_id);
+ break;
+ default:
+ // Leave success as false.
+ break;
+ }
+
+ // All success cases get here. We avoid writing the type above so that the
+ // output param is untouched (defaults to VARTYPE_UNDEFINED) even in the
+ // failure case.
+ if (success)
+ var_.type = static_cast<PP_VarType>(type);
+ return success;
+}
+
+// SerializedVar ---------------------------------------------------------------
+
+SerializedVar::SerializedVar() : inner_(new Inner) {
+}
+
+SerializedVar::SerializedVar(VarSerializationRules* serialization_rules)
+ : inner_(new Inner(serialization_rules)) {
+}
+
+SerializedVar::SerializedVar(VarSerializationRules* serialization_rules,
+ const PP_Var& var)
+ : inner_(new Inner(serialization_rules, var)) {
+}
+
+SerializedVar::~SerializedVar() {
+}
+
+// SerializedVarSendInput ------------------------------------------------------
+
+SerializedVarSendInput::SerializedVarSendInput(Dispatcher* dispatcher,
+ const PP_Var& var)
+ : SerializedVar(dispatcher->serialization_rules(), var) {
+ dispatcher->serialization_rules()->SendCallerOwned(var,
+ inner_->GetStringPtr());
+}
+
+// static
+void SerializedVarSendInput::ConvertVector(Dispatcher* dispatcher,
+ const PP_Var* input,
+ size_t input_count,
+ std::vector<SerializedVar>* output) {
+ output->resize(input_count);
+ for (size_t i = 0; i < input_count; i++) {
+ (*output)[i] = SerializedVar(dispatcher->serialization_rules(), input[i]);
+ dispatcher->serialization_rules()->SendCallerOwned(
+ input[i], (*output)[i].inner_->GetStringPtr());
+ }
+}
+
+// ReceiveSerializedVarReturnValue ---------------------------------------------
+
+ReceiveSerializedVarReturnValue::ReceiveSerializedVarReturnValue() {
+}
+
+PP_Var ReceiveSerializedVarReturnValue::Return(Dispatcher* dispatcher) {
+ inner_->set_serialization_rules(dispatcher->serialization_rules());
+ inner_->SetVar(inner_->serialization_rules()->ReceivePassRef(
+ inner_->GetIncompleteVar(), inner_->GetString()));
+ return inner_->GetVar();
+}
+
+// ReceiveSerializedException --------------------------------------------------
+
+ReceiveSerializedException::ReceiveSerializedException(Dispatcher* dispatcher,
+ PP_Var* exception)
+ : SerializedVar(dispatcher->serialization_rules()),
+ exception_(exception) {
+}
+
+ReceiveSerializedException::~ReceiveSerializedException() {
+ if (exception_) {
+ // When an output exception is specified, it will take ownership of the
+ // reference.
+ inner_->SetVar(inner_->serialization_rules()->ReceivePassRef(
+ inner_->GetIncompleteVar(), inner_->GetString()));
+ *exception_ = inner_->GetVar();
+ } else {
+ // When no output exception is specified, the browser thinks we have a ref
+ // to an object that we don't want (this will happen only in the plugin
+ // since the browser will always specify an out exception for the plugin to
+ // write into).
+ //
+ // Strings don't need this handling since we can just avoid creating a
+ // Var from the std::string in the first place.
+ if (inner_->GetVar().type == PP_VARTYPE_OBJECT)
+ inner_->serialization_rules()->ReleaseObjectRef(inner_->GetVar());
+ }
+}
+
+bool ReceiveSerializedException::IsThrown() const {
+ return exception_ && exception_->type != PP_VARTYPE_UNDEFINED;
+}
+
+// ReceiveSerializedVarVectorOutParam ------------------------------------------
+
+ReceiveSerializedVarVectorOutParam::ReceiveSerializedVarVectorOutParam(
+ Dispatcher* dispatcher,
+ uint32_t* output_count,
+ PP_Var** output)
+ : dispatcher_(dispatcher),
+ output_count_(output_count),
+ output_(output) {
+}
+
+ReceiveSerializedVarVectorOutParam::~ReceiveSerializedVarVectorOutParam() {
+ *output_count_ = static_cast<uint32_t>(vector_.size());
+ if (!vector_.size()) {
+ *output_ = NULL;
+ return;
+ }
+
+ *output_ = static_cast<PP_Var*>(malloc(vector_.size() * sizeof(PP_Var)));
+ for (size_t i = 0; i < vector_.size(); i++) {
+ // Here we just mimic what happens when returning a value.
+ ReceiveSerializedVarReturnValue converted;
+ SerializedVar* serialized = &converted;
+ *serialized = vector_[i];
+ (*output_)[i] = converted.Return(dispatcher_);
+ }
+}
+
+std::vector<SerializedVar>* ReceiveSerializedVarVectorOutParam::OutParam() {
+ return &vector_;
+}
+
+// SerializedVarReceiveInput ---------------------------------------------------
+
+SerializedVarReceiveInput::SerializedVarReceiveInput(
+ const SerializedVar& serialized)
+ : serialized_(serialized),
+ dispatcher_(NULL),
+ var_(PP_MakeUndefined()) {
+}
+
+SerializedVarReceiveInput::~SerializedVarReceiveInput() {
+}
+
+PP_Var SerializedVarReceiveInput::Get(Dispatcher* dispatcher) {
+ serialized_.inner_->set_serialization_rules(
+ dispatcher->serialization_rules());
+
+ // Ensure that when the serialized var goes out of scope it cleans up the
+ // stuff we're making in BeginReceiveCallerOwned.
+ serialized_.inner_->set_cleanup_mode(SerializedVar::END_RECEIVE_CALLER_OWNED);
+
+ serialized_.inner_->SetVar(
+ serialized_.inner_->serialization_rules()->BeginReceiveCallerOwned(
+ serialized_.inner_->GetIncompleteVar(),
+ serialized_.inner_->GetStringPtr()));
+ return serialized_.inner_->GetVar();
+}
+
+// SerializedVarVectorReceiveInput ---------------------------------------------
+
+SerializedVarVectorReceiveInput::SerializedVarVectorReceiveInput(
+ const std::vector<SerializedVar>& serialized)
+ : serialized_(serialized) {
+}
+
+SerializedVarVectorReceiveInput::~SerializedVarVectorReceiveInput() {
+ for (size_t i = 0; i < deserialized_.size(); i++) {
+ serialized_[i].inner_->serialization_rules()->EndReceiveCallerOwned(
+ deserialized_[i]);
+ }
+}
+
+PP_Var* SerializedVarVectorReceiveInput::Get(Dispatcher* dispatcher,
+ uint32_t* array_size) {
+ deserialized_.resize(serialized_.size());
+ for (size_t i = 0; i < serialized_.size(); i++) {
+ // The vector must be able to clean themselves up after this call is
+ // torn down.
+ serialized_[i].inner_->set_serialization_rules(
+ dispatcher->serialization_rules());
+
+ serialized_[i].inner_->SetVar(
+ serialized_[i].inner_->serialization_rules()->BeginReceiveCallerOwned(
+ serialized_[i].inner_->GetIncompleteVar(),
+ serialized_[i].inner_->GetStringPtr()));
+ deserialized_[i] = serialized_[i].inner_->GetVar();
+ }
+
+ *array_size = static_cast<uint32_t>(serialized_.size());
+ return deserialized_.size() > 0 ? &deserialized_[0] : NULL;
+}
+
+// SerializedVarReturnValue ----------------------------------------------------
+
+SerializedVarReturnValue::SerializedVarReturnValue(SerializedVar* serialized)
+ : serialized_(serialized) {
+}
+
+void SerializedVarReturnValue::Return(Dispatcher* dispatcher,
+ const PP_Var& var) {
+ serialized_->inner_->set_serialization_rules(
+ dispatcher->serialization_rules());
+ serialized_->inner_->SetVar(var);
+
+ // Var must clean up after our BeginSendPassRef call.
+ serialized_->inner_->set_cleanup_mode(SerializedVar::END_SEND_PASS_REF);
+
+ dispatcher->serialization_rules()->BeginSendPassRef(
+ serialized_->inner_->GetIncompleteVar(),
+ serialized_->inner_->GetStringPtr());
+}
+
+// SerializedVarOutParam -------------------------------------------------------
+
+SerializedVarOutParam::SerializedVarOutParam(SerializedVar* serialized)
+ : serialized_(serialized),
+ writable_var_(PP_MakeUndefined()) {
+}
+
+SerializedVarOutParam::~SerializedVarOutParam() {
+ if (serialized_->inner_->serialization_rules()) {
+ // When unset, OutParam wasn't called. We'll just leave the var untouched
+ // in that case.
+ serialized_->inner_->SetVar(writable_var_);
+ serialized_->inner_->serialization_rules()->BeginSendPassRef(
+ writable_var_, serialized_->inner_->GetStringPtr());
+
+ // Normally the current object will be created on the stack to wrap a
+ // SerializedVar and won't have a scope around the actual IPC send. So we
+ // need to tell the SerializedVar to do the begin/end send pass ref calls.
+ serialized_->inner_->set_cleanup_mode(SerializedVar::END_SEND_PASS_REF);
+ }
+}
+
+PP_Var* SerializedVarOutParam::OutParam(Dispatcher* dispatcher) {
+ serialized_->inner_->set_serialization_rules(
+ dispatcher->serialization_rules());
+ return &writable_var_;
+}
+
+// SerializedVarVectorOutParam -------------------------------------------------
+
+SerializedVarVectorOutParam::SerializedVarVectorOutParam(
+ std::vector<SerializedVar>* serialized)
+ : dispatcher_(NULL),
+ serialized_(serialized),
+ count_(0),
+ array_(NULL) {
+}
+
+SerializedVarVectorOutParam::~SerializedVarVectorOutParam() {
+ DCHECK(dispatcher_);
+
+ // Convert the array written by the pepper code to the serialized structure.
+ // Note we can't use resize here, we have to allocate a new SerializedVar
+ // for each serialized item. See ParamTraits<vector<SerializedVar>>::Read.
+ serialized_->reserve(count_);
+ for (uint32_t i = 0; i < count_; i++) {
+ // Just mimic what we do for regular OutParams.
+ SerializedVar var;
+ SerializedVarOutParam out(&var);
+ *out.OutParam(dispatcher_) = array_[i];
+ serialized_->push_back(var);
+ }
+
+ // When returning arrays, the pepper code expects the caller to take
+ // ownership of the array.
+ free(array_);
+}
+
+PP_Var** SerializedVarVectorOutParam::ArrayOutParam(Dispatcher* dispatcher) {
+ DCHECK(!dispatcher_); // Should only be called once.
+ dispatcher_ = dispatcher;
+ return &array_;
+}
+
+} // namespace proxy
+} // namespace pp
+
diff --git a/ppapi/proxy/serialized_var.h b/ppapi/proxy/serialized_var.h
new file mode 100644
index 0000000..21adfd0
--- /dev/null
+++ b/ppapi/proxy/serialized_var.h
@@ -0,0 +1,404 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PPAPI_PROXY_SERIALIZED_VAR_H_
+#define PPAPI_PROXY_SERIALIZED_VAR_H_
+
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/ref_counted.h"
+#include "ppapi/c/pp_var.h"
+
+namespace IPC {
+class Message;
+}
+
+namespace pp {
+namespace proxy {
+
+class Dispatcher;
+class VarSerializationRules;
+
+// This class encapsulates a var so that we can serialize and deserialize it
+// The problem is that for strings, serialization and deserialization requires
+// knowledge from outside about how to get at or create a string. So this
+// object groups the var with a dispatcher so that string values can be set or
+// gotten.
+//
+// Declare IPC messages as using this type, but don't use it directly (it has
+// no useful public methods). Instead, instantiate one of the helper classes
+// below which are conveniently named for each use case to prevent screwups.
+//
+// Design background
+// -----------------
+// This is sadly super complicated. The IPC system needs a consistent type to
+// use for sending and receiving vars (this is a SerializedVar). But there are
+// different combinations of reference counting for sending and receiving
+// objects and for dealing with strings
+//
+// This makes SerializedVar complicate and easy to mess up. To make it
+// reasonable to use all functions are protected and there are a use-specific
+// classes that encapsulate exactly one type of use in a way that typically
+// won't compile if you do the wrong thing.
+//
+// The IPC system is designed to pass things around and will make copies in
+// some cases, so our system must be designed so that this stuff will work.
+// This is challenging when the SerializedVar must to some cleanup after the
+// message is sent. To work around this, we create an inner class using a
+// linked_ptr so all copies of a SerializedVar can share and we can guarantee
+// that the actual data will get cleaned up on shutdown.
+//
+// Constness
+// ---------
+// SerializedVar basically doesn't support const. Everything is mutable and
+// most functions are declared const. This unfortunateness is because of the
+// way the IPC system works. When deserializing, it will have a const
+// SerializedVar in a Tuple and this will be given to the function. We kind of
+// want to modify that to convert strings and do refcounting.
+//
+// The helper classes used for accessing the SerializedVar have more reasonable
+// behavior and will enforce that you don't do stupid things.
+class SerializedVar {
+ public:
+ enum CleanupMode {
+ // The serialized var won't do anything special in the destructor (default).
+ CLEANUP_NONE,
+
+ // The serialized var will call EndSendPassRef in the destructor.
+ END_SEND_PASS_REF,
+
+ // The serialized var will call EndReceiveCallerOwned in the destructor.
+ END_RECEIVE_CALLER_OWNED
+ };
+
+ SerializedVar();
+ ~SerializedVar();
+
+ // Backend implementation for IPC::ParamTraits<SerializedVar>.
+ void WriteToMessage(IPC::Message* m) const {
+ inner_->WriteToMessage(m);
+ }
+ bool ReadFromMessage(const IPC::Message* m, void** iter) {
+ return inner_->ReadFromMessage(m, iter);
+ }
+
+ protected:
+ friend class SerializedVarReceiveInput;
+ friend class SerializedVarReturnValue;
+ friend class SerializedVarOutParam;
+ friend class SerializedVarSendInput;
+ friend class SerializedVarVectorReceiveInput;
+
+ class Inner : public base::RefCounted<Inner> {
+ public:
+ Inner();
+ Inner(VarSerializationRules* serialization_rules);
+ Inner(VarSerializationRules* serialization_rules, const PP_Var& var);
+ ~Inner();
+
+ VarSerializationRules* serialization_rules() {
+ return serialization_rules_;
+ }
+ void set_serialization_rules(VarSerializationRules* serialization_rules) {
+ serialization_rules_ = serialization_rules;
+ }
+
+ void set_cleanup_mode(CleanupMode cm) { cleanup_mode_ = cm; }
+
+ // See outer class's declarations above.
+ PP_Var GetVar() const;
+ PP_Var GetIncompleteVar() const;
+ void SetVar(PP_Var var);
+ const std::string& GetString() const;
+ std::string* GetStringPtr();
+
+ void WriteToMessage(IPC::Message* m) const;
+ bool ReadFromMessage(const IPC::Message* m, void** iter);
+
+ private:
+ // Rules for serializing and deserializing vars for this process type.
+ // This may be NULL, but must be set before trying to serialize to IPC when
+ // sending, or before converting back to a PP_Var when receiving.
+ VarSerializationRules* serialization_rules_;
+
+ // If this is set to VARTYPE_STRING and the 'value.id' is 0, then the
+ // string_value_ contains the string. This means that the caller hasn't
+ // called Deserialize with a valid Dispatcher yet, which is how we can
+ // convert the serialized string value to a PP_Var string ID.
+ //
+ // This var may not be complete until the serialization rules are set when
+ // reading from IPC since we'll need that to convert the string_value to
+ // a string ID. Before this, the as_id will be 0 for VARTYPE_STRING.
+ PP_Var var_;
+
+ // Holds the literal string value to/from IPC. This will be valid of the
+ // var_ is VARTYPE_STRING.
+ std::string string_value_;
+
+ CleanupMode cleanup_mode_;
+
+#ifndef NDEBUG
+ // When being sent or received over IPC, we should only be serialized or
+ // deserialized once. These flags help us assert this is true.
+ mutable bool has_been_serialized_;
+ mutable bool has_been_deserialized_;
+#endif
+
+ DISALLOW_COPY_AND_ASSIGN(Inner);
+ };
+
+ SerializedVar(VarSerializationRules* serialization_rules);
+ SerializedVar(VarSerializationRules* serialization, const PP_Var& var);
+
+ mutable scoped_refptr<Inner> inner_;
+};
+
+// Helpers for message sending side --------------------------------------------
+
+// For sending a value to the remote side.
+//
+// Example for API:
+// void MyFunction(PP_Var)
+// IPC message:
+// IPC_MESSAGE_ROUTED1(MyFunction, SerializedVar);
+// Sender would be:
+// void MyFunctionProxy(PP_Var param) {
+// Send(new MyFunctionMsg(SerializedVarSendInput(param));
+// }
+class SerializedVarSendInput : public SerializedVar {
+ public:
+ SerializedVarSendInput(Dispatcher* dispatcher, const PP_Var& var);
+
+ // Helper function for serializing a vector of input vars for serialization.
+ static void ConvertVector(Dispatcher* dispatcher,
+ const PP_Var* input,
+ size_t input_count,
+ std::vector<SerializedVar>* output);
+
+ private:
+ // Disallow the empty constructor, but keep the default copy constructor
+ // which is required to send the object to the IPC system.
+ SerializedVarSendInput();
+};
+
+// For the calling side of a function returning a var. The sending side uses
+// SerializedVarReturnValue.
+//
+// Example for API:
+// PP_Var MyFunction()
+// IPC message:
+// IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar);
+// Message handler would be:
+// PP_Var MyFunctionProxy() {
+// ReceiveSerializedVarReturnValue result;
+// Send(new MyFunctionMsg(&result));
+// return result.Return(dispatcher());
+// }
+class ReceiveSerializedVarReturnValue : public SerializedVar {
+ public:
+ // Note that we can't set the dispatcher in the constructor because the
+ // data will be overridden when the return value is set.
+ ReceiveSerializedVarReturnValue();
+
+ PP_Var Return(Dispatcher* dispatcher);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ReceiveSerializedVarReturnValue);
+};
+
+// Example for API:
+// "void MyFunction(PP_Var* exception);"
+// IPC message:
+// IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar);
+// Message handler would be:
+// void OnMsgMyFunction(PP_Var* exception) {
+// ReceiveSerializedException se(dispatcher(), exception)
+// Send(new PpapiHostMsg_Foo(&se));
+// }
+class ReceiveSerializedException : public SerializedVar {
+ public:
+ ReceiveSerializedException(Dispatcher* dispatcher, PP_Var* exception);
+ ~ReceiveSerializedException();
+
+ // Returns true if the exception passed in the constructor is set. Check
+ // this before actually issuing the IPC.
+ bool IsThrown() const;
+
+ private:
+ // The input/output exception we're wrapping. May be NULL.
+ PP_Var* exception_;
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(ReceiveSerializedException);
+};
+
+// Helper class for when we're returning a vector of Vars. When it goes out
+// of scope it will automatically convert the vector filled by the IPC layer
+// into the array specified by the constructor params.
+//
+// Example for API:
+// "void MyFunction(uint32_t* count, PP_Var** vars);"
+// IPC message:
+// IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, std::vector<SerializedVar>);
+// Proxy function:
+// void MyFunction(uint32_t* count, PP_Var** vars) {
+// ReceiveSerializedVarVectorOutParam vect(dispatcher, count, vars);
+// Send(new MyMsg(vect.OutParam()));
+// }
+class ReceiveSerializedVarVectorOutParam {
+ public:
+ ReceiveSerializedVarVectorOutParam(Dispatcher* dispatcher,
+ uint32_t* output_count,
+ PP_Var** output);
+ ~ReceiveSerializedVarVectorOutParam();
+
+ std::vector<SerializedVar>* OutParam();
+
+ private:
+ Dispatcher* dispatcher_;
+ uint32_t* output_count_;
+ PP_Var** output_;
+
+ std::vector<SerializedVar> vector_;
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(ReceiveSerializedVarVectorOutParam);
+};
+
+// Helpers for message receiving side ------------------------------------------
+
+// For receiving a value from the remote side.
+//
+// Example for API:
+// void MyFunction(PP_Var)
+// IPC message:
+// IPC_MESSAGE_ROUTED1(MyFunction, SerializedVar);
+// Message handler would be:
+// void OnMsgMyFunction(SerializedVarReceiveInput param) {
+// MyFunction(param.Get());
+// }
+class SerializedVarReceiveInput {
+ public:
+ // We rely on the implicit constructor here since the IPC layer will call
+ // us with a SerializedVar. Pass this object by value, the copy constructor
+ // will pass along the pointer (as cheap as passing a pointer arg).
+ SerializedVarReceiveInput(const SerializedVar& serialized);
+ ~SerializedVarReceiveInput();
+
+ PP_Var Get(Dispatcher* dispatcher);
+
+ private:
+ const SerializedVar& serialized_;
+
+ // Since the SerializedVar is const, we can't set its dispatcher (which is
+ // OK since we don't need to). But since we need it for our own uses, we
+ // track it here. Will be NULL before Get() is called.
+ Dispatcher* dispatcher_;
+ PP_Var var_;
+};
+
+// For receiving an input vector of vars from the remote side.
+//
+// Example:
+// OnMsgMyFunction(SerializedVarVectorReceiveInput vector) {
+// uint32_t size;
+// PP_Var* array = vector.Get(dispatcher, &size);
+// MyFunction(size, array);
+// }
+class SerializedVarVectorReceiveInput {
+ public:
+ SerializedVarVectorReceiveInput(const std::vector<SerializedVar>& serialized);
+ ~SerializedVarVectorReceiveInput();
+
+ // Only call Get() once. It will return a pointer to the converted array and
+ // place the array size in the out param. Will return NULL when the array is
+ // empty.
+ PP_Var* Get(Dispatcher* dispatcher, uint32_t* array_size);
+
+ private:
+ const std::vector<SerializedVar>& serialized_;
+
+ // Filled by Get().
+ std::vector<PP_Var> deserialized_;
+};
+
+// For the receiving side of a function returning a var. The calling side uses
+// ReceiveSerializedVarReturnValue.
+//
+// Example for API:
+// PP_Var MyFunction()
+// IPC message:
+// IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar);
+// Message handler would be:
+// void OnMsgMyFunction(SerializedVarReturnValue result) {
+// result.Return(dispatcher(), MyFunction());
+// }
+class SerializedVarReturnValue {
+ public:
+ // We rely on the implicit constructor here since the IPC layer will call
+ // us with a SerializedVar*. Pass this object by value, the copy constructor
+ // will pass along the pointer (as cheap as passing a pointer arg).
+ SerializedVarReturnValue(SerializedVar* serialized);
+
+ void Return(Dispatcher* dispatcher, const PP_Var& var);
+
+ private:
+ SerializedVar* serialized_;
+};
+
+// For writing an out param to the remote side.
+//
+// Example for API:
+// "void MyFunction(PP_Var* out);"
+// IPC message:
+// IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar);
+// Message handler would be:
+// void OnMsgMyFunction(SerializedVarOutParam out_param) {
+// MyFunction(out_param.OutParam(dispatcher()));
+// }
+class SerializedVarOutParam {
+ public:
+ // We rely on the implicit constructor here since the IPC layer will call
+ // us with a SerializedVar*. Pass this object by value, the copy constructor
+ // will pass along the pointer (as cheap as passing a pointer arg).
+ SerializedVarOutParam(SerializedVar* serialized);
+ ~SerializedVarOutParam();
+
+ // Call this function only once. The caller should write its result to the
+ // returned var pointer before this class goes out of scope. The var's
+ // initial value will be VARTYPE_UNDEFINED.
+ PP_Var* OutParam(Dispatcher* dispatcher);
+
+ private:
+ SerializedVar* serialized_;
+
+ // This is the value actually written by the code and returned by OutParam.
+ // We'll write this into serialized_ in our destructor.
+ PP_Var writable_var_;
+};
+
+// For returning an array of PP_Vars to the other side and transferring
+// ownership.
+//
+class SerializedVarVectorOutParam {
+ public:
+ SerializedVarVectorOutParam(std::vector<SerializedVar>* serialized);
+ ~SerializedVarVectorOutParam();
+
+ uint32_t* CountOutParam() { return &count_; }
+ PP_Var** ArrayOutParam(Dispatcher* dispatcher);
+
+ private:
+ Dispatcher* dispatcher_;
+ std::vector<SerializedVar>* serialized_;
+
+ uint32_t count_;
+ PP_Var* array_;
+};
+
+} // namespace proxy
+} // namespace pp
+
+#endif // PPAPI_PROXY_SERIALIZED_VAR_H_
+
diff --git a/ppapi/proxy/var_serialization_rules.h b/ppapi/proxy/var_serialization_rules.h
new file mode 100644
index 0000000..a6741ee
--- /dev/null
+++ b/ppapi/proxy/var_serialization_rules.h
@@ -0,0 +1,85 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PPAPI_PROXY_VAR_SERIALIZATION_RULES_H_
+#define PPAPI_PROXY_VAR_SERIALIZATION_RULES_H_
+
+#include "ppapi/c/pp_var.h"
+
+#include <string>
+
+namespace pp {
+namespace proxy {
+
+// Encapsulates the rules for serializing and deserializing vars to and from
+// the local process. The renderer and the plugin process each have separate
+// bookkeeping rules.
+class VarSerializationRules {
+ public:
+ virtual ~VarSerializationRules() {}
+
+ // Caller-owned calls --------------------------------------------------------
+ //
+ // A caller-owned call is when doing a function call with a "normal" input
+ // argument. The caller has a reference to the var, and the caller is
+ // responsible for freeing that reference.
+
+ // Prepares the given var for sending to the callee. If the var is a string,
+ // the value of that string will be placed in *str_val. If the var is not
+ // a string, str_val will be untouched and may be NULL.
+ virtual void SendCallerOwned(const PP_Var& var, std::string* str_val) = 0;
+
+ // When receiving a caller-owned variable, normally we don't have to do
+ // anything. However, in the case of strings, we need to deserialize the
+ // string from IPC, create a new PP_Var string in the local process, call the
+ // function, and then destroy the temporary string. These two functions
+ // handle that process
+ //
+ // BeginReceiveCallerOwned takes a var from IPC and an optional pointer to
+ // the deserialized string (which will be used only when var is a
+ // VARTYPE_STRING and may be NULL otherwise) and returns a new var
+ // representing the input in the local process. The output will be the same
+ // as the input except for strings.
+ //
+ // EndReceiveCallerOwned destroys the string created by Begin* and does
+ // nothing otherwise. It should be called with the result of Begin*.
+ virtual PP_Var BeginReceiveCallerOwned(const PP_Var& var,
+ const std::string* str_val) = 0;
+ virtual void EndReceiveCallerOwned(const PP_Var& var) = 0;
+
+ // Passinag refs -------------------------------------------------------------
+ //
+ // A pass-ref transfer is when ownership of a reference is passed from
+ // onen side to the other. Normally, this happens via return values and
+ // output arguments, as for exceptions. The code generating the value
+ // (the function returning it in the case of a return value) will AddRef
+ // the var on behalf of the consumer of the value. Responsibility for
+ // Release is on the consumer (the caller of the function in the case of a
+ // return value).
+
+ // Creates a var in the context of the local process from the given
+ // deserialized var and deserialized string (which will be used only when var
+ // is a VARTYPE_STRING and may be NULL otherwise). The input var/string
+ // should be the result of calling SendPassRef in the remote process.
+ virtual PP_Var ReceivePassRef(const PP_Var& var,
+ const std::string& str_val) = 0;
+
+ // Prepares a var to be sent to the remote side. One local reference will
+ // be passed to the remote side. Call Begin* before doing the send and End*
+ // after doing the send. See SendCallerOwned for a description of the string.
+ virtual void BeginSendPassRef(const PP_Var& var, std::string* str_val) = 0;
+ virtual void EndSendPassRef(const PP_Var& var) = 0;
+
+ // ---------------------------------------------------------------------------
+
+ virtual void ReleaseObjectRef(const PP_Var& var) = 0;
+
+ protected:
+ VarSerializationRules() {}
+};
+
+} // namespace proxy
+} // namespace pp
+
+#endif // PPAPI_PROXY_VAR_SERIALIZATION_RULES_H_