| OLD | NEW |
| 1 // Copyright (c) 2011 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" |
| (...skipping 15 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 | |
| 41 content::RenderView* ChromeV8Extension::GetCurrentRenderView() { | 36 content::RenderView* ChromeV8Extension::GetCurrentRenderView() { |
| 42 WebFrame* webframe = WebFrame::frameForCurrentContext(); | 37 WebFrame* webframe = WebFrame::frameForCurrentContext(); |
| 43 DCHECK(webframe) << "RetrieveCurrentFrame called when not in a V8 context."; | 38 DCHECK(webframe) << "RetrieveCurrentFrame called when not in a V8 context."; |
| 44 if (!webframe) | 39 if (!webframe) |
| 45 return NULL; | 40 return NULL; |
| 46 | 41 |
| 47 WebView* webview = webframe->view(); | 42 WebView* webview = webframe->view(); |
| 48 if (!webview) | 43 if (!webview) |
| 49 return NULL; // can happen during closing | 44 return NULL; // can happen during closing |
| 50 | 45 |
| 51 content::RenderView* renderview = content::RenderView::FromWebView(webview); | 46 content::RenderView* renderview = content::RenderView::FromWebView(webview); |
| 52 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; | 47 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; |
| 53 return renderview; | 48 return renderview; |
| 54 } | 49 } |
| 55 | 50 |
| 56 ChromeV8Extension::ChromeV8Extension(const char* name, int resource_id, | 51 ChromeV8Extension::ChromeV8Extension(ExtensionDispatcher* extension_dispatcher) |
| 57 ExtensionDispatcher* extension_dispatcher) | 52 : extension_dispatcher_(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); | 53 g_instances.Get().insert(this); |
| 65 } | 54 } |
| 66 | 55 |
| 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) { | |
| 77 g_instances.Get().insert(this); | |
| 78 } | |
| 79 | |
| 80 ChromeV8Extension::~ChromeV8Extension() { | 56 ChromeV8Extension::~ChromeV8Extension() { |
| 81 g_instances.Get().erase(this); | 57 g_instances.Get().erase(this); |
| 82 } | 58 } |
| 83 | 59 |
| 84 // static | 60 // static |
| 85 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { | 61 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { |
| 86 return g_instances.Get(); | 62 return g_instances.Get(); |
| 87 } | 63 } |
| 88 | 64 |
| 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 | |
| 102 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const { | 65 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const { |
| 103 content::RenderView* renderview = GetCurrentRenderView(); | 66 content::RenderView* renderview = GetCurrentRenderView(); |
| 104 if (!renderview) | 67 if (!renderview) |
| 105 return NULL; // this can happen as a tab is closing. | 68 return NULL; // this can happen as a tab is closing. |
| 106 | 69 |
| 107 WebDocument document = renderview->GetWebView()->mainFrame()->document(); | 70 WebDocument document = renderview->GetWebView()->mainFrame()->document(); |
| 108 GURL url = document.url(); | 71 GURL url = document.url(); |
| 109 const ExtensionSet* extensions = extension_dispatcher_->extensions(); | 72 const ExtensionSet* extensions = extension_dispatcher_->extensions(); |
| 110 if (!extensions->ExtensionBindingsAllowed( | 73 if (!extensions->ExtensionBindingsAllowed( |
| 111 ExtensionURLInfo(document.securityOrigin(), url))) | 74 ExtensionURLInfo(document.securityOrigin(), url))) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 static const char kMessage[] = | 108 static const char kMessage[] = |
| 146 "%s can only be used in an extension process."; | 109 "%s can only be used in an extension process."; |
| 147 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 110 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 148 v8::ThrowException( | 111 v8::ThrowException( |
| 149 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 112 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 150 return false; | 113 return false; |
| 151 } | 114 } |
| 152 | 115 |
| 153 return true; | 116 return true; |
| 154 } | 117 } |
| 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 |