multiple window support in chrome packaged app

This is an investigation report for chrome.app.window. Right now I am working on drafting a proposal for multiple window support for both desktop and mobile.

* Overview

chrome.app.window in chrome packaged app [1] is used to create an application window in the event page [2] to show application UI.  It is a good case study for multiple window support on desktop. By use of app.window API, web developer could create a customized window with different options, including:
1.       Window show state: fullscreen, maximized, minized
2.       Window coordinate and size (also support create window in another external display by specifying the correct position)
3.       Frameless window without native title bar (developer could draw a customized titlebar with JS)

Besides, there are some events defined in app.window for listening window state changes, like
1.       When the window bounds is changed
2.       When the window show state is changed
3.       when the window is closed

A typical usage for app.window in event page (e.g. main.js):

// launchData might carry some launch data passed by Web Intents protocol, like command arguments for native application.
chrome.app.runtime.onLaunched.addListener(function(launchData) {
    chrome.app.window.create("index.html",  { "id": "mywindow", "width": 400, "height":300 }, function(win) {
         // win has AppWindow object.
   });
});

* Implementation Notes

>From the implementation viewport, the app window created by chrome.app.create is represented as AppWindow. It is partially built on top of DOM window object, for example, it wraps to use window.resizeTo, window.moveTo. The AppWindow also keeps DOM window object in its 'contentWindow' member.  For the current script context, chrome.app.window.current() can return an AppWindow object bound to the current script context, and it can also be called to a handle to the script context for another page, e.g. otherWindow.chrome.app.window.current().

A packaged app could create multiple windows by chrome.app.window.create. The window can be created in both event page and any JS context. The onSuspend event will be triggered to chrome.runtime when the last window is closed, and will wait for a couple of seconds to exit event page.

* Why not DOM window?

The advantages of creating a dedicated AppWindow object to represent the packaged app window are obvious instead of using DOM window object:
1.       The DOM window object is a global object in JS context. All objects created by Javascript are under the 'window' object. It actually acts as an execution context for a HTML page, not just an exact window concept. With a separate AppWindow object, it could be natural to map the object to the real world concept exactly.
2.       DOM window object can't meet the requirements similar as native app. It is originally designed for web world. For example, window show state support, frameless support etc.
3.       window.open is a synchronous API call which is not recommended by chrome.* API. The communication between multiple window might be difficult and not straightforward. If you call window.open in packaged app, 1) no window created for local URL, but can return a DOM window object; 2) open a tab in Chrome browser for a remote URL.

Thanks...Hongbo

Received on Thursday, 20 June 2013 09:34:48 UTC