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 | #include "config.h" |
27 | #include "WebDriverService.h" |
28 | |
29 | #include "Capabilities.h" |
30 | #include "CommandResult.h" |
31 | #include <wtf/JSONValues.h> |
32 | |
33 | namespace WebDriver { |
34 | |
35 | Capabilities WebDriverService::platformCapabilities() |
36 | { |
37 | Capabilities capabilities; |
38 | capabilities.platformName = String("linux" ); |
39 | capabilities.setWindowRect = true; |
40 | return capabilities; |
41 | } |
42 | |
43 | bool WebDriverService::platformValidateCapability(const String& name, const RefPtr<JSON::Value>& value) const |
44 | { |
45 | if (name != "webkitgtk:browserOptions" ) |
46 | return true; |
47 | |
48 | RefPtr<JSON::Object> browserOptions; |
49 | if (!value->asObject(browserOptions)) |
50 | return false; |
51 | |
52 | if (browserOptions->isNull()) |
53 | return true; |
54 | |
55 | RefPtr<JSON::Value> binaryValue; |
56 | String binary; |
57 | if (browserOptions->getValue("binary"_s , binaryValue) && !binaryValue->asString(binary)) |
58 | return false; |
59 | |
60 | RefPtr<JSON::Value> useOverlayScrollbarsValue; |
61 | bool useOverlayScrollbars; |
62 | if (browserOptions->getValue("useOverlayScrollbars"_s , useOverlayScrollbarsValue) && !useOverlayScrollbarsValue->asBoolean(useOverlayScrollbars)) |
63 | return false; |
64 | |
65 | RefPtr<JSON::Value> browserArgumentsValue; |
66 | RefPtr<JSON::Array> browserArguments; |
67 | if (browserOptions->getValue("args"_s , browserArgumentsValue)) { |
68 | if (!browserArgumentsValue->asArray(browserArguments)) |
69 | return false; |
70 | |
71 | unsigned browserArgumentsLength = browserArguments->length(); |
72 | for (unsigned i = 0; i < browserArgumentsLength; ++i) { |
73 | RefPtr<JSON::Value> value = browserArguments->get(i); |
74 | String argument; |
75 | if (!value->asString(argument)) |
76 | return false; |
77 | } |
78 | } |
79 | |
80 | RefPtr<JSON::Value> certificatesValue; |
81 | if (browserOptions->getValue("certificates"_s , certificatesValue)) { |
82 | RefPtr<JSON::Array> certificates; |
83 | if (!certificatesValue->asArray(certificates)) |
84 | return false; |
85 | |
86 | unsigned certificatesLength = certificates->length(); |
87 | for (unsigned i = 0; i < certificatesLength; ++i) { |
88 | RefPtr<JSON::Value> certificateValue = certificates->get(i); |
89 | RefPtr<JSON::Object> certificate; |
90 | if (!certificateValue->asObject(certificate)) |
91 | return false; |
92 | |
93 | RefPtr<JSON::Value> hostValue; |
94 | String host; |
95 | if (!certificate->getValue("host"_s , hostValue) || !hostValue->asString(host)) |
96 | return false; |
97 | |
98 | RefPtr<JSON::Value> certificateFileValue; |
99 | String certificateFile; |
100 | if (!certificate->getValue("certificateFile"_s , certificateFileValue) || !certificateFileValue->asString(certificateFile)) |
101 | return false; |
102 | } |
103 | } |
104 | |
105 | return true; |
106 | } |
107 | |
108 | bool WebDriverService::platformMatchCapability(const String&, const RefPtr<JSON::Value>&) const |
109 | { |
110 | return true; |
111 | } |
112 | |
113 | void WebDriverService::platformParseCapabilities(const JSON::Object& matchedCapabilities, Capabilities& capabilities) const |
114 | { |
115 | capabilities.browserBinary = String(LIBEXECDIR "/webkit2gtk-" WEBKITGTK_API_VERSION_STRING "/MiniBrowser" ); |
116 | capabilities.browserArguments = Vector<String> { "--automation"_s }; |
117 | capabilities.useOverlayScrollbars = true; |
118 | |
119 | RefPtr<JSON::Object> browserOptions; |
120 | if (!matchedCapabilities.getObject("webkitgtk:browserOptions"_s , browserOptions)) |
121 | return; |
122 | |
123 | String browserBinary; |
124 | if (browserOptions->getString("binary"_s , browserBinary)) { |
125 | capabilities.browserBinary = browserBinary; |
126 | capabilities.browserArguments = WTF::nullopt; |
127 | } |
128 | |
129 | RefPtr<JSON::Array> browserArguments; |
130 | if (browserOptions->getArray("args"_s , browserArguments) && browserArguments->length()) { |
131 | unsigned browserArgumentsLength = browserArguments->length(); |
132 | capabilities.browserArguments = Vector<String>(); |
133 | capabilities.browserArguments->reserveInitialCapacity(browserArgumentsLength); |
134 | for (unsigned i = 0; i < browserArgumentsLength; ++i) { |
135 | RefPtr<JSON::Value> value = browserArguments->get(i); |
136 | String argument; |
137 | value->asString(argument); |
138 | ASSERT(!argument.isNull()); |
139 | capabilities.browserArguments->uncheckedAppend(WTFMove(argument)); |
140 | } |
141 | } |
142 | |
143 | bool useOverlayScrollbars; |
144 | if (browserOptions->getBoolean("useOverlayScrollbars"_s , useOverlayScrollbars)) |
145 | capabilities.useOverlayScrollbars = useOverlayScrollbars; |
146 | else |
147 | capabilities.useOverlayScrollbars = true; |
148 | |
149 | RefPtr<JSON::Array> certificates; |
150 | if (browserOptions->getArray("certificates"_s , certificates) && certificates->length()) { |
151 | unsigned certificatesLength = certificates->length(); |
152 | capabilities.certificates = Vector<std::pair<String, String>>(); |
153 | capabilities.certificates->reserveInitialCapacity(certificatesLength); |
154 | for (unsigned i = 0; i < certificatesLength; ++i) { |
155 | RefPtr<JSON::Value> value = certificates->get(i); |
156 | RefPtr<JSON::Object> certificate; |
157 | value->asObject(certificate); |
158 | ASSERT(certificate); |
159 | |
160 | String host; |
161 | certificate->getString("host"_s , host); |
162 | ASSERT(!host.isNull()); |
163 | |
164 | String certificateFile; |
165 | certificate->getString("certificateFile"_s , certificateFile); |
166 | ASSERT(!certificateFile.isNull()); |
167 | |
168 | capabilities.certificates->uncheckedAppend({ WTFMove(host), WTFMove(certificateFile) }); |
169 | } |
170 | } |
171 | } |
172 | |
173 | } // namespace WebDriver |
174 | |