Replace FINAL and OVERRIDE with their C++11 counterparts in content/renderer
[chromium-blink-merge.git] / content / renderer / pepper / pepper_try_catch.h
blob6ac42d16bb1484a6307188b0920b29299c021f8a
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"
15 namespace content {
17 class PepperPluginInstanceImpl;
19 // Base class for scripting TryCatch helpers.
20 class CONTENT_EXPORT PepperTryCatch {
21 public:
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);
38 protected:
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
42 // be deleted.
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
50 // during conversion.
51 V8VarConverter::AllowObjectVars convert_objects_;
54 // Catches var exceptions and emits a v8 exception.
55 class PepperTryCatchV8 : public PepperTryCatch {
56 public:
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_; }
66 // PepperTryCatch
67 virtual void SetException(const char* message) override;
68 virtual bool HasException() override;
69 virtual v8::Handle<v8::Context> GetContext() override;
71 private:
72 PP_Var exception_;
74 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8);
77 // Catches v8 exceptions and emits a var exception.
78 class PepperTryCatchVar : public PepperTryCatch {
79 public:
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,
84 PP_Var* exception);
85 virtual ~PepperTryCatchVar();
87 // PepperTryCatch
88 virtual void SetException(const char* message) override;
89 virtual bool HasException() override;
90 virtual v8::Handle<v8::Context> GetContext() override;
92 private:
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_;
101 PP_Var* exception_;
102 bool exception_is_set_;
104 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar);
107 } // namespace content
109 #endif // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_