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 "base/memory/ref_counted.h"
10 #include "content/common/content_export.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
;
20 // Base class for scripting TryCatch helpers.
21 class CONTENT_EXPORT PepperTryCatch
{
23 // PepperTryCatch objects should only be used as stack variables. This object
24 // takes a reference on the given PepperPluginInstanceImpl.
25 PepperTryCatch(PepperPluginInstanceImpl
* instance
,
26 V8VarConverter
* var_converter
);
27 virtual ~PepperTryCatch();
29 virtual void SetException(const char* message
) = 0;
30 virtual bool HasException() = 0;
31 // Gets the context to execute scripts in.
32 virtual v8::Local
<v8::Context
> GetContext() = 0;
34 // Convenience functions for doing conversions to/from V8 values and sets an
35 // exception if there is an error in the conversion.
36 v8::Local
<v8::Value
> ToV8(PP_Var var
);
37 ppapi::ScopedPPVar
FromV8(v8::Local
<v8::Value
> v8_value
);
40 // Make sure that |instance_| is alive for the lifetime of PepperTryCatch.
41 // PepperTryCatch is used mostly in Pepper scripting code, where it can be
42 // possible to enter JavaScript synchronously which can cause the plugin to
45 // Note that PepperTryCatch objects should only ever be on the stack, so this
46 // shouldn't keep the instance around for too long.
47 scoped_refptr
<PepperPluginInstanceImpl
> instance_
;
49 V8VarConverter
* var_converter_
;
52 // Catches var exceptions and emits a v8 exception.
53 class PepperTryCatchV8
: public PepperTryCatch
{
55 PepperTryCatchV8(PepperPluginInstanceImpl
* instance
,
56 V8VarConverter
* var_converter
,
57 v8::Isolate
* isolate
);
58 ~PepperTryCatchV8() override
;
60 bool ThrowException();
61 void ThrowException(const char* message
);
62 PP_Var
* exception() { return &exception_
; }
65 void SetException(const char* message
) override
;
66 bool HasException() override
;
67 v8::Local
<v8::Context
> GetContext() override
;
72 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8
);
75 // Catches v8 exceptions and emits a var exception.
76 class PepperTryCatchVar
: public PepperTryCatch
{
78 // The PP_Var exception will be placed in |exception|. The user of this class
79 // is responsible for managing the lifetime of the exception. It is valid to
80 // pass NULL for |exception| in which case no exception will be set.
81 PepperTryCatchVar(PepperPluginInstanceImpl
* instance
,
82 V8VarConverter
* var_converter
,
84 ~PepperTryCatchVar() override
;
87 void SetException(const char* message
) override
;
88 bool HasException() override
;
89 v8::Local
<v8::Context
> GetContext() override
;
92 // Code which uses PepperTryCatchVar doesn't typically have a HandleScope,
93 // make one for them. Note that this class is always allocated on the stack.
94 v8::HandleScope handle_scope_
;
96 v8::Local
<v8::Context
> context_
;
98 v8::TryCatch try_catch_
;
101 bool exception_is_set_
;
103 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar
);
106 } // namespace content
108 #endif // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_