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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_RENDERER_PEPPER_V8_VAR_CONVERTER_H 6 #define CONTENT_RENDERER_PEPPER_V8_VAR_CONVERTER_H 7 8 #include "base/basictypes.h" 9 #include "base/callback.h" 10 #include "base/compiler_specific.h" 11 #include "base/message_loop/message_loop_proxy.h" 12 #include "ppapi/c/pp_instance.h" 13 #include "ppapi/c/pp_var.h" 14 #include "ppapi/shared_impl/scoped_pp_var.h" 15 #include "v8/include/v8.h" 16 #include "content/common/content_export.h" 17 18 namespace content { 19 20 class ResourceConverter; 21 22 class CONTENT_EXPORT V8VarConverter { 23 public: 24 explicit V8VarConverter(PP_Instance instance); 25 // Constructor for testing. 26 V8VarConverter(PP_Instance instance, 27 scoped_ptr<ResourceConverter> resource_converter); 28 ~V8VarConverter(); 29 30 // Converts the given PP_Var to a v8::Value. True is returned upon success. 31 bool ToV8Value(const PP_Var& var, 32 v8::Handle<v8::Context> context, 33 v8::Handle<v8::Value>* result); 34 35 struct VarResult { 36 public: 37 VarResult() : completed_synchronously(false), success(false) {} 38 39 // True if the conversion completed synchronously and the callback will not 40 // be called. 41 bool completed_synchronously; 42 43 // True if the conversion was successful. Only valid if 44 // |completed_synchronously| is true. 45 bool success; 46 47 // The result if the conversion was successful. Only valid if 48 // |completed_synchronously| and |success| are true. 49 ppapi::ScopedPPVar var; 50 }; 51 52 // Converts the given v8::Value to a PP_Var. Every PP_Var in the reference 53 // graph in the result will have a refcount equal to the number of references 54 // to it in the graph. The root of the result will have one additional 55 // reference. The callback is run when conversion is complete with the 56 // resulting var and a bool indicating success or failure. Conversion may be 57 // asynchronous because converting some resources may result in communication 58 // across IPC. |context| is guaranteed to only be used synchronously. If 59 // the conversion can occur synchronously, |callback| will not be run, 60 // otherwise it will be run. 61 VarResult FromV8Value( 62 v8::Handle<v8::Value> val, 63 v8::Handle<v8::Context> context, 64 const base::Callback<void(const ppapi::ScopedPPVar&, bool)>& callback); 65 bool FromV8ValueSync(v8::Handle<v8::Value> val, 66 v8::Handle<v8::Context> context, 67 ppapi::ScopedPPVar* result_var); 68 private: 69 // Returns true on success, false on failure. 70 bool FromV8ValueInternal(v8::Handle<v8::Value> val, 71 v8::Handle<v8::Context> context, 72 ppapi::ScopedPPVar* result_var); 73 74 // The message loop to run the callback to |FromV8Value| from. 75 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 76 77 // The converter to use for converting V8 vars to resources. 78 scoped_ptr<ResourceConverter> resource_converter_; 79 80 DISALLOW_COPY_AND_ASSIGN(V8VarConverter); 81 }; 82 83 } // namespace content 84 85 #endif // CONTENT_RENDERER_PEPPER_V8_VAR_CONVERTER_H 86