PlacesEventCounts.cpp (2055B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "PlacesEventCounts.h" 8 9 #include "mozilla/ErrorResult.h" 10 #include "mozilla/dom/BindingUtils.h" 11 #include "mozilla/dom/PlacesEventBinding.h" 12 #include "mozilla/dom/PlacesEventCounts.h" 13 #include "mozilla/dom/PlacesObserversBinding.h" 14 15 namespace mozilla::dom { 16 17 // Only needed for refcounted objects. 18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(PlacesEventCounts) 19 NS_IMPL_CYCLE_COLLECTING_ADDREF(PlacesEventCounts) 20 NS_IMPL_CYCLE_COLLECTING_RELEASE(PlacesEventCounts) 21 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PlacesEventCounts) 22 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 23 NS_INTERFACE_MAP_ENTRY(nsISupports) 24 NS_INTERFACE_MAP_END 25 26 PlacesEventCounts::PlacesEventCounts() { 27 ErrorResult rv; 28 for (auto eventType : MakeWebIDLEnumeratedRange<PlacesEventType>()) { 29 PlacesEventCounts_Binding::MaplikeHelpers::Set( 30 this, NS_ConvertUTF8toUTF16(GetEnumString(eventType)), 0, rv); 31 if (NS_WARN_IF(rv.Failed())) { 32 rv.SuppressException(); 33 return; 34 } 35 } 36 } 37 38 nsresult PlacesEventCounts::Increment(PlacesEventType aEventType) { 39 ErrorResult rv; 40 nsAutoCString eventName(GetEnumString(aEventType)); 41 uint64_t count = PlacesEventCounts_Binding::MaplikeHelpers::Get( 42 this, NS_ConvertUTF8toUTF16(eventName), rv); 43 if (MOZ_UNLIKELY(rv.Failed())) { 44 return rv.StealNSResult(); 45 } 46 PlacesEventCounts_Binding::MaplikeHelpers::Set( 47 this, NS_ConvertUTF8toUTF16(eventName), ++count, rv); 48 if (MOZ_UNLIKELY(rv.Failed())) { 49 return rv.StealNSResult(); 50 } 51 return NS_OK; 52 } 53 54 JSObject* PlacesEventCounts::WrapObject(JSContext* aCx, 55 JS::Handle<JSObject*> aGivenProto) { 56 return PlacesEventCounts_Binding::Wrap(aCx, this, aGivenProto); 57 } 58 59 } // namespace mozilla::dom