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.
5 // Custom binding for the GCM API.
7 var binding
= require('binding').Binding
.create('gcm');
8 var forEach
= require('utils').forEach
;
10 binding
.registerCustomHook(function(bindingsAPI
) {
11 var apiFunctions
= bindingsAPI
.apiFunctions
;
12 var gcm
= bindingsAPI
.compiledApi
;
14 apiFunctions
.setUpdateArgumentsPostValidate(
15 'send', function(message
, callback
) {
16 // Validate message.data.
18 forEach(message
.data
, function(property
, value
) {
19 if (property
.length
== 0)
20 throw new Error("One of data keys is empty.");
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
);
30 payloadSize
+= property
.length
+ value
.length
;
33 if (payloadSize
> gcm
.MAX_MESSAGE_SIZE
)
34 throw new Error("Payload exceeded allowed size limit. Payload size is: "
38 throw new Error("No data to send.");
44 exports
.binding
= binding
.generate();