summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/quota_internals/message_dispatcher.js
blob: 7088a6842a2d9e06a16c6280e4c46b61952ddad4 (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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// require cr.js
// require cr/event_target.js
// require cr/util.js

/**
 * Bridge between the browser and the page.
 * In this file:
 *   * define EventTargets to receive message from the browser,
 *   * dispatch browser messages to EventTarget,
 *   * define interface to request data to the browser.
 */

cr.define('cr.quota', function() {
  'use strict';

  /**
   * Post requestInfo message to Browser.
   */
  function requestInfo() {
    chrome.send('requestInfo');
  }

  /**
   * Callback entry point from Browser.
   * Messages are Dispatched as Event to:
   *   * onAvailableSpaceUpdated,
   *   * onGlobalInfoUpdated,
   *   * onPerHostInfoUpdated,
   *   * onPerOriginInfoUpdated,
   *   * onStatisticsUpdated.
   * @param {string} message Message label. Possible Values are:
   *   * 'AvailableSpaceUpdated',
   *   * 'GlobalInfoUpdated',
   *   * 'PerHostInfoUpdated',
   *   * 'PerOriginInfoUpdated',
   *   * 'StatisticsUpdated'.
   * @param {Object} detail Message specific additional data.
   */
  function messageHandler(message, detail) {
    var target = null;
    switch (message) {
      case 'AvailableSpaceUpdated':
        target = cr.quota.onAvailableSpaceUpdated;
        break;
      case 'GlobalInfoUpdated':
        target = cr.quota.onGlobalInfoUpdated;
        break;
      case 'PerHostInfoUpdated':
        target = cr.quota.onPerHostInfoUpdated;
        break;
      case 'PerOriginInfoUpdated':
        target = cr.quota.onPerOriginInfoUpdated;
        break;
      case 'StatisticsUpdated':
        target = cr.quota.onStatisticsUpdated;
        break;
      default:
        console.error('Unknown Message');
        break;
    }
    if (target) {
      var event = cr.doc.createEvent('CustomEvent');
      event.initCustomEvent('update', false, false, detail);
      target.dispatchEvent(event);
    }
  }

  return {
    onAvailableSpaceUpdated: new cr.EventTarget(),
    onGlobalInfoUpdated: new cr.EventTarget(),
    onPerHostInfoUpdated: new cr.EventTarget(),
    onPerOriginInfoUpdated: new cr.EventTarget(),
    onStatisticsUpdated: new cr.EventTarget(),

    requestInfo: requestInfo,
    messageHandler: messageHandler
  };
});