1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)function makeURL(toolchain, config) {
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return 'index.html?tc=' + toolchain + '&config=' + config;
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)function createWindow(url) {
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  console.log('loading ' + url);
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  chrome.app.window.create(url, {
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    width: 1024,
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    height: 800,
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    frame: 'none'
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  });
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)function onLaunched(launchData) {
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Send and XHR to get the URL to load from a configuration file.
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Normally you won't need to do this; just call:
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // chrome.app.window.create('<your url>', {...});
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // In the SDK we want to be able to load different URLs (for different
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // toolchain/config combinations) from the commandline, so we to read
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // this information from the file "run_package_config".
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  var xhr = new XMLHttpRequest();
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  xhr.open('GET', 'run_package_config', true);
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  xhr.onload = function() {
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    var toolchain_config = this.responseText.split(' ');
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    createWindow(makeURL.apply(null, toolchain_config));
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  };
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  xhr.onerror = function() {
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Can't find the config file, just load the default.
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    createWindow('index.html');
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  };
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  xhr.send();
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)chrome.app.runtime.onLaunched.addListener(onLaunched);
41