Home | History | Annotate | Download | only in pepper
      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 "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"
     14 
     15 namespace content {
     16 
     17 class PepperPluginInstanceImpl;
     18 
     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();
     27 
     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;
     32 
     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);
     37 
     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.
     43   //
     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_;
     47 
     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_;
     52 };
     53 
     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();
     61 
     62   bool ThrowException();
     63   void ThrowException(const char* message);
     64   PP_Var* exception() { return &exception_; }
     65 
     66   // PepperTryCatch
     67   virtual void SetException(const char* message) OVERRIDE;
     68   virtual bool HasException() OVERRIDE;
     69   virtual v8::Handle<v8::Context> GetContext() OVERRIDE;
     70 
     71  private:
     72   PP_Var exception_;
     73 
     74   DISALLOW_COPY_AND_ASSIGN(PepperTryCatchV8);
     75 };
     76 
     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();
     86 
     87   // PepperTryCatch
     88   virtual void SetException(const char* message) OVERRIDE;
     89   virtual bool HasException() OVERRIDE;
     90   virtual v8::Handle<v8::Context> GetContext() OVERRIDE;
     91 
     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_;
     96 
     97   v8::Handle<v8::Context> context_;
     98 
     99   v8::TryCatch try_catch_;
    100 
    101   PP_Var* exception_;
    102   bool exception_is_set_;
    103 
    104   DISALLOW_COPY_AND_ASSIGN(PepperTryCatchVar);
    105 };
    106 
    107 }  // namespace content
    108 
    109 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_TRY_CATCH_H_
    110