This source file includes following definitions.
- GetMountPointPath
- next_id_
- Get
- AddObserver
- RemoveObserver
- RegisterFileSystem
- UnregisterFileSystem
- GetRegisteredFileSystems
#include "chrome/browser/chromeos/file_system_provider/service.h"
#include "base/files/file_path.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/chromeos/file_system_provider/observer.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system.h"
#include "chrome/browser/chromeos/file_system_provider/service_factory.h"
#include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "content/public/browser/browser_thread.h"
#include "webkit/browser/fileapi/external_mount_points.h"
namespace chromeos {
namespace file_system_provider {
namespace {
const base::FilePath::CharType kProvidedMountPointRoot[] =
FILE_PATH_LITERAL("/provided");
const size_t kMaxFileSystems = 16;
base::FilePath GetMountPointPath(Profile* profile,
std::string extension_id,
int file_system_id) {
chromeos::User* const user =
chromeos::UserManager::IsInitialized()
? chromeos::UserManager::Get()->GetUserByProfile(
profile->GetOriginalProfile())
: NULL;
const std::string user_suffix = user ? "-" + user->username_hash() : "";
return base::FilePath(kProvidedMountPointRoot).AppendASCII(
extension_id + "-" + base::IntToString(file_system_id) + user_suffix);
}
}
Service::Service(Profile* profile) : profile_(profile), next_id_(1) {}
Service::~Service() {}
Service* Service::Get(content::BrowserContext* context) {
return ServiceFactory::Get(context);
}
void Service::AddObserver(Observer* observer) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(observer);
observers_.AddObserver(observer);
}
void Service::RemoveObserver(Observer* observer) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(observer);
observers_.RemoveObserver(observer);
}
int Service::RegisterFileSystem(const std::string& extension_id,
const std::string& file_system_name) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (file_systems_.size() + 1 > kMaxFileSystems)
return 0;
int file_system_id = next_id_;
fileapi::ExternalMountPoints* const mount_points =
fileapi::ExternalMountPoints::GetSystemInstance();
DCHECK(mount_points);
const base::FilePath& mount_point_path =
GetMountPointPath(profile_, extension_id, file_system_id);
const std::string mount_point_name =
mount_point_path.BaseName().AsUTF8Unsafe();
if (!mount_points->RegisterFileSystem(mount_point_name,
fileapi::kFileSystemTypeProvided,
fileapi::FileSystemMountOption(),
mount_point_path)) {
return 0;
}
ProvidedFileSystem file_system(
extension_id, file_system_id, file_system_name, mount_point_path);
file_systems_[file_system_id] = file_system;
FOR_EACH_OBSERVER(
Observer, observers_, OnProvidedFileSystemRegistered(file_system));
next_id_++;
return file_system_id;
}
bool Service::UnregisterFileSystem(const std::string& extension_id,
int file_system_id) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id);
if (file_system_it == file_systems_.end() ||
file_system_it->second.extension_id() != extension_id) {
return false;
}
fileapi::ExternalMountPoints* const mount_points =
fileapi::ExternalMountPoints::GetSystemInstance();
DCHECK(mount_points);
const std::string mount_point_name =
file_system_it->second.mount_path().BaseName().value();
if (!mount_points->RevokeFileSystem(mount_point_name))
return false;
FOR_EACH_OBSERVER(Observer,
observers_,
OnProvidedFileSystemUnregistered(file_system_it->second));
file_systems_.erase(file_system_it);
return true;
}
std::vector<ProvidedFileSystem> Service::GetRegisteredFileSystems() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
std::vector<ProvidedFileSystem> result;
for (FileSystemMap::const_iterator it = file_systems_.begin();
it != file_systems_.end();
++it) {
result.push_back(it->second);
}
return result;
}
}
}