| 1 | /* |
| 2 | * Copyright (C) 2019 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. 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 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <WebCore/RegistrableDomain.h> |
| 30 | #include <pal/SessionID.h> |
| 31 | #include <wtf/HashMap.h> |
| 32 | #include <wtf/RunLoop.h> |
| 33 | #include <wtf/text/WTFString.h> |
| 34 | |
| 35 | namespace WebKit { |
| 36 | |
| 37 | class WebProcessPool; |
| 38 | class WebProcessProxy; |
| 39 | class WebsiteDataStore; |
| 40 | |
| 41 | class WebProcessCache { |
| 42 | WTF_MAKE_FAST_ALLOCATED; |
| 43 | public: |
| 44 | explicit WebProcessCache(WebProcessPool&); |
| 45 | |
| 46 | bool addProcessIfPossible(Ref<WebProcessProxy>&&); |
| 47 | RefPtr<WebProcessProxy> takeProcess(const WebCore::RegistrableDomain&, WebsiteDataStore&); |
| 48 | |
| 49 | void updateCapacity(WebProcessPool&); |
| 50 | unsigned capacity() const { return m_capacity; } |
| 51 | |
| 52 | unsigned size() const { return m_processesPerRegistrableDomain.size(); } |
| 53 | |
| 54 | void clear(); |
| 55 | void setApplicationIsActive(bool); |
| 56 | |
| 57 | void clearAllProcessesForSession(PAL::SessionID); |
| 58 | |
| 59 | enum class ShouldShutDownProcess { No, Yes }; |
| 60 | void removeProcess(WebProcessProxy&, ShouldShutDownProcess); |
| 61 | |
| 62 | private: |
| 63 | static Seconds cachedProcessLifetime; |
| 64 | static Seconds clearingDelayAfterApplicationResignsActive; |
| 65 | |
| 66 | class CachedProcess { |
| 67 | WTF_MAKE_FAST_ALLOCATED; |
| 68 | public: |
| 69 | CachedProcess(Ref<WebProcessProxy>&&); |
| 70 | ~CachedProcess(); |
| 71 | |
| 72 | Ref<WebProcessProxy> takeProcess(); |
| 73 | WebProcessProxy& process() { ASSERT(m_process); return *m_process; } |
| 74 | |
| 75 | private: |
| 76 | void evictionTimerFired(); |
| 77 | |
| 78 | RefPtr<WebProcessProxy> m_process; |
| 79 | RunLoop::Timer<CachedProcess> m_evictionTimer; |
| 80 | }; |
| 81 | |
| 82 | bool canCacheProcess(WebProcessProxy&) const; |
| 83 | void platformInitialize(); |
| 84 | bool addProcess(std::unique_ptr<CachedProcess>&&); |
| 85 | |
| 86 | unsigned m_capacity { 0 }; |
| 87 | |
| 88 | HashMap<uint64_t, std::unique_ptr<CachedProcess>> m_pendingAddRequests; |
| 89 | HashMap<WebCore::RegistrableDomain, std::unique_ptr<CachedProcess>> m_processesPerRegistrableDomain; |
| 90 | RunLoop::Timer<WebProcessCache> m_evictionTimer; |
| 91 | }; |
| 92 | |
| 93 | } // namespace WebKit |
| 94 | |