michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "PlaceInfo.h" michael@0: #include "VisitInfo.h" michael@0: #include "nsIURI.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsIXPConnect.h" michael@0: #include "mozilla/Services.h" michael@0: #include "jsapi.h" michael@0: michael@0: namespace mozilla { michael@0: namespace places { michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// PlaceInfo michael@0: michael@0: PlaceInfo::PlaceInfo(int64_t aId, michael@0: const nsCString& aGUID, michael@0: already_AddRefed aURI, michael@0: const nsString& aTitle, michael@0: int64_t aFrecency) michael@0: : mId(aId) michael@0: , mGUID(aGUID) michael@0: , mURI(aURI) michael@0: , mTitle(aTitle) michael@0: , mFrecency(aFrecency) michael@0: , mVisitsAvailable(false) michael@0: { michael@0: NS_PRECONDITION(mURI, "Must provide a non-null uri!"); michael@0: } michael@0: michael@0: PlaceInfo::PlaceInfo(int64_t aId, michael@0: const nsCString& aGUID, michael@0: already_AddRefed aURI, michael@0: const nsString& aTitle, michael@0: int64_t aFrecency, michael@0: const VisitsArray& aVisits) michael@0: : mId(aId) michael@0: , mGUID(aGUID) michael@0: , mURI(aURI) michael@0: , mTitle(aTitle) michael@0: , mFrecency(aFrecency) michael@0: , mVisits(aVisits) michael@0: , mVisitsAvailable(true) michael@0: { michael@0: NS_PRECONDITION(mURI, "Must provide a non-null uri!"); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// mozIPlaceInfo michael@0: michael@0: NS_IMETHODIMP michael@0: PlaceInfo::GetPlaceId(int64_t* _placeId) michael@0: { michael@0: *_placeId = mId; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: PlaceInfo::GetGuid(nsACString& _guid) michael@0: { michael@0: _guid = mGUID; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: PlaceInfo::GetUri(nsIURI** _uri) michael@0: { michael@0: NS_ADDREF(*_uri = mURI); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: PlaceInfo::GetTitle(nsAString& _title) michael@0: { michael@0: _title = mTitle; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: PlaceInfo::GetFrecency(int64_t* _frecency) michael@0: { michael@0: *_frecency = mFrecency; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: PlaceInfo::GetVisits(JSContext* aContext, michael@0: JS::MutableHandle _visits) michael@0: { michael@0: // If the visits data was not provided, return null rather michael@0: // than an empty array to distinguish this case from the case michael@0: // of a place without any visit. michael@0: if (!mVisitsAvailable) { michael@0: _visits.setNull(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // TODO bug 625913 when we use this in situations that have more than one michael@0: // visit here, we will likely want to make this cache the value. michael@0: JS::Rooted visits(aContext, michael@0: JS_NewArrayObject(aContext, 0)); michael@0: NS_ENSURE_TRUE(visits, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: JS::Rooted global(aContext, JS::CurrentGlobalOrNull(aContext)); michael@0: NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED); michael@0: michael@0: nsCOMPtr xpc = mozilla::services::GetXPConnect(); michael@0: michael@0: for (VisitsArray::size_type idx = 0; idx < mVisits.Length(); idx++) { michael@0: nsCOMPtr wrapper; michael@0: nsresult rv = xpc->WrapNative(aContext, global, mVisits[idx], michael@0: NS_GET_IID(mozIVisitInfo), michael@0: getter_AddRefs(wrapper)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: JS::Rooted jsobj(aContext, wrapper->GetJSObject()); michael@0: NS_ENSURE_STATE(jsobj); michael@0: michael@0: bool rc = JS_SetElement(aContext, visits, idx, jsobj); michael@0: NS_ENSURE_TRUE(rc, NS_ERROR_UNEXPECTED); michael@0: } michael@0: michael@0: _visits.setObject(*visits); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// nsISupports michael@0: michael@0: NS_IMPL_ISUPPORTS( michael@0: PlaceInfo michael@0: , mozIPlaceInfo michael@0: ) michael@0: michael@0: } // namespace places michael@0: } // namespace mozilla