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 #include "chrome/renderer/extensions/chrome_v8_context.h" | 5 #include "chrome/renderer/extensions/chrome_v8_context.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/common/extensions/api/extension_api.h" | 11 #include "chrome/common/extensions/api/extension_api.h" |
12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
13 #include "chrome/common/extensions/extension_set.h" | 13 #include "chrome/common/extensions/extension_set.h" |
14 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 14 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
15 #include "chrome/renderer/extensions/module_system.h" | 15 #include "chrome/renderer/extensions/module_system.h" |
16 #include "chrome/renderer/extensions/user_script_slave.h" | 16 #include "chrome/renderer/extensions/user_script_slave.h" |
17 #include "content/public/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
20 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" |
21 | 21 |
22 namespace extensions { | 22 namespace extensions { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const char kChromeHidden[] = "chromeHidden"; | 26 const char kChromeHidden[] = "chromeHidden"; |
27 const char kUnavailableMessage[] = "You do not have permission to access this " | |
28 "API. Ensure that the required permission " | |
29 "or manifest property is included in your " | |
30 "manifest.json."; | |
31 | |
32 const char kValidateCallbacks[] = "validateCallbacks"; | 27 const char kValidateCallbacks[] = "validateCallbacks"; |
33 const char kValidateAPI[] = "validateAPI"; | 28 const char kValidateAPI[] = "validateAPI"; |
34 | 29 |
35 } // namespace | 30 } // namespace |
36 | 31 |
37 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 32 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, |
38 WebKit::WebFrame* web_frame, | 33 WebKit::WebFrame* web_frame, |
39 const Extension* extension, | 34 const Extension* extension, |
40 Feature::Context context_type) | 35 Feature::Context context_type) |
41 : v8_context_(v8_context), | 36 : v8_context_(v8_context), |
42 web_frame_(web_frame), | 37 web_frame_(web_frame), |
43 extension_(extension), | 38 extension_(extension), |
44 context_type_(context_type), | 39 context_type_(context_type) { |
45 available_extension_apis_initialized_(false) { | |
46 VLOG(1) << "Created context:\n" | 40 VLOG(1) << "Created context:\n" |
47 << " extension id: " << GetExtensionID() << "\n" | 41 << " extension id: " << GetExtensionID() << "\n" |
48 << " frame: " << web_frame_ << "\n" | 42 << " frame: " << web_frame_ << "\n" |
49 << " context type: " << GetContextTypeDescription(); | 43 << " context type: " << GetContextTypeDescription(); |
50 } | 44 } |
51 | 45 |
52 ChromeV8Context::~ChromeV8Context() { | 46 ChromeV8Context::~ChromeV8Context() { |
53 VLOG(1) << "Destroyed context for extension\n" | 47 VLOG(1) << "Destroyed context for extension\n" |
54 << " extension id: " << GetExtensionID(); | 48 << " extension id: " << GetExtensionID(); |
55 Invalidate(); | 49 Invalidate(); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 web_frame_->callFunctionEvenIfScriptDisabled(function, | 141 web_frame_->callFunctionEvenIfScriptDisabled(function, |
148 v8::Object::New(), | 142 v8::Object::New(), |
149 argc, | 143 argc, |
150 argv); | 144 argv); |
151 if (result) | 145 if (result) |
152 *result = result_temp; | 146 *result = result_temp; |
153 | 147 |
154 return true; | 148 return true; |
155 } | 149 } |
156 | 150 |
157 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { | |
158 if (!available_extension_apis_initialized_) { | |
159 available_extension_apis_ = | |
160 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( | |
161 context_type_, | |
162 extension_, | |
163 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)); | |
164 available_extension_apis_initialized_ = true; | |
165 } | |
166 return available_extension_apis_; | |
167 } | |
168 | |
169 Feature::Availability ChromeV8Context::GetAvailability( | 151 Feature::Availability ChromeV8Context::GetAvailability( |
170 const std::string& api_name) { | 152 const std::string& api_name) { |
171 const std::set<std::string>& available_apis = GetAvailableExtensionAPIs(); | 153 return ExtensionAPI::GetSharedInstance()->IsAvailable( |
172 | 154 api_name, |
173 // TODO(cduvall/kalman): Switch to ExtensionAPI::IsAvailable() once Features | 155 extension_, |
174 // are complete. | 156 context_type_, |
175 if (available_apis.find(api_name) != available_apis.end()) | 157 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)); |
176 return Feature::CreateAvailability(Feature::IS_AVAILABLE, ""); | |
177 | |
178 return Feature::CreateAvailability(Feature::INVALID_CONTEXT, | |
179 kUnavailableMessage); | |
180 } | 158 } |
181 | 159 |
182 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, | 160 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, |
183 int manifest_version) { | 161 int manifest_version) { |
184 v8::HandleScope handle_scope; | 162 v8::HandleScope handle_scope; |
185 v8::Handle<v8::Value> argv[] = { | 163 v8::Handle<v8::Value> argv[] = { |
186 v8::String::New(GetExtensionID().c_str()), | 164 v8::String::New(GetExtensionID().c_str()), |
187 v8::String::New(GetContextTypeDescription().c_str()), | 165 v8::String::New(GetContextTypeDescription().c_str()), |
188 v8::Boolean::New(is_incognito_process), | 166 v8::Boolean::New(is_incognito_process), |
189 v8::Integer::New(manifest_version), | 167 v8::Integer::New(manifest_version), |
(...skipping 12 matching lines...) Expand all Loading... |
202 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; | 180 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; |
203 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; | 181 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; |
204 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; | 182 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; |
205 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; | 183 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; |
206 } | 184 } |
207 NOTREACHED(); | 185 NOTREACHED(); |
208 return ""; | 186 return ""; |
209 } | 187 } |
210 | 188 |
211 } // namespace extensions | 189 } // namespace extensions |
OLD | NEW |