Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / v8_var_converter.cc
index b09fccd..6923776 100644 (file)
@@ -15,7 +15,9 @@
 #include "base/memory/scoped_ptr.h"
 #include "content/public/renderer/renderer_ppapi_host.h"
 #include "content/renderer/pepper/host_array_buffer_var.h"
+#include "content/renderer/pepper/host_globals.h"
 #include "content/renderer/pepper/resource_converter.h"
+#include "content/renderer/pepper/v8object_var.h"
 #include "ppapi/shared_impl/array_var.h"
 #include "ppapi/shared_impl/dictionary_var.h"
 #include "ppapi/shared_impl/var.h"
@@ -28,6 +30,7 @@ using ppapi::ArrayVar;
 using ppapi::DictionaryVar;
 using ppapi::ScopedPPVar;
 using ppapi::StringVar;
+using ppapi::V8ObjectVar;
 using std::make_pair;
 
 namespace {
@@ -80,6 +83,7 @@ typedef base::hash_set<HashedHandle> ParentHandleSet;
 // value was created as a result of calling the function.
 bool GetOrCreateV8Value(v8::Handle<v8::Context> context,
                         const PP_Var& var,
+                        bool object_vars_allowed,
                         v8::Handle<v8::Value>* result,
                         bool* did_create,
                         VarHandleMap* visited_ids,
@@ -150,9 +154,17 @@ bool GetOrCreateV8Value(v8::Handle<v8::Context> context,
     case PP_VARTYPE_DICTIONARY:
       *result = v8::Object::New(isolate);
       break;
-    case PP_VARTYPE_OBJECT:
-      result->Clear();
-      return false;
+    case PP_VARTYPE_OBJECT: {
+      DCHECK(object_vars_allowed);
+      scoped_refptr<V8ObjectVar> v8_object_var = V8ObjectVar::FromPPVar(var);
+      if (!v8_object_var) {
+        NOTREACHED();
+        result->Clear();
+        return false;
+      }
+      *result = v8_object_var->GetHandle();
+      break;
+    }
     case PP_VARTYPE_RESOURCE:
       if (!resource_converter->ToV8Value(var, context, result)) {
         result->Clear();
@@ -174,6 +186,8 @@ bool GetOrCreateV8Value(v8::Handle<v8::Context> context,
 // calling the function.
 bool GetOrCreateVar(v8::Handle<v8::Value> val,
                     v8::Handle<v8::Context> context,
+                    PP_Instance instance,
+                    bool object_vars_allowed,
                     PP_Var* result,
                     bool* did_create,
                     HandleVarMap* visited_handles,
@@ -197,6 +211,7 @@ bool GetOrCreateVar(v8::Handle<v8::Value> val,
     }
   }
 
+  v8::Isolate* isolate = context->GetIsolate();
   if (val->IsUndefined()) {
     *result = PP_MakeUndefined();
   } else if (val->IsNull()) {
@@ -214,12 +229,15 @@ bool GetOrCreateVar(v8::Handle<v8::Value> val,
     *result = (new ArrayVar())->GetPPVar();
   } else if (val->IsObject()) {
     scoped_ptr<blink::WebArrayBuffer> web_array_buffer(
-        blink::WebArrayBufferConverter::createFromV8Value(
-            val, context->GetIsolate()));
+        blink::WebArrayBufferConverter::createFromV8Value(val, isolate));
     if (web_array_buffer.get()) {
       scoped_refptr<HostArrayBufferVar> buffer_var(
           new HostArrayBufferVar(*web_array_buffer));
       *result = buffer_var->GetPPVar();
+    } else if (object_vars_allowed) {
+      v8::Handle<v8::Object> object = val->ToObject();
+      *result = content::HostGlobals::Get()->
+          host_var_tracker()->V8ObjectVarForV8Object(instance, object);
     } else {
       bool was_resource;
       if (!resource_converter->FromV8Value(
@@ -252,14 +270,26 @@ bool CanHaveChildren(PP_Var var) {
 }  // namespace
 
 V8VarConverter::V8VarConverter(PP_Instance instance)
-    : message_loop_proxy_(base::MessageLoopProxy::current()) {
+    : instance_(instance),
+      object_vars_allowed_(false),
+      message_loop_proxy_(base::MessageLoopProxy::current()) {
+  resource_converter_.reset(new ResourceConverterImpl(
+      instance, RendererPpapiHost::GetForPPInstance(instance)));
+}
+
+V8VarConverter::V8VarConverter(PP_Instance instance, bool object_vars_allowed)
+    : instance_(instance),
+      object_vars_allowed_(object_vars_allowed),
+      message_loop_proxy_(base::MessageLoopProxy::current()) {
   resource_converter_.reset(new ResourceConverterImpl(
       instance, RendererPpapiHost::GetForPPInstance(instance)));
 }
 
 V8VarConverter::V8VarConverter(PP_Instance instance,
                                scoped_ptr<ResourceConverter> resource_converter)
-    : message_loop_proxy_(base::MessageLoopProxy::current()),
+    : instance_(instance),
+      object_vars_allowed_(false),
+      message_loop_proxy_(base::MessageLoopProxy::current()),
       resource_converter_(resource_converter.release()) {}
 
 V8VarConverter::~V8VarConverter() {}
@@ -305,6 +335,7 @@ bool V8VarConverter::ToV8Value(const PP_Var& var,
     bool did_create = false;
     if (!GetOrCreateV8Value(context,
                             current_var,
+                            object_vars_allowed_,
                             &current_v8,
                             &did_create,
                             &visited_ids,
@@ -334,6 +365,7 @@ bool V8VarConverter::ToV8Value(const PP_Var& var,
         v8::Handle<v8::Value> child_v8;
         if (!GetOrCreateV8Value(context,
                                 child_var,
+                                object_vars_allowed_,
                                 &child_v8,
                                 &did_create,
                                 &visited_ids,
@@ -369,6 +401,7 @@ bool V8VarConverter::ToV8Value(const PP_Var& var,
         v8::Handle<v8::Value> child_v8;
         if (!GetOrCreateV8Value(context,
                                 child_var,
+                                object_vars_allowed_,
                                 &child_v8,
                                 &did_create,
                                 &visited_ids,
@@ -455,6 +488,8 @@ bool V8VarConverter::FromV8ValueInternal(
     bool did_create = false;
     if (!GetOrCreateVar(current_v8,
                         context,
+                        instance_,
+                        object_vars_allowed_,
                         &current_var,
                         &did_create,
                         &visited_handles,
@@ -492,6 +527,8 @@ bool V8VarConverter::FromV8ValueInternal(
         PP_Var child_var;
         if (!GetOrCreateVar(child_v8,
                             context,
+                            instance_,
+                            object_vars_allowed_,
                             &child_var,
                             &did_create,
                             &visited_handles,
@@ -541,6 +578,8 @@ bool V8VarConverter::FromV8ValueInternal(
         PP_Var child_var;
         if (!GetOrCreateVar(child_v8,
                             context,
+                            instance_,
+                            object_vars_allowed_,
                             &child_var,
                             &did_create,
                             &visited_handles,