1// Copyright 2013 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// Custom binding for the GCM API.
6
7var binding = require('binding').Binding.create('gcm');
8var forEach = require('utils').forEach;
9
10binding.registerCustomHook(function(bindingsAPI) {
11  var apiFunctions = bindingsAPI.apiFunctions;
12  var gcm = bindingsAPI.compiledApi;
13
14  apiFunctions.setUpdateArgumentsPostValidate(
15    'send', function(message, callback) {
16      // Validate message.data.
17      var payloadSize = 0;
18      forEach(message.data, function(property, value) {
19        if (property.length == 0)
20          throw new Error("One of data keys is empty.");
21
22        var lowerCasedProperty = property.toLowerCase();
23        // Issue an error for forbidden prefixes of property names.
24        if (lowerCasedProperty.indexOf("goog.") == 0 ||
25            lowerCasedProperty.indexOf("google") == 0 ||
26            property.indexOf("collapse_key") == 0) {
27          throw new Error("Invalid data key: " + property);
28        }
29
30        payloadSize += property.length + value.length;
31      });
32
33      if (payloadSize > gcm.MAX_MESSAGE_SIZE)
34        throw new Error("Payload exceeded allowed size limit. Payload size is: "
35            + payloadSize);
36
37      if (payloadSize == 0)
38        throw new Error("No data to send.");
39
40      return arguments;
41    });
42});
43
44exports.binding = binding.generate();
45