| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/filesystem/file_system_impl.h" | 5 #include "components/filesystem/file_system_impl.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
| 15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/posix/eintr_wrapper.h" | |
| 19 #include "components/filesystem/directory_impl.h" | 18 #include "components/filesystem/directory_impl.h" |
| 19 #include "mojo/application/public/cpp/application_connection.h" |
| 20 | 20 |
| 21 namespace filesystem { | 21 namespace filesystem { |
| 22 | 22 |
| 23 namespace { | |
| 24 | |
| 25 base::ScopedFD CreateAndOpenTemporaryDirectory( | |
| 26 scoped_ptr<base::ScopedTempDir>* temp_dir) { | |
| 27 (*temp_dir).reset(new base::ScopedTempDir()); | |
| 28 CHECK((*temp_dir)->CreateUniqueTempDir()); | |
| 29 | |
| 30 base::ScopedFD temp_dir_fd(HANDLE_EINTR( | |
| 31 open((*temp_dir)->path().value().c_str(), O_RDONLY | O_DIRECTORY, 0))); | |
| 32 PCHECK(temp_dir_fd.is_valid()); | |
| 33 DVLOG(1) << "Made a temporary directory: " << (*temp_dir)->path().value(); | |
| 34 return temp_dir_fd.Pass(); | |
| 35 } | |
| 36 | |
| 37 #ifndef NDEBUG | |
| 38 base::ScopedFD OpenMojoDebugDirectory() { | |
| 39 const char* home_dir_name = getenv("HOME"); | |
| 40 if (!home_dir_name || !home_dir_name[0]) { | |
| 41 LOG(ERROR) << "HOME not set"; | |
| 42 return base::ScopedFD(); | |
| 43 } | |
| 44 base::FilePath mojo_debug_dir_name = | |
| 45 base::FilePath(home_dir_name).Append("MojoDebug"); | |
| 46 return base::ScopedFD(HANDLE_EINTR( | |
| 47 open(mojo_debug_dir_name.value().c_str(), O_RDONLY | O_DIRECTORY, 0))); | |
| 48 } | |
| 49 #endif | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 FileSystemImpl::FileSystemImpl(mojo::ApplicationConnection* connection, | 23 FileSystemImpl::FileSystemImpl(mojo::ApplicationConnection* connection, |
| 54 mojo::InterfaceRequest<FileSystem> request) | 24 mojo::InterfaceRequest<FileSystem> request) |
| 55 : binding_(this, request.Pass()) { | 25 : remote_application_url_(connection->GetRemoteApplicationURL()), |
| 56 // TODO(vtl): record other app's URL | 26 binding_(this, request.Pass()) { |
| 57 } | 27 } |
| 58 | 28 |
| 59 FileSystemImpl::~FileSystemImpl() { | 29 FileSystemImpl::~FileSystemImpl() { |
| 60 } | 30 } |
| 61 | 31 |
| 62 void FileSystemImpl::OpenFileSystem(const mojo::String& file_system, | 32 void FileSystemImpl::OpenFileSystem(const mojo::String& file_system, |
| 63 mojo::InterfaceRequest<Directory> directory, | 33 mojo::InterfaceRequest<Directory> directory, |
| 64 const OpenFileSystemCallback& callback) { | 34 const OpenFileSystemCallback& callback) { |
| 65 base::ScopedFD dir_fd; | |
| 66 // Set only if the |DirectoryImpl| will own a temporary directory. | 35 // Set only if the |DirectoryImpl| will own a temporary directory. |
| 67 scoped_ptr<base::ScopedTempDir> temp_dir; | 36 scoped_ptr<base::ScopedTempDir> temp_dir; |
| 68 if (file_system.is_null()) { | 37 base::FilePath path; |
| 69 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=! | 38 if (file_system.get() == std::string("temp")) { |
| 70 dir_fd.reset(CreateAndOpenTemporaryDirectory(&temp_dir).release()); | 39 temp_dir.reset(new base::ScopedTempDir); |
| 71 DCHECK(temp_dir); | 40 CHECK(temp_dir->CreateUniqueTempDir()); |
| 72 } else if (file_system.get() == std::string("debug")) { | 41 path = temp_dir->path(); |
| 73 #ifdef NDEBUG | 42 } else if (file_system.get() == std::string("origin")) { |
| 74 LOG(WARNING) << "~/MojoDebug only available in Debug builds"; | 43 // TODO(erg): We should serve a persistent directory based on the |
| 75 #else | 44 // subdirectory |remote_application_url_| of a profile directory. |
| 76 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=! | |
| 77 dir_fd.reset(OpenMojoDebugDirectory().release()); | |
| 78 #endif | |
| 79 if (!dir_fd.is_valid()) { | |
| 80 LOG(ERROR) << "~/MojoDebug unavailable"; | |
| 81 callback.Run(ERROR_UNAVAILABLE); | |
| 82 return; | |
| 83 } | |
| 84 } else { | |
| 85 LOG(ERROR) << "Unknown file system: " << file_system.get(); | |
| 86 callback.Run(ERROR_UNIMPLEMENTED); | |
| 87 return; | |
| 88 } | 45 } |
| 89 | 46 |
| 90 new DirectoryImpl(directory.Pass(), dir_fd.Pass(), temp_dir.Pass()); | 47 if (!path.empty()) { |
| 91 callback.Run(ERROR_OK); | 48 new DirectoryImpl(directory.Pass(), path, temp_dir.Pass()); |
| 49 callback.Run(ERROR_OK); |
| 50 } else { |
| 51 callback.Run(ERROR_FAILED); |
| 52 } |
| 92 } | 53 } |
| 93 | 54 |
| 94 } // namespace filesystem | 55 } // namespace filesystem |
| OLD | NEW |