c5b19ca5a39a51a08a5dc5b9d125baab155e8a43
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / android2 / ProtocolIntents.java
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.ipc.invalidation.ticl.android2;
17
18 import com.google.ipc.invalidation.external.client.types.ErrorInfo;
19 import com.google.ipc.invalidation.ticl.proto.AndroidService.AndroidNetworkSendRequest;
20 import com.google.ipc.invalidation.ticl.proto.AndroidService.AndroidSchedulerEvent;
21 import com.google.ipc.invalidation.ticl.proto.AndroidService.ClientDowncall;
22 import com.google.ipc.invalidation.ticl.proto.AndroidService.ClientDowncall.AckDowncall;
23 import com.google.ipc.invalidation.ticl.proto.AndroidService.ClientDowncall.RegistrationDowncall;
24 import com.google.ipc.invalidation.ticl.proto.AndroidService.ClientDowncall.StartDowncall;
25 import com.google.ipc.invalidation.ticl.proto.AndroidService.ClientDowncall.StopDowncall;
26 import com.google.ipc.invalidation.ticl.proto.AndroidService.InternalDowncall;
27 import com.google.ipc.invalidation.ticl.proto.AndroidService.InternalDowncall.CreateClient;
28 import com.google.ipc.invalidation.ticl.proto.AndroidService.InternalDowncall.NetworkStatus;
29 import com.google.ipc.invalidation.ticl.proto.AndroidService.InternalDowncall.ServerMessage;
30 import com.google.ipc.invalidation.ticl.proto.AndroidService.ListenerUpcall;
31 import com.google.ipc.invalidation.ticl.proto.AndroidService.ListenerUpcall.ErrorUpcall;
32 import com.google.ipc.invalidation.ticl.proto.AndroidService.ListenerUpcall.InvalidateUpcall;
33 import com.google.ipc.invalidation.ticl.proto.AndroidService.ListenerUpcall.ReadyUpcall;
34 import com.google.ipc.invalidation.ticl.proto.AndroidService.ListenerUpcall.RegistrationFailureUpcall;
35 import com.google.ipc.invalidation.ticl.proto.AndroidService.ListenerUpcall.RegistrationStatusUpcall;
36 import com.google.ipc.invalidation.ticl.proto.AndroidService.ListenerUpcall.ReissueRegistrationsUpcall;
37 import com.google.ipc.invalidation.ticl.proto.Client.AckHandleP;
38 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.ClientConfigP;
39 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.InvalidationMessage;
40 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.InvalidationP;
41 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP;
42 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.Version;
43 import com.google.ipc.invalidation.util.Bytes;
44
45 import android.content.Intent;
46
47 import java.util.Collection;
48
49 /**
50  * Factory class for {@link Intent}s used between the application, Ticl, and listener in the
51  * Android Ticl.
52  *
53  */
54 public class ProtocolIntents {
55   /** Version of the on-device protocol. */
56   static final Version ANDROID_PROTOCOL_VERSION_VALUE = Version.create(1, 0);
57
58   /** Key of Intent byte[] extra holding a client downcall protocol buffer. */
59   public static final String CLIENT_DOWNCALL_KEY = "ipcinv-downcall";
60
61   /** Key of Intent byte[] extra holding an internal downcall protocol buffer. */
62   public static final String INTERNAL_DOWNCALL_KEY = "ipcinv-internal-downcall";
63
64   /** Key of Intent byte[] extra holding a listener upcall protocol buffer. */
65   public static final String LISTENER_UPCALL_KEY = "ipcinv-upcall";
66
67   /** Key of Intent byte[] extra holding a schedule event protocol buffer. */
68   public static final String SCHEDULER_KEY = "ipcinv-scheduler";
69
70   /** Key of Intent byte[] extra holding an outbound message protocol buffer. */
71   public static final String OUTBOUND_MESSAGE_KEY = "ipcinv-outbound-message";
72
73   /** Key of Intent byte[] extra holding an invalidation message protocol buffer. */
74   public static final String BACKGROUND_INVALIDATION_KEY = "ipcinv-background-inv";
75
76   /** Intents corresponding to calls on {@code InvalidationClient}. */
77   public static class ClientDowncalls {
78     public static Intent newStartIntent() {
79       Intent intent = new Intent();
80       intent.putExtra(CLIENT_DOWNCALL_KEY, ClientDowncall.createWithStart(
81           ANDROID_PROTOCOL_VERSION_VALUE, StartDowncall.DEFAULT_INSTANCE).toByteArray());
82       return intent;
83     }
84
85     public static Intent newStopIntent() {
86       Intent intent = new Intent();
87       intent.putExtra(CLIENT_DOWNCALL_KEY, ClientDowncall.createWithStop(
88           ANDROID_PROTOCOL_VERSION_VALUE, StopDowncall.DEFAULT_INSTANCE).toByteArray());
89       return intent;
90     }
91
92     public static Intent newAcknowledgeIntent(byte[] ackHandleData) {
93       AckDowncall ackDowncall = AckDowncall.create(new Bytes(ackHandleData));
94       Intent intent = new Intent();
95       intent.putExtra(CLIENT_DOWNCALL_KEY, ClientDowncall.createWithAck(
96           ANDROID_PROTOCOL_VERSION_VALUE, ackDowncall).toByteArray());
97       return intent;
98     }
99
100     public static Intent newRegistrationIntent(Collection<ObjectIdP> registrations) {
101       RegistrationDowncall regDowncall =
102           RegistrationDowncall.createWithRegistrations(registrations);
103       Intent intent = new Intent();
104       intent.putExtra(CLIENT_DOWNCALL_KEY, ClientDowncall.createWithRegistrations(
105           ANDROID_PROTOCOL_VERSION_VALUE, regDowncall).toByteArray());
106       return intent;
107     }
108
109     public static Intent newUnregistrationIntent(Collection<ObjectIdP> unregistrations) {
110       RegistrationDowncall regDowncall =
111           RegistrationDowncall.createWithUnregistrations(unregistrations);
112       Intent intent = new Intent();
113       intent.putExtra(CLIENT_DOWNCALL_KEY, ClientDowncall.createWithRegistrations(
114           ANDROID_PROTOCOL_VERSION_VALUE, regDowncall).toByteArray());
115       return intent;
116     }
117
118     private ClientDowncalls() {
119       // Disallow instantiation.
120     }
121   }
122
123   /** Intents for non-public calls on the Ticl (currently, network-related calls. */
124   public static class InternalDowncalls {
125     public static Intent newServerMessageIntent(Bytes serverMessage) {
126       Intent intent = new Intent();
127       intent.putExtra(INTERNAL_DOWNCALL_KEY, InternalDowncall.createWithServerMessage(
128           ANDROID_PROTOCOL_VERSION_VALUE, ServerMessage.create(serverMessage))
129           .toByteArray());
130       return intent;
131     }
132
133     public static Intent newNetworkStatusIntent(Boolean status) {
134       Intent intent = new Intent();
135       intent.putExtra(INTERNAL_DOWNCALL_KEY, InternalDowncall.createWithNetworkStatus(
136           ANDROID_PROTOCOL_VERSION_VALUE, NetworkStatus.create(status)).toByteArray());
137       return intent;
138     }
139
140     public static Intent newNetworkAddrChangeIntent() {
141       Intent intent = new Intent();
142       intent.putExtra(INTERNAL_DOWNCALL_KEY, InternalDowncall.createWithNetworkAddrChange(
143           ANDROID_PROTOCOL_VERSION_VALUE, true).toByteArray());
144       return intent;
145     }
146
147     public static Intent newCreateClientIntent(int clientType, Bytes clientName,
148         ClientConfigP config, boolean skipStartForTest) {
149       CreateClient createClient =
150           CreateClient.create(clientType, clientName, config, skipStartForTest);
151       Intent intent = new Intent();
152       intent.putExtra(INTERNAL_DOWNCALL_KEY, InternalDowncall.createWithCreateClient(
153           ANDROID_PROTOCOL_VERSION_VALUE, createClient).toByteArray());
154       return intent;
155     }
156
157     private InternalDowncalls() {
158       // Disallow instantiation.
159     }
160   }
161
162   /** Intents corresponding to calls on {@code InvalidationListener}. */
163   public static class ListenerUpcalls {
164     public static Intent newReadyIntent() {
165       Intent intent = new Intent();
166       intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithReady(
167           ANDROID_PROTOCOL_VERSION_VALUE, ReadyUpcall.DEFAULT_INSTANCE).toByteArray());
168       return intent;
169     }
170
171     public static Intent newInvalidateIntent(InvalidationP invalidation, AckHandleP ackHandle) {
172       Intent intent = new Intent();
173       InvalidateUpcall invUpcall =
174           InvalidateUpcall.createWithInvalidation(new Bytes(ackHandle.toByteArray()), invalidation);
175       intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithInvalidate(
176           ANDROID_PROTOCOL_VERSION_VALUE, invUpcall).toByteArray());
177       return intent;
178     }
179
180     public static Intent newInvalidateUnknownIntent(ObjectIdP object, AckHandleP ackHandle) {
181       Intent intent = new Intent();
182       InvalidateUpcall invUpcall =
183           InvalidateUpcall.createWithInvalidateUnknown(new Bytes(ackHandle.toByteArray()), object);
184       intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithInvalidate(
185           ANDROID_PROTOCOL_VERSION_VALUE, invUpcall).toByteArray());
186       return intent;
187     }
188
189     public static Intent newInvalidateAllIntent(AckHandleP ackHandle) {
190       Intent intent = new Intent();
191       InvalidateUpcall invUpcall = InvalidateUpcall.createWithInvalidateAll(
192           new Bytes(ackHandle.toByteArray()), true);
193       intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithInvalidate(
194           ANDROID_PROTOCOL_VERSION_VALUE, invUpcall).toByteArray());
195       return intent;
196     }
197
198     public static Intent newRegistrationStatusIntent(ObjectIdP object, boolean isRegistered) {
199       Intent intent = new Intent();
200       RegistrationStatusUpcall regUpcall = RegistrationStatusUpcall.create(object, isRegistered);
201       intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithRegistrationStatus(
202           ANDROID_PROTOCOL_VERSION_VALUE, regUpcall).toByteArray());
203       return intent;
204     }
205
206     public static Intent newRegistrationFailureIntent(ObjectIdP object, boolean isTransient,
207         String message) {
208       Intent intent = new Intent();
209       RegistrationFailureUpcall regUpcall =
210           RegistrationFailureUpcall.create(object, isTransient, message);
211       intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithRegistrationFailure(
212           ANDROID_PROTOCOL_VERSION_VALUE, regUpcall).toByteArray());
213       return intent;
214     }
215
216     public static Intent newReissueRegistrationsIntent(byte[] prefix, int length) {
217       Intent intent = new Intent();
218       ReissueRegistrationsUpcall reissueRegistrations =
219           ReissueRegistrationsUpcall.create(new Bytes(prefix), length);
220       intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithReissueRegistrations(
221           ANDROID_PROTOCOL_VERSION_VALUE, reissueRegistrations).toByteArray());
222       return intent;
223     }
224
225     public static Intent newErrorIntent(ErrorInfo errorInfo) {
226       Intent intent = new Intent();
227       ErrorUpcall errorUpcall = ErrorUpcall.create(errorInfo.getErrorReason(),
228           errorInfo.getErrorMessage(), errorInfo.isTransient());
229       intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithError(
230           ANDROID_PROTOCOL_VERSION_VALUE, errorUpcall).toByteArray());
231       return intent;
232     }
233
234     private ListenerUpcalls() {
235       // Disallow instantiation.
236     }
237   }
238
239   /** Returns a new intent encoding a request to execute the scheduled action {@code eventName}. */
240   public static Intent newSchedulerIntent(String eventName, long ticlId) {
241     byte[] eventBytes = AndroidSchedulerEvent.create(ANDROID_PROTOCOL_VERSION_VALUE, eventName,
242         ticlId).toByteArray();
243     return new Intent().putExtra(SCHEDULER_KEY, eventBytes);
244   }
245
246   /** Returns a new intent encoding a message to send to the data center. */
247   public static Intent newOutboundMessageIntent(byte[] message) {
248     byte[] payloadBytes = AndroidNetworkSendRequest.create(ANDROID_PROTOCOL_VERSION_VALUE,
249         new Bytes(message)).toByteArray();
250     return new Intent().putExtra(OUTBOUND_MESSAGE_KEY, payloadBytes);
251   }
252
253   /** Returns a new intent encoding background invalidation messages. */
254   public static Intent newBackgroundInvalidationIntent(InvalidationMessage invalidationMessage) {
255     byte[] payloadBytes = invalidationMessage.toByteArray();
256     return new Intent().putExtra(BACKGROUND_INVALIDATION_KEY, payloadBytes);
257   }
258
259   private ProtocolIntents() {
260     // Disallow instantiation.
261   }
262 }