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 "ppapi/c/pp_var.h"
11 #include "ppapi/shared_impl/scoped_pp_var.h"
12 #include "v8/include/v8.h"
16 class PepperPluginInstanceImpl
;
18 // Base class for scripting TryCatch helpers.
19 class CONTENT_EXPORT PepperTryCatch
{
21 PepperTryCatch(PepperPluginInstanceImpl
* instance
,
22 bool convert_objects
);
23 virtual ~PepperTryCatch();
25 virtual void SetException(const char* message
) = 0;
26 // Gets the plugin context. Virtual so it can be overriden for testing.
27 virtual v8::Handle
<v8::Context
> GetContext();
29 // Convenience functions for doing conversions to/from V8 values and sets an
30 // exception if there is an error in the conversion.
31 v8::Handle
<v8::Value
> ToV8(PP_Var var
);
32 ppapi::ScopedPPVar
FromV8(v8::Handle
<v8::Value
> v8_value
);
35 PepperPluginInstanceImpl
* instance_
;
37 // Whether To/FromV8 should convert object vars. If set to false, an exception
38 // should be set if they are encountered during conversion.
39 bool convert_objects_
;
42 // Catches var exceptions and emits a v8 exception.
43 class PepperTryCatchV8
: public PepperTryCatch
{
45 PepperTryCatchV8(PepperPluginInstanceImpl
* instance
,
47 v8::Isolate
* isolate
);
48 virtual ~PepperTryCatchV8();
51 bool ThrowException();
52 void ThrowException(const char* message
);
53 PP_Var
* exception() { return &exception_
; }
56 virtual void SetException(const char* message
) OVERRIDE
;
61 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8
);
64 // Catches v8 exceptions and emits a var exception.
65 class PepperTryCatchVar
: public PepperTryCatch
{
67 // 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
69 // pass NULL for |exception| in which case no exception will be set.
70 PepperTryCatchVar(PepperPluginInstanceImpl
* instance
,
73 virtual ~PepperTryCatchVar();
78 virtual void SetException(const char* message
) OVERRIDE
;
81 // 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 v8::HandleScope handle_scope_
;
85 v8::TryCatch try_catch_
;
88 bool exception_is_set_
;
90 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar
);
93 } // namespace content
95 #endif // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_