OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 6 |
| 7 #include <map> |
| 8 #include <set> |
| 9 #include <iterator> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "base/port.h" |
| 13 #include "chrome/browser/sync/syncable/syncable.h" |
| 14 #include "chrome/browser/sync/util/event_sys-inl.h" |
| 15 #include "chrome/browser/sync/util/path_helpers.h" |
| 16 |
| 17 namespace syncable { |
| 18 |
| 19 static const PSTR_CHAR kSyncDataDatabaseFilename[] = PSTR("SyncData.sqlite3"); |
| 20 |
| 21 DirectoryManagerEvent DirectoryManagerShutdownEvent() { |
| 22 DirectoryManagerEvent event; |
| 23 event.what_happened = DirectoryManagerEvent::SHUTDOWN; |
| 24 return event; |
| 25 } |
| 26 |
| 27 // static |
| 28 const PathString DirectoryManager::GetSyncDataDatabaseFilename() { |
| 29 return PathString(kSyncDataDatabaseFilename); |
| 30 } |
| 31 |
| 32 const PathString DirectoryManager::GetSyncDataDatabasePath() const { |
| 33 PathString path(root_path_); |
| 34 path.append(kSyncDataDatabaseFilename); |
| 35 return path; |
| 36 } |
| 37 |
| 38 DirectoryManager::DirectoryManager(const PathString& path) |
| 39 : root_path_(AppendSlash(path)), |
| 40 channel_(new Channel(DirectoryManagerShutdownEvent())), |
| 41 managed_directory_(NULL) { |
| 42 CHECK(0 == pthread_mutex_init(&mutex_, NULL)); |
| 43 } |
| 44 |
| 45 DirectoryManager::~DirectoryManager() { |
| 46 DCHECK_EQ(managed_directory_, static_cast<Directory*>(NULL)) |
| 47 << "Dir " << managed_directory_->name() << " not closed!"; |
| 48 pthread_mutex_lock(&mutex_); |
| 49 delete channel_; |
| 50 pthread_mutex_unlock(&mutex_); |
| 51 CHECK(0 == pthread_mutex_destroy(&mutex_)); |
| 52 } |
| 53 |
| 54 bool DirectoryManager::Open(const PathString& name) { |
| 55 bool was_open = false; |
| 56 const DirOpenResult result = OpenImpl(name, |
| 57 GetSyncDataDatabasePath(), &was_open); |
| 58 if (!was_open) { |
| 59 DirectoryManagerEvent event; |
| 60 event.dirname = name; |
| 61 if (syncable::OPENED == result) { |
| 62 event.what_happened = DirectoryManagerEvent::OPENED; |
| 63 } else { |
| 64 event.what_happened = DirectoryManagerEvent::OPEN_FAILED; |
| 65 event.error = result; |
| 66 } |
| 67 channel_->NotifyListeners(event); |
| 68 } |
| 69 return syncable::OPENED == result; |
| 70 } |
| 71 |
| 72 // Opens a directory. Returns false on error. |
| 73 DirOpenResult DirectoryManager::OpenImpl(const PathString& name, |
| 74 const PathString& path, |
| 75 bool* was_open) { |
| 76 pthread_mutex_lock(&mutex_); |
| 77 // Check to see if it's already open. |
| 78 bool opened = false; |
| 79 if (managed_directory_) { |
| 80 DCHECK_EQ(ComparePathNames(name, managed_directory_->name()), 0) |
| 81 << "Can't open more than one directory."; |
| 82 opened = *was_open = true; |
| 83 } |
| 84 pthread_mutex_unlock(&mutex_); |
| 85 if (opened) |
| 86 return syncable::OPENED; |
| 87 // Otherwise, open it. |
| 88 |
| 89 Directory* dir = new Directory; |
| 90 const DirOpenResult result = dir->Open(path, name); |
| 91 if (syncable::OPENED == result) { |
| 92 pthread_mutex_lock(&mutex_); |
| 93 managed_directory_ = dir; |
| 94 pthread_mutex_unlock(&mutex_); |
| 95 } else { |
| 96 delete dir; |
| 97 } |
| 98 return result; |
| 99 } |
| 100 |
| 101 // Marks a directory as closed. It might take a while until all the |
| 102 // file handles and resources are freed by other threads. |
| 103 void DirectoryManager::Close(const PathString& name) { |
| 104 // Erase from mounted and opened directory lists. |
| 105 pthread_mutex_lock(&mutex_); |
| 106 |
| 107 if (!managed_directory_ || |
| 108 ComparePathNames(name, managed_directory_->name()) != 0) { |
| 109 // It wasn't open; |
| 110 pthread_mutex_unlock(&mutex_); |
| 111 return; |
| 112 } |
| 113 pthread_mutex_unlock(&mutex_); |
| 114 |
| 115 // Notify listeners. |
| 116 managed_directory_->channel()->NotifyListeners(DIRECTORY_CLOSED); |
| 117 DirectoryManagerEvent event = { DirectoryManagerEvent::CLOSED, name }; |
| 118 channel_->NotifyListeners(event); |
| 119 |
| 120 delete managed_directory_; |
| 121 managed_directory_ = NULL; |
| 122 } |
| 123 |
| 124 // Marks all directories as closed. It might take a while until all the |
| 125 // file handles and resources are freed by other threads. |
| 126 void DirectoryManager::CloseAllDirectories() { |
| 127 if (managed_directory_) |
| 128 Close(managed_directory_->name()); |
| 129 } |
| 130 |
| 131 void DirectoryManager::FinalSaveChangesForAll() { |
| 132 pthread_mutex_lock(&mutex_); |
| 133 if (managed_directory_) |
| 134 managed_directory_->SaveChanges(); |
| 135 pthread_mutex_unlock(&mutex_); |
| 136 } |
| 137 |
| 138 void DirectoryManager::GetOpenDirectories(DirNames* result) { |
| 139 result->clear(); |
| 140 pthread_mutex_lock(&mutex_); |
| 141 if (managed_directory_) |
| 142 result->push_back(managed_directory_->name()); |
| 143 pthread_mutex_unlock(&mutex_); |
| 144 } |
| 145 |
| 146 ScopedDirLookup::ScopedDirLookup(DirectoryManager* dirman, |
| 147 const PathString& name) : dirman_(dirman) { |
| 148 dir_ = dirman->managed_directory_ && |
| 149 (ComparePathNames(name, dirman->managed_directory_->name()) == 0) ? |
| 150 dirman->managed_directory_ : NULL; |
| 151 good_ = dir_; |
| 152 good_checked_ = false; |
| 153 } |
| 154 |
| 155 ScopedDirLookup::~ScopedDirLookup() { } |
| 156 |
| 157 Directory* ScopedDirLookup::operator -> () const { |
| 158 CHECK(good_checked_); |
| 159 DCHECK(good_); |
| 160 return dir_; |
| 161 } |
| 162 |
| 163 ScopedDirLookup::operator Directory* () const { |
| 164 CHECK(good_checked_); |
| 165 DCHECK(good_); |
| 166 return dir_; |
| 167 } |
| 168 |
| 169 } // namespace syncable |
OLD | NEW |