| 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 "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" | 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 // static | 56 // static |
| 57 bool ModuleSystem::IsPresentInCurrentContext() { | 57 bool ModuleSystem::IsPresentInCurrentContext() { |
| 58 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 58 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
| 59 if (global.IsEmpty()) | 59 if (global.IsEmpty()) |
| 60 return false; | 60 return false; |
| 61 v8::Handle<v8::Value> module_system = | 61 v8::Handle<v8::Value> module_system = |
| 62 global->GetHiddenValue(v8::String::New(kModuleSystem)); | 62 global->GetHiddenValue(v8::String::New(kModuleSystem)); |
| 63 return !module_system.IsEmpty() && !module_system->IsUndefined(); | 63 return !module_system.IsEmpty() && !module_system->IsUndefined(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { |
| 67 DumpException(try_catch); |
| 68 if (exception_handler_.get()) |
| 69 exception_handler_->HandleUncaughtException(); |
| 70 } |
| 71 |
| 66 // static | 72 // static |
| 67 void ModuleSystem::DumpException(const v8::TryCatch& try_catch) { | 73 void ModuleSystem::DumpException(const v8::TryCatch& try_catch) { |
| 68 v8::HandleScope handle_scope; | 74 v8::HandleScope handle_scope; |
| 69 | 75 |
| 70 v8::Handle<v8::Message> message(try_catch.Message()); | 76 v8::Handle<v8::Message> message(try_catch.Message()); |
| 71 if (message.IsEmpty()) { | 77 if (message.IsEmpty()) { |
| 72 LOG(ERROR) << "try_catch has no message"; | 78 LOG(ERROR) << "try_catch has no message"; |
| 73 return; | 79 return; |
| 74 } | 80 } |
| 75 | 81 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 natives->Get(v8::String::NewSymbol("require")), | 142 natives->Get(v8::String::NewSymbol("require")), |
| 137 natives->Get(v8::String::NewSymbol("requireNative")), | 143 natives->Get(v8::String::NewSymbol("requireNative")), |
| 138 exports, | 144 exports, |
| 139 }; | 145 }; |
| 140 { | 146 { |
| 141 WebKit::WebScopedMicrotaskSuppression suppression; | 147 WebKit::WebScopedMicrotaskSuppression suppression; |
| 142 v8::TryCatch try_catch; | 148 v8::TryCatch try_catch; |
| 143 try_catch.SetCaptureMessage(true); | 149 try_catch.SetCaptureMessage(true); |
| 144 func->Call(global, 3, args); | 150 func->Call(global, 3, args); |
| 145 if (try_catch.HasCaught()) { | 151 if (try_catch.HasCaught()) { |
| 146 DumpException(try_catch); | 152 HandleException(try_catch); |
| 147 return v8::Undefined(); | 153 return v8::Undefined(); |
| 148 } | 154 } |
| 149 } | 155 } |
| 150 modules->Set(module_name, exports); | 156 modules->Set(module_name, exports); |
| 151 return handle_scope.Close(exports); | 157 return handle_scope.Close(exports); |
| 152 } | 158 } |
| 153 | 159 |
| 154 void ModuleSystem::CallModuleMethod(const std::string& module_name, | 160 void ModuleSystem::CallModuleMethod(const std::string& module_name, |
| 155 const std::string& method_name) { | 161 const std::string& method_name) { |
| 156 v8::HandleScope handle_scope; | 162 v8::HandleScope handle_scope; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 167 v8::Handle<v8::Function> func = | 173 v8::Handle<v8::Function> func = |
| 168 v8::Handle<v8::Function>::Cast(value); | 174 v8::Handle<v8::Function>::Cast(value); |
| 169 // TODO(jeremya/koz): refer to context_ here, not the current context. | 175 // TODO(jeremya/koz): refer to context_ here, not the current context. |
| 170 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 176 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
| 171 { | 177 { |
| 172 WebKit::WebScopedMicrotaskSuppression suppression; | 178 WebKit::WebScopedMicrotaskSuppression suppression; |
| 173 v8::TryCatch try_catch; | 179 v8::TryCatch try_catch; |
| 174 try_catch.SetCaptureMessage(true); | 180 try_catch.SetCaptureMessage(true); |
| 175 func->Call(global, 0, NULL); | 181 func->Call(global, 0, NULL); |
| 176 if (try_catch.HasCaught()) | 182 if (try_catch.HasCaught()) |
| 177 DumpException(try_catch); | 183 HandleException(try_catch); |
| 178 } | 184 } |
| 179 } | 185 } |
| 180 | 186 |
| 181 void ModuleSystem::RegisterNativeHandler(const std::string& name, | 187 void ModuleSystem::RegisterNativeHandler(const std::string& name, |
| 182 scoped_ptr<NativeHandler> native_handler) { | 188 scoped_ptr<NativeHandler> native_handler) { |
| 183 native_handler_map_[name] = | 189 native_handler_map_[name] = |
| 184 linked_ptr<NativeHandler>(native_handler.release()); | 190 linked_ptr<NativeHandler>(native_handler.release()); |
| 185 } | 191 } |
| 186 | 192 |
| 187 void ModuleSystem::OverrideNativeHandler(const std::string& name) { | 193 void ModuleSystem::OverrideNativeHandler(const std::string& name) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 | 250 |
| 245 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | 251 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
| 246 v8::Handle<v8::String> name) { | 252 v8::Handle<v8::String> name) { |
| 247 v8::HandleScope handle_scope; | 253 v8::HandleScope handle_scope; |
| 248 WebKit::WebScopedMicrotaskSuppression suppression; | 254 WebKit::WebScopedMicrotaskSuppression suppression; |
| 249 v8::Handle<v8::Value> result; | 255 v8::Handle<v8::Value> result; |
| 250 v8::TryCatch try_catch; | 256 v8::TryCatch try_catch; |
| 251 try_catch.SetCaptureMessage(true); | 257 try_catch.SetCaptureMessage(true); |
| 252 v8::Handle<v8::Script> script(v8::Script::New(code, name)); | 258 v8::Handle<v8::Script> script(v8::Script::New(code, name)); |
| 253 if (try_catch.HasCaught()) { | 259 if (try_catch.HasCaught()) { |
| 254 DumpException(try_catch); | 260 HandleException(try_catch); |
| 255 return handle_scope.Close(result); | 261 return handle_scope.Close(result); |
| 256 } | 262 } |
| 257 | 263 |
| 258 result = script->Run(); | 264 result = script->Run(); |
| 259 if (try_catch.HasCaught()) | 265 if (try_catch.HasCaught()) |
| 260 DumpException(try_catch); | 266 HandleException(try_catch); |
| 261 | 267 |
| 262 return handle_scope.Close(result); | 268 return handle_scope.Close(result); |
| 263 } | 269 } |
| 264 | 270 |
| 265 v8::Handle<v8::Value> ModuleSystem::GetSource( | 271 v8::Handle<v8::Value> ModuleSystem::GetSource( |
| 266 v8::Handle<v8::String> source_name) { | 272 v8::Handle<v8::String> source_name) { |
| 267 v8::HandleScope handle_scope; | 273 v8::HandleScope handle_scope; |
| 268 std::string module_name = *v8::String::AsciiValue(source_name); | 274 std::string module_name = *v8::String::AsciiValue(source_name); |
| 269 if (!source_map_->Contains(module_name)) | 275 if (!source_map_->Contains(module_name)) |
| 270 return v8::Undefined(); | 276 return v8::Undefined(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 291 v8::Handle<v8::String> right = v8::String::New("\n})"); | 297 v8::Handle<v8::String> right = v8::String::New("\n})"); |
| 292 return handle_scope.Close( | 298 return handle_scope.Close( |
| 293 v8::String::Concat(left, v8::String::Concat(source, right))); | 299 v8::String::Concat(left, v8::String::Concat(source, right))); |
| 294 } | 300 } |
| 295 | 301 |
| 296 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 302 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
| 297 return v8::ThrowException(v8::String::New(message.c_str())); | 303 return v8::ThrowException(v8::String::New(message.c_str())); |
| 298 } | 304 } |
| 299 | 305 |
| 300 } // extensions | 306 } // extensions |
| OLD | NEW |