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