summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/syncable/directory_manager.cc
blob: fbdeb462a6f85db1db04a42c08f9888141bd43d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/syncable/directory_manager.h"

#include <map>
#include <set>
#include <iterator>

#include "base/logging.h"
#include "base/port.h"
#include "base/scoped_ptr.h"
#include "chrome/browser/sync/syncable/syncable.h"
#include "chrome/common/deprecated/event_sys-inl.h"

using browser_sync::Cryptographer;

namespace syncable {

static const FilePath::CharType kSyncDataDatabaseFilename[] =
    FILE_PATH_LITERAL("SyncData.sqlite3");

DirectoryManagerEvent DirectoryManagerShutdownEvent() {
  DirectoryManagerEvent event;
  event.what_happened = DirectoryManagerEvent::SHUTDOWN;
  return event;
}

// static
const FilePath DirectoryManager::GetSyncDataDatabaseFilename() {
  return FilePath(kSyncDataDatabaseFilename);
}

const FilePath DirectoryManager::GetSyncDataDatabasePath() const {
  return root_path_.Append(GetSyncDataDatabaseFilename());
}

DirectoryManager::DirectoryManager(const FilePath& path)
    : root_path_(path),
      managed_directory_(NULL),
      channel_(new Channel(DirectoryManagerShutdownEvent())),
      cryptographer_(new Cryptographer) {
}

DirectoryManager::~DirectoryManager() {
  AutoLock lock(lock_);
  DCHECK_EQ(managed_directory_, static_cast<Directory*>(NULL))
      << "Dir " << managed_directory_->name() << " not closed!";
  delete channel_;
}

bool DirectoryManager::Open(const std::string& name) {
  bool was_open = false;
  const DirOpenResult result = OpenImpl(name,
      GetSyncDataDatabasePath(), &was_open);
  return syncable::OPENED == result;
}

// Opens a directory.  Returns false on error.
DirOpenResult DirectoryManager::OpenImpl(const std::string& name,
                                         const FilePath& path,
                                         bool* was_open) {
  bool opened = false;
  {
    AutoLock lock(lock_);
    // Check to see if it's already open.
    if (managed_directory_) {
      DCHECK_EQ(ComparePathNames(name, managed_directory_->name()), 0)
          << "Can't open more than one directory.";
      opened = *was_open = true;
    }
  }

  if (opened)
    return syncable::OPENED;
  // Otherwise, open it.

  scoped_ptr<Directory> dir(new Directory);
  const DirOpenResult result = dir->Open(path, name);
  if (syncable::OPENED == result) {
    AutoLock lock(lock_);
    managed_directory_ = dir.release();
  }
  return result;
}

// Marks a directory as closed.  It might take a while until all the file
// handles and resources are freed by other threads.
void DirectoryManager::Close(const std::string& name) {
  // Erase from mounted and opened directory lists.
  {
    AutoLock lock(lock_);
    if (!managed_directory_ ||
        ComparePathNames(name, managed_directory_->name()) != 0) {
      // It wasn't open.
      return;
    }
  }

  // TODO(timsteele): No lock?!
  // Notify listeners.
  managed_directory_->channel()->NotifyListeners(DIRECTORY_CLOSED);
  DirectoryManagerEvent event = { DirectoryManagerEvent::CLOSED, name };
  channel_->NotifyListeners(event);

  delete managed_directory_;
  managed_directory_ = NULL;
}

void DirectoryManager::FinalSaveChangesForAll() {
  AutoLock lock(lock_);
  if (managed_directory_)
    managed_directory_->SaveChanges();
}

void DirectoryManager::GetOpenDirectories(DirNames* result) {
  result->clear();
  AutoLock lock(lock_);
  if (managed_directory_)
    result->push_back(managed_directory_->name());
}

ScopedDirLookup::ScopedDirLookup(DirectoryManager* dirman,
                                 const std::string& name) : dirman_(dirman) {
  dir_ = dirman->managed_directory_ &&
         (ComparePathNames(name, dirman->managed_directory_->name()) == 0) ?
         dirman->managed_directory_ : NULL;
  good_ = dir_ != NULL;
  good_checked_ = false;
}

ScopedDirLookup::~ScopedDirLookup() { }

Directory* ScopedDirLookup::operator -> () const {
  CHECK(good_checked_);
  DCHECK(good_);
  return dir_;
}

ScopedDirLookup::operator Directory* () const {
  CHECK(good_checked_);
  DCHECK(good_);
  return dir_;
}

}  // namespace syncable