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