1/*
2 * Copyright (C) 2017 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "Capabilities.h"
29#include <wtf/HashMap.h>
30#include <wtf/JSONValues.h>
31
32#if USE(GLIB)
33#include <wtf/glib/GRefPtr.h>
34typedef struct _GDBusConnection GDBusConnection;
35typedef struct _GDBusInterfaceVTable GDBusInterfaceVTable;
36typedef struct _GSubprocess GSubprocess;
37#endif
38
39namespace WebDriver {
40
41struct ConnectToBrowserAsyncData;
42
43class SessionHost {
44 WTF_MAKE_FAST_ALLOCATED(SessionHost);
45public:
46 explicit SessionHost(Capabilities&& capabilities)
47 : m_capabilities(WTFMove(capabilities))
48 {
49 }
50 ~SessionHost();
51
52 bool isConnected() const;
53
54 const String& sessionID() const { return m_sessionID; }
55 const Capabilities& capabilities() const { return m_capabilities; }
56
57 void connectToBrowser(Function<void (Optional<String> error)>&&);
58 void startAutomationSession(Function<void (bool, Optional<String>)>&&);
59
60 struct CommandResponse {
61 RefPtr<JSON::Object> responseObject;
62 bool isError { false };
63 };
64 long sendCommandToBackend(const String&, RefPtr<JSON::Object>&& parameters, Function<void (CommandResponse&&)>&&);
65
66private:
67 struct Target {
68 uint64_t id { 0 };
69 CString name;
70 bool paired { false };
71 };
72
73 void inspectorDisconnected();
74 void sendMessageToBackend(long, const String&);
75 void dispatchMessage(const String&);
76
77#if USE(GLIB)
78 static void dbusConnectionClosedCallback(SessionHost*);
79 static const GDBusInterfaceVTable s_interfaceVTable;
80 void launchBrowser(Function<void (Optional<String> error)>&&);
81 void connectToBrowser(std::unique_ptr<ConnectToBrowserAsyncData>&&);
82 bool matchCapabilities(GVariant*);
83 bool buildSessionCapabilities(GVariantBuilder*) const;
84 void setupConnection(GRefPtr<GDBusConnection>&&);
85 void setTargetList(uint64_t connectionID, Vector<Target>&&);
86 void sendMessageToFrontend(uint64_t connectionID, uint64_t targetID, const char* message);
87#endif
88
89 Capabilities m_capabilities;
90
91 String m_sessionID;
92 uint64_t m_connectionID { 0 };
93 Target m_target;
94
95 HashMap<long, Function<void (CommandResponse&&)>> m_commandRequests;
96
97#if USE(GLIB)
98 Function<void (bool, Optional<String>)> m_startSessionCompletionHandler;
99 GRefPtr<GSubprocess> m_browser;
100 GRefPtr<GDBusConnection> m_dbusConnection;
101 GRefPtr<GCancellable> m_cancellable;
102#endif
103};
104
105} // namespace WebDriver
106