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