1 | /* |
2 | * Copyright (C) 2013 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 | #include "config.h" |
27 | #include "StatisticsRequest.h" |
28 | |
29 | #include "APIArray.h" |
30 | #include "APIDictionary.h" |
31 | |
32 | namespace WebKit { |
33 | |
34 | StatisticsRequest::StatisticsRequest(Ref<DictionaryCallback>&& callback) |
35 | : m_callback(WTFMove(callback)) |
36 | { |
37 | } |
38 | |
39 | StatisticsRequest::~StatisticsRequest() |
40 | { |
41 | if (m_callback) |
42 | m_callback->invalidate(); |
43 | } |
44 | |
45 | uint64_t StatisticsRequest::addOutstandingRequest() |
46 | { |
47 | static std::atomic<int64_t> uniqueRequestID; |
48 | |
49 | uint64_t requestID = ++uniqueRequestID; |
50 | m_outstandingRequests.add(requestID); |
51 | return requestID; |
52 | } |
53 | |
54 | static void addToDictionaryFromHashMap(API::Dictionary* dictionary, const HashMap<String, uint64_t>& map) |
55 | { |
56 | HashMap<String, uint64_t>::const_iterator end = map.end(); |
57 | for (HashMap<String, uint64_t>::const_iterator it = map.begin(); it != end; ++it) |
58 | dictionary->set(it->key, RefPtr<API::UInt64>(API::UInt64::create(it->value)).get()); |
59 | } |
60 | |
61 | static Ref<API::Dictionary> createDictionaryFromHashMap(const HashMap<String, uint64_t>& map) |
62 | { |
63 | Ref<API::Dictionary> result = API::Dictionary::create(); |
64 | addToDictionaryFromHashMap(result.ptr(), map); |
65 | return result; |
66 | } |
67 | |
68 | void StatisticsRequest::completedRequest(uint64_t requestID, const StatisticsData& data) |
69 | { |
70 | ASSERT(m_outstandingRequests.contains(requestID)); |
71 | m_outstandingRequests.remove(requestID); |
72 | |
73 | if (!m_responseDictionary) |
74 | m_responseDictionary = API::Dictionary::create(); |
75 | |
76 | // FIXME (Multi-WebProcess) <rdar://problem/13200059>: This code overwrites any previous response data received. |
77 | // When getting responses from multiple WebProcesses we need to combine items instead of clobbering them. |
78 | |
79 | addToDictionaryFromHashMap(m_responseDictionary.get(), data.statisticsNumbers); |
80 | |
81 | if (!data.javaScriptProtectedObjectTypeCounts.isEmpty()) |
82 | m_responseDictionary->set("JavaScriptProtectedObjectTypeCounts" , createDictionaryFromHashMap(data.javaScriptProtectedObjectTypeCounts)); |
83 | if (!data.javaScriptObjectTypeCounts.isEmpty()) |
84 | m_responseDictionary->set("JavaScriptObjectTypeCounts" , createDictionaryFromHashMap(data.javaScriptObjectTypeCounts)); |
85 | |
86 | if (!data.webCoreCacheStatistics.isEmpty()) { |
87 | Vector<RefPtr<API::Object>> cacheStatistics; |
88 | cacheStatistics.reserveInitialCapacity(data.webCoreCacheStatistics.size()); |
89 | |
90 | for (const auto& statistic : data.webCoreCacheStatistics) |
91 | cacheStatistics.uncheckedAppend(createDictionaryFromHashMap(statistic)); |
92 | |
93 | m_responseDictionary->set("WebCoreCacheStatistics" , API::Array::create(WTFMove(cacheStatistics))); |
94 | } |
95 | |
96 | if (m_outstandingRequests.isEmpty()) { |
97 | m_callback->performCallbackWithReturnValue(m_responseDictionary.get()); |
98 | m_callback = nullptr; |
99 | } |
100 | } |
101 | |
102 | } // namespace WebKit |
103 | |