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