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/string_split.h" | 9 #include "base/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/user_script_slave.h" | 16 #include "chrome/renderer/extensions/user_script_slave.h" |
16 #include "content/public/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
19 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 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."; |
26 | 31 |
27 #ifndef NDEBUG | 32 #ifndef NDEBUG |
28 const char kValidateCallbacks[] = "validateCallbacks"; | 33 const char kValidateCallbacks[] = "validateCallbacks"; |
29 const char kValidateAPI[] = "validateAPI"; | 34 const char kValidateAPI[] = "validateAPI"; |
30 #endif | 35 #endif |
31 | 36 |
32 } // namespace | 37 } // namespace |
33 | 38 |
34 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 39 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, |
35 WebKit::WebFrame* web_frame, | 40 WebKit::WebFrame* web_frame, |
36 const Extension* extension, | 41 const Extension* extension, |
37 Feature::Context context_type) | 42 Feature::Context context_type) |
38 : v8_context_(v8::Persistent<v8::Context>::New(v8_context->GetIsolate(), | 43 : v8_context_(v8::Persistent<v8::Context>::New(v8_context->GetIsolate(), |
39 v8_context)), | 44 v8_context)), |
40 web_frame_(web_frame), | 45 web_frame_(web_frame), |
41 extension_(extension), | 46 extension_(extension), |
42 context_type_(context_type) { | 47 context_type_(context_type), |
| 48 available_extension_apis_initialized_(false) { |
43 VLOG(1) << "Created context:\n" | 49 VLOG(1) << "Created context:\n" |
44 << " extension id: " << GetExtensionID() << "\n" | 50 << " extension id: " << GetExtensionID() << "\n" |
45 << " frame: " << web_frame_ << "\n" | 51 << " frame: " << web_frame_ << "\n" |
46 << " context type: " << GetContextTypeDescription(); | 52 << " context type: " << GetContextTypeDescription(); |
47 } | 53 } |
48 | 54 |
49 ChromeV8Context::~ChromeV8Context() { | 55 ChromeV8Context::~ChromeV8Context() { |
50 VLOG(1) << "Destroyed context for extension\n" | 56 VLOG(1) << "Destroyed context for extension\n" |
51 << " extension id: " << GetExtensionID(); | 57 << " extension id: " << GetExtensionID(); |
52 v8_context_.Dispose(v8_context_->GetIsolate()); | 58 v8_context_.Dispose(v8_context_->GetIsolate()); |
53 } | 59 } |
54 | 60 |
| 61 void ChromeV8Context::Invalidate() { |
| 62 module_system_->Invalidate(); |
| 63 } |
| 64 |
55 std::string ChromeV8Context::GetExtensionID() { | 65 std::string ChromeV8Context::GetExtensionID() { |
56 return extension_ ? extension_->id() : ""; | 66 return extension_ ? extension_->id() : ""; |
57 } | 67 } |
58 | 68 |
59 // static | 69 // static |
60 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( | 70 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( |
61 v8::Handle<v8::Context> context) { | 71 v8::Handle<v8::Context> context) { |
62 v8::Local<v8::Object> global = context->Global(); | 72 v8::Local<v8::Object> global = context->Global(); |
63 v8::Local<v8::Value> hidden = global->GetHiddenValue( | 73 v8::Local<v8::Value> hidden = global->GetHiddenValue( |
64 v8::String::New(kChromeHidden)); | 74 v8::String::New(kChromeHidden)); |
65 | 75 |
66 if (hidden.IsEmpty() || hidden->IsUndefined()) { | 76 if (hidden.IsEmpty() || hidden->IsUndefined()) { |
67 hidden = v8::Object::New(); | 77 hidden = v8::Object::New(); |
68 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); | 78 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); |
69 | 79 |
70 #ifndef NDEBUG | 80 #ifndef NDEBUG |
71 // Tell schema_generated_bindings.js to validate callbacks and events | 81 // Tell bindings.js to validate callbacks and events against their schema |
72 // against their schema definitions. | 82 // definitions. |
73 v8::Local<v8::Object>::Cast(hidden)->Set( | 83 v8::Local<v8::Object>::Cast(hidden)->Set( |
74 v8::String::New(kValidateCallbacks), v8::True()); | 84 v8::String::New(kValidateCallbacks), v8::True()); |
75 // Tell schema_generated_bindings.js to validate API for ambiguity. | 85 // Tell bindings.js to validate API for ambiguity. |
76 v8::Local<v8::Object>::Cast(hidden)->Set( | 86 v8::Local<v8::Object>::Cast(hidden)->Set( |
77 v8::String::New(kValidateAPI), v8::True()); | 87 v8::String::New(kValidateAPI), v8::True()); |
78 #endif | 88 #endif |
79 } | 89 } |
80 | 90 |
81 DCHECK(hidden->IsObject()); | 91 DCHECK(hidden->IsObject()); |
82 return v8::Local<v8::Object>::Cast(hidden); | 92 return v8::Local<v8::Object>::Cast(hidden); |
83 } | 93 } |
84 | 94 |
85 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { | 95 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 v8::Object::New(), | 146 v8::Object::New(), |
137 argc, | 147 argc, |
138 argv); | 148 argv); |
139 if (result) | 149 if (result) |
140 *result = result_temp; | 150 *result = result_temp; |
141 | 151 |
142 return true; | 152 return true; |
143 } | 153 } |
144 | 154 |
145 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { | 155 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { |
146 if (!available_extension_apis_.get()) { | 156 if (!available_extension_apis_initialized_) { |
147 available_extension_apis_ = | 157 available_extension_apis_ = |
148 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( | 158 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( |
149 context_type_, | 159 context_type_, |
150 extension_, | 160 extension_, |
151 UserScriptSlave::GetDataSourceURLForFrame( | 161 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)); |
152 web_frame_)).Pass(); | 162 available_extension_apis_initialized_ = true; |
153 } | 163 } |
154 return *(available_extension_apis_.get()); | 164 return available_extension_apis_; |
| 165 } |
| 166 |
| 167 Feature::Availability ChromeV8Context::GetAvailability( |
| 168 const std::string& api_name) { |
| 169 const std::set<std::string>& available_apis = GetAvailableExtensionAPIs(); |
| 170 |
| 171 // TODO(cduvall/kalman): Switch to ExtensionAPI::IsAvailable() once Features |
| 172 // are complete. |
| 173 if (available_apis.find(api_name) != available_apis.end()) |
| 174 return Feature::CreateAvailability(Feature::IS_AVAILABLE, ""); |
| 175 |
| 176 return Feature::CreateAvailability(Feature::INVALID_CONTEXT, |
| 177 kUnavailableMessage); |
155 } | 178 } |
156 | 179 |
157 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, | 180 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, |
158 int manifest_version) { | 181 int manifest_version) { |
159 v8::HandleScope handle_scope; | 182 v8::HandleScope handle_scope; |
160 v8::Handle<v8::Value> argv[] = { | 183 v8::Handle<v8::Value> argv[] = { |
161 v8::String::New(GetExtensionID().c_str()), | 184 v8::String::New(GetExtensionID().c_str()), |
162 v8::String::New(GetContextTypeDescription().c_str()), | 185 v8::String::New(GetContextTypeDescription().c_str()), |
163 v8::Boolean::New(is_incognito_process), | 186 v8::Boolean::New(is_incognito_process), |
164 v8::Integer::New(manifest_version), | 187 v8::Integer::New(manifest_version), |
(...skipping 12 matching lines...) Expand all Loading... |
177 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; | 200 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; |
178 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; | 201 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; |
179 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; | 202 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; |
180 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; | 203 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; |
181 } | 204 } |
182 NOTREACHED(); | 205 NOTREACHED(); |
183 return ""; | 206 return ""; |
184 } | 207 } |
185 | 208 |
186 } // namespace extensions | 209 } // namespace extensions |
OLD | NEW |