OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.physicalweb; | 5 package org.chromium.chrome.browser.physicalweb; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.os.AsyncTask; | 8 import android.os.AsyncTask; |
9 | 9 |
10 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
11 import org.chromium.base.ThreadUtils; | 11 import org.chromium.base.ThreadUtils; |
| 12 import org.chromium.chrome.GoogleAPIKeys; |
| 13 import org.chromium.chrome.browser.ChromeVersionInfo; |
12 | 14 |
13 import org.json.JSONArray; | 15 import org.json.JSONArray; |
14 import org.json.JSONException; | 16 import org.json.JSONException; |
15 import org.json.JSONObject; | 17 import org.json.JSONObject; |
16 | 18 |
17 import java.net.MalformedURLException; | 19 import java.net.MalformedURLException; |
18 import java.util.ArrayList; | 20 import java.util.ArrayList; |
19 import java.util.Collection; | 21 import java.util.Collection; |
20 | 22 |
21 /** | 23 /** |
22 * This class sends requests to the Physical Web Service. | 24 * This class sends requests to the Physical Web Service. |
23 */ | 25 */ |
24 class PwsClient { | 26 class PwsClient { |
25 private static final String TAG = "PhysicalWeb"; | 27 private static final String TAG = "PhysicalWeb"; |
26 private static final String ENDPOINT_URL = "https://url-caster.appspot.com/r
esolve-scan"; | 28 private static final String ENDPOINT_URL = "https://test-physicalweb.sandbox
.googleapis.com/v1alpha1/urls:resolve"; |
27 | 29 |
28 /** | 30 /** |
29 * Callback that is run after the PWS sends a response to a resolve-scan req
uest. | 31 * Callback that is run after the PWS sends a response to a resolve-scan req
uest. |
30 */ | 32 */ |
31 public interface ResolveScanCallback { | 33 public interface ResolveScanCallback { |
32 /** | 34 /** |
33 * Handle newly returned PwsResults. | 35 * Handle newly returned PwsResults. |
34 * @param pwsResults The results returned by the PWS. | 36 * @param pwsResults The results returned by the PWS. |
35 */ | 37 */ |
36 public void onPwsResults(Collection<PwsResult> pwsResults); | 38 public void onPwsResults(Collection<PwsResult> pwsResults); |
37 } | 39 } |
38 | 40 |
39 /** | 41 /** |
40 * Callback that is run after receiving the response to an icon fetch reques
t. | 42 * Callback that is run after receiving the response to an icon fetch reques
t. |
41 */ | 43 */ |
42 public interface FetchIconCallback { | 44 public interface FetchIconCallback { |
43 /** | 45 /** |
44 * Handle newly returned favicon Bitmaps. | 46 * Handle newly returned favicon Bitmaps. |
45 * @param iconUrl The favicon URL. | 47 * @param iconUrl The favicon URL. |
46 * @param iconBitmap The icon image data. | 48 * @param iconBitmap The icon image data. |
47 */ | 49 */ |
48 public void onIconReceived(String iconUrl, Bitmap iconBitmap); | 50 public void onIconReceived(String iconUrl, Bitmap iconBitmap); |
49 } | 51 } |
50 | 52 |
| 53 private String apiKey() { |
| 54 if (ChromeVersionInfo.isStableBuild()) { |
| 55 return GoogleAPIKeys.GOOGLE_API_KEY; |
| 56 } else { |
| 57 return GoogleAPIKeys.GOOGLE_API_KEY_PHYSICAL_WEB_TEST; |
| 58 } |
| 59 } |
| 60 |
51 private static JSONObject createResolveScanPayload(Collection<String> urls) | 61 private static JSONObject createResolveScanPayload(Collection<String> urls) |
52 throws JSONException { | 62 throws JSONException { |
53 // Encode the urls. | 63 // Encode the urls. |
54 JSONArray objects = new JSONArray(); | 64 JSONArray objects = new JSONArray(); |
55 for (String url : urls) { | 65 for (String url : urls) { |
56 JSONObject obj = new JSONObject(); | 66 JSONObject obj = new JSONObject(); |
57 obj.put("url", url); | 67 obj.put("url", url); |
58 objects.put(obj); | 68 objects.put(obj); |
59 } | 69 } |
60 | 70 |
61 // Organize the data into a single object. | 71 // Organize the data into a single object. |
62 JSONObject jsonObject = new JSONObject(); | 72 JSONObject jsonObject = new JSONObject(); |
63 jsonObject.put("objects", objects); | 73 jsonObject.put("urls", objects); |
64 return jsonObject; | 74 return jsonObject; |
65 } | 75 } |
66 | 76 |
67 private static Collection<PwsResult> parseResolveScanResponse(JSONObject res
ult) { | 77 private static Collection<PwsResult> parseResolveScanResponse(JSONObject res
ult) { |
68 // Get the metadata array. | 78 // Get the metadata array. |
69 Collection<PwsResult> pwsResults = new ArrayList<>(); | 79 Collection<PwsResult> pwsResults = new ArrayList<>(); |
70 JSONArray metadata; | 80 JSONArray metadata; |
71 try { | 81 try { |
72 metadata = result.getJSONArray("metadata"); | 82 metadata = result.getJSONArray("metadata"); |
73 } catch (JSONException e) { | 83 } catch (JSONException e) { |
74 Log.e(TAG, "PWS returned invalid metadata", e); | 84 Log.e(TAG, "PWS returned invalid metadata", e); |
75 return pwsResults; | 85 return pwsResults; |
76 } | 86 } |
77 | 87 |
78 // Loop through the metadata for each url. | 88 // Loop through the metadata for each url. |
79 for (int i = 0; i < metadata.length(); i++) { | 89 for (int i = 0; i < metadata.length(); i++) { |
80 try { | 90 try { |
81 JSONObject obj = metadata.getJSONObject(i); | 91 JSONObject obj = metadata.getJSONObject(i); |
82 String requestUrl = obj.getString("id"); | 92 JSONObject pageInfo = obj.getJSONObject("pageInfo"); |
83 String siteUrl = obj.getString("url"); | 93 String scannedUrl = pageInfo.getString("scannedUrl"); |
84 String iconUrl = obj.optString("icon", null); | 94 String resolvedUrl = pageInfo.getString("resolvedUrl"); |
85 String title = obj.optString("title", ""); | 95 String iconUrl = pageInfo.optString("icon", null); |
86 String description = obj.optString("description", ""); | 96 String title = pageInfo.optString("title", ""); |
87 pwsResults.add(new PwsResult(requestUrl, siteUrl, iconUrl, title
, description)); | 97 String description = pageInfo.optString("description", ""); |
| 98 pwsResults.add(new PwsResult(scannedUrl, resolvedUrl, iconUrl, t
itle, description)); |
88 } catch (JSONException e) { | 99 } catch (JSONException e) { |
89 Log.e(TAG, "PWS returned invalid metadata", e); | 100 Log.e(TAG, "PWS returned invalid metadata", e); |
90 continue; | 101 continue; |
91 } | 102 } |
92 } | 103 } |
93 return pwsResults; | 104 return pwsResults; |
94 } | 105 } |
95 | 106 |
96 /** | 107 /** |
97 * Send an HTTP request to the PWS to resolve a set of URLs. | 108 * Send an HTTP request to the PWS to resolve a set of URLs. |
(...skipping 21 matching lines...) Expand all Loading... |
119 } | 130 } |
120 Log.e(TAG, "Error making request to PWS%s", httpErr, e); | 131 Log.e(TAG, "Error making request to PWS%s", httpErr, e); |
121 resolveScanCallback.onPwsResults(new ArrayList<PwsResult>()); | 132 resolveScanCallback.onPwsResults(new ArrayList<PwsResult>()); |
122 } | 133 } |
123 }; | 134 }; |
124 | 135 |
125 // Create the request. | 136 // Create the request. |
126 HttpRequest request = null; | 137 HttpRequest request = null; |
127 try { | 138 try { |
128 JSONObject payload = createResolveScanPayload(broadcastUrls); | 139 JSONObject payload = createResolveScanPayload(broadcastUrls); |
| 140 String url = ENDPOINT_URL + "?key=" + apiKey(); |
129 request = new JsonObjectHttpRequest(ENDPOINT_URL, payload, requestCa
llback); | 141 request = new JsonObjectHttpRequest(ENDPOINT_URL, payload, requestCa
llback); |
130 } catch (MalformedURLException e) { | 142 } catch (MalformedURLException e) { |
131 Log.e(TAG, "Error creating PWS HTTP request", e); | 143 Log.e(TAG, "Error creating PWS HTTP request", e); |
132 return; | 144 return; |
133 } catch (JSONException e) { | 145 } catch (JSONException e) { |
134 Log.e(TAG, "Error creating PWS JSON payload", e); | 146 Log.e(TAG, "Error creating PWS JSON payload", e); |
135 return; | 147 return; |
136 } | 148 } |
137 // The callback will be called on the main thread. | 149 // The callback will be called on the main thread. |
138 AsyncTask.THREAD_POOL_EXECUTOR.execute(request); | 150 AsyncTask.THREAD_POOL_EXECUTOR.execute(request); |
(...skipping 30 matching lines...) Expand all Loading... |
169 try { | 181 try { |
170 request = new BitmapHttpRequest(iconUrl, requestCallback); | 182 request = new BitmapHttpRequest(iconUrl, requestCallback); |
171 } catch (MalformedURLException e) { | 183 } catch (MalformedURLException e) { |
172 Log.e(TAG, "Error creating icon request", e); | 184 Log.e(TAG, "Error creating icon request", e); |
173 return; | 185 return; |
174 } | 186 } |
175 // The callback will be called on the main thread. | 187 // The callback will be called on the main thread. |
176 AsyncTask.THREAD_POOL_EXECUTOR.execute(request); | 188 AsyncTask.THREAD_POOL_EXECUTOR.execute(request); |
177 } | 189 } |
178 } | 190 } |
OLD | NEW |