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