1 // Copyright 2019 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 CHROME_BROWSER_SERIAL_SERIAL_CHOOSER_CONTEXT_H_
6 #define CHROME_BROWSER_SERIAL_SERIAL_CHOOSER_CONTEXT_H_
7 
8 #include <map>
9 #include <memory>
10 #include <set>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 
15 #include "base/memory/weak_ptr.h"
16 #include "base/unguessable_token.h"
17 #include "components/permissions/chooser_context_base.h"
18 #include "content/public/browser/serial_delegate.h"
19 #include "mojo/public/cpp/bindings/pending_remote.h"
20 #include "mojo/public/cpp/bindings/remote.h"
21 #include "services/device/public/mojom/serial.mojom-forward.h"
22 #include "third_party/blink/public/mojom/serial/serial.mojom.h"
23 #include "url/gurl.h"
24 #include "url/origin.h"
25 
26 class Profile;
27 
28 namespace base {
29 class Value;
30 }
31 
32 class SerialChooserContext : public permissions::ChooserContextBase,
33                              public device::mojom::SerialPortManagerClient {
34  public:
35   using PortObserver = content::SerialDelegate::Observer;
36 
37   explicit SerialChooserContext(Profile* profile);
38   ~SerialChooserContext() override;
39 
40   // ChooserContextBase:
41   bool IsValidObject(const base::Value& object) override;
42   base::string16 GetObjectDisplayName(const base::Value& object) override;
43 
44   // In addition these methods from ChooserContextBase are overridden in order
45   // to expose ephemeral devices through the public interface.
46   std::vector<std::unique_ptr<Object>> GetGrantedObjects(
47       const url::Origin& requesting_origin,
48       const url::Origin& embedding_origin) override;
49   std::vector<std::unique_ptr<Object>> GetAllGrantedObjects() override;
50   void RevokeObjectPermission(const url::Origin& requesting_origin,
51                               const url::Origin& embedding_origin,
52                               const base::Value& object) override;
53 
54   // Serial-specific interface for granting and checking permissions.
55   void GrantPortPermission(const url::Origin& requesting_origin,
56                            const url::Origin& embedding_origin,
57                            const device::mojom::SerialPortInfo& port);
58   bool HasPortPermission(const url::Origin& requesting_origin,
59                          const url::Origin& embedding_origin,
60                          const device::mojom::SerialPortInfo& port);
61   static bool CanStorePersistentEntry(
62       const device::mojom::SerialPortInfo& port);
63 
64   device::mojom::SerialPortManager* GetPortManager();
65 
66   void AddPortObserver(PortObserver* observer);
67   void RemovePortObserver(PortObserver* observer);
68 
69   void SetPortManagerForTesting(
70       mojo::PendingRemote<device::mojom::SerialPortManager> manager);
71   void FlushPortManagerConnectionForTesting();
72   base::WeakPtr<SerialChooserContext> AsWeakPtr();
73 
74   // SerialPortManagerClient implementation.
75   void OnPortAdded(device::mojom::SerialPortInfoPtr port) override;
76   void OnPortRemoved(device::mojom::SerialPortInfoPtr port) override;
77 
78  private:
79   void EnsurePortManagerConnection();
80   void SetUpPortManagerConnection(
81       mojo::PendingRemote<device::mojom::SerialPortManager> manager);
82   void OnPortManagerConnectionError();
83   void OnGetPorts(const url::Origin& requesting_origin,
84                   const url::Origin& embedding_origin,
85                   blink::mojom::SerialService::GetPortsCallback callback,
86                   std::vector<device::mojom::SerialPortInfoPtr> ports);
87 
88   const bool is_incognito_;
89 
90   // Tracks the set of ports to which an origin (potentially embedded in another
91   // origin) has access to. Key is (requesting_origin, embedding_origin).
92   std::map<std::pair<url::Origin, url::Origin>,
93            std::set<base::UnguessableToken>>
94       ephemeral_ports_;
95 
96   // Holds information about ports in |ephemeral_ports_|.
97   std::map<base::UnguessableToken, base::Value> port_info_;
98 
99   mojo::Remote<device::mojom::SerialPortManager> port_manager_;
100   mojo::Receiver<device::mojom::SerialPortManagerClient> client_receiver_{this};
101   base::ObserverList<PortObserver> port_observer_list_;
102 
103   base::WeakPtrFactory<SerialChooserContext> weak_factory_{this};
104 
105   DISALLOW_COPY_AND_ASSIGN(SerialChooserContext);
106 };
107 
108 #endif  // CHROME_BROWSER_SERIAL_SERIAL_CHOOSER_CONTEXT_H_
109