| OLD | NEW |
| 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/drive/drive_api_util.h" | 5 #include "chrome/browser/drive/drive_api_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/md5.h" | 11 #include "base/md5.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "google_apis/drive/drive_api_parser.h" | 18 #include "google_apis/drive/drive_api_parser.h" |
| 19 #include "google_apis/drive/gdata_wapi_parser.h" | 19 #include "google_apis/drive/gdata_wapi_parser.h" |
| 20 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
| 21 #include "third_party/re2/re2/re2.h" | 21 #include "third_party/re2/re2/re2.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace drive { | 24 namespace drive { |
| 25 namespace util { | 25 namespace util { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 std::string GetMimeTypeFromEntryKind(google_apis::DriveEntryKind kind) { | |
| 29 switch (kind) { | |
| 30 case google_apis::ENTRY_KIND_DOCUMENT: | |
| 31 return kGoogleDocumentMimeType; | |
| 32 case google_apis::ENTRY_KIND_SPREADSHEET: | |
| 33 return kGoogleSpreadsheetMimeType; | |
| 34 case google_apis::ENTRY_KIND_PRESENTATION: | |
| 35 return kGooglePresentationMimeType; | |
| 36 case google_apis::ENTRY_KIND_DRAWING: | |
| 37 return kGoogleDrawingMimeType; | |
| 38 case google_apis::ENTRY_KIND_TABLE: | |
| 39 return kGoogleTableMimeType; | |
| 40 case google_apis::ENTRY_KIND_FORM: | |
| 41 return kGoogleFormMimeType; | |
| 42 default: | |
| 43 return std::string(); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 // Returns the argument string. | 28 // Returns the argument string. |
| 48 std::string Identity(const std::string& resource_id) { return resource_id; } | 29 std::string Identity(const std::string& resource_id) { return resource_id; } |
| 49 | 30 |
| 31 struct HostedDocumentKind { |
| 32 const char* mime_type; |
| 33 const char* extension; |
| 34 }; |
| 35 |
| 36 const HostedDocumentKind kHostedDocumentKinds[] = { |
| 37 {kGoogleDocumentMimeType, ".gdoc"}, |
| 38 {kGoogleSpreadsheetMimeType, ".gsheet"}, |
| 39 {kGooglePresentationMimeType, ".gslides"}, |
| 40 {kGoogleDrawingMimeType, ".gdraw"}, |
| 41 {kGoogleTableMimeType, ".gtable"}, |
| 42 {kGoogleFormMimeType, ".gform"} |
| 43 }; |
| 44 |
| 50 } // namespace | 45 } // namespace |
| 51 | 46 |
| 52 | 47 |
| 53 std::string EscapeQueryStringValue(const std::string& str) { | 48 std::string EscapeQueryStringValue(const std::string& str) { |
| 54 std::string result; | 49 std::string result; |
| 55 result.reserve(str.size()); | 50 result.reserve(str.size()); |
| 56 for (size_t i = 0; i < str.size(); ++i) { | 51 for (size_t i = 0; i < str.size(); ++i) { |
| 57 if (str[i] == '\\' || str[i] == '\'') { | 52 if (str[i] == '\\' || str[i] == '\'') { |
| 58 result.push_back('\\'); | 53 result.push_back('\\'); |
| 59 } | 54 } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 if (!entry) { | 161 if (!entry) { |
| 167 callback.Run(google_apis::GDATA_PARSE_ERROR, GURL()); | 162 callback.Run(google_apis::GDATA_PARSE_ERROR, GURL()); |
| 168 return; | 163 return; |
| 169 } | 164 } |
| 170 | 165 |
| 171 const google_apis::Link* share_link = | 166 const google_apis::Link* share_link = |
| 172 entry->GetLinkByType(google_apis::Link::LINK_SHARE); | 167 entry->GetLinkByType(google_apis::Link::LINK_SHARE); |
| 173 callback.Run(error, share_link ? share_link->href() : GURL()); | 168 callback.Run(error, share_link ? share_link->href() : GURL()); |
| 174 } | 169 } |
| 175 | 170 |
| 176 scoped_ptr<google_apis::FileResource> ConvertResourceEntryToFileResource( | |
| 177 const google_apis::ResourceEntry& entry) { | |
| 178 scoped_ptr<google_apis::FileResource> file(new google_apis::FileResource); | |
| 179 | |
| 180 file->set_file_id(entry.resource_id()); | |
| 181 file->set_title(entry.title()); | |
| 182 file->set_created_date(entry.published_time()); | |
| 183 | |
| 184 if (std::find(entry.labels().begin(), entry.labels().end(), | |
| 185 "shared-with-me") != entry.labels().end()) { | |
| 186 // Set current time to mark the file is shared_with_me, since ResourceEntry | |
| 187 // doesn't have |shared_with_me_date| equivalent. | |
| 188 file->set_shared_with_me_date(base::Time::Now()); | |
| 189 } | |
| 190 | |
| 191 file->set_shared(std::find(entry.labels().begin(), entry.labels().end(), | |
| 192 "shared") != entry.labels().end()); | |
| 193 | |
| 194 if (entry.is_folder()) { | |
| 195 file->set_mime_type(kDriveFolderMimeType); | |
| 196 } else { | |
| 197 std::string mime_type = GetMimeTypeFromEntryKind(entry.kind()); | |
| 198 if (mime_type.empty()) | |
| 199 mime_type = entry.content_mime_type(); | |
| 200 file->set_mime_type(mime_type); | |
| 201 } | |
| 202 | |
| 203 file->set_md5_checksum(entry.file_md5()); | |
| 204 file->set_file_size(entry.file_size()); | |
| 205 | |
| 206 file->mutable_labels()->set_trashed(entry.deleted()); | |
| 207 file->set_etag(entry.etag()); | |
| 208 | |
| 209 google_apis::ImageMediaMetadata* image_media_metadata = | |
| 210 file->mutable_image_media_metadata(); | |
| 211 image_media_metadata->set_width(entry.image_width()); | |
| 212 image_media_metadata->set_height(entry.image_height()); | |
| 213 image_media_metadata->set_rotation(entry.image_rotation()); | |
| 214 | |
| 215 std::vector<google_apis::ParentReference>* parents = file->mutable_parents(); | |
| 216 for (size_t i = 0; i < entry.links().size(); ++i) { | |
| 217 using google_apis::Link; | |
| 218 const Link& link = *entry.links()[i]; | |
| 219 switch (link.type()) { | |
| 220 case Link::LINK_PARENT: { | |
| 221 google_apis::ParentReference parent; | |
| 222 parent.set_parent_link(link.href()); | |
| 223 | |
| 224 std::string file_id = | |
| 225 drive::util::ExtractResourceIdFromUrl(link.href()); | |
| 226 parent.set_file_id(file_id); | |
| 227 parents->push_back(parent); | |
| 228 break; | |
| 229 } | |
| 230 case Link::LINK_ALTERNATE: | |
| 231 file->set_alternate_link(link.href()); | |
| 232 break; | |
| 233 default: | |
| 234 break; | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 file->set_modified_date(entry.updated_time()); | |
| 239 file->set_last_viewed_by_me_date(entry.last_viewed_time()); | |
| 240 | |
| 241 return file.Pass(); | |
| 242 } | |
| 243 | |
| 244 google_apis::DriveEntryKind GetKind( | |
| 245 const google_apis::FileResource& file_resource) { | |
| 246 if (file_resource.IsDirectory()) | |
| 247 return google_apis::ENTRY_KIND_FOLDER; | |
| 248 | |
| 249 const std::string& mime_type = file_resource.mime_type(); | |
| 250 if (mime_type == kGoogleDocumentMimeType) | |
| 251 return google_apis::ENTRY_KIND_DOCUMENT; | |
| 252 if (mime_type == kGoogleSpreadsheetMimeType) | |
| 253 return google_apis::ENTRY_KIND_SPREADSHEET; | |
| 254 if (mime_type == kGooglePresentationMimeType) | |
| 255 return google_apis::ENTRY_KIND_PRESENTATION; | |
| 256 if (mime_type == kGoogleDrawingMimeType) | |
| 257 return google_apis::ENTRY_KIND_DRAWING; | |
| 258 if (mime_type == kGoogleTableMimeType) | |
| 259 return google_apis::ENTRY_KIND_TABLE; | |
| 260 if (mime_type == kGoogleFormMimeType) | |
| 261 return google_apis::ENTRY_KIND_FORM; | |
| 262 if (mime_type == "application/pdf") | |
| 263 return google_apis::ENTRY_KIND_PDF; | |
| 264 return google_apis::ENTRY_KIND_FILE; | |
| 265 } | |
| 266 | |
| 267 scoped_ptr<google_apis::ResourceEntry> | 171 scoped_ptr<google_apis::ResourceEntry> |
| 268 ConvertFileResourceToResourceEntry( | 172 ConvertFileResourceToResourceEntry( |
| 269 const google_apis::FileResource& file_resource) { | 173 const google_apis::FileResource& file_resource) { |
| 270 scoped_ptr<google_apis::ResourceEntry> entry(new google_apis::ResourceEntry); | 174 scoped_ptr<google_apis::ResourceEntry> entry(new google_apis::ResourceEntry); |
| 271 | 175 |
| 272 // ResourceEntry | 176 // ResourceEntry |
| 273 entry->set_resource_id(file_resource.file_id()); | 177 entry->set_resource_id(file_resource.file_id()); |
| 274 entry->set_id(file_resource.file_id()); | 178 entry->set_id(file_resource.file_id()); |
| 275 entry->set_kind(GetKind(file_resource)); | 179 if (file_resource.IsDirectory()) |
| 180 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_FOLDER); |
| 181 else if (IsHostedDocument(file_resource.mime_type())) |
| 182 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_UNKNOWN); |
| 183 else |
| 184 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_FILE); |
| 276 entry->set_title(file_resource.title()); | 185 entry->set_title(file_resource.title()); |
| 277 entry->set_published_time(file_resource.created_date()); | 186 entry->set_published_time(file_resource.created_date()); |
| 278 | 187 |
| 279 std::vector<std::string> labels; | 188 std::vector<std::string> labels; |
| 280 if (!file_resource.shared_with_me_date().is_null()) | 189 if (!file_resource.shared_with_me_date().is_null()) |
| 281 labels.push_back("shared-with-me"); | 190 labels.push_back("shared-with-me"); |
| 282 if (file_resource.shared()) | 191 if (file_resource.shared()) |
| 283 labels.push_back("shared"); | 192 labels.push_back("shared"); |
| 284 entry->set_labels(labels); | 193 entry->set_labels(labels); |
| 285 | 194 |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 base::MD5Update(&context, base::StringPiece(buffer.get(), result)); | 337 base::MD5Update(&context, base::StringPiece(buffer.get(), result)); |
| 429 } | 338 } |
| 430 | 339 |
| 431 base::MD5Digest digest; | 340 base::MD5Digest digest; |
| 432 base::MD5Final(&digest, &context); | 341 base::MD5Final(&digest, &context); |
| 433 return MD5DigestToBase16(digest); | 342 return MD5DigestToBase16(digest); |
| 434 } | 343 } |
| 435 | 344 |
| 436 const char kWapiRootDirectoryResourceId[] = "folder:root"; | 345 const char kWapiRootDirectoryResourceId[] = "folder:root"; |
| 437 | 346 |
| 347 std::string GetHostedDocumentExtension(const std::string& mime_type) { |
| 348 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 349 if (mime_type == kHostedDocumentKinds[i].mime_type) |
| 350 return kHostedDocumentKinds[i].extension; |
| 351 } |
| 352 return std::string(); |
| 353 } |
| 354 |
| 355 std::string GetHostedDocumentMimeType(const std::string& extension) { |
| 356 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 357 if (extension == kHostedDocumentKinds[i].extension) |
| 358 return kHostedDocumentKinds[i].mime_type; |
| 359 } |
| 360 return std::string(); |
| 361 } |
| 362 |
| 363 bool IsHostedDocument(const std::string& mime_type) { |
| 364 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 365 if (mime_type == kHostedDocumentKinds[i].mime_type) |
| 366 return true; |
| 367 } |
| 368 return false; |
| 369 } |
| 370 |
| 371 bool IsHostedDocumentByExtension(const std::string& extension) { |
| 372 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 373 if (extension == kHostedDocumentKinds[i].extension) |
| 374 return true; |
| 375 } |
| 376 return false; |
| 377 } |
| 378 |
| 438 } // namespace util | 379 } // namespace util |
| 439 } // namespace drive | 380 } // namespace drive |
| OLD | NEW |