- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / extensions / chrome_v8_context.h
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/request_sender.h"
14 #include "chrome/renderer/extensions/safe_builtins.h"
15 #include "chrome/renderer/extensions/scoped_persistent.h"
16 #include "extensions/common/features/feature.h"
17 #include "v8/include/v8.h"
18
19 namespace WebKit {
20 class WebFrame;
21 }
22
23 namespace content {
24 class RenderView;
25 }
26
27 namespace extensions {
28 class Extension;
29
30 // Chrome's wrapper for a v8 context.
31 class 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_.IsEmpty();
47   }
48
49   v8::Handle<v8::Context> v8_context() const {
50     return v8_context_.NewHandle(v8::Isolate::GetCurrent());
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   v8::Isolate* isolate() const {
120     return isolate_;
121   }
122
123  private:
124   // The v8 context the bindings are accessible to.
125   ScopedPersistent<v8::Context> v8_context_;
126
127   // The WebFrame associated with this context. This can be NULL because this
128   // object can outlive is destroyed asynchronously.
129   WebKit::WebFrame* web_frame_;
130
131   // The extension associated with this context, or NULL if there is none. This
132   // might be a hosted app in the case that this context is hosting a web URL.
133   scoped_refptr<const Extension> extension_;
134
135   // The type of context.
136   Feature::Context context_type_;
137
138   // Owns and structures the JS that is injected to set up extension bindings.
139   scoped_ptr<ModuleSystem> module_system_;
140
141   // Contains safe copies of builtin objects like Function.prototype.
142   SafeBuiltins safe_builtins_;
143
144   v8::Isolate* isolate_;
145
146   DISALLOW_COPY_AND_ASSIGN(ChromeV8Context);
147 };
148
149 }  // namespace extensions
150
151 #endif  // CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_