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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_parser.cc

Issue 10546128: Reland after revert - Adds parsing for the app_id field from an (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/browser/chromeos/gdata/gdata_parser.h" 5 #include "chrome/browser/chromeos/gdata/gdata_parser.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/json/json_value_converter.h" 9 #include "base/json/json_value_converter.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 const char kHrefAttr[] = "href"; 117 const char kHrefAttr[] = "href";
118 const char kLabelAttr[] = "label"; 118 const char kLabelAttr[] = "label";
119 const char kNameAttr[] = "name"; 119 const char kNameAttr[] = "name";
120 const char kRelAttr[] = "rel"; 120 const char kRelAttr[] = "rel";
121 const char kSchemeAttr[] = "scheme"; 121 const char kSchemeAttr[] = "scheme";
122 const char kSrcAttr[] = "src"; 122 const char kSrcAttr[] = "src";
123 const char kTermAttr[] = "term"; 123 const char kTermAttr[] = "term";
124 const char kTypeAttr[] = "type"; 124 const char kTypeAttr[] = "type";
125 const char kValueAttr[] = "value"; 125 const char kValueAttr[] = "value";
126 126
127 // Link Prefixes
128 const char kOpenWithPrefix[] = "http://schemas.google.com/docs/2007#open-with-";
129 const size_t kOpenWithPrefixSize = arraysize(kOpenWithPrefix) - 1;
130
127 struct EntryKindMap { 131 struct EntryKindMap {
128 DocumentEntry::EntryKind kind; 132 DocumentEntry::EntryKind kind;
129 const char* entry; 133 const char* entry;
130 const char* extension; 134 const char* extension;
131 }; 135 };
132 136
133 const EntryKindMap kEntryKindMap[] = { 137 const EntryKindMap kEntryKindMap[] = {
134 { DocumentEntry::ITEM, "item", NULL}, 138 { DocumentEntry::ITEM, "item", NULL},
135 { DocumentEntry::DOCUMENT, "document", ".gdoc"}, 139 { DocumentEntry::DOCUMENT, "document", ".gdoc"},
136 { DocumentEntry::SPREADSHEET, "spreadsheet", ".gsheet"}, 140 { DocumentEntry::SPREADSHEET, "spreadsheet", ".gsheet"},
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 } while (depth == xml_reader->Depth() && (skip_read || xml_reader->Next())); 273 } while (depth == xml_reader->Depth() && (skip_read || xml_reader->Next()));
270 return author; 274 return author;
271 } 275 }
272 276
273 //////////////////////////////////////////////////////////////////////////////// 277 ////////////////////////////////////////////////////////////////////////////////
274 // Link implementation 278 // Link implementation
275 279
276 Link::Link() : type_(Link::UNKNOWN) { 280 Link::Link() : type_(Link::UNKNOWN) {
277 } 281 }
278 282
283 Link::~Link() {
284 }
285
286 // static
287 bool Link::GetAppID(const base::StringPiece& rel, std::string* app_id) {
288 DCHECK(app_id);
289 // Fast return path if the link clearly isn't an OPEN_WITH link.
290 if (rel.size() < kOpenWithPrefixSize) {
291 app_id->clear();
292 return true;
293 }
294
295 const std::string kOpenWithPrefixStr(kOpenWithPrefix);
296 if (StartsWithASCII(rel.as_string(), kOpenWithPrefixStr, false)) {
297 *app_id = rel.as_string().substr(kOpenWithPrefixStr.size());
298 return true;
299 }
300
301 app_id->clear();
302 return true;
303 }
304
279 // static. 305 // static.
280 bool Link::GetLinkType(const base::StringPiece& rel, Link::LinkType* result) { 306 bool Link::GetLinkType(const base::StringPiece& rel, Link::LinkType* type) {
307 DCHECK(type);
281 for (size_t i = 0; i < arraysize(kLinkTypeMap); i++) { 308 for (size_t i = 0; i < arraysize(kLinkTypeMap); i++) {
282 if (rel == kLinkTypeMap[i].rel) { 309 if (rel == kLinkTypeMap[i].rel) {
283 *result = kLinkTypeMap[i].type; 310 *type = kLinkTypeMap[i].type;
284 return true; 311 return true;
285 } 312 }
286 } 313 }
314
315 // OPEN_WITH links have extra information at the end of the rel that is unique
316 // for each one, so we can't just check the usual map. This check is slightly
317 // redundant to provide a quick skip if it's obviously not an OPEN_WITH url.
318 if (rel.size() >= kOpenWithPrefixSize &&
319 StartsWithASCII(rel.as_string(), kOpenWithPrefix, false)) {
320 *type = OPEN_WITH;
321 return true;
322 }
323
287 // Let unknown link types through, just report it; if the link type is needed 324 // Let unknown link types through, just report it; if the link type is needed
288 // in the future, add it into LinkType and kLinkTypeMap. 325 // in the future, add it into LinkType and kLinkTypeMap.
289 DVLOG(1) << "Ignoring unknown link type for rel " << rel; 326 DVLOG(1) << "Ignoring unknown link type for rel " << rel;
290 *result = UNKNOWN; 327 *type = UNKNOWN;
291 return true; 328 return true;
292 } 329 }
293 330
294 // static 331 // static
295 void Link::RegisterJSONConverter(base::JSONValueConverter<Link>* converter) { 332 void Link::RegisterJSONConverter(base::JSONValueConverter<Link>* converter) {
296 converter->RegisterCustomField<Link::LinkType>( 333 converter->RegisterCustomField<Link::LinkType>(kRelField,
297 kRelField, &Link::type_, &Link::GetLinkType); 334 &Link::type_,
335 &Link::GetLinkType);
336 // We have to register kRelField twice because we extract two different pieces
337 // of data from the same rel field.
338 converter->RegisterCustomField<std::string>(kRelField,
339 &Link::app_id_,
340 &Link::GetAppID);
298 converter->RegisterCustomField(kHrefField, &Link::href_, &GetGURLFromString); 341 converter->RegisterCustomField(kHrefField, &Link::href_, &GetGURLFromString);
299 converter->RegisterStringField(kTitleField, &Link::title_); 342 converter->RegisterStringField(kTitleField, &Link::title_);
300 converter->RegisterStringField(kTypeField, &Link::mime_type_); 343 converter->RegisterStringField(kTypeField, &Link::mime_type_);
301 } 344 }
302 345
303 // static. 346 // static.
304 Link* Link::CreateFromXml(XmlReader* xml_reader) { 347 Link* Link::CreateFromXml(XmlReader* xml_reader) {
305 if (xml_reader->NodeName() != kLinkNode) 348 if (xml_reader->NodeName() != kLinkNode)
306 return NULL; 349 return NULL;
307 350
308 Link* link = new Link(); 351 Link* link = new Link();
309 xml_reader->NodeAttribute(kTypeAttr, &link->mime_type_); 352 xml_reader->NodeAttribute(kTypeAttr, &link->mime_type_);
310 353
311 std::string href; 354 std::string href;
312 if (xml_reader->NodeAttribute(kHrefAttr, &href)) 355 if (xml_reader->NodeAttribute(kHrefAttr, &href))
313 link->href_ = GURL(href); 356 link->href_ = GURL(href);
314 357
315 std::string rel; 358 std::string rel;
316 if (xml_reader->NodeAttribute(kRelAttr, &rel)) 359 if (xml_reader->NodeAttribute(kRelAttr, &rel)) {
317 GetLinkType(rel, &link->type_); 360 GetLinkType(rel, &link->type_);
361 if (link->type_ == OPEN_WITH)
362 GetAppID(rel, &link->app_id_);
363 }
318 364
319 return link; 365 return link;
320 } 366 }
321 367
322 //////////////////////////////////////////////////////////////////////////////// 368 ////////////////////////////////////////////////////////////////////////////////
323 // FeedLink implementation 369 // FeedLink implementation
324 370
325 FeedLink::FeedLink() : type_(FeedLink::UNKNOWN) { 371 FeedLink::FeedLink() : type_(FeedLink::UNKNOWN) {
326 } 372 }
327 373
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 bool AccountMetadataFeed::Parse(const base::Value& value) { 952 bool AccountMetadataFeed::Parse(const base::Value& value) {
907 base::JSONValueConverter<AccountMetadataFeed> converter; 953 base::JSONValueConverter<AccountMetadataFeed> converter;
908 if (!converter.Convert(value, this)) { 954 if (!converter.Convert(value, this)) {
909 LOG(ERROR) << "Unable to parse: Invalid account metadata feed!"; 955 LOG(ERROR) << "Unable to parse: Invalid account metadata feed!";
910 return false; 956 return false;
911 } 957 }
912 return true; 958 return true;
913 } 959 }
914 960
915 } // namespace gdata 961 } // namespace gdata
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/gdata/gdata_parser.h ('k') | chrome/browser/chromeos/gdata/gdata_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698