| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_extension.h" | 5 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 static base::LazyInstance<ChromeV8Extension::InstanceSet> g_instances = | 29 static base::LazyInstance<ChromeV8Extension::InstanceSet> g_instances = |
| 30 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 | 34 |
| 35 // static | 35 // static |
| 36 base::StringPiece ChromeV8Extension::GetStringResource(int resource_id) { |
| 37 return ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 38 } |
| 39 |
| 40 // static |
| 36 content::RenderView* ChromeV8Extension::GetCurrentRenderView() { | 41 content::RenderView* ChromeV8Extension::GetCurrentRenderView() { |
| 37 WebFrame* webframe = WebFrame::frameForCurrentContext(); | 42 WebFrame* webframe = WebFrame::frameForCurrentContext(); |
| 38 DCHECK(webframe) << "RetrieveCurrentFrame called when not in a V8 context."; | 43 DCHECK(webframe) << "RetrieveCurrentFrame called when not in a V8 context."; |
| 39 if (!webframe) | 44 if (!webframe) |
| 40 return NULL; | 45 return NULL; |
| 41 | 46 |
| 42 WebView* webview = webframe->view(); | 47 WebView* webview = webframe->view(); |
| 43 if (!webview) | 48 if (!webview) |
| 44 return NULL; // can happen during closing | 49 return NULL; // can happen during closing |
| 45 | 50 |
| 46 content::RenderView* renderview = content::RenderView::FromWebView(webview); | 51 content::RenderView* renderview = content::RenderView::FromWebView(webview); |
| 47 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; | 52 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; |
| 48 return renderview; | 53 return renderview; |
| 49 } | 54 } |
| 50 | 55 |
| 51 ChromeV8Extension::ChromeV8Extension(ExtensionDispatcher* extension_dispatcher) | 56 ChromeV8Extension::ChromeV8Extension(const char* name, int resource_id, |
| 52 : extension_dispatcher_(extension_dispatcher) { | 57 ExtensionDispatcher* extension_dispatcher) |
| 58 : v8::Extension(name, |
| 59 GetStringResource(resource_id).data(), |
| 60 0, // num dependencies |
| 61 NULL, // dependencies array |
| 62 GetStringResource(resource_id).size()), // source length |
| 63 extension_dispatcher_(extension_dispatcher) { |
| 64 g_instances.Get().insert(this); |
| 65 } |
| 66 |
| 67 ChromeV8Extension::ChromeV8Extension(const char* name, int resource_id, |
| 68 int dependency_count, |
| 69 const char** dependencies, |
| 70 ExtensionDispatcher* extension_dispatcher) |
| 71 : v8::Extension(name, |
| 72 GetStringResource(resource_id).data(), |
| 73 dependency_count, |
| 74 dependencies, |
| 75 GetStringResource(resource_id).size()), |
| 76 extension_dispatcher_(extension_dispatcher) { |
| 53 g_instances.Get().insert(this); | 77 g_instances.Get().insert(this); |
| 54 } | 78 } |
| 55 | 79 |
| 56 ChromeV8Extension::~ChromeV8Extension() { | 80 ChromeV8Extension::~ChromeV8Extension() { |
| 57 g_instances.Get().erase(this); | 81 g_instances.Get().erase(this); |
| 58 } | 82 } |
| 59 | 83 |
| 60 // static | 84 // static |
| 61 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { | 85 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { |
| 62 return g_instances.Get(); | 86 return g_instances.Get(); |
| 63 } | 87 } |
| 64 | 88 |
| 89 void ChromeV8Extension::ContextWillBeReleased(ChromeV8Context* context) { |
| 90 handlers_.erase(context); |
| 91 } |
| 92 |
| 93 ChromeV8ExtensionHandler* ChromeV8Extension::GetHandler( |
| 94 ChromeV8Context* context) const { |
| 95 HandlerMap::const_iterator iter = handlers_.find(context); |
| 96 if (iter == handlers_.end()) |
| 97 return NULL; |
| 98 else |
| 99 return iter->second.get(); |
| 100 } |
| 101 |
| 65 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const { | 102 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const { |
| 66 content::RenderView* renderview = GetCurrentRenderView(); | 103 content::RenderView* renderview = GetCurrentRenderView(); |
| 67 if (!renderview) | 104 if (!renderview) |
| 68 return NULL; // this can happen as a tab is closing. | 105 return NULL; // this can happen as a tab is closing. |
| 69 | 106 |
| 70 WebDocument document = renderview->GetWebView()->mainFrame()->document(); | 107 WebDocument document = renderview->GetWebView()->mainFrame()->document(); |
| 71 GURL url = document.url(); | 108 GURL url = document.url(); |
| 72 const ExtensionSet* extensions = extension_dispatcher_->extensions(); | 109 const ExtensionSet* extensions = extension_dispatcher_->extensions(); |
| 73 if (!extensions->ExtensionBindingsAllowed( | 110 if (!extensions->ExtensionBindingsAllowed( |
| 74 ExtensionURLInfo(document.securityOrigin(), url))) | 111 ExtensionURLInfo(document.securityOrigin(), url))) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 static const char kMessage[] = | 145 static const char kMessage[] = |
| 109 "%s can only be used in an extension process."; | 146 "%s can only be used in an extension process."; |
| 110 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 147 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 111 v8::ThrowException( | 148 v8::ThrowException( |
| 112 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 149 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 113 return false; | 150 return false; |
| 114 } | 151 } |
| 115 | 152 |
| 116 return true; | 153 return true; |
| 117 } | 154 } |
| 155 |
| 156 v8::Handle<v8::FunctionTemplate> |
| 157 ChromeV8Extension::GetNativeFunction(v8::Handle<v8::String> name) { |
| 158 if (name->Equals(v8::String::New("GetChromeHidden"))) { |
| 159 return v8::FunctionTemplate::New(GetChromeHidden); |
| 160 } |
| 161 |
| 162 if (name->Equals(v8::String::New("Print"))) { |
| 163 return v8::FunctionTemplate::New(Print); |
| 164 } |
| 165 |
| 166 return v8::FunctionTemplate::New(HandleNativeFunction, |
| 167 v8::External::New(this)); |
| 168 } |
| 169 |
| 170 // static |
| 171 v8::Handle<v8::Value> ChromeV8Extension::HandleNativeFunction( |
| 172 const v8::Arguments& arguments) { |
| 173 ChromeV8Extension* self = GetFromArguments<ChromeV8Extension>(arguments); |
| 174 ChromeV8Context* context = |
| 175 self->extension_dispatcher()->v8_context_set().GetCurrent(); |
| 176 CHECK(context) << "Unknown V8 context. Maybe a native function is getting " |
| 177 << "called during parse of a v8 extension, before the context " |
| 178 << "has been registered."; |
| 179 |
| 180 ChromeV8ExtensionHandler* handler = self->GetHandler(context); |
| 181 if (!handler) { |
| 182 handler = self->CreateHandler(context); |
| 183 if (handler) |
| 184 self->handlers_[context] = linked_ptr<ChromeV8ExtensionHandler>(handler); |
| 185 } |
| 186 CHECK(handler) << "No handler for v8 extension: " << self->name(); |
| 187 |
| 188 std::string name = *v8::String::AsciiValue(arguments.Callee()->GetName()); |
| 189 return handler->HandleNativeFunction(name, arguments); |
| 190 } |
| 191 |
| 192 ChromeV8ExtensionHandler* ChromeV8Extension::CreateHandler( |
| 193 ChromeV8Context* context) { |
| 194 return NULL; |
| 195 } |
| 196 |
| 197 v8::Handle<v8::Value> ChromeV8Extension::GetChromeHidden( |
| 198 const v8::Arguments& args) { |
| 199 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent()); |
| 200 } |
| 201 |
| 202 v8::Handle<v8::Value> ChromeV8Extension::Print(const v8::Arguments& args) { |
| 203 if (args.Length() < 1) |
| 204 return v8::Undefined(); |
| 205 |
| 206 std::vector<std::string> components; |
| 207 for (int i = 0; i < args.Length(); ++i) |
| 208 components.push_back(*v8::String::Utf8Value(args[i]->ToString())); |
| 209 |
| 210 LOG(ERROR) << JoinString(components, ','); |
| 211 return v8::Undefined(); |
| 212 } |
| OLD | NEW |