| 1 | /* |
| 2 | * Copyright (C) 2015-2016 Apple Inc. 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. ``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 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 | #pragma once |
| 27 | |
| 28 | #include "HeapObserver.h" |
| 29 | #include "InspectorAgentBase.h" |
| 30 | #include "InspectorBackendDispatchers.h" |
| 31 | #include "InspectorFrontendDispatchers.h" |
| 32 | #include <wtf/Forward.h> |
| 33 | #include <wtf/Noncopyable.h> |
| 34 | #include <wtf/Seconds.h> |
| 35 | |
| 36 | namespace JSC { |
| 37 | struct HeapSnapshotNode; |
| 38 | } |
| 39 | |
| 40 | namespace Inspector { |
| 41 | |
| 42 | class InjectedScriptManager; |
| 43 | typedef String ErrorString; |
| 44 | |
| 45 | class JS_EXPORT_PRIVATE InspectorHeapAgent : public InspectorAgentBase, public HeapBackendDispatcherHandler, public JSC::HeapObserver { |
| 46 | WTF_MAKE_NONCOPYABLE(InspectorHeapAgent); |
| 47 | WTF_MAKE_FAST_ALLOCATED; |
| 48 | public: |
| 49 | InspectorHeapAgent(AgentContext&); |
| 50 | virtual ~InspectorHeapAgent() = default; |
| 51 | |
| 52 | void didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*) override; |
| 53 | void willDestroyFrontendAndBackend(DisconnectReason) override; |
| 54 | |
| 55 | // HeapBackendDispatcherHandler |
| 56 | void enable(ErrorString&) override; |
| 57 | void disable(ErrorString&) override; |
| 58 | void gc(ErrorString&) final; |
| 59 | void snapshot(ErrorString&, double* timestamp, String* snapshotData) final; |
| 60 | void startTracking(ErrorString&) final; |
| 61 | void stopTracking(ErrorString&) final; |
| 62 | void getPreview(ErrorString&, int heapObjectId, Optional<String>& resultString, RefPtr<Protocol::Debugger::FunctionDetails>&, RefPtr<Protocol::Runtime::ObjectPreview>&) final; |
| 63 | void getRemoteObject(ErrorString&, int heapObjectId, const String* optionalObjectGroup, RefPtr<Protocol::Runtime::RemoteObject>& result) final; |
| 64 | |
| 65 | // HeapObserver |
| 66 | void willGarbageCollect() override; |
| 67 | void didGarbageCollect(JSC::CollectionScope) override; |
| 68 | |
| 69 | protected: |
| 70 | void clearHeapSnapshots(); |
| 71 | |
| 72 | virtual void dispatchGarbageCollectedEvent(Protocol::Heap::GarbageCollection::Type, Seconds startTime, Seconds endTime); |
| 73 | |
| 74 | private: |
| 75 | Optional<JSC::HeapSnapshotNode> nodeForHeapObjectIdentifier(ErrorString&, unsigned heapObjectIdentifier); |
| 76 | |
| 77 | InjectedScriptManager& m_injectedScriptManager; |
| 78 | std::unique_ptr<HeapFrontendDispatcher> m_frontendDispatcher; |
| 79 | RefPtr<HeapBackendDispatcher> m_backendDispatcher; |
| 80 | InspectorEnvironment& m_environment; |
| 81 | |
| 82 | bool m_enabled { false }; |
| 83 | bool m_tracking { false }; |
| 84 | Seconds m_gcStartTime { Seconds::nan() }; |
| 85 | }; |
| 86 | |
| 87 | } // namespace Inspector |
| 88 | |