| 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" | |
| 16 #include "chrome/renderer/extensions/user_script_slave.h" | 15 #include "chrome/renderer/extensions/user_script_slave.h" |
| 17 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 20 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
| 21 | 20 |
| 22 namespace extensions { | 21 namespace extensions { |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 const char kChromeHidden[] = "chromeHidden"; | 25 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 | 26 |
| 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::Persistent<v8::Context>::New(v8_context->GetIsolate(), | 36 : v8_context_(v8::Persistent<v8::Context>::New(v8_context->GetIsolate(), |
| 42 v8_context)), | 37 v8_context)), |
| 43 web_frame_(web_frame), | 38 web_frame_(web_frame), |
| 44 extension_(extension), | 39 extension_(extension), |
| 45 context_type_(context_type), | 40 context_type_(context_type) { |
| 46 available_extension_apis_initialized_(false) { | |
| 47 VLOG(1) << "Created context:\n" | 41 VLOG(1) << "Created context:\n" |
| 48 << " extension id: " << GetExtensionID() << "\n" | 42 << " extension id: " << GetExtensionID() << "\n" |
| 49 << " frame: " << web_frame_ << "\n" | 43 << " frame: " << web_frame_ << "\n" |
| 50 << " context type: " << GetContextTypeDescription(); | 44 << " context type: " << GetContextTypeDescription(); |
| 51 } | 45 } |
| 52 | 46 |
| 53 ChromeV8Context::~ChromeV8Context() { | 47 ChromeV8Context::~ChromeV8Context() { |
| 54 VLOG(1) << "Destroyed context for extension\n" | 48 VLOG(1) << "Destroyed context for extension\n" |
| 55 << " extension id: " << GetExtensionID(); | 49 << " extension id: " << GetExtensionID(); |
| 56 v8_context_.Dispose(v8_context_->GetIsolate()); | 50 v8_context_.Dispose(v8_context_->GetIsolate()); |
| 57 } | 51 } |
| 58 | 52 |
| 59 void ChromeV8Context::Invalidate() { | |
| 60 if (module_system_) | |
| 61 module_system_->Invalidate(); | |
| 62 web_frame_ = NULL; | |
| 63 } | |
| 64 | |
| 65 std::string ChromeV8Context::GetExtensionID() { | 53 std::string ChromeV8Context::GetExtensionID() { |
| 66 return extension_ ? extension_->id() : ""; | 54 return extension_ ? extension_->id() : ""; |
| 67 } | 55 } |
| 68 | 56 |
| 69 // static | 57 // static |
| 70 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( | 58 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( |
| 71 v8::Handle<v8::Context> context) { | 59 v8::Handle<v8::Context> context) { |
| 72 v8::Local<v8::Object> global = context->Global(); | 60 v8::Local<v8::Object> global = context->Global(); |
| 73 v8::Local<v8::Value> hidden = global->GetHiddenValue( | 61 v8::Local<v8::Value> hidden = global->GetHiddenValue( |
| 74 v8::String::New(kChromeHidden)); | 62 v8::String::New(kChromeHidden)); |
| 75 | 63 |
| 76 if (hidden.IsEmpty() || hidden->IsUndefined()) { | 64 if (hidden.IsEmpty() || hidden->IsUndefined()) { |
| 77 hidden = v8::Object::New(); | 65 hidden = v8::Object::New(); |
| 78 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); | 66 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); |
| 79 | 67 |
| 80 if (DCHECK_IS_ON()) { | 68 if (DCHECK_IS_ON()) { |
| 81 // Tell bindings.js to validate callbacks and events against their schema | 69 // Tell schema_generated_bindings.js to validate callbacks and events |
| 82 // definitions. | 70 // against their schema definitions. |
| 83 v8::Local<v8::Object>::Cast(hidden)->Set( | 71 v8::Local<v8::Object>::Cast(hidden)->Set( |
| 84 v8::String::New(kValidateCallbacks), v8::True()); | 72 v8::String::New(kValidateCallbacks), v8::True()); |
| 85 // Tell bindings.js to validate API for ambiguity. | 73 // Tell schema_generated_bindings.js to validate API for ambiguity. |
| 86 v8::Local<v8::Object>::Cast(hidden)->Set( | 74 v8::Local<v8::Object>::Cast(hidden)->Set( |
| 87 v8::String::New(kValidateAPI), v8::True()); | 75 v8::String::New(kValidateAPI), v8::True()); |
| 88 } | 76 } |
| 89 } | 77 } |
| 90 | 78 |
| 91 DCHECK(hidden->IsObject()); | 79 DCHECK(hidden->IsObject()); |
| 92 return v8::Local<v8::Object>::Cast(hidden); | 80 return v8::Local<v8::Object>::Cast(hidden); |
| 93 } | 81 } |
| 94 | 82 |
| 95 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { | 83 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { |
| 96 v8::Local<v8::Object> global = v8_context_->Global(); | 84 v8::Local<v8::Object> global = v8_context_->Global(); |
| 97 return global->GetHiddenValue(v8::String::New(kChromeHidden)); | 85 return global->GetHiddenValue(v8::String::New(kChromeHidden)); |
| 98 } | 86 } |
| 99 | 87 |
| 100 content::RenderView* ChromeV8Context::GetRenderView() const { | 88 content::RenderView* ChromeV8Context::GetRenderView() const { |
| 101 if (web_frame_ && web_frame_->view()) | 89 if (web_frame_ && web_frame_->view()) |
| 102 return content::RenderView::FromWebView(web_frame_->view()); | 90 return content::RenderView::FromWebView(web_frame_->view()); |
| 103 else | 91 else |
| 104 return NULL; | 92 return NULL; |
| 105 } | 93 } |
| 106 | 94 |
| 107 bool ChromeV8Context::CallChromeHiddenMethod( | 95 bool ChromeV8Context::CallChromeHiddenMethod( |
| 108 const std::string& function_name, | 96 const std::string& function_name, |
| 109 int argc, | 97 int argc, |
| 110 v8::Handle<v8::Value>* argv, | 98 v8::Handle<v8::Value>* argv, |
| 111 v8::Handle<v8::Value>* result) const { | 99 v8::Handle<v8::Value>* result) const { |
| 112 v8::Context::Scope context_scope(v8_context_); | 100 v8::Context::Scope context_scope(v8_context_); |
| 113 | 101 |
| 114 // ChromeV8ContextSet calls Invalidate() and then schedules a task to delete | 102 // ChromeV8ContextSet calls clear_web_frame() and then schedules a task to |
| 115 // this object. This check prevents a race from attempting to execute script | 103 // delete this object. This check prevents a race from attempting to execute |
| 116 // on a NULL web_frame_. | 104 // script on a NULL web_frame_. |
| 117 if (!web_frame_) | 105 if (!web_frame_) |
| 118 return false; | 106 return false; |
| 119 | 107 |
| 120 // Look up the function name, which may be a sub-property like | 108 // Look up the function name, which may be a sub-property like |
| 121 // "Port.dispatchOnMessage" in the hidden global variable. | 109 // "Port.dispatchOnMessage" in the hidden global variable. |
| 122 v8::Local<v8::Value> value = v8::Local<v8::Value>::New(GetChromeHidden()); | 110 v8::Local<v8::Value> value = v8::Local<v8::Value>::New(GetChromeHidden()); |
| 123 if (value.IsEmpty()) | 111 if (value.IsEmpty()) |
| 124 return false; | 112 return false; |
| 125 | 113 |
| 126 std::vector<std::string> components; | 114 std::vector<std::string> components; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 146 v8::Object::New(), | 134 v8::Object::New(), |
| 147 argc, | 135 argc, |
| 148 argv); | 136 argv); |
| 149 if (result) | 137 if (result) |
| 150 *result = result_temp; | 138 *result = result_temp; |
| 151 | 139 |
| 152 return true; | 140 return true; |
| 153 } | 141 } |
| 154 | 142 |
| 155 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { | 143 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { |
| 156 if (!available_extension_apis_initialized_) { | 144 if (!available_extension_apis_.get()) { |
| 157 available_extension_apis_ = | 145 available_extension_apis_ = |
| 158 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( | 146 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( |
| 159 context_type_, | 147 context_type_, |
| 160 extension_, | 148 extension_, |
| 161 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)); | 149 UserScriptSlave::GetDataSourceURLForFrame( |
| 162 available_extension_apis_initialized_ = true; | 150 web_frame_)).Pass(); |
| 163 } | 151 } |
| 164 return available_extension_apis_; | 152 return *(available_extension_apis_.get()); |
| 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); | |
| 178 } | 153 } |
| 179 | 154 |
| 180 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, | 155 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, |
| 181 int manifest_version) { | 156 int manifest_version) { |
| 182 v8::HandleScope handle_scope; | 157 v8::HandleScope handle_scope; |
| 183 v8::Handle<v8::Value> argv[] = { | 158 v8::Handle<v8::Value> argv[] = { |
| 184 v8::String::New(GetExtensionID().c_str()), | 159 v8::String::New(GetExtensionID().c_str()), |
| 185 v8::String::New(GetContextTypeDescription().c_str()), | 160 v8::String::New(GetContextTypeDescription().c_str()), |
| 186 v8::Boolean::New(is_incognito_process), | 161 v8::Boolean::New(is_incognito_process), |
| 187 v8::Integer::New(manifest_version), | 162 v8::Integer::New(manifest_version), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 200 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; | 175 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; |
| 201 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; | 176 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; |
| 202 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; | 177 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; |
| 203 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; | 178 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; |
| 204 } | 179 } |
| 205 NOTREACHED(); | 180 NOTREACHED(); |
| 206 return ""; | 181 return ""; |
| 207 } | 182 } |
| 208 | 183 |
| 209 } // namespace extensions | 184 } // namespace extensions |
| OLD | NEW |