| 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/extension_set.h" | 12 #include "chrome/common/extensions/extension_set.h" |
| 12 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 13 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 14 #include "chrome/renderer/extensions/user_script_slave.h" |
| 13 #include "content/public/renderer/render_view.h" | 15 #include "content/public/renderer/render_view.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 16 #include "v8/include/v8.h" | 18 #include "v8/include/v8.h" |
| 17 | 19 |
| 18 using extensions::Feature; | 20 using extensions::Feature; |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const char kChromeHidden[] = "chromeHidden"; | 24 const char kChromeHidden[] = "chromeHidden"; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 35 case Feature::WEB_PAGE_CONTEXT: return "web page"; | 37 case Feature::WEB_PAGE_CONTEXT: return "web page"; |
| 36 } | 38 } |
| 37 NOTREACHED(); | 39 NOTREACHED(); |
| 38 return ""; | 40 return ""; |
| 39 } | 41 } |
| 40 | 42 |
| 41 } // namespace | 43 } // namespace |
| 42 | 44 |
| 43 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 45 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, |
| 44 WebKit::WebFrame* web_frame, | 46 WebKit::WebFrame* web_frame, |
| 45 const std::string& extension_id, | 47 const Extension* extension, |
| 46 Feature::Context context_type) | 48 Feature::Context context_type) |
| 47 : v8_context_(v8::Persistent<v8::Context>::New(v8_context)), | 49 : v8_context_(v8::Persistent<v8::Context>::New(v8_context)), |
| 48 web_frame_(web_frame), | 50 web_frame_(web_frame), |
| 49 extension_id_(extension_id), | 51 extension_(extension), |
| 50 context_type_(context_type) { | 52 context_type_(context_type) { |
| 51 VLOG(1) << "Created context for extension\n" | 53 VLOG(1) << "Created context:\n" |
| 52 << " id: " << extension_id << "\n" | 54 << " extension id: " << GetExtensionID() << "\n" |
| 53 << " frame: " << web_frame_ << "\n" | 55 << " frame: " << web_frame_ << "\n" |
| 54 << " context type: " << GetContextTypeDescription(context_type); | 56 << " context type: " << GetContextTypeDescription(context_type); |
| 55 } | 57 } |
| 56 | 58 |
| 57 ChromeV8Context::~ChromeV8Context() { | 59 ChromeV8Context::~ChromeV8Context() { |
| 58 VLOG(1) << "Destroyed context for extension\n" | 60 VLOG(1) << "Destroyed context for extension\n" |
| 59 << " id: " << extension_id_; | 61 << " extension id: " << GetExtensionID(); |
| 60 v8_context_.Dispose(); | 62 v8_context_.Dispose(); |
| 61 } | 63 } |
| 62 | 64 |
| 65 std::string ChromeV8Context::GetExtensionID() { |
| 66 return extension_ ? extension_->id() : ""; |
| 67 } |
| 68 |
| 63 // static | 69 // static |
| 64 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( | 70 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( |
| 65 v8::Handle<v8::Context> context) { | 71 v8::Handle<v8::Context> context) { |
| 66 v8::Local<v8::Object> global = context->Global(); | 72 v8::Local<v8::Object> global = context->Global(); |
| 67 v8::Local<v8::Value> hidden = global->GetHiddenValue( | 73 v8::Local<v8::Value> hidden = global->GetHiddenValue( |
| 68 v8::String::New(kChromeHidden)); | 74 v8::String::New(kChromeHidden)); |
| 69 | 75 |
| 70 if (hidden.IsEmpty() || hidden->IsUndefined()) { | 76 if (hidden.IsEmpty() || hidden->IsUndefined()) { |
| 71 hidden = v8::Object::New(); | 77 hidden = v8::Object::New(); |
| 72 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); | 78 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 web_frame_->callFunctionEvenIfScriptDisabled(function, | 139 web_frame_->callFunctionEvenIfScriptDisabled(function, |
| 134 v8::Object::New(), | 140 v8::Object::New(), |
| 135 argc, | 141 argc, |
| 136 argv); | 142 argv); |
| 137 if (result) | 143 if (result) |
| 138 *result = result_temp; | 144 *result = result_temp; |
| 139 | 145 |
| 140 return true; | 146 return true; |
| 141 } | 147 } |
| 142 | 148 |
| 149 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { |
| 150 if (!available_extension_apis_.get()) { |
| 151 available_extension_apis_ = |
| 152 extensions::ExtensionAPI::GetSharedInstance()->GetAPIsForContext( |
| 153 context_type_, |
| 154 extension_, |
| 155 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)).Pass(); |
| 156 } |
| 157 return *(available_extension_apis_.get()); |
| 158 } |
| 159 |
| 143 void ChromeV8Context::DispatchOnLoadEvent(bool is_extension_process, | 160 void ChromeV8Context::DispatchOnLoadEvent(bool is_extension_process, |
| 144 bool is_incognito_process, | 161 bool is_incognito_process, |
| 145 int manifest_version) const { | 162 int manifest_version) { |
| 146 v8::HandleScope handle_scope; | 163 v8::HandleScope handle_scope; |
| 147 v8::Handle<v8::Value> argv[4]; | 164 v8::Handle<v8::Value> argv[4]; |
| 148 argv[0] = v8::String::New(extension_id_.c_str()); | 165 argv[0] = v8::String::New(GetExtensionID().c_str()); |
| 149 argv[1] = v8::Boolean::New(is_extension_process); | 166 argv[1] = v8::Boolean::New(is_extension_process); |
| 150 argv[2] = v8::Boolean::New(is_incognito_process); | 167 argv[2] = v8::Boolean::New(is_incognito_process); |
| 151 argv[3] = v8::Integer::New(manifest_version); | 168 argv[3] = v8::Integer::New(manifest_version); |
| 152 CallChromeHiddenMethod("dispatchOnLoad", arraysize(argv), argv, NULL); | 169 CallChromeHiddenMethod("dispatchOnLoad", arraysize(argv), argv, NULL); |
| 153 } | 170 } |
| 154 | 171 |
| 155 void ChromeV8Context::DispatchOnUnloadEvent() const { | 172 void ChromeV8Context::DispatchOnUnloadEvent() { |
| 156 v8::HandleScope handle_scope; | 173 v8::HandleScope handle_scope; |
| 157 CallChromeHiddenMethod("dispatchOnUnload", 0, NULL, NULL); | 174 CallChromeHiddenMethod("dispatchOnUnload", 0, NULL, NULL); |
| 158 } | 175 } |
| OLD | NEW |