Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/renderer/extensions/chrome_v8_extension.cc

Issue 9386001: Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "chrome/common/extensions/extension.h" 11 #include "chrome/common/extensions/extension.h"
12 #include "chrome/common/extensions/extension_set.h" 12 #include "chrome/common/extensions/extension_set.h"
13 #include "chrome/common/extensions/api/extension_api.h" 13 #include "chrome/common/extensions/api/extension_api.h"
14 #include "chrome/renderer/extensions/chrome_v8_context.h" 14 #include "chrome/renderer/extensions/chrome_v8_context.h"
15 #include "chrome/renderer/extensions/extension_dispatcher.h" 15 #include "chrome/renderer/extensions/extension_dispatcher.h"
16 #include "content/public/renderer/render_view.h" 16 #include "content/public/renderer/render_view.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
20 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.h"
21 #include "grit/renderer_resources.h"
22
23 #include <iostream>
Aaron Boodman 2012/02/16 01:54:42 These seem unneeded.
koz (OOO until 15th September) 2012/02/27 01:06:14 Done.
24 using namespace std;
21 25
22 using extensions::ExtensionAPI; 26 using extensions::ExtensionAPI;
23 using WebKit::WebDocument; 27 using WebKit::WebDocument;
24 using WebKit::WebFrame; 28 using WebKit::WebFrame;
25 using WebKit::WebView; 29 using WebKit::WebView;
26 30
27 namespace { 31 namespace {
28 32
29 static base::LazyInstance<ChromeV8Extension::InstanceSet> g_instances = 33 static base::LazyInstance<ChromeV8Extension::InstanceSet> g_instances =
30 LAZY_INSTANCE_INITIALIZER; 34 LAZY_INSTANCE_INITIALIZER;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 ChromeV8Extension::~ChromeV8Extension() { 84 ChromeV8Extension::~ChromeV8Extension() {
81 g_instances.Get().erase(this); 85 g_instances.Get().erase(this);
82 } 86 }
83 87
84 // static 88 // static
85 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() { 89 const ChromeV8Extension::InstanceSet& ChromeV8Extension::GetAll() {
86 return g_instances.Get(); 90 return g_instances.Get();
87 } 91 }
88 92
89 void ChromeV8Extension::ContextWillBeReleased(ChromeV8Context* context) { 93 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 } 94 }
101 95
102 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const { 96 const Extension* ChromeV8Extension::GetExtensionForCurrentRenderView() const {
103 content::RenderView* renderview = GetCurrentRenderView(); 97 content::RenderView* renderview = GetCurrentRenderView();
104 if (!renderview) 98 if (!renderview)
105 return NULL; // this can happen as a tab is closing. 99 return NULL; // this can happen as a tab is closing.
106 100
107 WebDocument document = renderview->GetWebView()->mainFrame()->document(); 101 WebDocument document = renderview->GetWebView()->mainFrame()->document();
108 GURL url = document.url(); 102 GURL url = document.url();
109 const ExtensionSet* extensions = extension_dispatcher_->extensions(); 103 const ExtensionSet* extensions = extension_dispatcher_->extensions();
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 "%s can only be used in an extension process."; 140 "%s can only be used in an extension process.";
147 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); 141 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str());
148 v8::ThrowException( 142 v8::ThrowException(
149 v8::Exception::Error(v8::String::New(error_msg.c_str()))); 143 v8::Exception::Error(v8::String::New(error_msg.c_str())));
150 return false; 144 return false;
151 } 145 }
152 146
153 return true; 147 return true;
154 } 148 }
155 149
156 v8::Handle<v8::FunctionTemplate> 150 v8::Handle<v8::Value> ChromeV8Extension::HandleNativeFunction(
157 ChromeV8Extension::GetNativeFunction(v8::Handle<v8::String> name) { 151 const std::string& function_name, const v8::Arguments& args) {
158 if (name->Equals(v8::String::New("GetChromeHidden"))) { 152 CHECK(false) << "Failed to handle native call to " << function_name;
159 return v8::FunctionTemplate::New(GetChromeHidden); 153 return v8::Undefined();
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 } 154 }
169 155
170 // static 156 // static
171 v8::Handle<v8::Value> ChromeV8Extension::HandleNativeFunction( 157 v8::Handle<v8::Value> ChromeV8Extension::Router(const v8::Arguments& args) {
172 const v8::Arguments& arguments) { 158 std::string callee_name = *v8::String::AsciiValue(args.Callee()->GetName());
173 ChromeV8Extension* self = GetFromArguments<ChromeV8Extension>(arguments); 159 ChromeV8Extension* extension = static_cast<ChromeV8Extension*>(
174 ChromeV8Context* context = 160 args.Data().As<v8::External>()->Value());
175 self->extension_dispatcher()->v8_context_set().GetCurrent(); 161 return extension->HandleNativeFunction(callee_name, args);
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 } 162 }
191 163
192 ChromeV8ExtensionHandler* ChromeV8Extension::CreateHandler( 164 void ChromeV8Extension::RouteFunctionHere(const char* name,
193 ChromeV8Context* context) { 165 v8::Handle<v8::Object> object) {
194 return NULL; 166 RouteFunctionToStatic(name, Router, object);
195 } 167 }
196 168
197 v8::Handle<v8::Value> ChromeV8Extension::GetChromeHidden( 169 void ChromeV8Extension::RouteFunctionToStatic(const char* name,
198 const v8::Arguments& args) { 170 HandlerFunction function, v8::Handle<v8::Object> object) {
199 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent()); 171 v8::Handle<v8::Function> v8_function = v8::FunctionTemplate::New(
172 function, v8::External::New(this))->GetFunction();
173 v8_function->SetName(v8::String::New(name));
174 object->Set(v8::String::NewSymbol(name), v8_function);
200 } 175 }
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698