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 "content/common/content_export.h" | 9 #include "content/common/content_export.h" |
| 10 #include "content/renderer/pepper/v8_var_converter.h" |
10 #include "ppapi/c/pp_var.h" | 11 #include "ppapi/c/pp_var.h" |
11 #include "ppapi/shared_impl/scoped_pp_var.h" | 12 #include "ppapi/shared_impl/scoped_pp_var.h" |
12 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
13 | 14 |
14 namespace content { | 15 namespace content { |
15 | 16 |
16 class PepperPluginInstanceImpl; | 17 class PepperPluginInstanceImpl; |
17 | 18 |
18 // Base class for scripting TryCatch helpers. | 19 // Base class for scripting TryCatch helpers. |
19 class CONTENT_EXPORT PepperTryCatch { | 20 class CONTENT_EXPORT PepperTryCatch { |
20 public: | 21 public: |
21 PepperTryCatch(PepperPluginInstanceImpl* instance, | 22 PepperTryCatch(PepperPluginInstanceImpl* instance, |
22 bool convert_objects); | 23 V8VarConverter::AllowObjectVars convert_objects); |
23 virtual ~PepperTryCatch(); | 24 virtual ~PepperTryCatch(); |
24 | 25 |
25 virtual void SetException(const char* message) = 0; | 26 virtual void SetException(const char* message) = 0; |
26 // Gets the plugin context. Virtual so it can be overriden for testing. | 27 // Gets the plugin context. Virtual so it can be overriden for testing. |
27 virtual v8::Handle<v8::Context> GetContext(); | 28 virtual v8::Handle<v8::Context> GetContext(); |
28 | 29 |
29 // Convenience functions for doing conversions to/from V8 values and sets an | 30 // Convenience functions for doing conversions to/from V8 values and sets an |
30 // exception if there is an error in the conversion. | 31 // exception if there is an error in the conversion. |
31 v8::Handle<v8::Value> ToV8(PP_Var var); | 32 v8::Handle<v8::Value> ToV8(PP_Var var); |
32 ppapi::ScopedPPVar FromV8(v8::Handle<v8::Value> v8_value); | 33 ppapi::ScopedPPVar FromV8(v8::Handle<v8::Value> v8_value); |
33 | 34 |
34 protected: | 35 protected: |
35 PepperPluginInstanceImpl* instance_; | 36 PepperPluginInstanceImpl* instance_; |
36 | 37 |
37 // Whether To/FromV8 should convert object vars. If set to false, an exception | 38 // Whether To/FromV8 should convert object vars. If set to |
38 // should be set if they are encountered during conversion. | 39 // kDisallowObjectVars, an exception should be set if they are encountered |
39 bool convert_objects_; | 40 // during conversion. |
| 41 V8VarConverter::AllowObjectVars convert_objects_; |
40 }; | 42 }; |
41 | 43 |
42 // Catches var exceptions and emits a v8 exception. | 44 // Catches var exceptions and emits a v8 exception. |
43 class PepperTryCatchV8 : public PepperTryCatch { | 45 class PepperTryCatchV8 : public PepperTryCatch { |
44 public: | 46 public: |
45 PepperTryCatchV8(PepperPluginInstanceImpl* instance, | 47 PepperTryCatchV8(PepperPluginInstanceImpl* instance, |
46 bool convert_objects, | 48 V8VarConverter::AllowObjectVars convert_objects, |
47 v8::Isolate* isolate); | 49 v8::Isolate* isolate); |
48 virtual ~PepperTryCatchV8(); | 50 virtual ~PepperTryCatchV8(); |
49 | 51 |
50 bool HasException(); | 52 bool HasException(); |
51 bool ThrowException(); | 53 bool ThrowException(); |
52 void ThrowException(const char* message); | 54 void ThrowException(const char* message); |
53 PP_Var* exception() { return &exception_; } | 55 PP_Var* exception() { return &exception_; } |
54 | 56 |
55 // PepperTryCatch | 57 // PepperTryCatch |
56 virtual void SetException(const char* message) OVERRIDE; | 58 virtual void SetException(const char* message) OVERRIDE; |
57 | 59 |
58 private: | 60 private: |
59 PP_Var exception_; | 61 PP_Var exception_; |
60 | 62 |
61 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8); | 63 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8); |
62 }; | 64 }; |
63 | 65 |
64 // Catches v8 exceptions and emits a var exception. | 66 // Catches v8 exceptions and emits a var exception. |
65 class PepperTryCatchVar : public PepperTryCatch { | 67 class PepperTryCatchVar : public PepperTryCatch { |
66 public: | 68 public: |
67 // The PP_Var exception will be placed in |exception|. The user of this class | 69 // The PP_Var exception will be placed in |exception|. The user of this class |
68 // is responsible for managing the lifetime of the exception. It is valid to | 70 // is responsible for managing the lifetime of the exception. It is valid to |
69 // pass NULL for |exception| in which case no exception will be set. | 71 // pass NULL for |exception| in which case no exception will be set. |
70 PepperTryCatchVar(PepperPluginInstanceImpl* instance, | 72 PepperTryCatchVar(PepperPluginInstanceImpl* instance, |
71 bool convert_objects, | |
72 PP_Var* exception); | 73 PP_Var* exception); |
73 virtual ~PepperTryCatchVar(); | 74 virtual ~PepperTryCatchVar(); |
74 | 75 |
75 bool HasException(); | 76 bool HasException(); |
76 | 77 |
77 // PepperTryCatch | 78 // PepperTryCatch |
78 virtual void SetException(const char* message) OVERRIDE; | 79 virtual void SetException(const char* message) OVERRIDE; |
79 | 80 |
80 private: | 81 private: |
81 // Code which uses PepperTryCatchVar doesn't typically have a HandleScope, | 82 // Code which uses PepperTryCatchVar doesn't typically have a HandleScope, |
82 // make one for them. Note that this class is always allocated on the stack. | 83 // make one for them. Note that this class is always allocated on the stack. |
83 v8::HandleScope handle_scope_; | 84 v8::HandleScope handle_scope_; |
84 | 85 |
85 v8::TryCatch try_catch_; | 86 v8::TryCatch try_catch_; |
86 | 87 |
87 PP_Var* exception_; | 88 PP_Var* exception_; |
88 bool exception_is_set_; | 89 bool exception_is_set_; |
89 | 90 |
90 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar); | 91 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar); |
91 }; | 92 }; |
92 | 93 |
93 } // namespace content | 94 } // namespace content |
94 | 95 |
95 #endif // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_ | 96 #endif // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_ |
OLD | NEW |