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 HEADLESS_LIB_BROWSER_PROTOCOL_HEADLESS_DEVTOOLS_SESSION_H_
6 #define HEADLESS_LIB_BROWSER_PROTOCOL_HEADLESS_DEVTOOLS_SESSION_H_
7 
8 #include <memory>
9 
10 #include "base/values.h"
11 #include "base/containers/flat_map.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/public/browser/devtools_manager_delegate.h"
14 #include "headless/lib/browser/protocol/forward.h"
15 #include "headless/lib/browser/protocol/protocol.h"
16 
17 namespace headless {
18 class HeadlessBrowserImpl;
19 class UberDispatcher;
20 
21 namespace protocol {
22 
23 class DomainHandler;
24 
25 class HeadlessDevToolsSession : public FrontendChannel {
26  public:
27   HeadlessDevToolsSession(base::WeakPtr<HeadlessBrowserImpl> browser,
28                           content::DevToolsAgentHostClientChannel* channel);
29   ~HeadlessDevToolsSession() override;
30 
31   void HandleCommand(
32       base::span<const uint8_t> message,
33       content::DevToolsManagerDelegate::NotHandledCallback callback);
34 
35  private:
36   void AddHandler(std::unique_ptr<DomainHandler> handler);
37 
38   // FrontendChannel:
39   void SendProtocolResponse(int call_id,
40                             std::unique_ptr<Serializable> message) override;
41   void SendProtocolNotification(std::unique_ptr<Serializable> message) override;
42   void FlushProtocolNotifications() override;
43   void FallThrough(int call_id,
44                    crdtp::span<uint8_t> method,
45                    crdtp::span<uint8_t> message) override;
46 
47   base::WeakPtr<HeadlessBrowserImpl> browser_;
48   UberDispatcher dispatcher_;
49   std::vector<std::unique_ptr<DomainHandler>> handlers_;
50   base::flat_map<int, content::DevToolsManagerDelegate::NotHandledCallback>
51       pending_commands_;
52   content::DevToolsAgentHostClientChannel* client_channel_;
53   DISALLOW_COPY_AND_ASSIGN(HeadlessDevToolsSession);
54 };
55 
56 }  // namespace protocol
57 }  // namespace headless
58 
59 #endif  // HEADLESS_LIB_BROWSER_PROTOCOL_HEADLESS_DEVTOOLS_SESSION_H_
60