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/chromeos/gdata/gdata_files.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 // Paste paths parts back together in reverse order from upward tree | 70 // Paste paths parts back together in reverse order from upward tree |
71 // traversal. | 71 // traversal. |
72 for (std::vector<FilePath::StringType>::reverse_iterator iter = | 72 for (std::vector<FilePath::StringType>::reverse_iterator iter = |
73 parts.rbegin(); | 73 parts.rbegin(); |
74 iter != parts.rend(); ++iter) { | 74 iter != parts.rend(); ++iter) { |
75 path = path.Append(*iter); | 75 path = path.Append(*iter); |
76 } | 76 } |
77 return path; | 77 return path; |
78 } | 78 } |
79 | 79 |
80 void GDataFileBase::UseOriginalFileName() { | |
81 file_name_ = EscapeUtf8FileName(original_file_name_); | |
82 } | |
83 | |
80 // static. | 84 // static. |
81 GDataFileBase* GDataFileBase::FromDocumentEntry(GDataDirectory* parent, | 85 GDataFileBase* GDataFileBase::FromDocumentEntry(GDataDirectory* parent, |
82 DocumentEntry* doc) { | 86 DocumentEntry* doc) { |
83 DCHECK(parent); | 87 DCHECK(parent); |
84 DCHECK(doc); | 88 DCHECK(doc); |
85 if (doc->is_folder()) | 89 if (doc->is_folder()) |
86 return GDataDirectory::FromDocumentEntry(parent, doc); | 90 return GDataDirectory::FromDocumentEntry(parent, doc); |
87 else if (doc->is_hosted_document() || doc->is_file()) | 91 else if (doc->is_hosted_document() || doc->is_file()) |
88 return GDataFile::FromDocumentEntry(parent, doc); | 92 return GDataFile::FromDocumentEntry(parent, doc); |
89 | 93 |
(...skipping 28 matching lines...) Expand all Loading... | |
118 DCHECK(parent); | 122 DCHECK(parent); |
119 } | 123 } |
120 | 124 |
121 GDataFile::~GDataFile() { | 125 GDataFile::~GDataFile() { |
122 } | 126 } |
123 | 127 |
124 GDataFile* GDataFile::AsGDataFile() { | 128 GDataFile* GDataFile::AsGDataFile() { |
125 return this; | 129 return this; |
126 } | 130 } |
127 | 131 |
132 void GDataFile::UseOriginalFileName() { | |
133 if (is_hosted_document_) { | |
134 file_name_ = EscapeUtf8FileName(original_file_name_ + document_extension_); | |
135 } else { | |
136 GDataFileBase::UseOriginalFileName(); | |
137 } | |
138 } | |
139 | |
128 GDataFileBase* GDataFile::FromDocumentEntry(GDataDirectory* parent, | 140 GDataFileBase* GDataFile::FromDocumentEntry(GDataDirectory* parent, |
129 DocumentEntry* doc) { | 141 DocumentEntry* doc) { |
130 DCHECK(doc->is_hosted_document() || doc->is_file()); | 142 DCHECK(doc->is_hosted_document() || doc->is_file()); |
131 GDataFile* file = new GDataFile(parent); | 143 GDataFile* file = new GDataFile(parent); |
144 | |
145 // For regular files, the 'filename' and 'title' attribute in the metadata | |
146 // may be different (e.g. due to rename). To be consistent with the web | |
147 // interface and other client to use the 'title' attribute, instead of | |
148 // 'filename', as the file name in the local snapshot. | |
149 file->original_file_name_ = UTF16ToUTF8(doc->title()); | |
satorux1
2012/03/12 17:48:24
this looks like an important change. It'd be nicer
zel
2012/03/12 20:46:46
Is title required for regular (non-hosted) files t
Ben Chan
2012/03/13 00:29:21
Agree. Let me split the CL.
Ben Chan
2012/03/13 00:29:21
Yes, I noticed that both the web interface and the
| |
150 | |
132 // Check if this entry is a true file, or... | 151 // Check if this entry is a true file, or... |
133 if (doc->is_file()) { | 152 if (doc->is_file()) { |
134 file->original_file_name_ = UTF16ToUTF8(doc->filename()); | |
135 file->file_name_ = | |
136 GDataFileBase::EscapeUtf8FileName(file->original_file_name_); | |
137 file->file_info_.size = doc->file_size(); | 153 file->file_info_.size = doc->file_size(); |
138 file->file_md5_ = doc->file_md5(); | 154 file->file_md5_ = doc->file_md5(); |
139 } else { | 155 } else { |
140 DCHECK(doc->is_hosted_document()); | |
141 // ... a hosted document. | 156 // ... a hosted document. |
142 file->original_file_name_ = UTF16ToUTF8(doc->title()); | |
143 // Attach .g<something> extension to hosted documents so we can special | 157 // Attach .g<something> extension to hosted documents so we can special |
144 // case their handling in UI. | 158 // case their handling in UI. |
145 // TODO(zelidrag): Figure out better way how to pass entry info like kind | 159 // TODO(zelidrag): Figure out better way how to pass entry info like kind |
146 // to UI through the File API stack. | 160 // to UI through the File API stack. |
147 file->file_name_ = GDataFileBase::EscapeUtf8FileName( | 161 file->document_extension_ = doc->GetHostedDocumentExtension(); |
148 file->original_file_name_ + doc->GetHostedDocumentExtension()); | |
149 // We don't know the size of hosted docs and it does not matter since | 162 // We don't know the size of hosted docs and it does not matter since |
150 // is has no effect on the quota. | 163 // is has no effect on the quota. |
151 file->file_info_.size = 0; | 164 file->file_info_.size = 0; |
152 } | 165 } |
153 file->kind_ = doc->kind(); | 166 file->kind_ = doc->kind(); |
154 const Link* self_link = doc->GetLinkByType(Link::SELF); | 167 const Link* self_link = doc->GetLinkByType(Link::SELF); |
155 if (self_link) | 168 if (self_link) |
156 file->self_url_ = self_link->href(); | 169 file->self_url_ = self_link->href(); |
157 file->content_url_ = doc->content_url(); | 170 file->content_url_ = doc->content_url(); |
158 file->content_mime_type_ = doc->content_mime_type(); | 171 file->content_mime_type_ = doc->content_mime_type(); |
159 file->etag_ = doc->etag(); | 172 file->etag_ = doc->etag(); |
160 file->resource_id_ = doc->resource_id(); | 173 file->resource_id_ = doc->resource_id(); |
161 file->id_ = doc->id(); | 174 file->id_ = doc->id(); |
162 file->is_hosted_document_ = doc->is_hosted_document(); | 175 file->is_hosted_document_ = doc->is_hosted_document(); |
163 file->file_info_.last_modified = doc->updated_time(); | 176 file->file_info_.last_modified = doc->updated_time(); |
164 file->file_info_.last_accessed = doc->updated_time(); | 177 file->file_info_.last_accessed = doc->updated_time(); |
165 file->file_info_.creation_time = doc->published_time(); | 178 file->file_info_.creation_time = doc->published_time(); |
166 | 179 |
180 // UseOriginalFileName() must be called after |is_hosted_document_| is set. | |
181 file->UseOriginalFileName(); | |
182 | |
167 const Link* thumbnail_link = doc->GetLinkByType(Link::THUMBNAIL); | 183 const Link* thumbnail_link = doc->GetLinkByType(Link::THUMBNAIL); |
168 if (thumbnail_link) | 184 if (thumbnail_link) |
169 file->thumbnail_url_ = thumbnail_link->href(); | 185 file->thumbnail_url_ = thumbnail_link->href(); |
170 | 186 |
171 const Link* alternate_link = doc->GetLinkByType(Link::ALTERNATE); | 187 const Link* alternate_link = doc->GetLinkByType(Link::ALTERNATE); |
172 if (alternate_link) | 188 if (alternate_link) |
173 file->edit_url_ = alternate_link->href(); | 189 file->edit_url_ = alternate_link->href(); |
174 | 190 |
175 return file; | 191 return file; |
176 } | 192 } |
(...skipping 17 matching lines...) Expand all Loading... | |
194 return this; | 210 return this; |
195 } | 211 } |
196 | 212 |
197 // static | 213 // static |
198 GDataFileBase* GDataDirectory::FromDocumentEntry(GDataDirectory* parent, | 214 GDataFileBase* GDataDirectory::FromDocumentEntry(GDataDirectory* parent, |
199 DocumentEntry* doc) { | 215 DocumentEntry* doc) { |
200 DCHECK(parent); | 216 DCHECK(parent); |
201 DCHECK(doc->is_folder()); | 217 DCHECK(doc->is_folder()); |
202 GDataDirectory* dir = new GDataDirectory(parent); | 218 GDataDirectory* dir = new GDataDirectory(parent); |
203 dir->original_file_name_ = UTF16ToUTF8(doc->title()); | 219 dir->original_file_name_ = UTF16ToUTF8(doc->title()); |
204 dir->file_name_ = GDataFileBase::EscapeUtf8FileName(dir->original_file_name_); | 220 dir->UseOriginalFileName(); |
205 dir->file_info_.last_modified = doc->updated_time(); | 221 dir->file_info_.last_modified = doc->updated_time(); |
206 dir->file_info_.last_accessed = doc->updated_time(); | 222 dir->file_info_.last_accessed = doc->updated_time(); |
207 dir->file_info_.creation_time = doc->published_time(); | 223 dir->file_info_.creation_time = doc->published_time(); |
208 // Extract feed link. | 224 // Extract feed link. |
209 dir->start_feed_url_ = doc->content_url(); | 225 dir->start_feed_url_ = doc->content_url(); |
226 dir->resource_id_ = doc->resource_id(); | |
210 dir->content_url_ = doc->content_url(); | 227 dir->content_url_ = doc->content_url(); |
211 | 228 |
229 const Link* self_link = doc->GetLinkByType(Link::SELF); | |
230 if (self_link) | |
231 dir->self_url_ = self_link->href(); | |
232 | |
212 const Link* upload_link = doc->GetLinkByType(Link::RESUMABLE_CREATE_MEDIA); | 233 const Link* upload_link = doc->GetLinkByType(Link::RESUMABLE_CREATE_MEDIA); |
213 if (upload_link) | 234 if (upload_link) |
214 dir->upload_url_ = upload_link->href(); | 235 dir->upload_url_ = upload_link->href(); |
215 | 236 |
216 return dir; | 237 return dir; |
217 } | 238 } |
218 | 239 |
219 void GDataDirectory::RemoveChildren() { | 240 void GDataDirectory::RemoveChildren() { |
220 // Remove children from resource map first. | 241 // Remove children from resource map first. |
221 root_->RemoveFilesFromResourceMap(children_); | 242 root_->RemoveFilesFromResourceMap(children_); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 } | 274 } |
254 } | 275 } |
255 if (full_file_name.value() != file->file_name()) | 276 if (full_file_name.value() != file->file_name()) |
256 file->set_file_name(full_file_name.value()); | 277 file->set_file_name(full_file_name.value()); |
257 children_.insert(std::make_pair(file->file_name(), file)); | 278 children_.insert(std::make_pair(file->file_name(), file)); |
258 | 279 |
259 // Add file to resource map. | 280 // Add file to resource map. |
260 root_->AddFileToResourceMap(file); | 281 root_->AddFileToResourceMap(file); |
261 } | 282 } |
262 | 283 |
284 bool GDataDirectory::MoveFile(GDataFileBase* file, GDataDirectory* dir) { | |
285 DCHECK(file); | |
286 DCHECK(dir); | |
287 DCHECK_EQ(this, file->parent()); | |
288 | |
289 GDataFileCollection::iterator iter = children_.find(file->file_name()); | |
290 if (children_.find(file->file_name()) == children_.end()) | |
291 return false; | |
292 | |
293 DCHECK(iter->second); | |
294 children_.erase(iter); | |
295 | |
296 // The file name may have been changed due to prior de-duplication. | |
297 // We need to first restore the original name of the file before going | |
298 // through de-duplication again when it is added to another directory. | |
299 file->UseOriginalFileName(); | |
300 dir->AddFile(file); | |
301 file->set_parent(dir); | |
302 return true; | |
303 } | |
304 | |
263 bool GDataDirectory::RemoveFile(GDataFileBase* file) { | 305 bool GDataDirectory::RemoveFile(GDataFileBase* file) { |
264 GDataFileCollection::iterator iter = children_.find(file->file_name()); | 306 GDataFileCollection::iterator iter = children_.find(file->file_name()); |
265 if (iter == children_.end()) | 307 if (iter == children_.end()) |
266 return false; | 308 return false; |
267 | 309 |
268 DCHECK(iter->second); | 310 DCHECK(iter->second); |
269 | 311 |
270 // Remove file from resource map first. | 312 // Remove file from resource map first. |
271 root_->RemoveFileFromResourceMap(file); | 313 root_->RemoveFileFromResourceMap(file); |
272 | 314 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 cache_state |= GDataFile::CACHE_STATE_PINNED; | 460 cache_state |= GDataFile::CACHE_STATE_PINNED; |
419 | 461 |
420 DVLOG(1) << "Cache state for res_id " << res_id | 462 DVLOG(1) << "Cache state for res_id " << res_id |
421 << ", md5 " << entry->md5 | 463 << ", md5 " << entry->md5 |
422 << ": " << cache_state; | 464 << ": " << cache_state; |
423 | 465 |
424 return cache_state; | 466 return cache_state; |
425 } | 467 } |
426 | 468 |
427 } // namespace gdata | 469 } // namespace gdata |
OLD | NEW |