| 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 COMPUTER, INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "RemoteConnectionToTarget.h" |
| 28 | |
| 29 | #if ENABLE(REMOTE_INSPECTOR) |
| 30 | |
| 31 | #include "RemoteAutomationTarget.h" |
| 32 | #include "RemoteInspectionTarget.h" |
| 33 | #include "RemoteInspector.h" |
| 34 | |
| 35 | namespace Inspector { |
| 36 | |
| 37 | RemoteConnectionToTarget::RemoteConnectionToTarget(RemoteControllableTarget& target) |
| 38 | : m_target(&target) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | RemoteConnectionToTarget::~RemoteConnectionToTarget() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | bool RemoteConnectionToTarget::setup(bool isAutomaticInspection, bool automaticallyPause) |
| 47 | { |
| 48 | LockHolder lock(m_targetMutex); |
| 49 | if (!m_target) |
| 50 | return false; |
| 51 | |
| 52 | auto targetIdentifier = this->targetIdentifier().valueOr(0); |
| 53 | |
| 54 | if (!m_target || !m_target->remoteControlAllowed()) { |
| 55 | RemoteInspector::singleton().setupFailed(targetIdentifier); |
| 56 | m_target = nullptr; |
| 57 | } else if (is<RemoteInspectionTarget>(m_target)) { |
| 58 | auto target = downcast<RemoteInspectionTarget>(m_target); |
| 59 | target->connect(*this, isAutomaticInspection, automaticallyPause); |
| 60 | m_connected = true; |
| 61 | |
| 62 | RemoteInspector::singleton().updateTargetListing(targetIdentifier); |
| 63 | } else if (is<RemoteAutomationTarget>(m_target)) { |
| 64 | auto target = downcast<RemoteAutomationTarget>(m_target); |
| 65 | target->connect(*this); |
| 66 | m_connected = true; |
| 67 | |
| 68 | RemoteInspector::singleton().updateTargetListing(targetIdentifier); |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | void RemoteConnectionToTarget::sendMessageToTarget(const String& message) |
| 75 | { |
| 76 | RemoteControllableTarget* target = nullptr; |
| 77 | { |
| 78 | LockHolder lock(m_targetMutex); |
| 79 | if (!m_target) |
| 80 | return; |
| 81 | target = m_target; |
| 82 | } |
| 83 | |
| 84 | target->dispatchMessageFromRemote(message); |
| 85 | } |
| 86 | |
| 87 | void RemoteConnectionToTarget::close() |
| 88 | { |
| 89 | LockHolder lock(m_targetMutex); |
| 90 | if (!m_target) |
| 91 | return; |
| 92 | |
| 93 | auto targetIdentifier = m_target->targetIdentifier(); |
| 94 | |
| 95 | if (m_connected) |
| 96 | m_target->disconnect(*this); |
| 97 | |
| 98 | m_target = nullptr; |
| 99 | |
| 100 | RemoteInspector::singleton().updateTargetListing(targetIdentifier); |
| 101 | } |
| 102 | |
| 103 | void RemoteConnectionToTarget::targetClosed() |
| 104 | { |
| 105 | LockHolder lock(m_targetMutex); |
| 106 | m_target = nullptr; |
| 107 | } |
| 108 | |
| 109 | Optional<TargetID> RemoteConnectionToTarget::targetIdentifier() const |
| 110 | { |
| 111 | return m_target ? Optional<TargetID>(m_target->targetIdentifier()) : WTF::nullopt; |
| 112 | } |
| 113 | |
| 114 | void RemoteConnectionToTarget::sendMessageToFrontend(const String& message) |
| 115 | { |
| 116 | if (!m_target) |
| 117 | return; |
| 118 | |
| 119 | RemoteInspector::singleton().sendMessageToRemote(m_target->targetIdentifier(), message); |
| 120 | } |
| 121 | |
| 122 | } // namespace Inspector |
| 123 | |
| 124 | #endif // ENABLE(REMOTE_INSPECTOR) |
| 125 | |