Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / resources / service_worker / serviceworker_internals.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 cr.define('serviceworker', function() {
6     'use strict';
7
8     function update() {
9         chrome.send('getAllRegistrations');
10     }
11
12     function progressNodeFor(link) {
13         return link.parentNode.querySelector('.operation-status');
14     }
15
16     // All commands are sent with the partition_path and scope, and
17     // are all completed with 'onOperationComplete'.
18     var COMMANDS = ['unregister', 'start', 'stop', 'sync'];
19     function commandHandler(command) {
20         return function(event) {
21             var link = event.target;
22             progressNodeFor(link).style.display = 'inline';
23             chrome.send(command, [link.partition_path,
24                                   link.scope]);
25             return false;
26         };
27     };
28
29     function withNode(selector, partition_path, scope, callback) {
30         var links = document.querySelectorAll(selector);
31         for (var i = 0; i < links.length; ++i) {
32             var link = links[i];
33             if (partition_path == link.partition_path &&
34                 scope == link.scope) {
35                 callback(link);
36             }
37         }
38     }
39
40     // Fired from the backend after the start call has completed
41     function onOperationComplete(status, path, scope) {
42         // refreshes the ui, displaying any relevant buttons
43         withNode('button', path, scope, function(link) {
44             progressNodeFor(link).style.display = 'none';
45         });
46         update();
47     }
48
49     var allLogMessages = {};
50
51     // Fired once per partition from the backend.
52     function onPartitionData(registrations, partition_id, partition_path) {
53         var template;
54         var container = $('serviceworker-list');
55
56         // Existing templates are keyed by partition_path. This allows
57         // the UI to be updated in-place rather than refreshing the
58         // whole page.
59         for (var i = 0; i < container.childNodes.length; ++i) {
60             if (container.childNodes[i].partition_path == partition_path) {
61                 template = container.childNodes[i];
62             }
63         }
64
65         // This is probably the first time we're loading.
66         if (!template) {
67             template = jstGetTemplate('serviceworker-list-template');
68             container.appendChild(template);
69         }
70
71         // Set log for each worker versions.
72         if (!(partition_id in allLogMessages)) {
73             allLogMessages[partition_id] = {};
74         }
75         var logMessages = allLogMessages[partition_id];
76         registrations.forEach(function (worker) {
77             [worker.active, worker.pending].forEach(function (version) {
78                 if (version) {
79                     if (version.version_id in logMessages) {
80                         version.log = logMessages[version.version_id];
81                     } else {
82                         version.log = '';
83                     }
84                 }
85             });
86         });
87
88         jstProcess(new JsEvalContext({ registrations: registrations,
89                                        partition_id: partition_id,
90                                        partition_path: partition_path}),
91                    template);
92         for (var i = 0; i < COMMANDS.length; ++i) {
93             var handler = commandHandler(COMMANDS[i]);
94             var links = container.querySelectorAll('button.' + COMMANDS[i]);
95             for (var j = 0; j < links.length; ++j) {
96                 if (!links[j].hasClickEvent) {
97                     links[j].addEventListener('click', handler, false);
98                     links[j].hasClickEvent = true;
99                 }
100             }
101         }
102     }
103
104     function onWorkerStarted(partition_id, version_id, process_id, thread_id) {
105         update();
106     }
107
108     function onWorkerStopped(partition_id, version_id, process_id, thread_id) {
109         update();
110     }
111
112     function onErrorReported(partition_id,
113                              version_id,
114                              process_id,
115                              thread_id,
116                              error_info) {
117         outputLogMessage(partition_id,
118                          version_id,
119                          'Error: ' + JSON.stringify(error_info) + '\n');
120     }
121
122     function onConsoleMessageReported(partition_id,
123                              version_id,
124                              process_id,
125                              thread_id,
126                              message) {
127         outputLogMessage(partition_id,
128                          version_id,
129                          'Console: ' + JSON.stringify(message) + '\n');
130     }
131
132     function onVersionStateChanged(partition_id, version_id) {
133         update();
134     }
135
136     function outputLogMessage(partition_id, version_id, message) {
137         if (!(partition_id in allLogMessages)) {
138             allLogMessages[partition_id] = {};
139         }
140         var logMessages = allLogMessages[partition_id];
141         if (version_id in logMessages) {
142             logMessages[version_id] += message;
143         } else {
144             logMessages[version_id] = message;
145         }
146
147         var logAreas =
148             document.querySelectorAll('textarea.serviceworker-log');
149         for (var i = 0; i < logAreas.length; ++i) {
150             var logArea = logAreas[i];
151             if (logArea.partition_id == partition_id &&
152                 logArea.version_id == version_id) {
153                 logArea.value += message;
154             }
155         }
156     }
157
158     return {
159         update: update,
160         onOperationComplete: onOperationComplete,
161         onPartitionData: onPartitionData,
162         onWorkerStarted: onWorkerStarted,
163         onWorkerStopped: onWorkerStopped,
164         onErrorReported: onErrorReported,
165         onConsoleMessageReported: onConsoleMessageReported,
166         onVersionStateChanged: onVersionStateChanged,
167     };
168 });
169
170 document.addEventListener('DOMContentLoaded', serviceworker.update);