summaryrefslogtreecommitdiffstats
path: root/src/tools/nacldeployqt/template_qtloader.cpp
blob: fdf3d313adf05592901352138e3ea9725820840e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const char *templateQtLoader = R"STRING_DELIMITER(
// This script is generated by nacldeployqt as a part of the standard
// Qt deployment. It can be used either as-is or as a basis for a
// custom deployment solution. Note that some parts of this script
// (such as onMessage) is required by Qt.
//
// Usage:
//
// 1) Create and configure the Qt loader object:
//
//   var qtLoader = new QtLoader({
//       src : "myapp.nmf",
//       type : "application/pnacl",
//   });
//
//  Where 'myapp.nmf' is the nmf file for your application. Note that
//  relative paths are relative to the loading html file, not qtloader.js.
//
// 2) Create the Qt DOM element
//
//   var element = qtLoader.createElement();
//
// If you show this element it will display a standard Qt
// progress loader. (The alternative is to delay showing
// it until you get a 'load' event indicating load complete)
//
// 3) Connect load progress event listeners to the element
//
//
// 4) Start loading
//
//   qtLoader.load()
//
// This starts the download and pNaCl translation of the application
// binary.
//
// 5) Send end receive messages
//
//  element.postMessage(message) // ends up in pp::Instance::HandleMessage
//  element.addEventListener('message', function(message){ ... })
//
// Additional supported Qt constructor config keys:
//   query           : Query string. Qt will parse this for pp:Instance::Init() arguments
//   environment     : JavaScript object with pp::Instance::Init() arguments
//   isChromeApp     : enable Chrome Application package mode
//   loadText        : Loading placeholder text
//
// The pp:Instance::Init arguments are available in two ways:
//     1) As argn, argv key/value pairs to Init() of a QPepperInstance subclass.
//     2) As environment variables: toUpper(key)=value.

function QtLoader(config) {
    var self = this;
    self.config = config;
    self.loadText = config.loadText !== undefined ? config.loadText : "Loading Qt:"
    self.type = config.type !== undefined ? config.type : "%APPTYPE%"
    self.environment = config.environment;
    self.loadTextElement = undefined;
    self.embed = undefined;
    self.listener = undefined;
    self.debug = false;

// Utility function for splitting Url query parameters
// ("?Foo=bar&Bar=baz") into key-value pars.
function decodeQuery(query) {
    if (query === undefined)
        return {}
    var vars = query.split('&');
    var keyValues = {}
    for (var i = 0; i < vars.length; i++) {
        // split key/value on the first '='
        var parts = vars[i].split(/=(.+)/);
        var key = decodeURIComponent(parts[0]);
        var value = decodeURIComponent(parts[1]);
        if (key && key.length > 0)
            keyValues[key] = value;
    }
    return keyValues;
}

function copyToClipboard(content)
{
    var copySource = document.createElement("div");
    copySource.contentEditable = true;
    var activeElement = document.activeElement.appendChild(copySource).parentNode;
    copySource.innerText = content;
    copySource.focus();
    document.execCommand("Copy", null, null);
    activeElement.removeChild(copySource);
}

function pasteFromClipboard()
{
    var pasteTarget = document.createElement("div");
    pasteTarget.contentEditable = true;
    var activeElement = document.activeElement.appendChild(pasteTarget).parentNode;
    pasteTarget.focus();
    document.execCommand("Paste", null, null);
    var content = pasteTarget.innerText;
    activeElement.removeChild(pasteTarget);
    return content;
}

// Qt message handler
function onMessage(messageEvent)
{
    // Expect messages to be in the form "tag:message",
    // and that the tag has a handler installed in the
    // qtMessageHandlers object.
    //
    // As a special case, messages with the "qtEval" tag
    // are evaluated with eval(). This allows Qt to inject
    // javaScript into the web page, for example to install
    // message handlers.

    if (this.qtMessageHandlers === undefined) {
        this.qtMessageHandlers = {}
        // Install message handlers needed by Qt:
        this.qtMessageHandlers["qtGetAppVersion"] = function(url) {
            embed.postMessage("qtGetAppVersion: " + navigator.appVersion);
        };
        this.qtMessageHandlers["qtOpenUrl"] = function(url) {
            window.open(url);
        };
        this.qtMessageHandlers["qtClipboardRequestCopy"] = function(content) {
            console.log("copy to clipbard " + content);
            copyToClipboard(content);
        };
        this.qtMessageHandlers["qtClipboardRequestPaste"] = function() {
            var content = pasteFromClipboard();
            console.log("paste from clipboard " + content);
            embed.postMessage("qtClipboardtPaste: " + content);
        };
    }

    // Give the message handlers access to the nacl module.
    var embed = self.embed

    var parts = messageEvent.data.split(/:(.+)/);
    var tag = parts[0];
    var message = parts[1];

    // Handle "qt" messages
    if (tag == "qtEval") {
        // "eval()" is not allowed for Chrome Apps
        if (self.config.isChromeApp !== undefined && !self.config.isChromeApp)
            eval(message)
    } else {
        if (this.qtMessageHandlers[tag] !== undefined)
            this.qtMessageHandlers[tag](message);
    }
}

function onProgress(event)
{
    if (self.debug)
        console.log("progress " + event.loaded  + " " + event.total)

    if (event.total == 0) {
        self.loadTextNode.innerHTML = self.loadText;
        return;
    }

    // calculate and pretty print load percentage
    var progress = 100 * event.loaded / event.total;
    var progressString = progress.toPrecision(2)
    if (progress == 100)
        progressString = 100;

    // update loader text
    self.loadTextNode.innerHTML = self.loadText + " " + progressString + "%"
}

function onLoad(event)
{
    if (self.debug)
        console.log("load: NaCl execution started.");

    // hide the loader and show the embed
    self.loadTextNode.style.display = "none"
    self.embed.style.position = "static"
    self.embed.style.visibility = "visible"
}

function loadScript(src, onload)
{
    var script = document.createElement('script')
    script.src = src
    script.onload = function () { onload() };
    document.head.appendChild(script);
}

// Create Qt container element, possibly re-using existingElement
function createElement(existingElement)
{
    // Create container div
    self.listener = existingElement || document.createElement("div");
    self.listener.setAttribute("class", "qt-container");

    // Add load and progress event listeners
    var useCapture = true;
    self.listener.addEventListener('message', onMessage, useCapture);
    self.listener.addEventListener('progress', onProgress, useCapture);
    self.listener.addEventListener('load', onLoad, useCapture);

    // Add loading text placeholder to container
    self.loadTextNode = document.createElement("span");
    self.loadTextNode.setAttribute("class", "qt-progress");
    self.listener.appendChild(self.loadTextNode);

    // Check for (P)NaCl support
    var pnaclSupported = navigator.mimeTypes['application/x-pnacl'] !== undefined;
    if (!pnaclSupported && self.type != "emscripten")
        self.loadTextNode.innerHTML = "Sorry! Google Chrome is required.";

    return self.listener;
}

function load()
{
    if (self.type == "emscripten")
        loadEmscripten()
    else
        loadNaCl();
}

function loadNaCl()
{
    // Create NaCl <embed> element.
    var embed = document.createElement("EMBED");
    self.embed = embed;
    embed.setAttribute("class", "qt-embed");
    embed.setAttribute("src", self.config.src);
    embed.setAttribute("type", self.type);

    // hide the nacl embed while loading. 'display = "none"' makes
    // NaCl stop loading; remove the element from the layout and
    // hide instead
    embed.style.position = "absolute"
    embed.style.visibility = "hidden"

    // Decode and set URL query string values as attributes on the embed tag.
    // This allows passing for example ?QSG_VISUALIZE=overdraw
    var queryHash = decodeQuery(self.config.query);
    for (var key in queryHash) {
        if (key !== undefined)
            embed.setAttribute(key, queryHash[key])
    }

    // Propagate extra environment variables from QtLoader config.
    for (var key in self.environment) {
        if (key !== undefined)
            embed.setAttribute(key, self.environment[key])
    }

    self.listener.appendChild(embed);
    self.listener.embed = embed;
}

function loadEmscripten()
{
    var width = self.listener.offsetWidth
    var height = self.listener.offsetHeight

    var embed = document.createElement("div");
    embed.setAttribute("class", "qt-embed");
    self.listener.appendChild(embed);
    self.listener.embed = embed;

    loadScript(config.src, function(){
        CreateInstance(width, height, embed);
        embed.finishLoading();
    })
}

function postMessage(message) {
    self.embed.postMessage(message)
}

// return object with public API
return {
    createElement : createElement,
    load : load,
    postMessage : postMessage
}

} // function QtLoader
)STRING_DELIMITER";