Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/media_galleries/fileapi/picasa/picasa_album_table_reade r.h" | 5 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_album_table_reade r.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" | 13 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" |
| 13 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h" | 14 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h" |
| 14 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_table_reader.h" | |
| 15 | 15 |
| 16 namespace picasa { | 16 namespace picasa { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // |variant_time| is specified as the number of days from Dec 30, 1899. | 20 // |variant_time| is specified as the number of days from Dec 30, 1899. |
| 21 base::Time TimeFromMicrosoftVariantTime(double variant_time) { | 21 base::Time TimeFromMicrosoftVariantTime(double variant_time) { |
| 22 base::TimeDelta variant_delta = base::TimeDelta::FromMicroseconds( | 22 base::TimeDelta variant_delta = base::TimeDelta::FromMicroseconds( |
| 23 static_cast<int64>(variant_time * base::Time::kMicrosecondsPerDay)); | 23 static_cast<int64>(variant_time * base::Time::kMicrosecondsPerDay)); |
| 24 | 24 |
| 25 return base::Time::FromLocalExploded(kPicasaVariantTimeEpoch) + variant_delta; | 25 return base::Time::FromLocalExploded(kPicasaVariantTimeEpoch) + variant_delta; |
| 26 } | 26 } |
| 27 | 27 |
| 28 base::PlatformFile MakePlatformFile(const base::FilePath& directory_path, | |
| 29 const std::string& suffix) { | |
| 30 base::FilePath path = directory_path.Append(base::FilePath::FromUTF8Unsafe( | |
| 31 std::string(kPicasaAlbumTableName) + "_" + suffix)); | |
| 32 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | |
| 33 return base::CreatePlatformFile(path, flags, NULL, NULL); | |
|
Greg Billock
2013/06/20 22:06:38
Is this guaranteed not to open the file? Looking a
tommycli
2013/06/20 23:14:46
Yes, the PlatformFiles are opened on the construct
| |
| 34 } | |
| 35 | |
| 36 base::PlatformFile MakeColumnPlatformFile(const base::FilePath& directory_path, | |
| 37 const std::string& column_name) { | |
| 38 return MakePlatformFile(directory_path, column_name + "." + kPmpExtension); | |
| 39 } | |
| 40 | |
| 28 } // namespace | 41 } // namespace |
| 29 | 42 |
| 30 AlbumInfo::AlbumInfo() {} | 43 AlbumInfo::AlbumInfo() {} |
| 31 | 44 |
| 32 AlbumInfo::AlbumInfo(const std::string& name, const base::Time& timestamp, | 45 AlbumInfo::AlbumInfo(const std::string& name, const base::Time& timestamp, |
| 33 const std::string& uid, const base::FilePath& path) | 46 const std::string& uid, const base::FilePath& path) |
| 34 : name(name), | 47 : name(name), |
| 35 timestamp(timestamp), | 48 timestamp(timestamp), |
| 36 uid(uid), | 49 uid(uid), |
| 37 path(path) { | 50 path(path) { |
| 38 } | 51 } |
| 39 | 52 |
| 40 AlbumInfo::~AlbumInfo() {} | 53 AlbumInfo::~AlbumInfo() {} |
| 41 | 54 |
| 55 PicasaAlbumTableFiles::PicasaAlbumTableFiles( | |
| 56 const base::FilePath& directory_path) { | |
| 57 indicator_file = MakePlatformFile(directory_path, "0"); | |
| 58 category_file = MakeColumnPlatformFile(directory_path, "category"); | |
| 59 date_file = MakeColumnPlatformFile(directory_path, "date"); | |
| 60 filename_file = MakeColumnPlatformFile(directory_path, "filename"); | |
| 61 name_file = MakeColumnPlatformFile(directory_path, "name"); | |
| 62 token_file = MakeColumnPlatformFile(directory_path, "token"); | |
| 63 uid_file = MakeColumnPlatformFile(directory_path, "uid"); | |
| 64 } | |
| 65 | |
| 42 PicasaAlbumTableReader::PicasaAlbumTableReader( | 66 PicasaAlbumTableReader::PicasaAlbumTableReader( |
| 43 const base::FilePath& directory_path) | 67 const PicasaAlbumTableFiles& table_files) |
| 44 : directory_path_(directory_path), | 68 : table_files_(table_files), |
| 45 initialized_(false) {} | 69 initialized_(false) { |
| 70 } | |
| 46 | 71 |
| 47 PicasaAlbumTableReader::~PicasaAlbumTableReader() {} | 72 PicasaAlbumTableReader::~PicasaAlbumTableReader() {} |
| 48 | 73 |
| 49 const std::vector<AlbumInfo>& PicasaAlbumTableReader::folders() const { | 74 const std::vector<AlbumInfo>& PicasaAlbumTableReader::folders() const { |
| 50 DCHECK(initialized_); | 75 DCHECK(initialized_); |
| 51 return folders_; | 76 return folders_; |
| 52 } | 77 } |
| 53 | 78 |
| 54 const std::vector<AlbumInfo>& PicasaAlbumTableReader::albums() const { | 79 const std::vector<AlbumInfo>& PicasaAlbumTableReader::albums() const { |
| 55 DCHECK(initialized_); | 80 DCHECK(initialized_); |
| 56 return albums_; | 81 return albums_; |
| 57 } | 82 } |
| 58 | 83 |
| 59 bool PicasaAlbumTableReader::Init() { | 84 bool PicasaAlbumTableReader::Init() { |
| 60 if (initialized_) | 85 if (initialized_) |
| 61 return true; | 86 return true; |
| 62 | 87 |
| 63 PmpTableReader pmp_reader(kPicasaAlbumTableName, directory_path_); | 88 if (table_files_.indicator_file == base::kInvalidPlatformFileValue) |
| 64 | |
| 65 const PmpColumnReader* category_column = | |
| 66 pmp_reader.AddColumn("category", PMP_TYPE_UINT32); | |
| 67 const PmpColumnReader* date_column = | |
| 68 pmp_reader.AddColumn("date", PMP_TYPE_DOUBLE64); | |
| 69 const PmpColumnReader* filename_column = | |
| 70 pmp_reader.AddColumn("filename", PMP_TYPE_STRING); | |
| 71 const PmpColumnReader* name_column = | |
| 72 pmp_reader.AddColumn("name", PMP_TYPE_STRING); | |
| 73 const PmpColumnReader* token_column = | |
| 74 pmp_reader.AddColumn("token", PMP_TYPE_STRING); | |
| 75 const PmpColumnReader* uid_column = | |
| 76 pmp_reader.AddColumn("uid", PMP_TYPE_STRING); | |
| 77 | |
| 78 if (pmp_reader.Columns().size() != 6) | |
| 79 return false; | 89 return false; |
| 80 | 90 |
| 81 for (uint32 i = 0; i < pmp_reader.RowCount(); i++) { | 91 PmpColumnReader category_column, date_column, filename_column, name_column, |
| 92 token_column, uid_column; | |
| 93 if (!category_column.Init(table_files_.category_file, PMP_TYPE_UINT32) || | |
|
Greg Billock
2013/06/20 22:06:38
Seeing this here, I think "ReadColumn" or somethin
tommycli
2013/06/20 23:14:46
Done.
| |
| 94 !date_column.Init(table_files_.date_file, PMP_TYPE_DOUBLE64) || | |
| 95 !filename_column.Init(table_files_.filename_file, PMP_TYPE_STRING) || | |
| 96 !name_column.Init(table_files_.name_file, PMP_TYPE_STRING) || | |
| 97 !token_column.Init(table_files_.token_file, PMP_TYPE_STRING) || | |
| 98 !uid_column.Init(table_files_.uid_file, PMP_TYPE_STRING)) { | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 uint32 row_count = 0; | |
| 103 row_count = std::max(row_count, category_column.rows()); | |
|
Greg Billock
2013/06/20 22:06:38
Given that !reader.ReadXxx(i) -> continue; below,
tommycli
2013/06/20 23:14:46
You are technically correct in that we could proba
| |
| 104 row_count = std::max(row_count, date_column.rows()); | |
| 105 row_count = std::max(row_count, filename_column.rows()); | |
| 106 row_count = std::max(row_count, name_column.rows()); | |
| 107 row_count = std::max(row_count, token_column.rows()); | |
| 108 row_count = std::max(row_count, uid_column.rows()); | |
| 109 | |
| 110 for (uint32 i = 0; i < row_count; i++) { | |
| 82 uint32 category = kAlbumCategoryInvalid; | 111 uint32 category = kAlbumCategoryInvalid; |
| 83 double date = 0; | 112 double date = 0; |
| 84 std::string name; | 113 std::string name; |
| 85 std::string uid; | 114 std::string uid; |
| 86 if (!category_column->ReadUInt32(i, &category) || | 115 if (!category_column.ReadUInt32(i, &category) || |
| 87 !date_column->ReadDouble64(i, &date) || | 116 !date_column.ReadDouble64(i, &date) || |
| 88 !name_column->ReadString(i, &name) || name.empty() || | 117 !name_column.ReadString(i, &name) || name.empty() || |
| 89 !uid_column->ReadString(i, &uid) || uid.empty()) { | 118 !uid_column.ReadString(i, &uid) || uid.empty()) { |
| 90 continue; | 119 continue; |
| 91 } | 120 } |
| 92 | 121 |
| 93 base::Time timestamp = TimeFromMicrosoftVariantTime(date); | 122 base::Time timestamp = TimeFromMicrosoftVariantTime(date); |
| 94 | 123 |
| 95 switch (category) { | 124 switch (category) { |
| 96 case kAlbumCategoryAlbum: { | 125 case kAlbumCategoryAlbum: { |
| 97 std::string token; | 126 std::string token; |
| 98 if (!token_column->ReadString(i, &token) || token.empty() || | 127 if (!token_column.ReadString(i, &token) || token.empty() || |
| 99 !StartsWithASCII(token, kAlbumTokenPrefix, false)) { | 128 !StartsWithASCII(token, kAlbumTokenPrefix, false)) { |
| 100 continue; | 129 continue; |
| 101 } | 130 } |
| 102 | 131 |
| 103 albums_.push_back(AlbumInfo(name, timestamp, uid, base::FilePath())); | 132 albums_.push_back(AlbumInfo(name, timestamp, uid, base::FilePath())); |
| 104 break; | 133 break; |
| 105 } | 134 } |
| 106 case kAlbumCategoryFolder: { | 135 case kAlbumCategoryFolder: { |
| 107 std::string filename; | 136 std::string filename; |
| 108 if (!filename_column->ReadString(i, &filename) || filename.empty()) | 137 if (!filename_column.ReadString(i, &filename) || filename.empty()) |
| 109 continue; | 138 continue; |
| 110 | 139 |
| 111 base::FilePath path = | 140 base::FilePath path = |
| 112 base::FilePath(base::FilePath::FromUTF8Unsafe(filename)); | 141 base::FilePath(base::FilePath::FromUTF8Unsafe(filename)); |
| 113 | 142 |
| 114 folders_.push_back(AlbumInfo(name, timestamp, uid, path)); | 143 folders_.push_back(AlbumInfo(name, timestamp, uid, path)); |
| 115 break; | 144 break; |
| 116 } | 145 } |
| 117 default: { | 146 default: { |
| 118 break; | 147 break; |
| 119 } | 148 } |
| 120 } | 149 } |
| 121 } | 150 } |
| 122 | 151 |
| 123 initialized_ = true; | 152 initialized_ = true; |
| 124 return true; | 153 return true; |
| 125 } | 154 } |
| 126 | 155 |
| 127 } // namespace picasa | 156 } // namespace picasa |
| OLD | NEW |