Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_try_catch.h
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.
4
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_
7
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"
14
15 namespace content {
16
17 class PepperPluginInstanceImpl;
18 class V8VarConverter;
19
20 // Base class for scripting TryCatch helpers.
21 class CONTENT_EXPORT PepperTryCatch {
22  public:
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();
28
29   virtual void SetException(const char* message) = 0;
30   virtual bool HasException() = 0;
31   // Gets the context to execute scripts in.
32   virtual v8::Handle<v8::Context> GetContext() = 0;
33
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::Handle<v8::Value> ToV8(PP_Var var);
37   ppapi::ScopedPPVar FromV8(v8::Handle<v8::Value> v8_value);
38
39  protected:
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
43   // be deleted.
44   //
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_;
48
49   V8VarConverter* var_converter_;
50 };
51
52 // Catches var exceptions and emits a v8 exception.
53 class PepperTryCatchV8 : public PepperTryCatch {
54  public:
55   PepperTryCatchV8(PepperPluginInstanceImpl* instance,
56                    V8VarConverter* var_converter,
57                    v8::Isolate* isolate);
58   ~PepperTryCatchV8() override;
59
60   bool ThrowException();
61   void ThrowException(const char* message);
62   PP_Var* exception() { return &exception_; }
63
64   // PepperTryCatch
65   void SetException(const char* message) override;
66   bool HasException() override;
67   v8::Handle<v8::Context> GetContext() override;
68
69  private:
70   PP_Var exception_;
71
72   DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8);
73 };
74
75 // Catches v8 exceptions and emits a var exception.
76 class PepperTryCatchVar : public PepperTryCatch {
77  public:
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,
83                     PP_Var* exception);
84   ~PepperTryCatchVar() override;
85
86   // PepperTryCatch
87   void SetException(const char* message) override;
88   bool HasException() override;
89   v8::Handle<v8::Context> GetContext() override;
90
91  private:
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_;
95
96   v8::Handle<v8::Context> context_;
97
98   v8::TryCatch try_catch_;
99
100   PP_Var* exception_;
101   bool exception_is_set_;
102
103   DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar);
104 };
105
106 }  // namespace content
107
108 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_