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
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_
8 #include "base/basictypes.h"
9 #include "content/common/content_export.h"
10 #include "content/renderer/pepper/v8_var_converter.h"
11 #include "ppapi/c/pp_var.h"
12 #include "ppapi/shared_impl/scoped_pp_var.h"
13 #include "v8/include/v8.h"
17 class PepperPluginInstanceImpl
;
19 // Base class for scripting TryCatch helpers.
20 class CONTENT_EXPORT PepperTryCatch
{
22 // PepperTryCatch objects should only be used as stack variables. This object
23 // takes a reference on the given PepperPluginInstanceImpl.
24 PepperTryCatch(PepperPluginInstanceImpl
* instance
,
25 V8VarConverter::AllowObjectVars convert_objects
);
26 virtual ~PepperTryCatch();
28 virtual void SetException(const char* message
) = 0;
29 virtual bool HasException() = 0;
30 // Gets the context to execute scripts in.
31 virtual v8::Handle
<v8::Context
> GetContext() = 0;
33 // Convenience functions for doing conversions to/from V8 values and sets an
34 // exception if there is an error in the conversion.
35 v8::Handle
<v8::Value
> ToV8(PP_Var var
);
36 ppapi::ScopedPPVar
FromV8(v8::Handle
<v8::Value
> v8_value
);
39 // 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 // possible to enter JavaScript synchronously which can cause the plugin to
44 // Note that PepperTryCatch objects should only ever be on the stack, so this
45 // shouldn't keep the instance around for too long.
46 scoped_refptr
<PepperPluginInstanceImpl
> instance_
;
48 // Whether To/FromV8 should convert object vars. If set to
49 // kDisallowObjectVars, an exception should be set if they are encountered
51 V8VarConverter::AllowObjectVars convert_objects_
;
54 // Catches var exceptions and emits a v8 exception.
55 class PepperTryCatchV8
: public PepperTryCatch
{
57 PepperTryCatchV8(PepperPluginInstanceImpl
* instance
,
58 V8VarConverter::AllowObjectVars convert_objects
,
59 v8::Isolate
* isolate
);
60 virtual ~PepperTryCatchV8();
62 bool ThrowException();
63 void ThrowException(const char* message
);
64 PP_Var
* exception() { return &exception_
; }
67 virtual void SetException(const char* message
) override
;
68 virtual bool HasException() override
;
69 virtual v8::Handle
<v8::Context
> GetContext() override
;
74 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8
);
77 // Catches v8 exceptions and emits a var exception.
78 class PepperTryCatchVar
: public PepperTryCatch
{
80 // 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
82 // pass NULL for |exception| in which case no exception will be set.
83 PepperTryCatchVar(PepperPluginInstanceImpl
* instance
,
85 virtual ~PepperTryCatchVar();
88 virtual void SetException(const char* message
) override
;
89 virtual bool HasException() override
;
90 virtual v8::Handle
<v8::Context
> GetContext() override
;
93 // 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.
95 v8::HandleScope handle_scope_
;
97 v8::Handle
<v8::Context
> context_
;
99 v8::TryCatch try_catch_
;
102 bool exception_is_set_
;
104 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar
);
107 } // namespace content
109 #endif // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_