OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/pepper/v8_var_converter.h" | 5 #include "content/renderer/pepper/v8_var_converter.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <stack> | 8 #include <stack> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 typedef base::hash_map<HashedHandle, ScopedPPVar> HandleVarMap; | 76 typedef base::hash_map<HashedHandle, ScopedPPVar> HandleVarMap; |
77 typedef base::hash_set<HashedHandle> ParentHandleSet; | 77 typedef base::hash_set<HashedHandle> ParentHandleSet; |
78 | 78 |
79 // Returns a V8 value which corresponds to a given PP_Var. If |var| is a | 79 // Returns a V8 value which corresponds to a given PP_Var. If |var| is a |
80 // reference counted PP_Var type, and it exists in |visited_ids|, the V8 value | 80 // reference counted PP_Var type, and it exists in |visited_ids|, the V8 value |
81 // associated with it in the map will be returned, otherwise a new V8 value will | 81 // associated with it in the map will be returned, otherwise a new V8 value will |
82 // be created and added to the map. |did_create| indicates whether a new v8 | 82 // be created and added to the map. |did_create| indicates whether a new v8 |
83 // value was created as a result of calling the function. | 83 // value was created as a result of calling the function. |
84 bool GetOrCreateV8Value(v8::Handle<v8::Context> context, | 84 bool GetOrCreateV8Value(v8::Handle<v8::Context> context, |
85 const PP_Var& var, | 85 const PP_Var& var, |
86 V8VarConverter::AllowObjectVars object_vars_allowed, | 86 bool object_vars_allowed, |
87 v8::Handle<v8::Value>* result, | 87 v8::Handle<v8::Value>* result, |
88 bool* did_create, | 88 bool* did_create, |
89 VarHandleMap* visited_ids, | 89 VarHandleMap* visited_ids, |
90 ParentVarSet* parent_ids, | 90 ParentVarSet* parent_ids, |
91 ResourceConverter* resource_converter) { | 91 ResourceConverter* resource_converter) { |
92 v8::Isolate* isolate = context->GetIsolate(); | 92 v8::Isolate* isolate = context->GetIsolate(); |
93 *did_create = false; | 93 *did_create = false; |
94 | 94 |
95 if (ppapi::VarTracker::IsVarTypeRefcounted(var.type)) { | 95 if (ppapi::VarTracker::IsVarTypeRefcounted(var.type)) { |
96 if (parent_ids->count(var.value.as_id) != 0) | 96 if (parent_ids->count(var.value.as_id) != 0) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 &host_buffer->webkit_buffer(), context->Global(), isolate); | 148 &host_buffer->webkit_buffer(), context->Global(), isolate); |
149 break; | 149 break; |
150 } | 150 } |
151 case PP_VARTYPE_ARRAY: | 151 case PP_VARTYPE_ARRAY: |
152 *result = v8::Array::New(isolate); | 152 *result = v8::Array::New(isolate); |
153 break; | 153 break; |
154 case PP_VARTYPE_DICTIONARY: | 154 case PP_VARTYPE_DICTIONARY: |
155 *result = v8::Object::New(isolate); | 155 *result = v8::Object::New(isolate); |
156 break; | 156 break; |
157 case PP_VARTYPE_OBJECT: { | 157 case PP_VARTYPE_OBJECT: { |
158 DCHECK(object_vars_allowed == V8VarConverter::kAllowObjectVars); | 158 DCHECK(object_vars_allowed); |
159 scoped_refptr<V8ObjectVar> v8_object_var = V8ObjectVar::FromPPVar(var); | 159 scoped_refptr<V8ObjectVar> v8_object_var = V8ObjectVar::FromPPVar(var); |
160 if (!v8_object_var.get()) { | 160 if (!v8_object_var.get()) { |
161 NOTREACHED(); | 161 NOTREACHED(); |
162 result->Clear(); | 162 result->Clear(); |
163 return false; | 163 return false; |
164 } | 164 } |
165 *result = v8_object_var->GetHandle(); | 165 *result = v8_object_var->GetHandle(); |
166 break; | 166 break; |
167 } | 167 } |
168 case PP_VARTYPE_RESOURCE: | 168 case PP_VARTYPE_RESOURCE: |
(...skipping 11 matching lines...) Expand all Loading... |
180 } | 180 } |
181 | 181 |
182 // For a given V8 value handle, this returns a PP_Var which corresponds to it. | 182 // For a given V8 value handle, this returns a PP_Var which corresponds to it. |
183 // If the handle already exists in |visited_handles|, the PP_Var associated with | 183 // If the handle already exists in |visited_handles|, the PP_Var associated with |
184 // it will be returned, otherwise a new V8 value will be created and added to | 184 // it will be returned, otherwise a new V8 value will be created and added to |
185 // the map. |did_create| indicates if a new PP_Var was created as a result of | 185 // the map. |did_create| indicates if a new PP_Var was created as a result of |
186 // calling the function. | 186 // calling the function. |
187 bool GetOrCreateVar(v8::Handle<v8::Value> val, | 187 bool GetOrCreateVar(v8::Handle<v8::Value> val, |
188 v8::Handle<v8::Context> context, | 188 v8::Handle<v8::Context> context, |
189 PP_Instance instance, | 189 PP_Instance instance, |
190 V8VarConverter::AllowObjectVars object_vars_allowed, | 190 bool object_vars_allowed, |
191 PP_Var* result, | 191 PP_Var* result, |
192 bool* did_create, | 192 bool* did_create, |
193 HandleVarMap* visited_handles, | 193 HandleVarMap* visited_handles, |
194 ParentHandleSet* parent_handles, | 194 ParentHandleSet* parent_handles, |
195 ResourceConverter* resource_converter) { | 195 ResourceConverter* resource_converter) { |
196 CHECK(!val.IsEmpty()); | 196 CHECK(!val.IsEmpty()); |
197 *did_create = false; | 197 *did_create = false; |
198 | 198 |
199 // Even though every v8 string primitive encountered will be a unique object, | 199 // Even though every v8 string primitive encountered will be a unique object, |
200 // we still add them to |visited_handles| so that the corresponding string | 200 // we still add them to |visited_handles| so that the corresponding string |
(...skipping 26 matching lines...) Expand all Loading... |
227 *result = StringVar::StringToPPVar(std::string(*utf8, utf8.length())); | 227 *result = StringVar::StringToPPVar(std::string(*utf8, utf8.length())); |
228 } else if (val->IsArray()) { | 228 } else if (val->IsArray()) { |
229 *result = (new ArrayVar())->GetPPVar(); | 229 *result = (new ArrayVar())->GetPPVar(); |
230 } else if (val->IsObject()) { | 230 } else if (val->IsObject()) { |
231 scoped_ptr<blink::WebArrayBuffer> web_array_buffer( | 231 scoped_ptr<blink::WebArrayBuffer> web_array_buffer( |
232 blink::WebArrayBufferConverter::createFromV8Value(val, isolate)); | 232 blink::WebArrayBufferConverter::createFromV8Value(val, isolate)); |
233 if (web_array_buffer.get()) { | 233 if (web_array_buffer.get()) { |
234 scoped_refptr<HostArrayBufferVar> buffer_var( | 234 scoped_refptr<HostArrayBufferVar> buffer_var( |
235 new HostArrayBufferVar(*web_array_buffer)); | 235 new HostArrayBufferVar(*web_array_buffer)); |
236 *result = buffer_var->GetPPVar(); | 236 *result = buffer_var->GetPPVar(); |
237 } else if (object_vars_allowed == V8VarConverter::kAllowObjectVars) { | 237 } else if (object_vars_allowed) { |
238 v8::Handle<v8::Object> object = val->ToObject(); | 238 v8::Handle<v8::Object> object = val->ToObject(); |
239 *result = content::HostGlobals::Get()-> | 239 *result = content::HostGlobals::Get()-> |
240 host_var_tracker()->V8ObjectVarForV8Object(instance, object); | 240 host_var_tracker()->V8ObjectVarForV8Object(instance, object); |
241 } else { | 241 } else { |
242 bool was_resource; | 242 bool was_resource; |
243 if (!resource_converter->FromV8Value( | 243 if (!resource_converter->FromV8Value( |
244 val->ToObject(), context, result, &was_resource)) | 244 val->ToObject(), context, result, &was_resource)) |
245 return false; | 245 return false; |
246 if (!was_resource) { | 246 if (!was_resource) { |
247 *result = (new DictionaryVar())->GetPPVar(); | 247 *result = (new DictionaryVar())->GetPPVar(); |
(...skipping 16 matching lines...) Expand all Loading... |
264 } | 264 } |
265 | 265 |
266 bool CanHaveChildren(PP_Var var) { | 266 bool CanHaveChildren(PP_Var var) { |
267 return var.type == PP_VARTYPE_ARRAY || var.type == PP_VARTYPE_DICTIONARY; | 267 return var.type == PP_VARTYPE_ARRAY || var.type == PP_VARTYPE_DICTIONARY; |
268 } | 268 } |
269 | 269 |
270 } // namespace | 270 } // namespace |
271 | 271 |
272 V8VarConverter::V8VarConverter(PP_Instance instance) | 272 V8VarConverter::V8VarConverter(PP_Instance instance) |
273 : instance_(instance), | 273 : instance_(instance), |
274 object_vars_allowed_(kDisallowObjectVars), | 274 object_vars_allowed_(false), |
275 message_loop_proxy_(base::MessageLoopProxy::current()) { | 275 message_loop_proxy_(base::MessageLoopProxy::current()) { |
276 resource_converter_.reset(new ResourceConverterImpl( | 276 resource_converter_.reset(new ResourceConverterImpl( |
277 instance, RendererPpapiHost::GetForPPInstance(instance))); | 277 instance, RendererPpapiHost::GetForPPInstance(instance))); |
278 } | 278 } |
279 | 279 |
280 V8VarConverter::V8VarConverter(PP_Instance instance, | 280 V8VarConverter::V8VarConverter(PP_Instance instance, bool object_vars_allowed) |
281 AllowObjectVars object_vars_allowed) | |
282 : instance_(instance), | 281 : instance_(instance), |
283 object_vars_allowed_(object_vars_allowed), | 282 object_vars_allowed_(object_vars_allowed), |
284 message_loop_proxy_(base::MessageLoopProxy::current()) { | 283 message_loop_proxy_(base::MessageLoopProxy::current()) { |
285 resource_converter_.reset(new ResourceConverterImpl( | 284 resource_converter_.reset(new ResourceConverterImpl( |
286 instance, RendererPpapiHost::GetForPPInstance(instance))); | 285 instance, RendererPpapiHost::GetForPPInstance(instance))); |
287 } | 286 } |
288 | 287 |
289 V8VarConverter::V8VarConverter(PP_Instance instance, | 288 V8VarConverter::V8VarConverter(PP_Instance instance, |
290 scoped_ptr<ResourceConverter> resource_converter) | 289 scoped_ptr<ResourceConverter> resource_converter) |
291 : instance_(instance), | 290 : instance_(instance), |
292 object_vars_allowed_(kDisallowObjectVars), | 291 object_vars_allowed_(false), |
293 message_loop_proxy_(base::MessageLoopProxy::current()), | 292 message_loop_proxy_(base::MessageLoopProxy::current()), |
294 resource_converter_(resource_converter.release()) {} | 293 resource_converter_(resource_converter.release()) {} |
295 | 294 |
296 V8VarConverter::~V8VarConverter() {} | 295 V8VarConverter::~V8VarConverter() {} |
297 | 296 |
298 // To/FromV8Value use a stack-based DFS search to traverse V8/Var graph. Each | 297 // To/FromV8Value use a stack-based DFS search to traverse V8/Var graph. Each |
299 // iteration, the top node on the stack examined. If the node has not been | 298 // iteration, the top node on the stack examined. If the node has not been |
300 // visited yet (i.e. sentinel == false) then it is added to the list of parents | 299 // visited yet (i.e. sentinel == false) then it is added to the list of parents |
301 // which contains all of the nodes on the path from the start node to the | 300 // which contains all of the nodes on the path from the start node to the |
302 // current node. Each of the current nodes children are examined. If they appear | 301 // current node. Each of the current nodes children are examined. If they appear |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 std::string(*name_utf8, name_utf8.length()), child_var); | 594 std::string(*name_utf8, name_utf8.length()), child_var); |
596 DCHECK(success); | 595 DCHECK(success); |
597 } | 596 } |
598 } | 597 } |
599 } | 598 } |
600 *result_var = root; | 599 *result_var = root; |
601 return true; | 600 return true; |
602 } | 601 } |
603 | 602 |
604 } // namespace content | 603 } // namespace content |
OLD | NEW |