- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / web_view / common / cleardata / guest.html
1 <!doctype html>
2 <!--
3  * Copyright 2013 The Chromium Authors. All rights reserved.  Use of this
4  * source code is governed by a BSD-style license that can be found in the
5  * LICENSE file.
6 -->
7 <html>
8   <head>
9     <script type="text/javascript">
10       // A guest that stores and deletes cookies.
11       // Note that the embedder has to initiate a postMessage first so that
12       // the guest has a reference to the embedder's window.
13
14       // The window reference of the embedder to send post message reply.
15       var embedderWindowChannel = null;
16
17       var notifyEmbedder = function(msg_array) {
18         embedderWindowChannel.postMessage(JSON.stringify(msg_array), '*');
19       };
20
21       var SPLIT_RE_ = /\s*;\s*/;
22       var setCookie = function(name, value) { // Just a random future time.
23         var futureDate = new Date((+new Date) + 10000 * 1000);
24         document.cookie =
25             name + '=' + value + ';expires=' + futureDate.toUTCString();
26       };
27       var getCookie = function(name) {
28         var nameEq = name + '=';
29         var parts = (document.cookie || '').split(SPLIT_RE_);
30         for (var i = 0; i < parts.length; ++i) {
31           var part = parts[i];
32           if (part.lastIndexOf(nameEq, 0) == 0) {
33             return part.substr(nameEq.length);
34           }
35           if (part == name) {
36             return '';
37           }
38         }
39         return undefined;
40       };
41
42       var addCookies = function() {
43         var names = ['foo', 'bar'];
44         var values = ['foovalue', 'barvalue'];
45         for (var i = 0; i < names.length; ++i) {
46           window.console.log('setCookie: ' + names[i] + ' = ' + values[i]);
47           setCookie(names[i], values[i]);
48         }
49         notifyEmbedder(['step2.cookies-added']);
50       };
51
52       var onPostMessageReceived = function(e) {
53         embedderWindowChannel = e.source;
54         var data = JSON.parse(e.data);
55         if (data[0] == 'create-channel') {
56           window.console.log('guest: create-channel');
57           notifyEmbedder(['channel-created']);
58           return;
59         }
60
61         window.console.log('guest.onPostMessageReceived: ' + data[0]);
62         // Tests.
63         // These logs trigger event listeners in the embedder.
64         switch (data[0]) {
65           case 'step1.add-cookies':
66             window.console.log('guest.' + data[0]);
67             addCookies();
68             break;
69           case 'step3.get-cookies':
70             window.console.log('guest.' + data[0]);
71             var retValues = ['step4.got-cookies'];
72             var cookieValues = [];
73             for (var i = 1; i < data.length; ++i) {
74               cookieValues.push(getCookie(data[i]));
75             }
76             retValues.push(cookieValues);
77             notifyEmbedder(retValues);
78             break;
79           default:
80             break;
81         }
82       };
83       window.addEventListener('message', onPostMessageReceived, false);
84     </script>
85   </head>
86   <body>
87     <div>Guest that stores and retrieves certain cookies.</div>
88   </body>
89 </html>