OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/sync/syncable/directory_manager.h" | 5 #include "chrome/browser/sync/syncable/directory_manager.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <iterator> | 9 #include <iterator> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 } | 37 } |
38 | 38 |
39 DirectoryManager::DirectoryManager(const FilePath& path) | 39 DirectoryManager::DirectoryManager(const FilePath& path) |
40 : root_path_(path), | 40 : root_path_(path), |
41 managed_directory_(NULL), | 41 managed_directory_(NULL), |
42 channel_(new Channel(DirectoryManagerShutdownEvent())), | 42 channel_(new Channel(DirectoryManagerShutdownEvent())), |
43 cryptographer_(new Cryptographer) { | 43 cryptographer_(new Cryptographer) { |
44 } | 44 } |
45 | 45 |
46 DirectoryManager::~DirectoryManager() { | 46 DirectoryManager::~DirectoryManager() { |
47 AutoLock lock(lock_); | 47 base::AutoLock lock(lock_); |
48 DCHECK_EQ(managed_directory_, static_cast<Directory*>(NULL)) | 48 DCHECK_EQ(managed_directory_, static_cast<Directory*>(NULL)) |
49 << "Dir " << managed_directory_->name() << " not closed!"; | 49 << "Dir " << managed_directory_->name() << " not closed!"; |
50 delete channel_; | 50 delete channel_; |
51 } | 51 } |
52 | 52 |
53 bool DirectoryManager::Open(const std::string& name) { | 53 bool DirectoryManager::Open(const std::string& name) { |
54 bool was_open = false; | 54 bool was_open = false; |
55 const DirOpenResult result = OpenImpl(name, | 55 const DirOpenResult result = OpenImpl(name, |
56 GetSyncDataDatabasePath(), &was_open); | 56 GetSyncDataDatabasePath(), &was_open); |
57 return syncable::OPENED == result; | 57 return syncable::OPENED == result; |
58 } | 58 } |
59 | 59 |
60 // Opens a directory. Returns false on error. | 60 // Opens a directory. Returns false on error. |
61 DirOpenResult DirectoryManager::OpenImpl(const std::string& name, | 61 DirOpenResult DirectoryManager::OpenImpl(const std::string& name, |
62 const FilePath& path, | 62 const FilePath& path, |
63 bool* was_open) { | 63 bool* was_open) { |
64 bool opened = false; | 64 bool opened = false; |
65 { | 65 { |
66 AutoLock lock(lock_); | 66 base::AutoLock lock(lock_); |
67 // Check to see if it's already open. | 67 // Check to see if it's already open. |
68 if (managed_directory_) { | 68 if (managed_directory_) { |
69 DCHECK_EQ(ComparePathNames(name, managed_directory_->name()), 0) | 69 DCHECK_EQ(ComparePathNames(name, managed_directory_->name()), 0) |
70 << "Can't open more than one directory."; | 70 << "Can't open more than one directory."; |
71 opened = *was_open = true; | 71 opened = *was_open = true; |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 if (opened) | 75 if (opened) |
76 return syncable::OPENED; | 76 return syncable::OPENED; |
77 // Otherwise, open it. | 77 // Otherwise, open it. |
78 | 78 |
79 scoped_ptr<Directory> dir(new Directory); | 79 scoped_ptr<Directory> dir(new Directory); |
80 const DirOpenResult result = dir->Open(path, name); | 80 const DirOpenResult result = dir->Open(path, name); |
81 if (syncable::OPENED == result) { | 81 if (syncable::OPENED == result) { |
82 AutoLock lock(lock_); | 82 base::AutoLock lock(lock_); |
83 managed_directory_ = dir.release(); | 83 managed_directory_ = dir.release(); |
84 } | 84 } |
85 return result; | 85 return result; |
86 } | 86 } |
87 | 87 |
88 // Marks a directory as closed. It might take a while until all the file | 88 // Marks a directory as closed. It might take a while until all the file |
89 // handles and resources are freed by other threads. | 89 // handles and resources are freed by other threads. |
90 void DirectoryManager::Close(const std::string& name) { | 90 void DirectoryManager::Close(const std::string& name) { |
91 // Erase from mounted and opened directory lists. | 91 // Erase from mounted and opened directory lists. |
92 { | 92 { |
93 AutoLock lock(lock_); | 93 base::AutoLock lock(lock_); |
94 if (!managed_directory_ || | 94 if (!managed_directory_ || |
95 ComparePathNames(name, managed_directory_->name()) != 0) { | 95 ComparePathNames(name, managed_directory_->name()) != 0) { |
96 // It wasn't open. | 96 // It wasn't open. |
97 return; | 97 return; |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 // TODO(timsteele): No lock?! | 101 // TODO(timsteele): No lock?! |
102 // Notify listeners. | 102 // Notify listeners. |
103 managed_directory_->channel()->NotifyListeners(DIRECTORY_CLOSED); | 103 managed_directory_->channel()->NotifyListeners(DIRECTORY_CLOSED); |
104 DirectoryManagerEvent event = { DirectoryManagerEvent::CLOSED, name }; | 104 DirectoryManagerEvent event = { DirectoryManagerEvent::CLOSED, name }; |
105 channel_->NotifyListeners(event); | 105 channel_->NotifyListeners(event); |
106 | 106 |
107 delete managed_directory_; | 107 delete managed_directory_; |
108 managed_directory_ = NULL; | 108 managed_directory_ = NULL; |
109 } | 109 } |
110 | 110 |
111 void DirectoryManager::FinalSaveChangesForAll() { | 111 void DirectoryManager::FinalSaveChangesForAll() { |
112 AutoLock lock(lock_); | 112 base::AutoLock lock(lock_); |
113 if (managed_directory_) | 113 if (managed_directory_) |
114 managed_directory_->SaveChanges(); | 114 managed_directory_->SaveChanges(); |
115 } | 115 } |
116 | 116 |
117 void DirectoryManager::GetOpenDirectories(DirNames* result) { | 117 void DirectoryManager::GetOpenDirectories(DirNames* result) { |
118 result->clear(); | 118 result->clear(); |
119 AutoLock lock(lock_); | 119 base::AutoLock lock(lock_); |
120 if (managed_directory_) | 120 if (managed_directory_) |
121 result->push_back(managed_directory_->name()); | 121 result->push_back(managed_directory_->name()); |
122 } | 122 } |
123 | 123 |
124 ScopedDirLookup::ScopedDirLookup(DirectoryManager* dirman, | 124 ScopedDirLookup::ScopedDirLookup(DirectoryManager* dirman, |
125 const std::string& name) : dirman_(dirman) { | 125 const std::string& name) : dirman_(dirman) { |
126 dir_ = dirman->managed_directory_ && | 126 dir_ = dirman->managed_directory_ && |
127 (ComparePathNames(name, dirman->managed_directory_->name()) == 0) ? | 127 (ComparePathNames(name, dirman->managed_directory_->name()) == 0) ? |
128 dirman->managed_directory_ : NULL; | 128 dirman->managed_directory_ : NULL; |
129 good_ = dir_ != NULL; | 129 good_ = dir_ != NULL; |
130 good_checked_ = false; | 130 good_checked_ = false; |
131 } | 131 } |
132 | 132 |
133 ScopedDirLookup::~ScopedDirLookup() { } | 133 ScopedDirLookup::~ScopedDirLookup() { } |
134 | 134 |
135 Directory* ScopedDirLookup::operator -> () const { | 135 Directory* ScopedDirLookup::operator -> () const { |
136 CHECK(good_checked_); | 136 CHECK(good_checked_); |
137 DCHECK(good_); | 137 DCHECK(good_); |
138 return dir_; | 138 return dir_; |
139 } | 139 } |
140 | 140 |
141 ScopedDirLookup::operator Directory* () const { | 141 ScopedDirLookup::operator Directory* () const { |
142 CHECK(good_checked_); | 142 CHECK(good_checked_); |
143 DCHECK(good_); | 143 DCHECK(good_); |
144 return dir_; | 144 return dir_; |
145 } | 145 } |
146 | 146 |
147 } // namespace syncable | 147 } // namespace syncable |
OLD | NEW |