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 3// found in the LICENSE file. 4 5#include "chrome/browser/chromeos/drive/resource_entry_conversion.h" 6 7#include <string> 8 9#include "base/logging.h" 10#include "base/time/time.h" 11#include "chrome/browser/chromeos/drive/drive.pb.h" 12#include "chrome/browser/chromeos/drive/file_system_util.h" 13#include "chrome/browser/drive/drive_api_util.h" 14#include "google_apis/drive/drive_api_parser.h" 15 16namespace drive { 17 18bool ConvertChangeResourceToResourceEntry( 19 const google_apis::ChangeResource& input, 20 ResourceEntry* out_entry, 21 std::string* out_parent_resource_id) { 22 DCHECK(out_entry); 23 DCHECK(out_parent_resource_id); 24 25 ResourceEntry converted; 26 std::string parent_resource_id; 27 if (input.file() && 28 !ConvertFileResourceToResourceEntry(*input.file(), &converted, 29 &parent_resource_id)) 30 return false; 31 32 converted.set_resource_id(input.file_id()); 33 converted.set_deleted(converted.deleted() || input.is_deleted()); 34 converted.set_modification_date(input.modification_date().ToInternalValue()); 35 36 out_entry->Swap(&converted); 37 swap(*out_parent_resource_id, parent_resource_id); 38 return true; 39} 40 41bool ConvertFileResourceToResourceEntry( 42 const google_apis::FileResource& input, 43 ResourceEntry* out_entry, 44 std::string* out_parent_resource_id) { 45 DCHECK(out_entry); 46 DCHECK(out_parent_resource_id); 47 ResourceEntry converted; 48 49 // For regular files, the 'filename' and 'title' attribute in the metadata 50 // may be different (e.g. due to rename). To be consistent with the web 51 // interface and other client to use the 'title' attribute, instead of 52 // 'filename', as the file name in the local snapshot. 53 converted.set_title(input.title()); 54 converted.set_base_name(util::NormalizeFileName(converted.title())); 55 converted.set_resource_id(input.file_id()); 56 57 // Gets parent Resource ID. On drive.google.com, a file can have multiple 58 // parents or no parent, but we are forcing a tree-shaped structure (i.e. no 59 // multi-parent or zero-parent entries). Therefore the first found "parent" is 60 // used for the entry. Tracked in http://crbug.com/158904. 61 std::string parent_resource_id; 62 if (!input.parents().empty()) 63 parent_resource_id = input.parents()[0].file_id(); 64 65 converted.set_deleted(input.labels().is_trashed()); 66 converted.set_shared_with_me(!input.shared_with_me_date().is_null()); 67 converted.set_shared(input.shared()); 68 69 PlatformFileInfoProto* file_info = converted.mutable_file_info(); 70 71 file_info->set_last_modified(input.modified_date().ToInternalValue()); 72 // If the file has never been viewed (last_viewed_by_me_date().is_null() == 73 // true), then we will set the last_accessed field in the protocol buffer to 74 // 0. 75 file_info->set_last_accessed( 76 input.last_viewed_by_me_date().ToInternalValue()); 77 file_info->set_creation_time(input.created_date().ToInternalValue()); 78 79 if (input.IsDirectory()) { 80 file_info->set_is_directory(true); 81 } else { 82 FileSpecificInfo* file_specific_info = 83 converted.mutable_file_specific_info(); 84 if (!input.IsHostedDocument()) { 85 file_info->set_size(input.file_size()); 86 file_specific_info->set_md5(input.md5_checksum()); 87 file_specific_info->set_is_hosted_document(false); 88 } else { 89 // Attach .g<something> extension to hosted documents so we can special 90 // case their handling in UI. 91 // TODO(satorux): Figure out better way how to pass input info like kind 92 // to UI through the File API stack. 93 const std::string document_extension = 94 drive::util::GetHostedDocumentExtension(input.mime_type()); 95 file_specific_info->set_document_extension(document_extension); 96 converted.set_base_name( 97 util::NormalizeFileName(converted.title() + document_extension)); 98 99 // We don't know the size of hosted docs and it does not matter since 100 // it has no effect on the quota. 101 file_info->set_size(0); 102 file_specific_info->set_is_hosted_document(true); 103 } 104 file_info->set_is_directory(false); 105 file_specific_info->set_content_mime_type(input.mime_type()); 106 107 if (!input.alternate_link().is_empty()) 108 file_specific_info->set_alternate_url(input.alternate_link().spec()); 109 110 const int64 image_width = input.image_media_metadata().width(); 111 if (image_width != -1) 112 file_specific_info->set_image_width(image_width); 113 114 const int64 image_height = input.image_media_metadata().height(); 115 if (image_height != -1) 116 file_specific_info->set_image_height(image_height); 117 118 const int64 image_rotation = input.image_media_metadata().rotation(); 119 if (image_rotation != -1) 120 file_specific_info->set_image_rotation(image_rotation); 121 } 122 123 out_entry->Swap(&converted); 124 swap(*out_parent_resource_id, parent_resource_id); 125 return true; 126} 127 128void ConvertResourceEntryToFileInfo(const ResourceEntry& entry, 129 base::File::Info* file_info) { 130 file_info->size = entry.file_info().size(); 131 file_info->is_directory = entry.file_info().is_directory(); 132 file_info->is_symbolic_link = entry.file_info().is_symbolic_link(); 133 file_info->last_modified = base::Time::FromInternalValue( 134 entry.file_info().last_modified()); 135 file_info->last_accessed = base::Time::FromInternalValue( 136 entry.file_info().last_accessed()); 137 file_info->creation_time = base::Time::FromInternalValue( 138 entry.file_info().creation_time()); 139} 140 141} // namespace drive 142