OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/script_context.h" | 5 #include "extensions/renderer/script_context.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 return "WEB_PAGE"; | 52 return "WEB_PAGE"; |
53 case Feature::BLESSED_WEB_PAGE_CONTEXT: | 53 case Feature::BLESSED_WEB_PAGE_CONTEXT: |
54 return "BLESSED_WEB_PAGE"; | 54 return "BLESSED_WEB_PAGE"; |
55 case Feature::WEBUI_CONTEXT: | 55 case Feature::WEBUI_CONTEXT: |
56 return "WEBUI"; | 56 return "WEBUI"; |
57 } | 57 } |
58 NOTREACHED(); | 58 NOTREACHED(); |
59 return std::string(); | 59 return std::string(); |
60 } | 60 } |
61 | 61 |
62 static std::string ToStringOrDefault( | |
63 const v8::Local<v8::String>& v8_string, | |
64 const std::string& dflt) { | |
65 if (v8_string.IsEmpty()) | |
66 return dflt; | |
67 std::string ascii_value = *v8::String::Utf8Value(v8_string); | |
68 return ascii_value.empty() ? dflt : ascii_value; | |
69 } | |
70 | |
71 } // namespace | 62 } // namespace |
72 | 63 |
73 // A gin::Runner that delegates to its ScriptContext. | 64 // A gin::Runner that delegates to its ScriptContext. |
74 class ScriptContext::Runner : public gin::Runner { | 65 class ScriptContext::Runner : public gin::Runner { |
75 public: | 66 public: |
76 explicit Runner(ScriptContext* context); | 67 explicit Runner(ScriptContext* context); |
77 | 68 |
78 // gin::Runner overrides. | 69 // gin::Runner overrides. |
79 void Run(const std::string& source, | 70 void Run(const std::string& source, |
80 const std::string& resource_name) override; | 71 const std::string& resource_name) override; |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 " context_type: %s\n" | 373 " context_type: %s\n" |
383 " effective extension id: %s\n" | 374 " effective extension id: %s\n" |
384 " effective context type: %s", | 375 " effective context type: %s", |
385 extension_.get() ? extension_->id().c_str() : "(none)", web_frame_, | 376 extension_.get() ? extension_->id().c_str() : "(none)", web_frame_, |
386 GetURL().spec().c_str(), GetContextTypeDescription().c_str(), | 377 GetURL().spec().c_str(), GetContextTypeDescription().c_str(), |
387 effective_extension_.get() ? effective_extension_->id().c_str() | 378 effective_extension_.get() ? effective_extension_->id().c_str() |
388 : "(none)", | 379 : "(none)", |
389 GetEffectiveContextTypeDescription().c_str()); | 380 GetEffectiveContextTypeDescription().c_str()); |
390 } | 381 } |
391 | 382 |
392 std::string ScriptContext::GetStackTraceAsString() const { | |
393 v8::Local<v8::StackTrace> stack_trace = | |
394 v8::StackTrace::CurrentStackTrace(isolate(), 10); | |
395 if (stack_trace.IsEmpty() || stack_trace->GetFrameCount() <= 0) { | |
396 return " <no stack trace>"; | |
397 } else { | |
398 std::string result; | |
399 for (int i = 0; i < stack_trace->GetFrameCount(); ++i) { | |
400 v8::Local<v8::StackFrame> frame = stack_trace->GetFrame(i); | |
401 CHECK(!frame.IsEmpty()); | |
402 result += base::StringPrintf( | |
403 "\n at %s (%s:%d:%d)", | |
404 ToStringOrDefault(frame->GetFunctionName(), "<anonymous>").c_str(), | |
405 ToStringOrDefault(frame->GetScriptName(), "<anonymous>").c_str(), | |
406 frame->GetLineNumber(), | |
407 frame->GetColumn()); | |
408 } | |
409 return result; | |
410 } | |
411 } | |
412 | |
413 ScriptContext::Runner::Runner(ScriptContext* context) : context_(context) { | 383 ScriptContext::Runner::Runner(ScriptContext* context) : context_(context) { |
414 } | 384 } |
415 | 385 |
416 void ScriptContext::Runner::Run(const std::string& source, | 386 void ScriptContext::Runner::Run(const std::string& source, |
417 const std::string& resource_name) { | 387 const std::string& resource_name) { |
418 context_->module_system()->RunString(source, resource_name); | 388 context_->module_system()->RunString(source, resource_name); |
419 } | 389 } |
420 | 390 |
421 v8::Local<v8::Value> ScriptContext::Runner::Call( | 391 v8::Local<v8::Value> ScriptContext::Runner::Call( |
422 v8::Local<v8::Function> function, | 392 v8::Local<v8::Function> function, |
423 v8::Local<v8::Value> receiver, | 393 v8::Local<v8::Value> receiver, |
424 int argc, | 394 int argc, |
425 v8::Local<v8::Value> argv[]) { | 395 v8::Local<v8::Value> argv[]) { |
426 return context_->CallFunction(function, argc, argv); | 396 return context_->CallFunction(function, argc, argv); |
427 } | 397 } |
428 | 398 |
429 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { | 399 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |
430 v8::HandleScope handle_scope(context_->isolate()); | 400 v8::HandleScope handle_scope(context_->isolate()); |
431 return gin::PerContextData::From(context_->v8_context())->context_holder(); | 401 return gin::PerContextData::From(context_->v8_context())->context_holder(); |
432 } | 402 } |
433 | 403 |
434 } // namespace extensions | 404 } // namespace extensions |
OLD | NEW |