Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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 CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_
      6 #define CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "chrome/renderer/extensions/module_system.h"
     13 #include "chrome/renderer/extensions/pepper_request_proxy.h"
     14 #include "chrome/renderer/extensions/request_sender.h"
     15 #include "chrome/renderer/extensions/safe_builtins.h"
     16 #include "chrome/renderer/extensions/scoped_persistent.h"
     17 #include "extensions/common/features/feature.h"
     18 #include "v8/include/v8.h"
     19 
     20 namespace blink {
     21 class WebFrame;
     22 }
     23 
     24 namespace content {
     25 class RenderView;
     26 }
     27 
     28 namespace extensions {
     29 class Extension;
     30 
     31 // Chrome's wrapper for a v8 context.
     32 class ChromeV8Context : public RequestSender::Source {
     33  public:
     34   ChromeV8Context(v8::Handle<v8::Context> context,
     35                   blink::WebFrame* frame,
     36                   const Extension* extension,
     37                   Feature::Context context_type);
     38   virtual ~ChromeV8Context();
     39 
     40   // Clears the WebFrame for this contexts and invalidates the associated
     41   // ModuleSystem.
     42   void Invalidate();
     43 
     44   // Returns true if this context is still valid, false if it isn't.
     45   // A context becomes invalid via Invalidate().
     46   bool is_valid() const {
     47     return !v8_context_.IsEmpty();
     48   }
     49 
     50   v8::Handle<v8::Context> v8_context() const {
     51     return v8_context_.NewHandle(v8::Isolate::GetCurrent());
     52   }
     53 
     54   const Extension* extension() const {
     55     return extension_.get();
     56   }
     57 
     58   blink::WebFrame* web_frame() const {
     59     return web_frame_;
     60   }
     61 
     62   Feature::Context context_type() const {
     63     return context_type_;
     64   }
     65 
     66   void set_module_system(scoped_ptr<ModuleSystem> module_system) {
     67     module_system_ = module_system.Pass();
     68   }
     69 
     70   ModuleSystem* module_system() { return module_system_.get(); }
     71 
     72   SafeBuiltins* safe_builtins() {
     73     return &safe_builtins_;
     74   }
     75   const SafeBuiltins* safe_builtins() const {
     76     return &safe_builtins_;
     77   }
     78 
     79   PepperRequestProxy* pepper_request_proxy() {
     80     return &pepper_request_proxy_;
     81   }
     82 
     83   // Returns the ID of the extension associated with this context, or empty
     84   // string if there is no such extension.
     85   std::string GetExtensionID() const;
     86 
     87   // Returns the RenderView associated with this context. Can return NULL if the
     88   // context is in the process of being destroyed.
     89   content::RenderView* GetRenderView() const;
     90 
     91   // Get the URL of this context's web frame.
     92   GURL GetURL() const;
     93 
     94   // Runs |function| with appropriate scopes. Doesn't catch exceptions, callers
     95   // must do that if they want.
     96   //
     97   // USE THIS METHOD RATHER THAN v8::Function::Call WHEREVER POSSIBLE.
     98   v8::Local<v8::Value> CallFunction(v8::Handle<v8::Function> function,
     99                                     int argc,
    100                                     v8::Handle<v8::Value> argv[]) const;
    101 
    102   // Fires the onunload event on the unload_event module.
    103   void DispatchOnUnloadEvent();
    104 
    105   // Returns the availability of the API |api_name|.
    106   Feature::Availability GetAvailability(const std::string& api_name);
    107 
    108   // Returns whether the API |api_name| or any part of the API could be
    109   // available in this context without taking into account the context's
    110   // extension.
    111   bool IsAnyFeatureAvailableToContext(const std::string& api_name);
    112 
    113   // Returns a string description of the type of context this is.
    114   std::string GetContextTypeDescription();
    115 
    116   // RequestSender::Source implementation.
    117   virtual ChromeV8Context* GetContext() OVERRIDE;
    118   virtual void OnResponseReceived(const std::string& name,
    119                                   int request_id,
    120                                   bool success,
    121                                   const base::ListValue& response,
    122                                   const std::string& error) OVERRIDE;
    123 
    124   v8::Isolate* isolate() const {
    125     return isolate_;
    126   }
    127 
    128  private:
    129   // The v8 context the bindings are accessible to.
    130   ScopedPersistent<v8::Context> v8_context_;
    131 
    132   // The WebFrame associated with this context. This can be NULL because this
    133   // object can outlive is destroyed asynchronously.
    134   blink::WebFrame* web_frame_;
    135 
    136   // The extension associated with this context, or NULL if there is none. This
    137   // might be a hosted app in the case that this context is hosting a web URL.
    138   scoped_refptr<const Extension> extension_;
    139 
    140   // The type of context.
    141   Feature::Context context_type_;
    142 
    143   // Owns and structures the JS that is injected to set up extension bindings.
    144   scoped_ptr<ModuleSystem> module_system_;
    145 
    146   // Contains safe copies of builtin objects like Function.prototype.
    147   SafeBuiltins safe_builtins_;
    148 
    149   // The proxy for this context for making API calls from Pepper via Javascript.
    150   PepperRequestProxy pepper_request_proxy_;
    151 
    152   v8::Isolate* isolate_;
    153 
    154   DISALLOW_COPY_AND_ASSIGN(ChromeV8Context);
    155 };
    156 
    157 }  // namespace extensions
    158 
    159 #endif  // CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_
    160