|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Microsoft Corporation. All rights reserved. |
| 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 "BidiBrowserAgent.h" |
| 28 | + |
| 29 | +#if ENABLE(WEBDRIVER_BIDI) |
| 30 | + |
| 31 | +#include "AutomationProtocolObjects.h" |
| 32 | +#include "BidiUserContext.h" |
| 33 | +#include "WebAutomationSession.h" |
| 34 | +#include "WebAutomationSessionMacros.h" |
| 35 | +#include "WebDriverBidiProtocolObjects.h" |
| 36 | +#include "WebProcessPool.h" |
| 37 | +#include "WebsiteDataStore.h" |
| 38 | +#include <pal/SessionID.h> |
| 39 | +#include <wtf/NeverDestroyed.h> |
| 40 | +#include <wtf/text/StringBuilder.h> |
| 41 | + |
| 42 | +namespace WebKit { |
| 43 | + |
| 44 | +using namespace Inspector; |
| 45 | +using UserContextInfo = Inspector::Protocol::BidiBrowser::UserContextInfo; |
| 46 | + |
| 47 | +namespace { |
| 48 | + |
| 49 | +String toUserContextIDProtocolString(const PAL::SessionID& sessionID) |
| 50 | +{ |
| 51 | + StringBuilder builder; |
| 52 | + builder.append(hex(sessionID.toUInt64(), 16)); |
| 53 | + return builder.toString(); |
| 54 | +} |
| 55 | + |
| 56 | +const String& defaultUserContextID() |
| 57 | +{ |
| 58 | + static NeverDestroyed<const String> contextID(MAKE_STATIC_STRING_IMPL("default")); |
| 59 | + return contextID; |
| 60 | +} |
| 61 | + |
| 62 | +} // namespace |
| 63 | + |
| 64 | +WTF_MAKE_TZONE_ALLOCATED_IMPL(BidiBrowserAgent); |
| 65 | + |
| 66 | +BidiBrowserAgent::BidiBrowserAgent(WebAutomationSession& session, BackendDispatcher& backendDispatcher) |
| 67 | + : m_session(session) |
| 68 | + , m_browserDomainDispatcher(BidiBrowserBackendDispatcher::create(backendDispatcher, this)) |
| 69 | +{ |
| 70 | +} |
| 71 | + |
| 72 | +BidiBrowserAgent::~BidiBrowserAgent() = default; |
| 73 | + |
| 74 | +// MARK: Inspector::BidiBrowserDispatcherHandler methods. |
| 75 | + |
| 76 | +Inspector::CommandResult<void> BidiBrowserAgent::close() |
| 77 | +{ |
| 78 | + RefPtr session = m_session.get(); |
| 79 | + SYNC_FAIL_WITH_PREDEFINED_ERROR_IF(!session, InternalError); |
| 80 | + |
| 81 | + session->terminate(); |
| 82 | + |
| 83 | + return { }; |
| 84 | +} |
| 85 | + |
| 86 | +Inspector::CommandResult<String> BidiBrowserAgent::createUserContext() |
| 87 | +{ |
| 88 | + String error; |
| 89 | + auto userContext = platformCreateUserContext(error); |
| 90 | + SYNC_FAIL_WITH_PREDEFINED_ERROR_AND_DETAILS_IF(!userContext, InternalError, error); |
| 91 | + |
| 92 | + PAL::SessionID sessionID = userContext->dataStore().sessionID(); |
| 93 | + String userContextID = toUserContextIDProtocolString(sessionID); |
| 94 | + m_userContexts.set(userContextID, WTFMove(userContext)); |
| 95 | + |
| 96 | + return userContextID; |
| 97 | +} |
| 98 | + |
| 99 | +Inspector::CommandResult<Ref<JSON::ArrayOf<UserContextInfo>>> BidiBrowserAgent::getUserContexts() |
| 100 | +{ |
| 101 | + auto userContexts = JSON::ArrayOf<UserContextInfo>::create(); |
| 102 | + userContexts->addItem(UserContextInfo::create() |
| 103 | + .setUserContext(defaultUserContextID()) |
| 104 | + .release()); |
| 105 | + for (const auto& pair : m_userContexts) { |
| 106 | + userContexts->addItem(UserContextInfo::create() |
| 107 | + .setUserContext(pair.key) |
| 108 | + .release()); |
| 109 | + } |
| 110 | + return userContexts; |
| 111 | +} |
| 112 | + |
| 113 | +Inspector::CommandResult<void> BidiBrowserAgent::removeUserContext(const String& userContext) |
| 114 | +{ |
| 115 | + // https://www.w3.org/TR/webdriver-bidi/#command-browser-removeUserContext step 2. |
| 116 | + SYNC_FAIL_WITH_PREDEFINED_ERROR_AND_DETAILS_IF(userContext == defaultUserContextID(), InvalidParameter, "Cannot delete default user context."_s); |
| 117 | + |
| 118 | + auto it = m_userContexts.find(userContext); |
| 119 | + // https://www.w3.org/TR/webdriver-bidi/#command-browser-removeUserContext step 4. |
| 120 | + SYNC_FAIL_WITH_PREDEFINED_ERROR_AND_DETAILS_IF(it == m_userContexts.end(), InvalidParameter, "no such user context"_s); |
| 121 | + |
| 122 | + // TODO: track and close all pages that belong to this user context. |
| 123 | + m_userContexts.remove(it); |
| 124 | + return { }; |
| 125 | +} |
| 126 | + |
| 127 | +#if !PLATFORM(GTK) |
| 128 | +std::unique_ptr<BidiUserContext> BidiBrowserAgent::platformCreateUserContext(String& error) |
| 129 | +{ |
| 130 | + error = "User context creation is not implemented for this platform yet."_s; |
| 131 | + return nullptr; |
| 132 | +} |
| 133 | +#endif |
| 134 | + |
| 135 | +} // namespace WebKit |
| 136 | + |
| 137 | +#endif // ENABLE(WEBDRIVER_BIDI) |
| 138 | + |
0 commit comments