Home | History | Annotate | Download | only in extensions
      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 
      7 var binding = require('binding').Binding.create('gcm');
      8 var forEach = require('utils').forEach;
      9 
     10 binding.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         // Issue an error for forbidden prefixes of property names.
     23         if (property.indexOf("goog.") == 0 ||
     24             property.indexOf("google") == 0) {
     25           throw new Error("Invalid data key: " + property);
     26         }
     27 
     28         payloadSize += property.length + value.length;
     29       });
     30 
     31       if (payloadSize > gcm.MAX_MESSAGE_SIZE)
     32         throw new Error("Payload exceeded allowed size limit. Payload size is: "
     33             + payloadSize);
     34 
     35       if (payloadSize == 0)
     36         throw new Error("No data to send.");
     37 
     38       return arguments;
     39     });
     40 });
     41 
     42 exports.binding = binding.generate();
     43