Commit 1ff21f95 authored by yzshen's avatar yzshen Committed by Commit bot

Mojo JS bindings: make sure that console.log() could be used in the bindings.

It is convenient for debugging.

Although console.log() is supported by chrome directly, mojo bindings currently are loaded using gin. Therefore, console.log() is not always available and needs to be defined as a module. After we move away from gin/AMD, we should be able to get rid of this.

BUG=579646
TBR=jochen@chromium.org
(deferring to eugenebut)

Review-Url: https://codereview.chromium.org/2618693004
Cr-Commit-Position: refs/heads/master@{#442972}
parent 34db9f35
...@@ -141,6 +141,7 @@ ...@@ -141,6 +141,7 @@
#include "content/renderer/web_frame_utils.h" #include "content/renderer/web_frame_utils.h"
#include "content/renderer/web_ui_extension.h" #include "content/renderer/web_ui_extension.h"
#include "crypto/sha2.h" #include "crypto/sha2.h"
#include "gin/modules/console.h"
#include "gin/modules/module_registry.h" #include "gin/modules/module_registry.h"
#include "media/audio/audio_output_device.h" #include "media/audio/audio_output_device.h"
#include "media/base/audio_renderer_mixer_input.h" #include "media/base/audio_renderer_mixer_input.h"
...@@ -2621,6 +2622,9 @@ void RenderFrameImpl::EnsureMojoBuiltinsAreAvailable( ...@@ -2621,6 +2622,9 @@ void RenderFrameImpl::EnsureMojoBuiltinsAreAvailable(
return; return;
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
registry->AddBuiltinModule(isolate, gin::Console::kModuleName,
gin::Console::GetModule(isolate));
registry->AddBuiltinModule(isolate, mojo::edk::js::Core::kModuleName, registry->AddBuiltinModule(isolate, mojo::edk::js::Core::kModuleName,
mojo::edk::js::Core::GetModule(isolate)); mojo::edk::js::Core::GetModule(isolate));
registry->AddBuiltinModule(isolate, mojo::edk::js::Support::kModuleName, registry->AddBuiltinModule(isolate, mojo::edk::js::Support::kModuleName,
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "extensions/renderer/process_info_native_handler.h" #include "extensions/renderer/process_info_native_handler.h"
#include "gin/converter.h" #include "gin/converter.h"
#include "gin/dictionary.h" #include "gin/dictionary.h"
#include "gin/modules/console.h"
#include "mojo/edk/js/core.h" #include "mojo/edk/js/core.h"
#include "mojo/edk/js/handle.h" #include "mojo/edk/js/handle.h"
#include "mojo/edk/js/support.h" #include "mojo/edk/js/support.h"
...@@ -153,6 +154,9 @@ void ApiTestEnvironment::RegisterModules() { ...@@ -153,6 +154,9 @@ void ApiTestEnvironment::RegisterModules() {
"exports.$set('AttachFilteredEvent', function() {});" "exports.$set('AttachFilteredEvent', function() {});"
"exports.$set('MatchAgainstEventFilter', function() { return [] });"); "exports.$set('MatchAgainstEventFilter', function() { return [] });");
gin::ModuleRegistry::From(env()->context()->v8_context())
->AddBuiltinModule(env()->isolate(), gin::Console::kModuleName,
gin::Console::GetModule(env()->isolate()));
gin::ModuleRegistry::From(env()->context()->v8_context()) gin::ModuleRegistry::From(env()->context()->v8_context())
->AddBuiltinModule(env()->isolate(), mojo::edk::js::Core::kModuleName, ->AddBuiltinModule(env()->isolate(), mojo::edk::js::Core::kModuleName,
mojo::edk::js::Core::GetModule(env()->isolate())); mojo::edk::js::Core::GetModule(env()->isolate()));
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
<include name="IDR_MOJO_UNICODE_JS" file="../../mojo/public/js/unicode.js" flattenhtml="true" type="BINDATA" /> <include name="IDR_MOJO_UNICODE_JS" file="../../mojo/public/js/unicode.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_MOJO_VALIDATOR_JS" file="../../mojo/public/js/validator.js" flattenhtml="true" type="BINDATA" /> <include name="IDR_MOJO_VALIDATOR_JS" file="../../mojo/public/js/validator.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_IOS_MOJO_SUPPORT_JS" file="webui/resources/support.js" flattenhtml="true" type="BINDATA" /> <include name="IDR_IOS_MOJO_SUPPORT_JS" file="webui/resources/support.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_IOS_CONSOLE_JS" file="webui/resources/console.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_IOS_MOJO_SYNC_MESSAGE_CHANNEL_JS" file="webui/resources/sync_message_channel.js" flattenhtml="true" type="BINDATA" /> <include name="IDR_IOS_MOJO_SYNC_MESSAGE_CHANNEL_JS" file="webui/resources/sync_message_channel.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_IOS_MOJO_HANDLE_UTIL_JS" file="webui/resources/handle_util.js" flattenhtml="true" type="BINDATA" /> <include name="IDR_IOS_MOJO_HANDLE_UTIL_JS" file="webui/resources/handle_util.js" flattenhtml="true" type="BINDATA" />
<include name="IDR_IOS_MOJO_CORE_JS" file="webui/resources/core.js" flattenhtml="true" type="BINDATA" /> <include name="IDR_IOS_MOJO_CORE_JS" file="webui/resources/core.js" flattenhtml="true" type="BINDATA" />
......
...@@ -226,6 +226,7 @@ const char kScriptCommandPrefix[] = "webui"; ...@@ -226,6 +226,7 @@ const char kScriptCommandPrefix[] = "webui";
{mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS}, {mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS},
{mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS}, {mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS},
{mojo::kValidatorModuleName, IDR_MOJO_VALIDATOR_JS}, {mojo::kValidatorModuleName, IDR_MOJO_VALIDATOR_JS},
{web::kConsoleModuleName, IDR_IOS_CONSOLE_JS},
{web::kSyncMessageChannelModuleName, {web::kSyncMessageChannelModuleName,
IDR_IOS_MOJO_SYNC_MESSAGE_CHANNEL_JS}, IDR_IOS_MOJO_SYNC_MESSAGE_CHANNEL_JS},
{web::kHandleUtilModuleName, IDR_IOS_MOJO_HANDLE_UTIL_JS}, {web::kHandleUtilModuleName, IDR_IOS_MOJO_HANDLE_UTIL_JS},
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
namespace web { namespace web {
const char kConsoleModuleName[] = "console";
const char kSyncMessageChannelModuleName[] = const char kSyncMessageChannelModuleName[] =
"ios/mojo/public/js/sync_message_channel"; "ios/mojo/public/js/sync_message_channel";
const char kHandleUtilModuleName[] = "ios/mojo/public/js/handle_util"; const char kHandleUtilModuleName[] = "ios/mojo/public/js/handle_util";
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace web { namespace web {
extern const char kConsoleModuleName[];
extern const char kSyncMessageChannelModuleName[]; extern const char kSyncMessageChannelModuleName[];
extern const char kHandleUtilModuleName[]; extern const char kHandleUtilModuleName[];
extern const char kSupportModuleName[]; extern const char kSupportModuleName[];
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Module "console"
//
// This module provides basic logging support. The reason to define this module
// to forward calls to the |console| object exposed by the browser, instead of
// using that object directly: mojo JS bindings are currently loaded using gin
// and the |console| object is not always available. When the Mojo JS bindings
// move away from gin, this module could be removed.
define("console", [], function() {
/**
* Logs a message to the console.
* @param {string} message to log.
*/
function log(message) {
console.log(message);
}
var exports = {};
exports.log = log;
return exports;
});
...@@ -3,11 +3,12 @@ ...@@ -3,11 +3,12 @@
// found in the LICENSE file. // found in the LICENSE file.
define("mojo/public/js/router", [ define("mojo/public/js/router", [
"console",
"mojo/public/js/codec", "mojo/public/js/codec",
"mojo/public/js/core", "mojo/public/js/core",
"mojo/public/js/connector", "mojo/public/js/connector",
"mojo/public/js/validator", "mojo/public/js/validator",
], function(codec, core, connector, validator) { ], function(console, codec, core, connector, validator) {
var Connector = connector.Connector; var Connector = connector.Connector;
var MessageReader = codec.MessageReader; var MessageReader = codec.MessageReader;
...@@ -117,8 +118,12 @@ define("mojo/public/js/router", [ ...@@ -117,8 +118,12 @@ define("mojo/public/js/router", [
var reader = new MessageReader(message); var reader = new MessageReader(message);
var requestID = reader.requestID; var requestID = reader.requestID;
var completer = this.completers_.get(requestID); var completer = this.completers_.get(requestID);
this.completers_.delete(requestID); if (completer) {
completer.resolve(message); this.completers_.delete(requestID);
completer.resolve(message);
} else {
console.log("Unexpected response with request ID: " + requestID);
}
} else { } else {
if (this.incomingReceiver_) if (this.incomingReceiver_)
this.incomingReceiver_.accept(message); this.incomingReceiver_.accept(message);
...@@ -127,10 +132,12 @@ define("mojo/public/js/router", [ ...@@ -127,10 +132,12 @@ define("mojo/public/js/router", [
Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { Router.prototype.handleInvalidIncomingMessage_ = function(message, error) {
if (!this.testingController_) { if (!this.testingController_) {
// TODO(yzshen): Consider logging and notifying the embedder. // TODO(yzshen): Consider notifying the embedder.
// TODO(yzshen): This should also trigger connection error handler. // TODO(yzshen): This should also trigger connection error handler.
// Consider making accept() return a boolean and let the connector deal // Consider making accept() return a boolean and let the connector deal
// with this, as the C++ code does. // with this, as the C++ code does.
console.log("Invalid message: " + validator.validationError[error]);
this.close(); this.close();
return; return;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment