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