| 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 "base/platform_file.h" | 7 #include "base/platform_file.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 const base::PlatformFileInfo& file_info, | 373 const base::PlatformFileInfo& file_info, |
| 374 PlatformFileInfoProto* proto) { | 374 PlatformFileInfoProto* proto) { |
| 375 proto->set_size(file_info.size); | 375 proto->set_size(file_info.size); |
| 376 proto->set_is_directory(file_info.is_directory); | 376 proto->set_is_directory(file_info.is_directory); |
| 377 proto->set_is_symbolic_link(file_info.is_symbolic_link); | 377 proto->set_is_symbolic_link(file_info.is_symbolic_link); |
| 378 proto->set_last_modified(file_info.last_modified.ToInternalValue()); | 378 proto->set_last_modified(file_info.last_modified.ToInternalValue()); |
| 379 proto->set_last_accessed(file_info.last_accessed.ToInternalValue()); | 379 proto->set_last_accessed(file_info.last_accessed.ToInternalValue()); |
| 380 proto->set_creation_time(file_info.creation_time.ToInternalValue()); | 380 proto->set_creation_time(file_info.creation_time.ToInternalValue()); |
| 381 } | 381 } |
| 382 | 382 |
| 383 bool GDataEntry::FromProto(const GDataEntryProto& proto) { | 383 void GDataEntry::FromProto(const GDataEntryProto& proto) { |
| 384 ConvertProtoToPlatformFileInfo(proto.file_info(), &file_info_); | 384 ConvertProtoToPlatformFileInfo(proto.file_info(), &file_info_); |
| 385 | 385 |
| 386 // Don't copy from proto.base_name() as base_name_ is computed in | 386 // Don't copy from proto.base_name() as base_name_ is computed in |
| 387 // SetBaseNameFromTitle(). | 387 // SetBaseNameFromTitle(). |
| 388 title_ = proto.title(); | 388 title_ = proto.title(); |
| 389 resource_id_ = proto.resource_id(); | 389 resource_id_ = proto.resource_id(); |
| 390 parent_resource_id_ = proto.parent_resource_id(); | 390 parent_resource_id_ = proto.parent_resource_id(); |
| 391 edit_url_ = GURL(proto.edit_url()); | 391 edit_url_ = GURL(proto.edit_url()); |
| 392 content_url_ = GURL(proto.content_url()); | 392 content_url_ = GURL(proto.content_url()); |
| 393 upload_url_ = GURL(proto.upload_url()); | 393 upload_url_ = GURL(proto.upload_url()); |
| 394 SetBaseNameFromTitle(); | 394 SetBaseNameFromTitle(); |
| 395 | |
| 396 // Reject older protobuf that does not contain the upload URL. This URL is | |
| 397 // necessary for uploading files. | |
| 398 if (!proto.has_upload_url()) { | |
| 399 LOG(ERROR) << "Incompatible proto detected (no upload URL): " | |
| 400 << proto.title(); | |
| 401 return false; | |
| 402 } | |
| 403 | |
| 404 return true; | |
| 405 } | 395 } |
| 406 | 396 |
| 407 void GDataEntry::ToProto(GDataEntryProto* proto) const { | 397 void GDataEntry::ToProto(GDataEntryProto* proto) const { |
| 408 ConvertPlatformFileInfoToProto(file_info_, proto->mutable_file_info()); | 398 ConvertPlatformFileInfoToProto(file_info_, proto->mutable_file_info()); |
| 409 | 399 |
| 410 // The base_name field is used in GetFileInfoByPathAsync(). As shown in | 400 // The base_name field is used in GetFileInfoByPathAsync(). As shown in |
| 411 // FromProto(), the value is discarded when deserializing from proto. | 401 // FromProto(), the value is discarded when deserializing from proto. |
| 412 proto->set_base_name(base_name_); | 402 proto->set_base_name(base_name_); |
| 413 proto->set_title(title_); | 403 proto->set_title(title_); |
| 414 proto->set_resource_id(resource_id_); | 404 proto->set_resource_id(resource_id_); |
| 415 proto->set_parent_resource_id(parent_resource_id_); | 405 proto->set_parent_resource_id(parent_resource_id_); |
| 416 proto->set_edit_url(edit_url_.spec()); | 406 proto->set_edit_url(edit_url_.spec()); |
| 417 proto->set_content_url(content_url_.spec()); | 407 proto->set_content_url(content_url_.spec()); |
| 418 proto->set_upload_url(upload_url_.spec()); | 408 proto->set_upload_url(upload_url_.spec()); |
| 419 } | 409 } |
| 420 | 410 |
| 421 void GDataEntry::ToProtoFull(GDataEntryProto* proto) const { | 411 void GDataEntry::ToProtoFull(GDataEntryProto* proto) const { |
| 422 if (AsGDataFileConst()) { | 412 if (AsGDataFileConst()) { |
| 423 AsGDataFileConst()->ToProto(proto); | 413 AsGDataFileConst()->ToProto(proto); |
| 424 } else if (AsGDataDirectoryConst()) { | 414 } else if (AsGDataDirectoryConst()) { |
| 425 // Unlike files, directories don't have directory specific info, so just | 415 // Unlike files, directories don't have directory specific info, so just |
| 426 // calling GDataEntry::ToProto(). | 416 // calling GDataEntry::ToProto(). |
| 427 ToProto(proto); | 417 ToProto(proto); |
| 428 } else { | 418 } else { |
| 429 NOTREACHED(); | 419 NOTREACHED(); |
| 430 } | 420 } |
| 431 } | 421 } |
| 432 | 422 |
| 433 bool GDataFile::FromProto(const GDataEntryProto& proto) { | 423 void GDataFile::FromProto(const GDataEntryProto& proto) { |
| 434 DCHECK(!proto.file_info().is_directory()); | 424 DCHECK(!proto.file_info().is_directory()); |
| 435 | 425 |
| 436 if (!GDataEntry::FromProto(proto)) | 426 GDataEntry::FromProto(proto); |
| 437 return false; | |
| 438 | 427 |
| 439 thumbnail_url_ = GURL(proto.file_specific_info().thumbnail_url()); | 428 thumbnail_url_ = GURL(proto.file_specific_info().thumbnail_url()); |
| 440 alternate_url_ = GURL(proto.file_specific_info().alternate_url()); | 429 alternate_url_ = GURL(proto.file_specific_info().alternate_url()); |
| 441 content_mime_type_ = proto.file_specific_info().content_mime_type(); | 430 content_mime_type_ = proto.file_specific_info().content_mime_type(); |
| 442 file_md5_ = proto.file_specific_info().file_md5(); | 431 file_md5_ = proto.file_specific_info().file_md5(); |
| 443 document_extension_ = proto.file_specific_info().document_extension(); | 432 document_extension_ = proto.file_specific_info().document_extension(); |
| 444 is_hosted_document_ = proto.file_specific_info().is_hosted_document(); | 433 is_hosted_document_ = proto.file_specific_info().is_hosted_document(); |
| 445 | |
| 446 return true; | |
| 447 } | 434 } |
| 448 | 435 |
| 449 void GDataFile::ToProto(GDataEntryProto* proto) const { | 436 void GDataFile::ToProto(GDataEntryProto* proto) const { |
| 450 GDataEntry::ToProto(proto); | 437 GDataEntry::ToProto(proto); |
| 451 DCHECK(!proto->file_info().is_directory()); | 438 DCHECK(!proto->file_info().is_directory()); |
| 452 GDataFileSpecificInfo* file_specific_info = | 439 GDataFileSpecificInfo* file_specific_info = |
| 453 proto->mutable_file_specific_info(); | 440 proto->mutable_file_specific_info(); |
| 454 file_specific_info->set_thumbnail_url(thumbnail_url_.spec()); | 441 file_specific_info->set_thumbnail_url(thumbnail_url_.spec()); |
| 455 file_specific_info->set_alternate_url(alternate_url_.spec()); | 442 file_specific_info->set_alternate_url(alternate_url_.spec()); |
| 456 file_specific_info->set_content_mime_type(content_mime_type_); | 443 file_specific_info->set_content_mime_type(content_mime_type_); |
| 457 file_specific_info->set_file_md5(file_md5_); | 444 file_specific_info->set_file_md5(file_md5_); |
| 458 file_specific_info->set_document_extension(document_extension_); | 445 file_specific_info->set_document_extension(document_extension_); |
| 459 file_specific_info->set_is_hosted_document(is_hosted_document_); | 446 file_specific_info->set_is_hosted_document(is_hosted_document_); |
| 460 } | 447 } |
| 461 | 448 |
| 462 bool GDataDirectory::FromProto(const GDataDirectoryProto& proto) { | 449 void GDataDirectory::FromProto(const GDataDirectoryProto& proto) { |
| 463 DCHECK(proto.gdata_entry().file_info().is_directory()); | 450 DCHECK(proto.gdata_entry().file_info().is_directory()); |
| 464 DCHECK(!proto.gdata_entry().has_file_specific_info()); | 451 DCHECK(!proto.gdata_entry().has_file_specific_info()); |
| 465 | 452 |
| 466 for (int i = 0; i < proto.child_files_size(); ++i) { | 453 for (int i = 0; i < proto.child_files_size(); ++i) { |
| 467 scoped_ptr<GDataFile> file(directory_service_->CreateGDataFile()); | 454 scoped_ptr<GDataFile> file(directory_service_->CreateGDataFile()); |
| 468 if (!file->FromProto(proto.child_files(i))) { | 455 file->FromProto(proto.child_files(i)); |
| 469 RemoveChildren(); | |
| 470 return false; | |
| 471 } | |
| 472 AddEntry(file.release()); | 456 AddEntry(file.release()); |
| 473 } | 457 } |
| 474 for (int i = 0; i < proto.child_directories_size(); ++i) { | 458 for (int i = 0; i < proto.child_directories_size(); ++i) { |
| 475 scoped_ptr<GDataDirectory> dir(directory_service_->CreateGDataDirectory()); | 459 scoped_ptr<GDataDirectory> dir(directory_service_->CreateGDataDirectory()); |
| 476 if (!dir->FromProto(proto.child_directories(i))) { | 460 dir->FromProto(proto.child_directories(i)); |
| 477 RemoveChildren(); | |
| 478 return false; | |
| 479 } | |
| 480 AddEntry(dir.release()); | 461 AddEntry(dir.release()); |
| 481 } | 462 } |
| 482 | 463 |
| 483 // The states of the directory should be updated after children are | 464 // The states of the directory should be updated after children are |
| 484 // handled successfully, so that incomplete states are not left. | 465 // handled successfully, so that incomplete states are not left. |
| 485 if (!GDataEntry::FromProto(proto.gdata_entry())) | 466 GDataEntry::FromProto(proto.gdata_entry()); |
| 486 return false; | |
| 487 | |
| 488 return true; | |
| 489 } | 467 } |
| 490 | 468 |
| 491 void GDataDirectory::ToProto(GDataDirectoryProto* proto) const { | 469 void GDataDirectory::ToProto(GDataDirectoryProto* proto) const { |
| 492 GDataEntry::ToProto(proto->mutable_gdata_entry()); | 470 GDataEntry::ToProto(proto->mutable_gdata_entry()); |
| 493 DCHECK(proto->gdata_entry().file_info().is_directory()); | 471 DCHECK(proto->gdata_entry().file_info().is_directory()); |
| 494 | 472 |
| 495 for (GDataFileCollection::const_iterator iter = child_files_.begin(); | 473 for (GDataFileCollection::const_iterator iter = child_files_.begin(); |
| 496 iter != child_files_.end(); ++iter) { | 474 iter != child_files_.end(); ++iter) { |
| 497 GDataFile* file = iter->second; | 475 GDataFile* file = iter->second; |
| 498 file->ToProto(proto->add_child_files()); | 476 file->ToProto(proto->add_child_files()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 DCHECK(ok); | 514 DCHECK(ok); |
| 537 } else if (dir) { | 515 } else if (dir) { |
| 538 GDataDirectoryProto dir_proto; | 516 GDataDirectoryProto dir_proto; |
| 539 dir->ToProto(&dir_proto); | 517 dir->ToProto(&dir_proto); |
| 540 const bool ok = dir_proto.SerializeToString(serialized_proto); | 518 const bool ok = dir_proto.SerializeToString(serialized_proto); |
| 541 DCHECK(ok); | 519 DCHECK(ok); |
| 542 } | 520 } |
| 543 } | 521 } |
| 544 | 522 |
| 545 } // namespace gdata | 523 } // namespace gdata |
| OLD | NEW |