directory_manager.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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#pragma once 15 16#include <string> 17#include <vector> 18 19#include "base/basictypes.h" 20#include "base/file_path.h" 21#include "base/synchronization/lock.h" 22#include "chrome/browser/sync/syncable/dir_open_result.h" 23#include "chrome/browser/sync/syncable/path_name_cmp.h" 24#include "chrome/browser/sync/syncable/syncable.h" 25#include "chrome/browser/sync/util/cryptographer.h" 26#include "chrome/common/deprecated/event_sys.h" 27 28namespace sync_api { class BaseTransaction; } 29 30namespace syncable { 31 32struct DirectoryManagerEvent { 33 enum { 34 CLOSED, 35 CLOSED_ALL, 36 SHUTDOWN, 37 } what_happened; 38 std::string dirname; 39 typedef DirectoryManagerEvent EventType; 40 static inline bool IsChannelShutdownEvent(const EventType& event) { 41 return SHUTDOWN == event.what_happened; 42 } 43}; 44 45DirectoryManagerEvent DirectoryManagerShutdownEvent(); 46 47class DirectoryManager { 48 public: 49 typedef EventChannel<DirectoryManagerEvent> Channel; 50 51 // root_path specifies where db is stored. 52 explicit DirectoryManager(const FilePath& root_path); 53 virtual ~DirectoryManager(); 54 55 static const FilePath GetSyncDataDatabaseFilename(); 56 const FilePath GetSyncDataDatabasePath() const; 57 58 // Opens a directory. Returns false on error. 59 // Name parameter is the the user's login, 60 // MUST already have been converted to a common case. 61 bool Open(const std::string& name); 62 63 // Marks a directory as closed. It might take a while until all the 64 // file handles and resources are freed by other threads. 65 void Close(const std::string& name); 66 67 // Should be called at App exit. 68 void FinalSaveChangesForAll(); 69 70 // Gets the list of currently open directory names. 71 typedef std::vector<std::string> DirNames; 72 void GetOpenDirectories(DirNames* result); 73 74 Channel* channel() const { return channel_; } 75 76 browser_sync::Cryptographer* cryptographer() const { 77 return cryptographer_.get(); 78 } 79 80 protected: 81 DirOpenResult OpenImpl(const std::string& name, const FilePath& path, 82 bool* was_open); 83 84 // Helpers for friend class ScopedDirLookup: 85 friend class ScopedDirLookup; 86 87 const FilePath root_path_; 88 89 // protects managed_directory_ 90 base::Lock lock_; 91 Directory* managed_directory_; 92 93 Channel* const channel_; 94 95 scoped_ptr<browser_sync::Cryptographer> cryptographer_; 96 97 private: 98 DISALLOW_COPY_AND_ASSIGN(DirectoryManager); 99}; 100 101 102class ScopedDirLookup { 103 public: 104 ScopedDirLookup(DirectoryManager* dirman, const std::string& 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_ 129