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 | 9 |
9 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
11 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
12 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
13 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | 14 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
14 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 15 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
15 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 16 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
16 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" | 17 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 root_->set_title(kGDataRootDirectory); | 229 root_->set_title(kGDataRootDirectory); |
229 root_->SetBaseNameFromTitle(); | 230 root_->SetBaseNameFromTitle(); |
230 root_->set_resource_id(root_id); | 231 root_->set_resource_id(root_id); |
231 AddEntryToResourceMap(root_.get()); | 232 AddEntryToResourceMap(root_.get()); |
232 } | 233 } |
233 | 234 |
234 void GDataDirectoryService::ClearRoot() { | 235 void GDataDirectoryService::ClearRoot() { |
235 // Note that children have a reference to root_, | 236 // Note that children have a reference to root_, |
236 // so we need to delete them here. | 237 // so we need to delete them here. |
237 root_->RemoveChildren(); | 238 root_->RemoveChildren(); |
238 RemoveEntryFromResourceMap(root_.get()); | 239 RemoveEntryFromResourceMap(root_->resource_id()); |
239 DCHECK(resource_map_.empty()); | 240 DCHECK(resource_map_.empty()); |
240 resource_map_.clear(); | 241 resource_map_.clear(); |
241 root_.reset(); | 242 root_.reset(); |
242 } | 243 } |
243 | 244 |
244 void GDataDirectoryService::AddEntryToDirectory( | 245 void GDataDirectoryService::AddEntryToDirectory( |
245 GDataDirectory* directory, | 246 GDataDirectory* directory, |
246 GDataEntry* new_entry, | 247 GDataEntry* new_entry, |
247 const FileMoveCallback& callback) { | 248 const FileMoveCallback& callback) { |
248 DCHECK(directory); | 249 DCHECK(directory); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 DCHECK(parent); | 291 DCHECK(parent); |
291 DCHECK(!callback.is_null()); | 292 DCHECK(!callback.is_null()); |
292 DVLOG(1) << "RemoveEntryFromParent " << entry->GetFilePath().value(); | 293 DVLOG(1) << "RemoveEntryFromParent " << entry->GetFilePath().value(); |
293 | 294 |
294 parent->RemoveEntry(entry); | 295 parent->RemoveEntry(entry); |
295 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 296 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
296 base::Bind(callback, GDATA_FILE_OK, parent->GetFilePath())); | 297 base::Bind(callback, GDATA_FILE_OK, parent->GetFilePath())); |
297 } | 298 } |
298 | 299 |
299 void GDataDirectoryService::AddEntryToResourceMap(GDataEntry* entry) { | 300 void GDataDirectoryService::AddEntryToResourceMap(GDataEntry* entry) { |
300 // GDataFileSystem has already locked. | |
301 DVLOG(1) << "AddEntryToResourceMap " << entry->resource_id(); | 301 DVLOG(1) << "AddEntryToResourceMap " << entry->resource_id(); |
302 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); | 302 DCHECK(!entry->resource_id().empty()); |
| 303 std::pair<ResourceMap::iterator, bool> ret = |
| 304 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); |
| 305 DCHECK(ret.second); // resource_id did not previously exist in the map. |
303 } | 306 } |
304 | 307 |
305 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { | 308 void GDataDirectoryService::RemoveEntryFromResourceMap( |
306 // GDataFileSystem has already locked. | 309 const std::string& resource_id) { |
307 resource_map_.erase(entry->resource_id()); | 310 DVLOG(1) << "RemoveEntryFromResourceMap " << resource_id; |
| 311 DCHECK(!resource_id.empty()); |
| 312 size_t ret = resource_map_.erase(resource_id); |
| 313 DCHECK_EQ(1u, ret); // resource_id was found in the map. |
308 } | 314 } |
309 | 315 |
310 GDataEntry* GDataDirectoryService::FindEntryByPathSync( | 316 GDataEntry* GDataDirectoryService::FindEntryByPathSync( |
311 const FilePath& file_path) { | 317 const FilePath& file_path) { |
312 if (file_path == root_->GetFilePath()) | 318 if (file_path == root_->GetFilePath()) |
313 return root_.get(); | 319 return root_.get(); |
314 | 320 |
315 std::vector<FilePath::StringType> components; | 321 std::vector<FilePath::StringType> components; |
316 file_path.GetComponents(&components); | 322 file_path.GetComponents(&components); |
317 GDataDirectory* current_dir = root_.get(); | 323 GDataDirectory* current_dir = root_.get(); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 callback.Run(GDATA_FILE_ERROR_NOT_FOUND, FilePath()); | 468 callback.Run(GDATA_FILE_ERROR_NOT_FOUND, FilePath()); |
463 return; | 469 return; |
464 } | 470 } |
465 | 471 |
466 GDataDirectory* directory = directory_entry->AsGDataDirectory(); | 472 GDataDirectory* directory = directory_entry->AsGDataDirectory(); |
467 if (!directory) { | 473 if (!directory) { |
468 callback.Run(GDATA_FILE_ERROR_NOT_A_DIRECTORY, FilePath()); | 474 callback.Run(GDATA_FILE_ERROR_NOT_A_DIRECTORY, FilePath()); |
469 return; | 475 return; |
470 } | 476 } |
471 | 477 |
| 478 DVLOG(1) << "RefreshDirectoryInternal"; |
472 directory->RemoveChildFiles(); | 479 directory->RemoveChildFiles(); |
473 // Add files from file_map. | 480 // Add files from file_map. |
474 for (ResourceMap::const_iterator it = file_map.begin(); | 481 for (ResourceMap::const_iterator it = file_map.begin(); |
475 it != file_map.end(); ++it) { | 482 it != file_map.end(); ++it) { |
476 scoped_ptr<GDataEntry> entry(it->second); | 483 scoped_ptr<GDataEntry> entry(it->second); |
477 // Skip if it's not a file (i.e. directory). | 484 // Skip if it's not a file (i.e. directory). |
478 if (!entry->AsGDataFile()) | 485 if (!entry->AsGDataFile()) |
479 continue; | 486 continue; |
480 directory->AddEntry(entry.release()); | 487 directory->AddEntry(entry.release()); |
481 } | 488 } |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 DCHECK(result.get()); | 760 DCHECK(result.get()); |
754 | 761 |
755 result->second.path = second_path; | 762 result->second.path = second_path; |
756 result->second.error = error; | 763 result->second.error = error; |
757 result->second.proto = entry_proto.Pass(); | 764 result->second.proto = entry_proto.Pass(); |
758 | 765 |
759 callback.Run(result.Pass()); | 766 callback.Run(result.Pass()); |
760 } | 767 } |
761 | 768 |
762 } // namespace gdata | 769 } // namespace gdata |
OLD | NEW |