OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_PEPPER_TRY_CATCH_H_ | 5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_ |
6 #define CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_ | 6 #define CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/ref_counted.h" |
9 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
10 #include "content/renderer/pepper/v8_var_converter.h" | |
11 #include "ppapi/c/pp_var.h" | 11 #include "ppapi/c/pp_var.h" |
12 #include "ppapi/shared_impl/scoped_pp_var.h" | 12 #include "ppapi/shared_impl/scoped_pp_var.h" |
13 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 class PepperPluginInstanceImpl; | 17 class PepperPluginInstanceImpl; |
| 18 class V8VarConverter; |
18 | 19 |
19 // Base class for scripting TryCatch helpers. | 20 // Base class for scripting TryCatch helpers. |
20 class CONTENT_EXPORT PepperTryCatch { | 21 class CONTENT_EXPORT PepperTryCatch { |
21 public: | 22 public: |
22 // PepperTryCatch objects should only be used as stack variables. This object | 23 // PepperTryCatch objects should only be used as stack variables. This object |
23 // takes a reference on the given PepperPluginInstanceImpl. | 24 // takes a reference on the given PepperPluginInstanceImpl. |
24 PepperTryCatch(PepperPluginInstanceImpl* instance, | 25 PepperTryCatch(PepperPluginInstanceImpl* instance, |
25 V8VarConverter::AllowObjectVars convert_objects); | 26 V8VarConverter* var_converter); |
26 virtual ~PepperTryCatch(); | 27 virtual ~PepperTryCatch(); |
27 | 28 |
28 virtual void SetException(const char* message) = 0; | 29 virtual void SetException(const char* message) = 0; |
29 virtual bool HasException() = 0; | 30 virtual bool HasException() = 0; |
30 // Gets the context to execute scripts in. | 31 // Gets the context to execute scripts in. |
31 virtual v8::Handle<v8::Context> GetContext() = 0; | 32 virtual v8::Handle<v8::Context> GetContext() = 0; |
32 | 33 |
33 // Convenience functions for doing conversions to/from V8 values and sets an | 34 // Convenience functions for doing conversions to/from V8 values and sets an |
34 // exception if there is an error in the conversion. | 35 // exception if there is an error in the conversion. |
35 v8::Handle<v8::Value> ToV8(PP_Var var); | 36 v8::Handle<v8::Value> ToV8(PP_Var var); |
36 ppapi::ScopedPPVar FromV8(v8::Handle<v8::Value> v8_value); | 37 ppapi::ScopedPPVar FromV8(v8::Handle<v8::Value> v8_value); |
37 | 38 |
38 protected: | 39 protected: |
39 // Make sure that |instance_| is alive for the lifetime of PepperTryCatch. | 40 // Make sure that |instance_| is alive for the lifetime of PepperTryCatch. |
40 // PepperTryCatch is used mostly in Pepper scripting code, where it can be | 41 // PepperTryCatch is used mostly in Pepper scripting code, where it can be |
41 // possible to enter JavaScript synchronously which can cause the plugin to | 42 // possible to enter JavaScript synchronously which can cause the plugin to |
42 // be deleted. | 43 // be deleted. |
43 // | 44 // |
44 // Note that PepperTryCatch objects should only ever be on the stack, so this | 45 // Note that PepperTryCatch objects should only ever be on the stack, so this |
45 // shouldn't keep the instance around for too long. | 46 // shouldn't keep the instance around for too long. |
46 scoped_refptr<PepperPluginInstanceImpl> instance_; | 47 scoped_refptr<PepperPluginInstanceImpl> instance_; |
47 | 48 |
48 // Whether To/FromV8 should convert object vars. If set to | 49 V8VarConverter* var_converter_; |
49 // kDisallowObjectVars, an exception should be set if they are encountered | |
50 // during conversion. | |
51 V8VarConverter::AllowObjectVars convert_objects_; | |
52 }; | 50 }; |
53 | 51 |
54 // Catches var exceptions and emits a v8 exception. | 52 // Catches var exceptions and emits a v8 exception. |
55 class PepperTryCatchV8 : public PepperTryCatch { | 53 class PepperTryCatchV8 : public PepperTryCatch { |
56 public: | 54 public: |
57 PepperTryCatchV8(PepperPluginInstanceImpl* instance, | 55 PepperTryCatchV8(PepperPluginInstanceImpl* instance, |
58 V8VarConverter::AllowObjectVars convert_objects, | 56 V8VarConverter* var_converter, |
59 v8::Isolate* isolate); | 57 v8::Isolate* isolate); |
60 virtual ~PepperTryCatchV8(); | 58 virtual ~PepperTryCatchV8(); |
61 | 59 |
62 bool ThrowException(); | 60 bool ThrowException(); |
63 void ThrowException(const char* message); | 61 void ThrowException(const char* message); |
64 PP_Var* exception() { return &exception_; } | 62 PP_Var* exception() { return &exception_; } |
65 | 63 |
66 // PepperTryCatch | 64 // PepperTryCatch |
67 virtual void SetException(const char* message) override; | 65 virtual void SetException(const char* message) override; |
68 virtual bool HasException() override; | 66 virtual bool HasException() override; |
69 virtual v8::Handle<v8::Context> GetContext() override; | 67 virtual v8::Handle<v8::Context> GetContext() override; |
70 | 68 |
71 private: | 69 private: |
72 PP_Var exception_; | 70 PP_Var exception_; |
73 | 71 |
74 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8); | 72 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8); |
75 }; | 73 }; |
76 | 74 |
77 // Catches v8 exceptions and emits a var exception. | 75 // Catches v8 exceptions and emits a var exception. |
78 class PepperTryCatchVar : public PepperTryCatch { | 76 class PepperTryCatchVar : public PepperTryCatch { |
79 public: | 77 public: |
80 // The PP_Var exception will be placed in |exception|. The user of this class | 78 // The PP_Var exception will be placed in |exception|. The user of this class |
81 // is responsible for managing the lifetime of the exception. It is valid to | 79 // is responsible for managing the lifetime of the exception. It is valid to |
82 // pass NULL for |exception| in which case no exception will be set. | 80 // pass NULL for |exception| in which case no exception will be set. |
83 PepperTryCatchVar(PepperPluginInstanceImpl* instance, | 81 PepperTryCatchVar(PepperPluginInstanceImpl* instance, |
| 82 V8VarConverter* var_converter, |
84 PP_Var* exception); | 83 PP_Var* exception); |
85 virtual ~PepperTryCatchVar(); | 84 virtual ~PepperTryCatchVar(); |
86 | 85 |
87 // PepperTryCatch | 86 // PepperTryCatch |
88 virtual void SetException(const char* message) override; | 87 virtual void SetException(const char* message) override; |
89 virtual bool HasException() override; | 88 virtual bool HasException() override; |
90 virtual v8::Handle<v8::Context> GetContext() override; | 89 virtual v8::Handle<v8::Context> GetContext() override; |
91 | 90 |
92 private: | 91 private: |
93 // Code which uses PepperTryCatchVar doesn't typically have a HandleScope, | 92 // Code which uses PepperTryCatchVar doesn't typically have a HandleScope, |
94 // make one for them. Note that this class is always allocated on the stack. | 93 // make one for them. Note that this class is always allocated on the stack. |
95 v8::HandleScope handle_scope_; | 94 v8::HandleScope handle_scope_; |
96 | 95 |
97 v8::Handle<v8::Context> context_; | 96 v8::Handle<v8::Context> context_; |
98 | 97 |
99 v8::TryCatch try_catch_; | 98 v8::TryCatch try_catch_; |
100 | 99 |
101 PP_Var* exception_; | 100 PP_Var* exception_; |
102 bool exception_is_set_; | 101 bool exception_is_set_; |
103 | 102 |
104 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar); | 103 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar); |
105 }; | 104 }; |
106 | 105 |
107 } // namespace content | 106 } // namespace content |
108 | 107 |
109 #endif // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_ | 108 #endif // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_ |
OLD | NEW |