Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "APZTestData.h"
7 : #include "mozilla/dom/APZTestDataBinding.h"
8 : #include "mozilla/dom/ToJSValue.h"
9 : #include "nsString.h"
10 :
11 : namespace mozilla {
12 : namespace layers {
13 :
14 : struct APZTestDataToJSConverter {
15 : template <typename Key, typename Value, typename KeyValuePair>
16 0 : static void ConvertMap(const std::map<Key, Value>& aFrom,
17 : dom::Sequence<KeyValuePair>& aOutTo,
18 : void (*aElementConverter)(const Key&, const Value&, KeyValuePair&)) {
19 0 : for (auto it = aFrom.begin(); it != aFrom.end(); ++it) {
20 0 : aOutTo.AppendElement(fallible);
21 0 : aElementConverter(it->first, it->second, aOutTo.LastElement());
22 : }
23 0 : }
24 :
25 0 : static void ConvertAPZTestData(const APZTestData& aFrom,
26 : dom::APZTestData& aOutTo) {
27 0 : ConvertMap(aFrom.mPaints, aOutTo.mPaints.Construct(), ConvertBucket);
28 0 : ConvertMap(aFrom.mRepaintRequests, aOutTo.mRepaintRequests.Construct(), ConvertBucket);
29 0 : }
30 :
31 0 : static void ConvertBucket(const SequenceNumber& aKey,
32 : const APZTestData::Bucket& aValue,
33 : dom::APZBucket& aOutKeyValuePair) {
34 0 : aOutKeyValuePair.mSequenceNumber.Construct() = aKey;
35 0 : ConvertMap(aValue, aOutKeyValuePair.mScrollFrames.Construct(), ConvertScrollFrameData);
36 0 : }
37 :
38 0 : static void ConvertScrollFrameData(const APZTestData::ViewID& aKey,
39 : const APZTestData::ScrollFrameData& aValue,
40 : dom::ScrollFrameData& aOutKeyValuePair) {
41 0 : aOutKeyValuePair.mScrollId.Construct() = aKey;
42 0 : ConvertMap(aValue, aOutKeyValuePair.mEntries.Construct(), ConvertEntry);
43 0 : }
44 :
45 0 : static void ConvertEntry(const std::string& aKey,
46 : const std::string& aValue,
47 : dom::ScrollFrameDataEntry& aOutKeyValuePair) {
48 0 : ConvertString(aKey, aOutKeyValuePair.mKey.Construct());
49 0 : ConvertString(aValue, aOutKeyValuePair.mValue.Construct());
50 0 : }
51 :
52 0 : static void ConvertString(const std::string& aFrom, nsString& aOutTo) {
53 0 : aOutTo = NS_ConvertUTF8toUTF16(aFrom.c_str(), aFrom.size());
54 0 : }
55 : };
56 :
57 : bool
58 0 : APZTestData::ToJS(JS::MutableHandleValue aOutValue, JSContext* aContext) const
59 : {
60 0 : dom::APZTestData result;
61 0 : APZTestDataToJSConverter::ConvertAPZTestData(*this, result);
62 0 : return dom::ToJSValue(aContext, result, aOutValue);
63 : }
64 :
65 : } // namespace layers
66 : } // namespace mozilla
|