1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROMEOS_COMPONENTS_DRIVEFS_DRIVEFS_BOOTSTRAP_H_
6 #define CHROMEOS_COMPONENTS_DRIVEFS_DRIVEFS_BOOTSTRAP_H_
7 
8 #include <memory>
9 
10 #include "base/component_export.h"
11 #include "base/files/scoped_file.h"
12 #include "base/macros.h"
13 #include "base/unguessable_token.h"
14 #include "chromeos/components/drivefs/mojom/drivefs.mojom.h"
15 #include "mojo/public/cpp/bindings/pending_remote.h"
16 #include "mojo/public/cpp/system/invitation.h"
17 
18 namespace drivefs {
19 
20 // Awaits for connection from DriveFS.
COMPONENT_EXPORT(DRIVEFS)21 class COMPONENT_EXPORT(DRIVEFS) DriveFsBootstrapListener {
22  public:
23   DriveFsBootstrapListener();
24   virtual ~DriveFsBootstrapListener();
25 
26   const base::UnguessableToken& pending_token() const { return pending_token_; }
27   virtual mojo::PendingRemote<mojom::DriveFsBootstrap> bootstrap();
28   bool is_connected() const { return connected_; }
29 
30  protected:
31   // Protected for stubbing out for testing.
32   virtual void SendInvitationOverPipe(base::ScopedFD handle);
33 
34  private:
35   void AcceptMojoConnection(base::ScopedFD handle);
36 
37   mojo::OutgoingInvitation invitation_;
38   mojo::PendingRemote<mojom::DriveFsBootstrap> bootstrap_;
39 
40   // The token passed to DriveFS as part of 'source path' used to match it to
41   // this instance.
42   base::UnguessableToken pending_token_;
43 
44   bool connected_ = false;
45 
46   DISALLOW_COPY_AND_ASSIGN(DriveFsBootstrapListener);
47 };
48 
49 // Establishes and holds mojo connection to DriveFS.
COMPONENT_EXPORT(DRIVEFS)50 class COMPONENT_EXPORT(DRIVEFS) DriveFsConnection {
51  public:
52   DriveFsConnection() = default;
53   virtual ~DriveFsConnection() = default;
54   virtual base::UnguessableToken Connect(mojom::DriveFsDelegate* delegate,
55                                          base::OnceClosure on_disconnected) = 0;
56   virtual mojom::DriveFs& GetDriveFs() = 0;
57 
58   static std::unique_ptr<DriveFsConnection> Create(
59       std::unique_ptr<DriveFsBootstrapListener> bootstrap_listener,
60       mojom::DriveFsConfigurationPtr config);
61 
62  private:
63   DISALLOW_COPY_AND_ASSIGN(DriveFsConnection);
64 };
65 
66 }  // namespace drivefs
67 
68 #endif  // CHROMEOS_COMPONENTS_DRIVEFS_DRIVEFS_BOOTSTRAP_H_
69