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_directory_service.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_directory_service.h" |
6 | 6 |
7 #include <leveldb/db.h> | 7 #include <leveldb/db.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 namespace gdata { | 22 namespace gdata { |
23 namespace { | 23 namespace { |
24 | 24 |
25 // m: prefix for filesystem metadata db keys, version and largest_changestamp. | 25 // m: prefix for filesystem metadata db keys, version and largest_changestamp. |
26 // r: prefix for resource id db keys. | 26 // r: prefix for resource id db keys. |
27 const char kDBKeyLargestChangestamp[] = "m:largest_changestamp"; | 27 const char kDBKeyLargestChangestamp[] = "m:largest_changestamp"; |
28 const char kDBKeyVersion[] = "m:version"; | 28 const char kDBKeyVersion[] = "m:version"; |
29 const char kDBKeyResourceIdPrefix[] = "r:"; | 29 const char kDBKeyResourceIdPrefix[] = "r:"; |
30 | 30 |
31 // Returns true if |proto| is a valid proto as the root directory. | |
32 // Used to reject incompatible proto. | |
33 bool IsValidRootDirectoryProto(const GDataDirectoryProto& proto) { | |
34 const GDataEntryProto& entry_proto = proto.gdata_entry(); | |
35 // The title field for the root directory was originally empty, then | |
36 // changed to "gdata", then changed to "drive". Discard the proto data if | |
37 // the older formats are detected. See crbug.com/128133 for details. | |
38 if (entry_proto.title() != "drive") { | |
39 LOG(ERROR) << "Incompatible proto detected (bad title): " | |
40 << entry_proto.title(); | |
41 return false; | |
42 } | |
43 // The title field for the root directory was originally empty. Discard | |
44 // the proto data if the older format is detected. | |
45 if (entry_proto.resource_id() != kGDataRootDirectoryResourceId) { | |
46 LOG(ERROR) << "Incompatible proto detected (bad resource ID): " | |
47 << entry_proto.resource_id(); | |
48 return false; | |
49 } | |
50 | |
51 return true; | |
52 } | |
53 | |
54 } // namespace | 31 } // namespace |
55 | 32 |
56 EntryInfoResult::EntryInfoResult() : error(GDATA_FILE_ERROR_FAILED) { | 33 EntryInfoResult::EntryInfoResult() : error(GDATA_FILE_ERROR_FAILED) { |
57 } | 34 } |
58 | 35 |
59 EntryInfoResult::~EntryInfoResult() { | 36 EntryInfoResult::~EntryInfoResult() { |
60 } | 37 } |
61 | 38 |
62 EntryInfoPairResult::EntryInfoPairResult() { | 39 EntryInfoPairResult::EntryInfoPairResult() { |
63 } | 40 } |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 GDataRootDirectoryProto proto; | 643 GDataRootDirectoryProto proto; |
667 if (!proto.ParseFromString(serialized_proto)) | 644 if (!proto.ParseFromString(serialized_proto)) |
668 return false; | 645 return false; |
669 | 646 |
670 if (proto.version() != kProtoVersion) { | 647 if (proto.version() != kProtoVersion) { |
671 LOG(ERROR) << "Incompatible proto detected (incompatible version): " | 648 LOG(ERROR) << "Incompatible proto detected (incompatible version): " |
672 << proto.version(); | 649 << proto.version(); |
673 return false; | 650 return false; |
674 } | 651 } |
675 | 652 |
676 if (!IsValidRootDirectoryProto(proto.gdata_directory())) | 653 root_->FromProto(proto.gdata_directory()); |
677 return false; | |
678 | |
679 if (!root_->FromProto(proto.gdata_directory())) | |
680 return false; | |
681 | 654 |
682 origin_ = FROM_CACHE; | 655 origin_ = FROM_CACHE; |
683 largest_changestamp_ = proto.largest_changestamp(); | 656 largest_changestamp_ = proto.largest_changestamp(); |
684 | 657 |
685 return true; | 658 return true; |
686 } | 659 } |
687 | 660 |
688 scoped_ptr<GDataEntry> GDataDirectoryService::FromProtoString( | 661 scoped_ptr<GDataEntry> GDataDirectoryService::FromProtoString( |
689 const std::string& serialized_proto) { | 662 const std::string& serialized_proto) { |
690 GDataEntryProto entry_proto; | 663 GDataEntryProto entry_proto; |
691 if (!entry_proto.ParseFromString(serialized_proto)) | 664 if (!entry_proto.ParseFromString(serialized_proto)) |
692 return scoped_ptr<GDataEntry>(); | 665 return scoped_ptr<GDataEntry>(); |
693 | 666 |
694 scoped_ptr<GDataEntry> entry; | 667 scoped_ptr<GDataEntry> entry; |
695 if (entry_proto.file_info().is_directory()) { | 668 if (entry_proto.file_info().is_directory()) { |
696 entry.reset(CreateGDataDirectory()); | 669 entry.reset(CreateGDataDirectory()); |
697 // Call GDataEntry::FromProto instead of GDataDirectory::FromProto because | 670 // Call GDataEntry::FromProto instead of GDataDirectory::FromProto because |
698 // the proto does not include children. | 671 // the proto does not include children. |
699 if (!entry->FromProto(entry_proto)) { | 672 entry->FromProto(entry_proto); |
700 NOTREACHED() << "FromProto (directory) failed"; | |
701 entry.reset(); | |
702 } | |
703 } else { | 673 } else { |
704 scoped_ptr<GDataFile> file(CreateGDataFile()); | 674 scoped_ptr<GDataFile> file(CreateGDataFile()); |
705 // Call GDataFile::FromProto. | 675 // Call GDataFile::FromProto. |
706 if (file->FromProto(entry_proto)) { | 676 file->FromProto(entry_proto); |
707 entry.reset(file.release()); | 677 entry.reset(file.release()); |
708 } else { | |
709 NOTREACHED() << "FromProto (file) failed"; | |
710 } | |
711 } | 678 } |
712 return entry.Pass(); | 679 return entry.Pass(); |
713 } | 680 } |
714 | 681 |
715 void GDataDirectoryService::GetEntryInfoPairByPathsAfterGetFirst( | 682 void GDataDirectoryService::GetEntryInfoPairByPathsAfterGetFirst( |
716 const FilePath& first_path, | 683 const FilePath& first_path, |
717 const FilePath& second_path, | 684 const FilePath& second_path, |
718 const GetEntryInfoPairCallback& callback, | 685 const GetEntryInfoPairCallback& callback, |
719 GDataFileError error, | 686 GDataFileError error, |
720 scoped_ptr<GDataEntryProto> entry_proto) { | 687 scoped_ptr<GDataEntryProto> entry_proto) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 DCHECK(result.get()); | 720 DCHECK(result.get()); |
754 | 721 |
755 result->second.path = second_path; | 722 result->second.path = second_path; |
756 result->second.error = error; | 723 result->second.error = error; |
757 result->second.proto = entry_proto.Pass(); | 724 result->second.proto = entry_proto.Pass(); |
758 | 725 |
759 callback.Run(result.Pass()); | 726 callback.Run(result.Pass()); |
760 } | 727 } |
761 | 728 |
762 } // namespace gdata | 729 } // namespace gdata |
OLD | NEW |