Namespace internal extension JavaScript modules with extensions:: so that they
can be identified in stack traces, and filtered out when showing to developers.

BUG=21734

Review URL: https://chromiumcodereview.appspot.com/23604050

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@222149 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/extensions/error_console/error_console_browsertest.cc b/chrome/browser/extensions/error_console/error_console_browsertest.cc
index e2bde82..3e2ab2fc 100644
--- a/chrome/browser/extensions/error_console/error_console_browsertest.cc
+++ b/chrome/browser/extensions/error_console/error_console_browsertest.cc
@@ -7,6 +7,7 @@
 #include "base/files/file_path.h"
 #include "base/prefs/pref_service.h"
 #include "base/strings/string16.h"
+#include "base/strings/stringprintf.h"
 #include "base/strings/utf_string_conversions.h"
 #include "chrome/browser/extensions/extension_browsertest.h"
 #include "chrome/browser/extensions/extension_service.h"
@@ -426,6 +427,9 @@
   const ErrorConsole::ErrorList& errors =
       error_console()->GetErrorsForExtension(extension->id());
 
+  std::string event_bindings_str =
+      base::StringPrintf("extensions::%s", kEventBindings);
+
   CheckRuntimeError(
       errors[0],
       extension->id(),
@@ -444,10 +448,10 @@
                   "extensions::SafeBuiltins",
                   std::string("Function.target.") + kAnonymousFunction);
   CheckStackFrame(
-      stack_trace[2], kEventBindings, "Event.dispatchToListener");
-  CheckStackFrame(stack_trace[3], kEventBindings, "Event.dispatch_");
-  CheckStackFrame(stack_trace[4], kEventBindings, "dispatchArgs");
-  CheckStackFrame(stack_trace[5], kEventBindings, "dispatchEvent");
+      stack_trace[2], event_bindings_str, "Event.dispatchToListener");
+  CheckStackFrame(stack_trace[3], event_bindings_str, "Event.dispatch_");
+  CheckStackFrame(stack_trace[4], event_bindings_str, "dispatchArgs");
+  CheckStackFrame(stack_trace[5], event_bindings_str, "dispatchEvent");
 }
 
 // Test that we can catch an error for calling an API with improper arguments.
@@ -463,10 +467,13 @@
   const ErrorConsole::ErrorList& errors =
       error_console()->GetErrorsForExtension(extension->id());
 
+  std::string schema_utils_str =
+      base::StringPrintf("extensions::%s", kSchemaUtils);
+
   CheckRuntimeError(
       errors[0],
       extension->id(),
-      kSchemaUtils,  // API calls are checked in schemaUtils.js.
+      schema_utils_str,  // API calls are checked in schemaUtils.js.
       false,  // not incognito
       "Uncaught Error: Invocation of form "
           "tabs.get(string, function) doesn't match definition "
@@ -478,7 +485,7 @@
   const StackTrace& stack_trace = GetStackTraceFromError(errors[0]);
   ASSERT_EQ(1u, stack_trace.size());
   CheckStackFrame(stack_trace[0],
-                  kSchemaUtils,
+                  schema_utils_str,
                   kAnonymousFunction);
 }
 
diff --git a/chrome/renderer/extensions/module_system.cc b/chrome/renderer/extensions/module_system.cc
index ffe446c7..f539d58a 100644
--- a/chrome/renderer/extensions/module_system.cc
+++ b/chrome/renderer/extensions/module_system.cc
@@ -210,7 +210,7 @@
   if (!exports->IsUndefined())
     return handle_scope.Close(exports);
 
-  std::string module_name_str = *v8::String::AsciiValue(module_name);
+  std::string module_name_str = *v8::String::Utf8Value(module_name);
   v8::Handle<v8::Value> source(GetSource(module_name_str));
   if (source.IsEmpty() || source->IsUndefined()) {
     Fatal(context_, "No source for require(" + module_name_str + ")");
@@ -376,7 +376,7 @@
   ModuleSystem* module_system = static_cast<ModuleSystem*>(
       v8::Handle<v8::External>::Cast(module_system_value)->Value());
 
-  std::string name = *v8::String::AsciiValue(
+  std::string name = *v8::String::Utf8Value(
       parameters->Get(v8::String::New(kModuleName))->ToString());
 
   // Switch to our v8 context because we need functions created while running
@@ -400,7 +400,7 @@
       parameters->Get(v8::String::New(kModuleField))->ToString();
 
   if (!module->Has(field)) {
-    std::string field_str = *v8::String::AsciiValue(field);
+    std::string field_str = *v8::String::Utf8Value(field);
     Fatal(module_system->context_,
           "Lazy require of " + name + "." + field_str + " did not set the " +
               field_str + " field");
@@ -468,10 +468,16 @@
   v8::HandleScope handle_scope(GetIsolate());
   v8::Context::Scope context_scope(context()->v8_context());
 
+  // Prepend extensions:: to |name| so that internal code can be differentiated
+  // from external code in stack traces. This has no effect on behaviour.
+  std::string internal_name = base::StringPrintf("extensions::%s",
+                                                 *v8::String::Utf8Value(name));
+
   WebKit::WebScopedMicrotaskSuppression suppression;
   v8::TryCatch try_catch;
   try_catch.SetCaptureMessage(true);
-  v8::Handle<v8::Script> script(v8::Script::New(code, name));
+  v8::Handle<v8::Script> script(v8::Script::New(
+      code, v8::String::New(internal_name.c_str(), internal_name.size())));
   if (try_catch.HasCaught()) {
     HandleException(try_catch);
     return v8::Undefined();
@@ -496,7 +502,7 @@
 void ModuleSystem::RequireNative(
     const v8::FunctionCallbackInfo<v8::Value>& args) {
   CHECK_EQ(1, args.Length());
-  std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
+  std::string native_name = *v8::String::Utf8Value(args[0]->ToString());
   args.GetReturnValue().Set(RequireNativeFromString(native_name));
 }
 
diff --git a/extensions/common/extension_urls.cc b/extensions/common/extension_urls.cc
index 38a1fff..ba72a63 100644
--- a/extensions/common/extension_urls.cc
+++ b/extensions/common/extension_urls.cc
@@ -4,6 +4,7 @@
 
 #include "extensions/common/extension_urls.h"
 
+#include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "extensions/common/constants.h"
 #include "url/gurl.h"
@@ -16,8 +17,9 @@
 
 bool IsSourceFromAnExtension(const base::string16& source) {
   return GURL(source).SchemeIs(kExtensionScheme) ||
-         source == base::UTF8ToUTF16(kEventBindings) ||
-         source == base::UTF8ToUTF16(kSchemaUtils);
+         StartsWith(source,
+                    base::ASCIIToUTF16("extensions::"),
+                    true /* case-sensitive */);
 }
 
 }  // namespace extensions