Chromium Code Reviews| Index: components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/GCDClient.java |
| diff --git a/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/GCDClient.java b/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/GCDClient.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f0ea934cebc94eb428740093f2a51704d8995224 |
| --- /dev/null |
| +++ b/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/GCDClient.java |
| @@ -0,0 +1,161 @@ |
| +// Copyright 2014 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. |
| + |
| +package org.chromium.components.devtools_bridge.apiary; |
| + |
| +import android.util.JsonReader; |
| + |
| +import org.apache.http.HttpResponse; |
| +import org.apache.http.client.HttpClient; |
| +import org.apache.http.client.HttpResponseException; |
| +import org.apache.http.client.ResponseHandler; |
| +import org.apache.http.client.methods.HttpDelete; |
| +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
| +import org.apache.http.client.methods.HttpPost; |
| +import org.apache.http.client.methods.HttpRequestBase; |
| +import org.apache.http.entity.StringEntity; |
| + |
| +import org.chromium.components.devtools_bridge.gcd.InstanceCredential; |
| +import org.chromium.components.devtools_bridge.gcd.InstanceDescription; |
| +import org.chromium.components.devtools_bridge.gcd.MessageReader; |
| +import org.chromium.components.devtools_bridge.gcd.MessageWriter; |
| + |
| +import java.io.IOException; |
| +import java.io.UnsupportedEncodingException; |
| +import java.net.URI; |
| + |
| +/** |
| + * Client for accessing GCD API. |
| + */ |
| +public class GCDClient { |
| + private static final String API_BASE = "https://www.googleapis.com/clouddevices/v1"; |
| + public static final String ENCODING = "UTF-8"; |
| + protected static final String CONTENT_TYPE = "application/json; charset=" + ENCODING; |
| + |
| + private final HttpClient mHttpClient; |
| + private final String mAPIKey; |
| + private final String mOAuthToken; |
| + |
| + GCDClient(HttpClient httpClient, String apiKey, String oAuthToken) { |
| + mHttpClient = httpClient; |
| + mAPIKey = apiKey; |
| + mOAuthToken = oAuthToken; |
| + } |
| + |
| + GCDClient(HttpClient httpClient, String apiKey) { |
| + this(httpClient, apiKey, null); |
| + } |
| + |
| + /** |
| + * Creation registration ticket if the first step in instance registration. Client must have |
|
mnaganov (inactive)
2014/10/30 18:18:21
nit: The first sentence is incomprehensible. Shoul
SeRya
2014/10/31 07:40:16
Done.
|
| + * user credentials. If it registered it will be associated with the user. Next step is patching |
|
mnaganov (inactive)
2014/10/30 18:18:21
nit: Again, the sentence isn't very clear. Should
SeRya
2014/10/31 07:40:16
Done.
|
| + * registration ticket. |
|
mnaganov (inactive)
2014/10/30 18:18:21
nit: "patching registration ticket" -> "registrati
SeRya
2014/10/31 07:40:16
Done.
|
| + */ |
| + public String createRegistrationTicket() throws IOException { |
| + assert mOAuthToken != null; |
| + |
| + return mHttpClient.execute( |
| + newHttpPost("/registrationTickets", "{\"userEmail\":\"me\"}"), |
| + new JsonResponseHandler<String>() { |
| + @Override |
| + public String readResponse(JsonReader reader) throws IOException { |
| + return new MessageReader(reader).readTicketId(); |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * Patching registration ticket. GCD gets device definition including commands metadata, |
| + * GCM channel description and instance user-visible name. |
|
mnaganov (inactive)
2014/10/30 18:18:21
nit: user-visible instance name
SeRya
2014/10/31 07:40:16
Done.
|
| + */ |
| + public void patchRegistrationTicket(String ticketId, InstanceDescription description) |
| + throws IOException { |
| + String content = new MessageWriter().writeTicketPatch(description).close().toString(); |
| + |
| + mHttpClient.execute( |
| + newHttpPatch("/registrationTickets/" + ticketId, content), |
| + new EmptyResponseHandler()); |
| + } |
| + |
| + /** |
| + * Finalizing registration. Client must be anonymous (GCD requirement). GCD provides |
| + * instance credentials needed for handling commands. |
| + */ |
| + public InstanceCredential finalizeRegistration(String ticketId) throws IOException { |
| + return mHttpClient.execute( |
| + newHttpPost("/registrationTickets/" + ticketId + "/finalize", ""), |
| + new JsonResponseHandler<InstanceCredential>() { |
| + @Override |
| + public InstanceCredential readResponse(JsonReader reader) throws IOException { |
| + return new MessageReader(reader).readInstanceCredential(); |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * Delete registered instance (unregisters). If client has instance credentials then |
|
mnaganov (inactive)
2014/10/30 18:18:21
nit: Deletes a ...
SeRya
2014/10/31 07:40:16
Done.
|
| + * instanceId must be it's own ID. If client has user credentials then instance must belong |
| + * to the user. |
| + */ |
| + public void deleteInstance(String instanceId) throws IOException { |
| + mHttpClient.execute( |
| + newHttpDelete("/devices/" + instanceId), |
| + new EmptyResponseHandler()); |
| + } |
| + |
| + private HttpPost newHttpPost(String path, String content) throws UnsupportedEncodingException { |
| + HttpPost request = new HttpPost(buildUrl(path)); |
| + setContent(request, content); |
| + initializeRequest(request); |
| + return request; |
| + } |
| + |
| + private HttpPatch newHttpPatch(String path, String content) |
| + throws UnsupportedEncodingException { |
| + HttpPatch request = new HttpPatch(buildUrl(path)); |
| + setContent(request, content); |
| + initializeRequest(request); |
| + return request; |
| + } |
| + |
| + private HttpDelete newHttpDelete(String path) { |
| + HttpDelete request = new HttpDelete(buildUrl(path)); |
| + initializeRequest(request); |
| + return request; |
| + } |
| + |
| + private String buildUrl(String path) { |
| + return API_BASE + path + "?key=" + mAPIKey; |
| + } |
| + |
| + private void setContent(HttpEntityEnclosingRequestBase request, String content) |
| + throws UnsupportedEncodingException { |
| + request.setEntity(new StringEntity(content, ENCODING)); |
| + request.addHeader("Content-Type", CONTENT_TYPE); |
| + } |
| + |
| + private void initializeRequest(HttpRequestBase request) { |
| + if (mOAuthToken != null) { |
| + request.addHeader("Authorization", "Bearer " + mOAuthToken); |
| + } |
| + } |
| + |
| + private static final class HttpPatch extends HttpEntityEnclosingRequestBase { |
| + public HttpPatch(String uri) { |
| + setURI(URI.create(uri)); |
| + } |
| + |
| + public String getMethod() { |
| + return "PATCH"; |
| + } |
| + } |
| + |
| + private static class EmptyResponseHandler implements ResponseHandler<Void> { |
| + @Override |
| + public Void handleResponse(HttpResponse response) throws HttpResponseException { |
| + JsonResponseHandler.checkStatus(response); |
| + return null; |
| + } |
| + } |
| +} |