• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/syncable.h"
24 #include "chrome/browser/sync/util/cryptographer.h"
25 #include "chrome/common/deprecated/event_sys.h"
26 
27 namespace sync_api { class BaseTransaction; }
28 namespace syncable { class BaseTransaction; }
29 
30 namespace syncable {
31 
32 struct DirectoryManagerEvent {
33   enum {
34     CLOSED,
35     CLOSED_ALL,
36     SHUTDOWN,
37   } what_happened;
38   std::string dirname;
39   typedef DirectoryManagerEvent EventType;
IsChannelShutdownEventDirectoryManagerEvent40   static inline bool IsChannelShutdownEvent(const EventType& event) {
41     return SHUTDOWN == event.what_happened;
42   }
43 };
44 
45 DirectoryManagerEvent DirectoryManagerShutdownEvent();
46 
47 class 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 
channel()74   Channel* channel() const { return channel_; }
75 
76   // Wrappers for cryptographer() that enforce holding a transaction.
77   // Note: the Cryptographer is NOT thread safe. It must only be accessed while
78   // the transaction is still active. The Cryptographer's pointer should not be
79   // stored separately.
GetCryptographer(const sync_api::BaseTransaction * trans)80   browser_sync::Cryptographer* GetCryptographer(
81       const sync_api::BaseTransaction* trans) const { return cryptographer(); }
GetCryptographer(const syncable::BaseTransaction * trans)82   browser_sync::Cryptographer* GetCryptographer(
83       const syncable::BaseTransaction* trans) const { return cryptographer(); }
84 
85  protected:
cryptographer()86   browser_sync::Cryptographer* cryptographer() const {
87     return cryptographer_.get();
88   }
89 
90   DirOpenResult OpenImpl(const std::string& name, const FilePath& path,
91                          bool* was_open);
92 
93   // Helpers for friend class ScopedDirLookup:
94   friend class ScopedDirLookup;
95 
96   const FilePath root_path_;
97 
98   // protects managed_directory_
99   base::Lock lock_;
100   Directory* managed_directory_;
101 
102   Channel* const channel_;
103 
104   scoped_ptr<browser_sync::Cryptographer> cryptographer_;
105 
106  private:
107   DISALLOW_COPY_AND_ASSIGN(DirectoryManager);
108 };
109 
110 
111 class ScopedDirLookup {
112  public:
113   ScopedDirLookup(DirectoryManager* dirman, const std::string& name);
114   ~ScopedDirLookup();
115 
good()116   inline bool good() {
117     good_checked_ = true;
118     return good_;
119   }
120   Directory* operator -> () const;
121   operator Directory* () const;
122 
123  protected:  // Don't allow creation on heap, except by sync API wrapper.
124   friend class sync_api::BaseTransaction;
new(size_t size)125   void* operator new(size_t size) { return (::operator new)(size); }
126 
127   Directory* dir_;
128   bool good_;
129   // Ensure that the programmer checks good before using the ScopedDirLookup.
130   // This member should can be removed if it ever shows up in profiling
131   bool good_checked_;
132   DirectoryManager* const dirman_;
133 };
134 
135 }  // namespace syncable
136 
137 #endif  // CHROME_BROWSER_SYNC_SYNCABLE_DIRECTORY_MANAGER_H_
138