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