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; |
| 27 virtual bool HasException() = 0; |
26 // Gets the plugin context. Virtual so it can be overriden for testing. | 28 // Gets the plugin context. Virtual so it can be overriden for testing. |
27 virtual v8::Handle<v8::Context> GetContext(); | 29 virtual v8::Handle<v8::Context> GetContext(); |
28 | 30 |
29 // Convenience functions for doing conversions to/from V8 values and sets an | 31 // Convenience functions for doing conversions to/from V8 values and sets an |
30 // exception if there is an error in the conversion. | 32 // exception if there is an error in the conversion. |
31 v8::Handle<v8::Value> ToV8(PP_Var var); | 33 v8::Handle<v8::Value> ToV8(PP_Var var); |
32 ppapi::ScopedPPVar FromV8(v8::Handle<v8::Value> v8_value); | 34 ppapi::ScopedPPVar FromV8(v8::Handle<v8::Value> v8_value); |
33 | 35 |
34 protected: | 36 protected: |
35 PepperPluginInstanceImpl* instance_; | 37 PepperPluginInstanceImpl* instance_; |
36 | 38 |
37 // Whether To/FromV8 should convert object vars. If set to false, an exception | 39 // Whether To/FromV8 should convert object vars. If set to |
38 // should be set if they are encountered during conversion. | 40 // kDisallowObjectVars, an exception should be set if they are encountered |
39 bool convert_objects_; | 41 // during conversion. |
| 42 V8VarConverter::AllowObjectVars convert_objects_; |
40 }; | 43 }; |
41 | 44 |
42 // Catches var exceptions and emits a v8 exception. | 45 // Catches var exceptions and emits a v8 exception. |
43 class PepperTryCatchV8 : public PepperTryCatch { | 46 class PepperTryCatchV8 : public PepperTryCatch { |
44 public: | 47 public: |
45 PepperTryCatchV8(PepperPluginInstanceImpl* instance, | 48 PepperTryCatchV8(PepperPluginInstanceImpl* instance, |
46 bool convert_objects, | 49 V8VarConverter::AllowObjectVars convert_objects, |
47 v8::Isolate* isolate); | 50 v8::Isolate* isolate); |
48 virtual ~PepperTryCatchV8(); | 51 virtual ~PepperTryCatchV8(); |
49 | 52 |
50 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; |
| 59 virtual bool HasException() OVERRIDE; |
57 | 60 |
58 private: | 61 private: |
59 PP_Var exception_; | 62 PP_Var exception_; |
60 | 63 |
61 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8); | 64 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8); |
62 }; | 65 }; |
63 | 66 |
64 // Catches v8 exceptions and emits a var exception. | 67 // Catches v8 exceptions and emits a var exception. |
65 class PepperTryCatchVar : public PepperTryCatch { | 68 class PepperTryCatchVar : public PepperTryCatch { |
66 public: | 69 public: |
67 // The PP_Var exception will be placed in |exception|. The user of this class | 70 // 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 | 71 // 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. | 72 // pass NULL for |exception| in which case no exception will be set. |
70 PepperTryCatchVar(PepperPluginInstanceImpl* instance, | 73 PepperTryCatchVar(PepperPluginInstanceImpl* instance, |
71 bool convert_objects, | |
72 PP_Var* exception); | 74 PP_Var* exception); |
73 virtual ~PepperTryCatchVar(); | 75 virtual ~PepperTryCatchVar(); |
74 | 76 |
75 bool HasException(); | |
76 | |
77 // PepperTryCatch | 77 // PepperTryCatch |
78 virtual void SetException(const char* message) OVERRIDE; | 78 virtual void SetException(const char* message) OVERRIDE; |
| 79 virtual bool HasException() 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 |