| Index: services/file_manager/file_manager_impl.cc
|
| diff --git a/services/file_manager/file_manager_impl.cc b/services/file_manager/file_manager_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..d81c3795bfb49a19401262f4572a69748dc5392e
|
| --- /dev/null
|
| +++ b/services/file_manager/file_manager_impl.cc
|
| @@ -0,0 +1,92 @@
|
| +// Copyright 2015 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 "services/file_manager/file_manager_impl.h"
|
| +
|
| +#include <fcntl.h>
|
| +#include <stdlib.h>
|
| +#include <sys/stat.h>
|
| +#include <sys/types.h>
|
| +#include <unistd.h>
|
| +
|
| +#include "base/files/file_path.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/files/scoped_file.h"
|
| +#include "base/logging.h"
|
| +#include "base/posix/eintr_wrapper.h"
|
| +#include "services/file_manager/directory_impl.h"
|
| +
|
| +namespace mojo {
|
| +namespace files {
|
| +
|
| +namespace {
|
| +
|
| +base::ScopedFD CreateAndOpenTemporaryDirectory(base::FilePath* temp_dir_name) {
|
| + DCHECK(temp_dir_name);
|
| + CHECK(base::CreateNewTempDirectory(base::FilePath::StringType(),
|
| + temp_dir_name));
|
| + base::ScopedFD temp_dir_fd(HANDLE_EINTR(
|
| + open(temp_dir_name->value().c_str(), O_RDONLY, O_DIRECTORY)));
|
| + PCHECK(temp_dir_fd.is_valid());
|
| + DVLOG(1) << "Made a temporary directory: " << temp_dir_name->value();
|
| + return temp_dir_fd.Pass();
|
| +}
|
| +
|
| +base::ScopedFD OpenMojoHomeDirectory() {
|
| + const char* home_dir_name = getenv("HOME");
|
| + if (!home_dir_name || !home_dir_name[0]) {
|
| + LOG(ERROR) << "HOME not set";
|
| + return base::ScopedFD();
|
| + }
|
| + base::FilePath mojo_home_dir_name =
|
| + base::FilePath(home_dir_name).Append("MojoHome");
|
| + return base::ScopedFD(HANDLE_EINTR(
|
| + open(mojo_home_dir_name.value().c_str(), O_RDONLY, O_DIRECTORY)));
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +FileManagerImpl::FileManagerImpl(ApplicationConnection* connection,
|
| + InterfaceRequest<FileManager> request)
|
| + : binding_(this, request.Pass()) {
|
| + // TODO(vtl): record other app's URL
|
| +}
|
| +
|
| +FileManagerImpl::~FileManagerImpl() {
|
| +}
|
| +
|
| +void FileManagerImpl::OpenFileSystem(FileSystem file_system,
|
| + InterfaceRequest<Directory> directory,
|
| + const Callback<void(Error)>& callback) {
|
| + base::ScopedFD dir_fd;
|
| + // Set only if the |DirectoryImpl| will own this directory (and should delete
|
| + // it on destruction).
|
| + base::FilePath owned_dir_name;
|
| + switch (file_system) {
|
| + case FILE_SYSTEM_TEMPORARY:
|
| + // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=!
|
| + dir_fd.reset(CreateAndOpenTemporaryDirectory(&owned_dir_name).release());
|
| + DCHECK(!owned_dir_name.empty());
|
| + break;
|
| + case FILE_SYSTEM_USER:
|
| + // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=!
|
| + dir_fd.reset(OpenMojoHomeDirectory().release());
|
| + if (!dir_fd.is_valid()) {
|
| + LOG(ERROR) << "~/MojoHome unavailable";
|
| + callback.Run(ERROR_UNAVAILABLE);
|
| + return;
|
| + }
|
| + break;
|
| + default:
|
| + LOG(ERROR) << "Unknown file system type: " << file_system;
|
| + callback.Run(ERROR_UNIMPLEMENTED);
|
| + return;
|
| + }
|
| +
|
| + new DirectoryImpl(directory.Pass(), dir_fd.Pass(), owned_dir_name);
|
| + callback.Run(ERROR_OK);
|
| +}
|
| +
|
| +} // namespace files
|
| +} // namespace mojo
|
|
|