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/drive/drive_files.h" | 5 #include "chrome/browser/chromeos/drive/drive_files.h" |
6 | 6 |
7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
8 #include "chrome/browser/chromeos/drive/drive.pb.h" | 8 #include "chrome/browser/chromeos/drive/drive.pb.h" |
9 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 9 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
10 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h" | 10 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 } | 108 } |
109 } | 109 } |
110 entry->set_base_name(full_file_name.value()); | 110 entry->set_base_name(full_file_name.value()); |
111 | 111 |
112 DVLOG(1) << "AddEntry: dir = " << GetFilePath().value() | 112 DVLOG(1) << "AddEntry: dir = " << GetFilePath().value() |
113 << ", file = " + entry->base_name() | 113 << ", file = " + entry->base_name() |
114 << ", parent resource = " << entry->parent_resource_id() | 114 << ", parent resource = " << entry->parent_resource_id() |
115 << ", resource = " + entry->resource_id(); | 115 << ", resource = " + entry->resource_id(); |
116 | 116 |
117 // Setup child and parent links. | 117 // Setup child and parent links. |
118 if (entry->proto().file_info().is_directory()) { | 118 children_.insert(std::make_pair(entry->base_name(), |
119 child_directories_.insert(std::make_pair(entry->base_name(), | 119 entry->resource_id())); |
120 entry->resource_id())); | |
121 } else { | |
122 child_files_.insert(std::make_pair(entry->base_name(), | |
123 entry->resource_id())); | |
124 } | |
125 entry->set_parent_resource_id(proto_.resource_id()); | 120 entry->set_parent_resource_id(proto_.resource_id()); |
126 } | 121 } |
127 | 122 |
128 void DriveDirectory::TakeOverEntries(DriveDirectory* dir) { | 123 void DriveDirectory::TakeOverEntries(DriveDirectory* dir) { |
129 for (ChildMap::const_iterator iter = dir->child_files_.begin(); | 124 for (ChildMap::const_iterator iter = dir->children_.begin(); |
130 iter != dir->child_files_.end(); ++iter) { | 125 iter != dir->children_.end(); ++iter) { |
131 TakeOverEntry(iter->second); | 126 TakeOverEntry(iter->second); |
132 } | 127 } |
133 dir->child_files_.clear(); | 128 dir->children_.clear(); |
134 | |
135 for (ChildMap::iterator iter = dir->child_directories_.begin(); | |
136 iter != dir->child_directories_.end(); ++iter) { | |
137 TakeOverEntry(iter->second); | |
138 } | |
139 dir->child_directories_.clear(); | |
140 } | 129 } |
141 | 130 |
142 void DriveDirectory::TakeOverEntry(const std::string& resource_id) { | 131 void DriveDirectory::TakeOverEntry(const std::string& resource_id) { |
143 DriveEntry* entry = resource_metadata_->GetEntryByResourceId(resource_id); | 132 DriveEntry* entry = resource_metadata_->GetEntryByResourceId(resource_id); |
144 DCHECK(entry); | 133 DCHECK(entry); |
145 resource_metadata_->RemoveEntryFromResourceMap(resource_id); | 134 resource_metadata_->RemoveEntryFromResourceMap(resource_id); |
146 entry->set_parent_resource_id(std::string()); | 135 entry->set_parent_resource_id(std::string()); |
147 AddEntry(entry); | 136 AddEntry(entry); |
148 } | 137 } |
149 | 138 |
150 void DriveDirectory::RemoveEntry(DriveEntry* entry) { | 139 void DriveDirectory::RemoveEntry(DriveEntry* entry) { |
151 DCHECK(entry); | 140 DCHECK(entry); |
152 | 141 |
153 RemoveChild(entry); | 142 RemoveChild(entry); |
154 delete entry; | 143 delete entry; |
155 } | 144 } |
156 | 145 |
157 std::string DriveDirectory::FindChild( | 146 std::string DriveDirectory::FindChild( |
158 const base::FilePath::StringType& file_name) const { | 147 const base::FilePath::StringType& file_name) const { |
159 ChildMap::const_iterator iter = child_files_.find(file_name); | 148 ChildMap::const_iterator iter = children_.find(file_name); |
160 if (iter != child_files_.end()) | 149 if (iter != children_.end()) |
161 return iter->second; | 150 return iter->second; |
162 | |
163 iter = child_directories_.find(file_name); | |
164 if (iter != child_directories_.end()) | |
165 return iter->second; | |
166 | |
167 return std::string(); | 151 return std::string(); |
168 } | 152 } |
169 | 153 |
170 void DriveDirectory::RemoveChild(DriveEntry* entry) { | 154 void DriveDirectory::RemoveChild(DriveEntry* entry) { |
171 DCHECK(entry); | 155 DCHECK(entry); |
172 | 156 |
173 const std::string& base_name(entry->base_name()); | 157 const std::string& base_name(entry->base_name()); |
174 // entry must be present in this directory. | 158 // entry must be present in this directory. |
175 DCHECK_EQ(entry->resource_id(), FindChild(base_name)); | 159 DCHECK_EQ(entry->resource_id(), FindChild(base_name)); |
176 // Remove entry from resource map first. | 160 // Remove entry from resource map first. |
177 resource_metadata_->RemoveEntryFromResourceMap(entry->resource_id()); | 161 resource_metadata_->RemoveEntryFromResourceMap(entry->resource_id()); |
178 | 162 |
179 // Then delete it from tree. | 163 // Then delete it from tree. |
180 child_files_.erase(base_name); | 164 children_.erase(base_name); |
181 child_directories_.erase(base_name); | |
182 | 165 |
183 entry->set_parent_resource_id(std::string()); | 166 entry->set_parent_resource_id(std::string()); |
184 } | 167 } |
185 | 168 |
186 void DriveDirectory::RemoveChildren() { | 169 void DriveDirectory::RemoveChildren() { |
187 RemoveChildFiles(); | 170 RemoveChildFiles(); |
188 RemoveChildDirectories(); | 171 RemoveChildDirectories(); |
| 172 DCHECK(children_.empty()); |
189 } | 173 } |
190 | 174 |
191 void DriveDirectory::RemoveChildFiles() { | 175 void DriveDirectory::RemoveChildFiles() { |
192 DVLOG(1) << "RemoveChildFiles " << proto_.resource_id(); | 176 DVLOG(1) << "RemoveChildFiles " << proto_.resource_id(); |
193 for (ChildMap::const_iterator iter = child_files_.begin(); | 177 for (ChildMap::iterator iter = children_.begin(), iter_next = iter; |
194 iter != child_files_.end(); ++iter) { | 178 iter != children_.end(); iter = iter_next) { |
| 179 ++iter_next; |
195 DriveEntry* child = resource_metadata_->GetEntryByResourceId(iter->second); | 180 DriveEntry* child = resource_metadata_->GetEntryByResourceId(iter->second); |
196 DCHECK(child); | 181 DCHECK(child); |
197 resource_metadata_->RemoveEntryFromResourceMap(iter->second); | 182 if (!child->proto().file_info().is_directory()) { |
198 delete child; | 183 resource_metadata_->RemoveEntryFromResourceMap(iter->second); |
| 184 delete child; |
| 185 children_.erase(iter); |
| 186 } |
199 } | 187 } |
200 child_files_.clear(); | |
201 } | 188 } |
202 | 189 |
203 void DriveDirectory::RemoveChildDirectories() { | 190 void DriveDirectory::RemoveChildDirectories() { |
204 for (ChildMap::iterator iter = child_directories_.begin(); | 191 for (ChildMap::iterator iter = children_.begin(), iter_next = iter; |
205 iter != child_directories_.end(); ++iter) { | 192 iter != children_.end(); iter = iter_next) { |
| 193 ++iter_next; |
206 DriveDirectory* dir = resource_metadata_->GetEntryByResourceId( | 194 DriveDirectory* dir = resource_metadata_->GetEntryByResourceId( |
207 iter->second)->AsDriveDirectory(); | 195 iter->second)->AsDriveDirectory(); |
208 DCHECK(dir); | 196 if (dir) { |
209 // Remove directories recursively. | 197 // Remove directories recursively. |
210 dir->RemoveChildren(); | 198 dir->RemoveChildren(); |
211 resource_metadata_->RemoveEntryFromResourceMap(iter->second); | 199 resource_metadata_->RemoveEntryFromResourceMap(iter->second); |
212 delete dir; | 200 delete dir; |
| 201 children_.erase(iter); |
| 202 } |
213 } | 203 } |
214 child_directories_.clear(); | |
215 } | 204 } |
216 | 205 |
217 void DriveDirectory::GetChildDirectoryPaths( | 206 void DriveDirectory::GetChildDirectoryPaths( |
218 std::set<base::FilePath>* child_dirs) { | 207 std::set<base::FilePath>* child_dirs) { |
219 for (ChildMap::const_iterator iter = child_directories_.begin(); | 208 for (ChildMap::const_iterator iter = children_.begin(); |
220 iter != child_directories_.end(); ++iter) { | 209 iter != children_.end(); ++iter) { |
221 DriveDirectory* dir = resource_metadata_->GetEntryByResourceId( | 210 DriveDirectory* dir = resource_metadata_->GetEntryByResourceId( |
222 iter->second)->AsDriveDirectory(); | 211 iter->second)->AsDriveDirectory(); |
223 DCHECK(dir); | 212 if (dir) { |
224 child_dirs->insert(dir->GetFilePath()); | 213 child_dirs->insert(dir->GetFilePath()); |
225 dir->GetChildDirectoryPaths(child_dirs); | 214 dir->GetChildDirectoryPaths(child_dirs); |
| 215 } |
226 } | 216 } |
227 } | 217 } |
228 | 218 |
229 // Convert to/from proto. | 219 // Convert to/from proto. |
230 | 220 |
231 void DriveEntry::FromProto(const DriveEntryProto& proto) { | 221 void DriveEntry::FromProto(const DriveEntryProto& proto) { |
232 proto_ = proto; | 222 proto_ = proto; |
233 SetBaseNameFromTitle(); | 223 SetBaseNameFromTitle(); |
234 } | 224 } |
235 | 225 |
(...skipping 12 matching lines...) Expand all Loading... |
248 scoped_ptr<DriveDirectory> dir(resource_metadata_->CreateDriveDirectory()); | 238 scoped_ptr<DriveDirectory> dir(resource_metadata_->CreateDriveDirectory()); |
249 dir->FromProto(proto.child_directories(i)); | 239 dir->FromProto(proto.child_directories(i)); |
250 AddEntry(dir.release()); | 240 AddEntry(dir.release()); |
251 } | 241 } |
252 } | 242 } |
253 | 243 |
254 void DriveDirectory::ToProto(DriveDirectoryProto* proto) const { | 244 void DriveDirectory::ToProto(DriveDirectoryProto* proto) const { |
255 *proto->mutable_drive_entry() = proto_; | 245 *proto->mutable_drive_entry() = proto_; |
256 DCHECK(proto->drive_entry().file_info().is_directory()); | 246 DCHECK(proto->drive_entry().file_info().is_directory()); |
257 | 247 |
258 for (ChildMap::const_iterator iter = child_files_.begin(); | 248 for (ChildMap::const_iterator iter = children_.begin(); |
259 iter != child_files_.end(); ++iter) { | 249 iter != children_.end(); ++iter) { |
260 DriveEntry* file = resource_metadata_->GetEntryByResourceId( | 250 DriveEntry* entry = resource_metadata_->GetEntryByResourceId( |
261 iter->second); | 251 iter->second); |
262 DCHECK(file); | 252 DCHECK(entry); |
263 DCHECK(!file->proto().file_info().is_directory()); | 253 DriveDirectory* dir = entry->AsDriveDirectory(); |
264 *proto->add_child_files() = file->proto(); | 254 if (dir) |
265 } | 255 dir->ToProto(proto->add_child_directories()); |
266 for (ChildMap::const_iterator iter = child_directories_.begin(); | 256 else |
267 iter != child_directories_.end(); ++iter) { | 257 *proto->add_child_files() = entry->proto(); |
268 DriveDirectory* dir = resource_metadata_->GetEntryByResourceId( | |
269 iter->second)->AsDriveDirectory(); | |
270 DCHECK(dir); | |
271 dir->ToProto(proto->add_child_directories()); | |
272 } | 258 } |
273 } | 259 } |
274 | 260 |
275 scoped_ptr<DriveEntryProtoVector> DriveDirectory::ToProtoVector() const { | 261 scoped_ptr<DriveEntryProtoVector> DriveDirectory::ToProtoVector() const { |
276 scoped_ptr<DriveEntryProtoVector> entries(new DriveEntryProtoVector); | 262 scoped_ptr<DriveEntryProtoVector> entries(new DriveEntryProtoVector); |
277 // Use ToProtoFull, as we don't want to include children in |proto|. | 263 for (ChildMap::const_iterator iter = children_.begin(); |
278 for (ChildMap::const_iterator iter = child_files_.begin(); | 264 iter != children_.end(); ++iter) { |
279 iter != child_files_.end(); ++iter) { | |
280 const DriveEntryProto& proto = | 265 const DriveEntryProto& proto = |
281 resource_metadata_->GetEntryByResourceId(iter->second)->proto(); | 266 resource_metadata_->GetEntryByResourceId(iter->second)->proto(); |
282 entries->push_back(proto); | 267 entries->push_back(proto); |
283 } | 268 } |
284 for (ChildMap::const_iterator iter = child_directories_.begin(); | |
285 iter != child_directories_.end(); ++iter) { | |
286 const DriveEntryProto& proto = | |
287 resource_metadata_->GetEntryByResourceId(iter->second)->proto(); | |
288 entries->push_back(proto); | |
289 } | |
290 | |
291 return entries.Pass(); | 269 return entries.Pass(); |
292 } | 270 } |
293 | 271 |
294 } // namespace drive | 272 } // namespace drive |
OLD | NEW |