Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / extensions / test / data / web_view / accept_touch_events / guest.js
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
3 // found in the LICENSE file.
4
5 var LOG = function(msg) {
6   window.console.log(msg);
7 };
8
9 var touchDiv;
10 var embedder;
11
12 function init() {
13   touchDiv = document.createElement('div');
14   touchDiv.innerText = 'With touch';
15   document.body.appendChild(touchDiv);
16 }
17
18 function handler() {}
19
20 function onAppCommand(command) {
21   LOG('onAppCommand, command = ' + command);
22   switch (command) {
23     case 'install-touch-handler':
24       touchDiv.addEventListener('touchstart', handler);
25       sendMessageToEmbedder('installed-touch-handler');
26       break;
27     case 'uninstall-touch-handler':
28       touchDiv.removeEventListener('touchstart', handler);
29       sendMessageToEmbedder('uninstalled-touch-handler');
30       break;
31   }
32 };
33
34 function sendMessageToEmbedder(message) {
35   if (!embedder) {
36      LOG('no embedder channel to send postMessage');
37      return;
38   }
39
40   embedder.postMessage(JSON.stringify([message]), '*');
41 }
42
43 window.addEventListener('message', function(e) {
44   embedder = e.source;
45   var data = JSON.parse(e.data);
46   if (data[0] == 'connect') {
47     sendMessageToEmbedder('connected');
48   }
49 });
50
51 window.onload = init;