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/module_system.h" | 5 #include "chrome/renderer/extensions/module_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 else | 62 else |
63 stack_trace = "<could not convert stack trace to string>"; | 63 stack_trace = "<could not convert stack trace to string>"; |
64 } | 64 } |
65 | 65 |
66 console::Fatal(v8::Context::GetCalling(), | 66 console::Fatal(v8::Context::GetCalling(), |
67 CreateExceptionString(try_catch) + "{" + stack_trace + "}"); | 67 CreateExceptionString(try_catch) + "{" + stack_trace + "}"); |
68 } | 68 } |
69 | 69 |
70 } // namespace | 70 } // namespace |
71 | 71 |
72 ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context, | 72 ModuleSystem::ModuleSystem(ChromeV8Context* context, |
73 SourceMap* source_map) | 73 SourceMap* source_map) |
74 : ObjectBackedNativeHandler(context), | 74 : ObjectBackedNativeHandler(context), |
75 source_map_(source_map), | 75 source_map_(source_map), |
76 natives_enabled_(0) { | 76 natives_enabled_(0) { |
77 RouteFunction("require", | 77 RouteFunction("require", |
78 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); | 78 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); |
79 RouteFunction("requireNative", | 79 RouteFunction("requireNative", |
80 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); | 80 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); |
81 | 81 |
82 v8::Handle<v8::Object> global(context->Global()); | 82 v8::Handle<v8::Object> global(context->v8_context()->Global()); |
83 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New()); | 83 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New()); |
84 global->SetHiddenValue(v8::String::New(kModuleSystem), | 84 global->SetHiddenValue(v8::String::New(kModuleSystem), |
85 v8::External::New(this)); | 85 v8::External::New(this)); |
86 } | 86 } |
87 | 87 |
88 ModuleSystem::~ModuleSystem() { | 88 ModuleSystem::~ModuleSystem() { |
89 Invalidate(); | 89 Invalidate(); |
90 } | 90 } |
91 | 91 |
92 void ModuleSystem::Invalidate() { | 92 void ModuleSystem::Invalidate() { |
93 if (!is_valid()) | 93 if (!is_valid()) |
94 return; | 94 return; |
95 | 95 |
96 // Clear the module system properties from the global context. It's polite, | 96 // Clear the module system properties from the global context. It's polite, |
97 // and we use this as a signal in lazy handlers that we no longer exist. | 97 // and we use this as a signal in lazy handlers that we no longer exist. |
98 { | 98 { |
99 v8::HandleScope scope; | 99 v8::HandleScope scope; |
100 v8::Handle<v8::Object> global = v8_context()->Global(); | 100 v8::Handle<v8::Object> global = context()->v8_context()->Global(); |
101 global->DeleteHiddenValue(v8::String::New(kModulesField)); | 101 global->DeleteHiddenValue(v8::String::New(kModulesField)); |
102 global->DeleteHiddenValue(v8::String::New(kModuleSystem)); | 102 global->DeleteHiddenValue(v8::String::New(kModuleSystem)); |
103 } | 103 } |
104 | 104 |
105 // Invalidate all of the successfully required handlers we own. | 105 // Invalidate all of the successfully required handlers we own. |
106 for (NativeHandlerMap::iterator it = native_handler_map_.begin(); | 106 for (NativeHandlerMap::iterator it = native_handler_map_.begin(); |
107 it != native_handler_map_.end(); ++it) { | 107 it != native_handler_map_.end(); ++it) { |
108 it->second->Invalidate(); | 108 it->second->Invalidate(); |
109 } | 109 } |
110 | 110 |
(...skipping 26 matching lines...) Expand all Loading... |
137 | 137 |
138 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { | 138 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { |
139 v8::HandleScope handle_scope; | 139 v8::HandleScope handle_scope; |
140 v8::Handle<v8::String> module_name = args[0]->ToString(); | 140 v8::Handle<v8::String> module_name = args[0]->ToString(); |
141 return handle_scope.Close(RequireForJsInner(module_name)); | 141 return handle_scope.Close(RequireForJsInner(module_name)); |
142 } | 142 } |
143 | 143 |
144 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( | 144 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( |
145 v8::Handle<v8::String> module_name) { | 145 v8::Handle<v8::String> module_name) { |
146 v8::HandleScope handle_scope; | 146 v8::HandleScope handle_scope; |
147 v8::Context::Scope context_scope(v8_context()); | 147 v8::Context::Scope context_scope(context()->v8_context()); |
148 | 148 |
149 v8::Handle<v8::Object> global(v8_context()->Global()); | 149 v8::Handle<v8::Object> global(context()->v8_context()->Global()); |
150 | 150 |
151 // The module system might have been deleted. This can happen if a different | 151 // The module system might have been deleted. This can happen if a different |
152 // context keeps a reference to us, but our frame is destroyed (e.g. | 152 // context keeps a reference to us, but our frame is destroyed (e.g. |
153 // background page keeps reference to chrome object in a closed popup). | 153 // background page keeps reference to chrome object in a closed popup). |
154 v8::Handle<v8::Value> modules_value = | 154 v8::Handle<v8::Value> modules_value = |
155 global->GetHiddenValue(v8::String::New(kModulesField)); | 155 global->GetHiddenValue(v8::String::New(kModulesField)); |
156 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { | 156 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
157 console::Warn(v8::Context::GetCalling(), "Extension view no longer exists"); | 157 console::Warn(v8::Context::GetCalling(), "Extension view no longer exists"); |
158 return v8::Undefined(); | 158 return v8::Undefined(); |
159 } | 159 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 v8::Handle<v8::Object>::Cast(module)->Get( | 229 v8::Handle<v8::Object>::Cast(module)->Get( |
230 v8::String::New(method_name.c_str())); | 230 v8::String::New(method_name.c_str())); |
231 if (value.IsEmpty() || !value->IsFunction()) { | 231 if (value.IsEmpty() || !value->IsFunction()) { |
232 console::Error(v8::Context::GetCalling(), | 232 console::Error(v8::Context::GetCalling(), |
233 module_name + "." + method_name + " is not a function"); | 233 module_name + "." + method_name + " is not a function"); |
234 return handle_scope.Close(v8::Undefined()); | 234 return handle_scope.Close(v8::Undefined()); |
235 } | 235 } |
236 | 236 |
237 v8::Handle<v8::Function> func = | 237 v8::Handle<v8::Function> func = |
238 v8::Handle<v8::Function>::Cast(value); | 238 v8::Handle<v8::Function>::Cast(value); |
239 v8::Handle<v8::Object> global(v8_context()->Global()); | 239 v8::Handle<v8::Object> global(context()->v8_context()->Global()); |
240 v8::Local<v8::Value> result; | 240 v8::Local<v8::Value> result; |
241 { | 241 { |
242 WebKit::WebScopedMicrotaskSuppression suppression; | 242 WebKit::WebScopedMicrotaskSuppression suppression; |
243 v8::TryCatch try_catch; | 243 v8::TryCatch try_catch; |
244 try_catch.SetCaptureMessage(true); | 244 try_catch.SetCaptureMessage(true); |
245 result = func->Call(global, args->size(), vector_as_array(args)); | 245 result = func->Call(global, args->size(), vector_as_array(args)); |
246 if (try_catch.HasCaught()) | 246 if (try_catch.HasCaught()) |
247 HandleException(try_catch); | 247 HandleException(try_catch); |
248 } | 248 } |
249 return handle_scope.Close(result); | 249 return handle_scope.Close(result); |
(...skipping 30 matching lines...) Expand all Loading... |
280 | 280 |
281 // static | 281 // static |
282 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner( | 282 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner( |
283 v8::Local<v8::String> property, | 283 v8::Local<v8::String> property, |
284 const v8::AccessorInfo& info, | 284 const v8::AccessorInfo& info, |
285 RequireFunction require_function) { | 285 RequireFunction require_function) { |
286 CHECK(!info.Data().IsEmpty()); | 286 CHECK(!info.Data().IsEmpty()); |
287 CHECK(info.Data()->IsObject()); | 287 CHECK(info.Data()->IsObject()); |
288 v8::HandleScope handle_scope; | 288 v8::HandleScope handle_scope; |
289 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); | 289 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
290 // This context should be the same as v8_context(). | 290 // This context should be the same as context()->v8_context(). |
291 v8::Handle<v8::Context> context = parameters->CreationContext(); | 291 v8::Handle<v8::Context> context = parameters->CreationContext(); |
292 v8::Handle<v8::Object> global(context->Global()); | 292 v8::Handle<v8::Object> global(context->Global()); |
293 v8::Handle<v8::Value> module_system_value = | 293 v8::Handle<v8::Value> module_system_value = |
294 global->GetHiddenValue(v8::String::New(kModuleSystem)); | 294 global->GetHiddenValue(v8::String::New(kModuleSystem)); |
295 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { | 295 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { |
296 // ModuleSystem has been deleted. | 296 // ModuleSystem has been deleted. |
297 // TODO(kalman): See comment in header file. | 297 // TODO(kalman): See comment in header file. |
298 console::Warn(v8::Context::GetCalling(), | 298 console::Warn(v8::Context::GetCalling(), |
299 "Module system has been deleted, does extension view exist?"); | 299 "Module system has been deleted, does extension view exist?"); |
300 return v8::Undefined(); | 300 return v8::Undefined(); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 449 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
450 v8::HandleScope handle_scope; | 450 v8::HandleScope handle_scope; |
451 v8::Handle<v8::String> left = v8::String::New( | 451 v8::Handle<v8::String> left = v8::String::New( |
452 "(function(require, requireNative, exports) {'use strict';"); | 452 "(function(require, requireNative, exports) {'use strict';"); |
453 v8::Handle<v8::String> right = v8::String::New("\n})"); | 453 v8::Handle<v8::String> right = v8::String::New("\n})"); |
454 return handle_scope.Close( | 454 return handle_scope.Close( |
455 v8::String::Concat(left, v8::String::Concat(source, right))); | 455 v8::String::Concat(left, v8::String::Concat(source, right))); |
456 } | 456 } |
457 | 457 |
458 } // extensions | 458 } // extensions |
OLD | NEW |