summaryrefslogtreecommitdiffstats
path: root/components/filesystem/file_system_app.h
blob: 56e33aff2d978969d067ae103279ca32e0fa4060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 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.

#ifndef COMPONENTS_FILESYSTEM_FILE_SYSTEM_APP_H_
#define COMPONENTS_FILESYSTEM_FILE_SYSTEM_APP_H_

#include "base/macros.h"
#include "components/filesystem/directory_impl.h"
#include "components/filesystem/file_system_impl.h"
#include "components/filesystem/public/interfaces/file_system.mojom.h"
#include "mojo/application/public/cpp/application_delegate.h"
#include "mojo/application/public/cpp/interface_factory.h"

namespace mojo {
class ApplicationImpl;
}

namespace filesystem {

class FileSystemApp : public mojo::ApplicationDelegate,
                      public mojo::InterfaceFactory<FileSystem> {
 public:
  FileSystemApp();
  ~FileSystemApp() override;

  // Called by individual FileSystem objects to register lifetime events.
  void RegisterDirectoryToClient(DirectoryImpl* directory,
                                 FileSystemClientPtr client);

 private:
  // We set the DirectoryImpl's error handler to this function. We do this so
  // that we can QuitNow() once the last DirectoryImpl has closed itself.
  void OnDirectoryConnectionError(DirectoryImpl* directory);

  // |ApplicationDelegate| override:
  void Initialize(mojo::ApplicationImpl* app) override;
  bool ConfigureIncomingConnection(
      mojo::ApplicationConnection* connection) override;
  bool OnShellConnectionError() override;

  // |InterfaceFactory<Files>| implementation:
  void Create(mojo::ApplicationConnection* connection,
              mojo::InterfaceRequest<FileSystem> request) override;

  // Use a vector to work around map not letting us use FileSystemClientPtr as
  // a value in a std::map. The move constructors are to allow us to deal with
  // FileSystemClientPtr inside a vector.
  struct Client {
    Client(DirectoryImpl* directory, FileSystemClientPtr fs_client);
    Client(Client&& rhs);
    ~Client();

    Client& operator=(Client&& rhs);

    DirectoryImpl* directory_;
    FileSystemClientPtr fs_client_;
  };
  std::vector<Client> client_mapping_;

  mojo::ApplicationImpl* app_;

  // Set to true when our shell connection is closed. On connection error, we
  // then broadcast a notification to all FileSystemClients that they should
  // shut down. Once the final one does, then we QuitNow().
  bool in_shutdown_;

  DISALLOW_COPY_AND_ASSIGN(FileSystemApp);
};

}  // namespace filesystem

#endif  // COMPONENTS_FILESYSTEM_FILE_SYSTEM_APP_H_