1// Copyright (c) 2011 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// require cr.js
6// require cr/event_target.js
7// require cr/util.js
8
9/**
10 * Bridge between the browser and the page.
11 * In this file:
12 *   * define EventTargets to recieve message from the browser,
13 *   * dispatch browser messages to EventTarget,
14 *   * define interface to request data to the browser.
15 */
16
17cr.define('cr.quota', function() {
18  'use strict';
19
20  /**
21   * Post requestInfo message to Browser.
22   */
23  function requestInfo() {
24    chrome.send('requestInfo');
25  }
26
27  /**
28   * Callback entry point from Browser.
29   * Messages are Dispatched as Event to:
30   *   * onAvailableSpaceUpdated,
31   *   * onGlobalInfoUpdated,
32   *   * onPerHostInfoUpdated,
33   *   * onPerOriginInfoUpdated,
34   *   * onStatisticsUpdated.
35   * @param {string} message Message label. Possible Values are:
36   *   * 'AvailableSpaceUpdated',
37   *   * 'GlobalInfoUpdated',
38   *   * 'PerHostInfoUpdated',
39   *   * 'PerOriginInfoUpdated',
40   *   * 'StatisticsUpdated'.
41   * @param {Object} detail Message specific additional data.
42   */
43  function messageHandler(message, detail) {
44    var target = null;
45    switch (message) {
46      case 'AvailableSpaceUpdated':
47        target = cr.quota.onAvailableSpaceUpdated;
48        break;
49      case 'GlobalInfoUpdated':
50        target = cr.quota.onGlobalInfoUpdated;
51        break;
52      case 'PerHostInfoUpdated':
53        target = cr.quota.onPerHostInfoUpdated;
54        break;
55      case 'PerOriginInfoUpdated':
56        target = cr.quota.onPerOriginInfoUpdated;
57        break;
58      case 'StatisticsUpdated':
59        target = cr.quota.onStatisticsUpdated;
60        break;
61      default:
62        console.error('Unknown Message');
63        break;
64    }
65    if (target) {
66      var event = cr.doc.createEvent('CustomEvent');
67      event.initCustomEvent('update', false, false, detail);
68      target.dispatchEvent(event);
69    }
70  }
71
72  return {
73    onAvailableSpaceUpdated: new cr.EventTarget(),
74    onGlobalInfoUpdated: new cr.EventTarget(),
75    onPerHostInfoUpdated: new cr.EventTarget(),
76    onPerOriginInfoUpdated: new cr.EventTarget(),
77    onStatisticsUpdated: new cr.EventTarget(),
78
79    requestInfo: requestInfo,
80    messageHandler: messageHandler
81  };
82});
83