Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsResult.java

Issue 2209333004: Add serialization capabilities to PwsResult (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 package org.chromium.chrome.browser.physicalweb; 4 package org.chromium.chrome.browser.physicalweb;
5 5
6 import org.chromium.base.Log; 6 import org.chromium.base.Log;
7 7
8 import org.json.JSONException;
9 import org.json.JSONObject;
10
8 import java.net.MalformedURLException; 11 import java.net.MalformedURLException;
9 import java.net.URL; 12 import java.net.URL;
10 13
11 /** 14 /**
12 * A result from the Physical Web Server. 15 * A result from the Physical Web Server.
13 * 16 *
14 * This represents metadata about a URL retrieved from from a PWS response. It does not 17 * This represents metadata about a URL retrieved from from a PWS response. It does not
15 * necessarily represent one response as a single PWS response may include metad ata about multiple 18 * necessarily represent one response as a single PWS response may include metad ata about multiple
16 * URLs. 19 * URLs.
17 */ 20 */
18 class PwsResult { 21 class PwsResult {
19 private static final String TAG = "PhysicalWeb"; 22 private static final String TAG = "PhysicalWeb";
23 private static final String PAGE_INFO_KEY = "pageInfo";
24 private static final String REQUEST_URL_KEY = "scannedUrl";
25 private static final String SITE_URL_KEY = "resolvedUrl";
26 private static final String ICON_KEY = "icon";
27 private static final String TITLE_KEY = "title";
28 private static final String DESCRIPTION_KEY = "description";
29 private static final String GROUP_ID_KEY = "groupId";
20 30
21 /** 31 /**
22 * The URL that was set in the request to the PWS. 32 * The URL that was set in the request to the PWS.
23 */ 33 */
24 public final String requestUrl; 34 public final String requestUrl;
25 35
26 /** 36 /**
27 * The destination URL that the requestUrl redirects to. 37 * The destination URL that the requestUrl redirects to.
28 */ 38 */
29 public final String siteUrl; 39 public final String siteUrl;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 if (groupId == null) { 75 if (groupId == null) {
66 try { 76 try {
67 groupIdToSet = new URL(siteUrl).getHost() + title; 77 groupIdToSet = new URL(siteUrl).getHost() + title;
68 } catch (MalformedURLException e) { 78 } catch (MalformedURLException e) {
69 Log.e(TAG, "PwsResult created with a malformed URL", e); 79 Log.e(TAG, "PwsResult created with a malformed URL", e);
70 groupIdToSet = siteUrl + title; 80 groupIdToSet = siteUrl + title;
71 } 81 }
72 } 82 }
73 this.groupId = groupIdToSet; 83 this.groupId = groupIdToSet;
74 } 84 }
85
86 /**
87 * Creates a JSON object that represents this data structure.
88 * @return a JSON serialization of this data structure.
89 * @throws JSONException if the values cannot be deserialized.
90 */
91 public JSONObject jsonSerialize() throws JSONException {
92 return new JSONObject()
93 .put(REQUEST_URL_KEY, requestUrl)
94 .put(SITE_URL_KEY, siteUrl)
95 .put(PAGE_INFO_KEY, new JSONObject()
96 .put(ICON_KEY, iconUrl)
97 .put(TITLE_KEY, title)
98 .put(DESCRIPTION_KEY, description)
99 .put(GROUP_ID_KEY, groupId));
gone 2016/08/04 21:31:47 nit: Might be nicer if you pulled out the inner cr
100 }
101
102 /**
103 * Populates a PwsResult with data from a given JSON object.
104 * @param jsonObject a serialized PwsResult.
105 * @return The PwsResult represented by the serialized object.
106 * @throws JSONException if the values cannot be serialized.
107 */
108 public static PwsResult jsonDeserialize(JSONObject jsonObject) throws JSONEx ception {
109 JSONObject pageInfo = jsonObject.getJSONObject(PAGE_INFO_KEY);
110 return new PwsResult(
111 jsonObject.getString(REQUEST_URL_KEY),
112 jsonObject.getString(SITE_URL_KEY),
113 pageInfo.optString(ICON_KEY, null),
114 pageInfo.optString(TITLE_KEY, ""),
115 pageInfo.optString(DESCRIPTION_KEY, null),
116 pageInfo.optString(GROUP_ID_KEY, null));
117 }
75 } 118 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698