1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A guest that logs console messages.
6 // Note that the embedder has to initiate a postMessage first so that
7 // the guest has a reference to the embedder's window.
8 window.console.log('Guest script loading.');
10 // The window reference of the embedder to send post message reply.
11 var embedderWindowChannel = null;
13 // A value that uniquely identifies the guest sending the messages to the
16 var notifyEmbedder = function(msg_array) {
17 var msg = msg_array.concat([channelId]);
18 embedderWindowChannel.postMessage(JSON.stringify(msg), '*');
21 var onPostMessageReceived = function(e) {
22 embedderWindowChannel = e.source;
23 var data = JSON.parse(e.data);
24 if (data[0] == 'create-channel') {
25 window.console.log('aaa. guest: create-channel');
27 notifyEmbedder(['channel-created']);
32 // These logs trigger event listeners in the embedder.
35 window.console.log('log-one'); // LevelLog = 0.
38 window.console.warn('log-two'); // LevelWarning = 1.
41 window.console.error('log-three'); // LevelDebug = 2.
44 window.console.debug('log-four'); // LevelError = -1.
47 throw Error('log-five');
52 window.addEventListener('message', onPostMessageReceived, false);