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_app.h" | 5 #include "components/filesystem/file_system_app.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
7 #include "mojo/shell/public/cpp/connection.h" | 11 #include "mojo/shell/public/cpp/connection.h" |
8 #include "mojo/shell/public/cpp/shell.h" | 12 #include "mojo/shell/public/cpp/shell.h" |
9 | 13 |
10 namespace filesystem { | 14 namespace filesystem { |
11 | 15 |
12 FileSystemApp::FileSystemApp() | 16 FileSystemApp::FileSystemApp() |
13 : shell_(nullptr), lock_table_(new LockTable) {} | 17 : shell_(nullptr), lock_table_(new LockTable), in_shutdown_(false) {} |
14 | 18 |
15 FileSystemApp::~FileSystemApp() {} | 19 FileSystemApp::~FileSystemApp() {} |
16 | 20 |
17 void FileSystemApp::Initialize(mojo::Shell* shell, const std::string& url, | 21 void FileSystemApp::Initialize(mojo::Shell* shell, const std::string& url, |
18 uint32_t id) { | 22 uint32_t id) { |
19 shell_ = shell; | 23 shell_ = shell; |
20 tracing_.Initialize(shell, url); | 24 tracing_.Initialize(shell, url); |
21 } | 25 } |
22 | 26 |
23 bool FileSystemApp::AcceptConnection(mojo::Connection* connection) { | 27 bool FileSystemApp::AcceptConnection(mojo::Connection* connection) { |
24 connection->AddInterface<FileSystem>(this); | 28 connection->AddInterface<FileSystem>(this); |
25 return true; | 29 return true; |
26 } | 30 } |
27 | 31 |
| 32 void FileSystemApp::RegisterDirectoryToClient(DirectoryImpl* directory, |
| 33 FileSystemClientPtr client) { |
| 34 directory->set_connection_error_handler( |
| 35 base::Bind(&FileSystemApp::OnDirectoryConnectionError, |
| 36 base::Unretained(this), |
| 37 directory)); |
| 38 client_mapping_.emplace_back(directory, std::move(client)); |
| 39 } |
| 40 |
| 41 bool FileSystemApp::ShellConnectionLost() { |
| 42 if (client_mapping_.empty()) { |
| 43 // If we have no current connections, we can shutdown immediately. |
| 44 return true; |
| 45 } |
| 46 |
| 47 in_shutdown_ = true; |
| 48 |
| 49 // We have live connections. Send a notification to each one indicating that |
| 50 // they should shutdown. |
| 51 for (std::vector<Client>::iterator it = client_mapping_.begin(); |
| 52 it != client_mapping_.end(); ++it) { |
| 53 it->fs_client_->OnFileSystemShutdown(); |
| 54 } |
| 55 |
| 56 return false; |
| 57 } |
| 58 |
28 // |InterfaceFactory<Files>| implementation: | 59 // |InterfaceFactory<Files>| implementation: |
29 void FileSystemApp::Create(mojo::Connection* connection, | 60 void FileSystemApp::Create(mojo::Connection* connection, |
30 mojo::InterfaceRequest<FileSystem> request) { | 61 mojo::InterfaceRequest<FileSystem> request) { |
31 new FileSystemImpl(connection, std::move(request), lock_table_.get()); | 62 new FileSystemImpl(this, connection, std::move(request), lock_table_.get()); |
| 63 } |
| 64 |
| 65 void FileSystemApp::OnDirectoryConnectionError(DirectoryImpl* directory) { |
| 66 for (std::vector<Client>::iterator it = client_mapping_.begin(); |
| 67 it != client_mapping_.end(); ++it) { |
| 68 if (it->directory_ == directory) { |
| 69 client_mapping_.erase(it); |
| 70 |
| 71 if (in_shutdown_ && client_mapping_.empty()) { |
| 72 // We just cleared the last directory after our shell connection went |
| 73 // away. Time to shut ourselves down. |
| 74 shell_->Quit(); |
| 75 } |
| 76 |
| 77 return; |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 FileSystemApp::Client::Client(DirectoryImpl* directory, |
| 83 FileSystemClientPtr fs_client) |
| 84 : directory_(directory), fs_client_(std::move(fs_client)) {} |
| 85 |
| 86 FileSystemApp::Client::Client(Client&& rhs) |
| 87 : directory_(rhs.directory_), fs_client_(std::move(rhs.fs_client_)) {} |
| 88 |
| 89 FileSystemApp::Client::~Client() {} |
| 90 |
| 91 FileSystemApp::Client& FileSystemApp::Client::operator=( |
| 92 FileSystemApp::Client&& rhs) { |
| 93 directory_ = rhs.directory_; |
| 94 fs_client_ = std::move(rhs.fs_client_); |
| 95 return *this; |
32 } | 96 } |
33 | 97 |
34 } // namespace filesystem | 98 } // namespace filesystem |
OLD | NEW |