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 // This used to do a lot of TLS-based management of multiple Directory objects. |
| 6 // We now can access Directory objects from any thread for general purpose |
| 7 // operations and we only ever have one Directory, so this class isn't doing |
| 8 // anything too fancy besides keeping calling and access conventions the same |
| 9 // for now. |
| 10 // TODO(timsteele): We can probably nuke this entire class and use raw |
| 11 // Directory objects everywhere. |
| 12 #ifndef CHROME_BROWSER_SYNC_SYNCABLE_DIRECTORY_MANAGER_H_ |
| 13 #define CHROME_BROWSER_SYNC_SYNCABLE_DIRECTORY_MANAGER_H_ |
| 14 |
| 15 #include <pthread.h> |
| 16 |
| 17 #include <vector> |
| 18 |
| 19 #include "base/atomicops.h" |
| 20 #include "base/basictypes.h" |
| 21 #include "chrome/browser/sync/syncable/dir_open_result.h" |
| 22 #include "chrome/browser/sync/syncable/path_name_cmp.h" |
| 23 #include "chrome/browser/sync/syncable/syncable.h" |
| 24 #include "chrome/browser/sync/util/event_sys.h" |
| 25 #include "chrome/browser/sync/util/sync_types.h" |
| 26 |
| 27 namespace sync_api { class BaseTransaction; } |
| 28 |
| 29 namespace syncable { |
| 30 |
| 31 struct DirectoryManagerEvent { |
| 32 enum { |
| 33 OPEN_FAILED, |
| 34 OPENED, |
| 35 CLOSED, |
| 36 CLOSED_ALL, |
| 37 SHUTDOWN, |
| 38 } what_happened; |
| 39 PathString dirname; |
| 40 DirOpenResult error; // Only for OPEN_FAILED. |
| 41 typedef DirectoryManagerEvent EventType; |
| 42 static inline bool IsChannelShutdownEvent(const EventType& event) { |
| 43 return SHUTDOWN == event.what_happened; |
| 44 } |
| 45 }; |
| 46 |
| 47 DirectoryManagerEvent DirectoryManagerShutdownEvent(); |
| 48 |
| 49 class DirectoryManager { |
| 50 public: |
| 51 typedef EventChannel<DirectoryManagerEvent> Channel; |
| 52 |
| 53 // root_path specifies where db is stored. |
| 54 explicit DirectoryManager(const PathString& root_path); |
| 55 ~DirectoryManager(); |
| 56 |
| 57 static const PathString GetSyncDataDatabaseFilename(); |
| 58 const PathString GetSyncDataDatabasePath() const; |
| 59 |
| 60 // Opens a directory. Returns false on error. |
| 61 // Name parameter is the the user's login, |
| 62 // MUST already have been converted to a common case. |
| 63 bool Open(const PathString& name); |
| 64 |
| 65 // Marks a directory as closed. It might take a while until all the |
| 66 // file handles and resources are freed by other threads. |
| 67 void Close(const PathString& name); |
| 68 |
| 69 // Marks all directories as closed. It might take a while until all the |
| 70 // file handles and resources are freed by other threads. |
| 71 void CloseAllDirectories(); |
| 72 |
| 73 // Should be called at App exit. |
| 74 void FinalSaveChangesForAll(); |
| 75 |
| 76 // Gets the list of currently open directory names. |
| 77 typedef std::vector<PathString> DirNames; |
| 78 void GetOpenDirectories(DirNames* result); |
| 79 |
| 80 Channel* channel() const { return channel_; } |
| 81 |
| 82 protected: |
| 83 DirOpenResult OpenImpl(const PathString& name, const PathString& path, |
| 84 bool* was_open); |
| 85 |
| 86 // Helpers for friend class ScopedDirLookup: |
| 87 friend class ScopedDirLookup; |
| 88 |
| 89 const PathString root_path_; |
| 90 // protects managed_directory_ |
| 91 mutable pthread_mutex_t mutex_; |
| 92 Directory* managed_directory_; |
| 93 |
| 94 Channel* const channel_; |
| 95 |
| 96 private: |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(DirectoryManager); |
| 99 }; |
| 100 |
| 101 |
| 102 class ScopedDirLookup { |
| 103 public: |
| 104 ScopedDirLookup(DirectoryManager* dirman, const PathString& name); |
| 105 ~ScopedDirLookup(); |
| 106 |
| 107 inline bool good() { |
| 108 good_checked_ = true; |
| 109 return good_; |
| 110 } |
| 111 Directory* operator -> () const; |
| 112 operator Directory* () const; |
| 113 |
| 114 protected: // Don't allow creation on heap, except by sync API wrapper. |
| 115 friend class sync_api::BaseTransaction; |
| 116 void* operator new(size_t size) { return (::operator new)(size); } |
| 117 |
| 118 Directory* dir_; |
| 119 bool good_; |
| 120 // Ensure that the programmer checks good before using the ScopedDirLookup |
| 121 // This member should can be removed if it ever shows up in profiling |
| 122 bool good_checked_; |
| 123 DirectoryManager* const dirman_; |
| 124 }; |
| 125 |
| 126 } // namespace syncable |
| 127 |
| 128 #endif // CHROME_BROWSER_SYNC_SYNCABLE_DIRECTORY_MANAGER_H_ |
OLD | NEW |