extensions: Use V8 Maybe version of GetFunction()

Also use As<v8::Object>() for return value of gin::CreateHandle()
because it should always be an object.

BUG=479065

Review URL: https://codereview.chromium.org/1208333002

Cr-Commit-Position: refs/heads/master@{#336731}
diff --git a/extensions/renderer/console.cc b/extensions/renderer/console.cc
index c2dc09b..b763e8e 100644
--- a/extensions/renderer/console.cc
+++ b/extensions/renderer/console.cc
@@ -14,10 +14,13 @@
 #include "extensions/renderer/extension_frame_helper.h"
 #include "extensions/renderer/script_context.h"
 #include "extensions/renderer/script_context_set.h"
+#include "extensions/renderer/v8_helpers.h"
 
 namespace extensions {
 namespace console {
 
+using namespace v8_helpers;
+
 namespace {
 
 // Writes |message| to stack to show up in minidump, then crashes.
@@ -62,7 +65,16 @@
       isolate,
       &BoundLogMethodCallback,
       v8::External::New(isolate, reinterpret_cast<void*>(log_method)));
-  target->Set(v8::String::NewFromUtf8(isolate, name.c_str()),
+  v8::Local<v8::Function> function;
+  if (!tmpl->GetFunction(isolate->GetCurrentContext()).ToLocal(&function)) {
+    LOG(FATAL) << "Could not create log function \"" << name << "\"";
+    return;
+  }
+  v8::Local<v8::String> v8_name = ToV8StringUnsafe(isolate, name);
+  if (!SetProperty(isolate->GetCurrentContext(), target, v8_name, function)) {
+    LOG(WARNING) << "Could not bind log method \"" << name << "\"";
+  }
+  SetProperty(isolate->GetCurrentContext(), target, v8_name,
               tmpl->GetFunction());
 }