toolkit/components/places/PlaceInfo.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "PlaceInfo.h"
michael@0 6 #include "VisitInfo.h"
michael@0 7 #include "nsIURI.h"
michael@0 8 #include "nsServiceManagerUtils.h"
michael@0 9 #include "nsIXPConnect.h"
michael@0 10 #include "mozilla/Services.h"
michael@0 11 #include "jsapi.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace places {
michael@0 15
michael@0 16 ////////////////////////////////////////////////////////////////////////////////
michael@0 17 //// PlaceInfo
michael@0 18
michael@0 19 PlaceInfo::PlaceInfo(int64_t aId,
michael@0 20 const nsCString& aGUID,
michael@0 21 already_AddRefed<nsIURI> aURI,
michael@0 22 const nsString& aTitle,
michael@0 23 int64_t aFrecency)
michael@0 24 : mId(aId)
michael@0 25 , mGUID(aGUID)
michael@0 26 , mURI(aURI)
michael@0 27 , mTitle(aTitle)
michael@0 28 , mFrecency(aFrecency)
michael@0 29 , mVisitsAvailable(false)
michael@0 30 {
michael@0 31 NS_PRECONDITION(mURI, "Must provide a non-null uri!");
michael@0 32 }
michael@0 33
michael@0 34 PlaceInfo::PlaceInfo(int64_t aId,
michael@0 35 const nsCString& aGUID,
michael@0 36 already_AddRefed<nsIURI> aURI,
michael@0 37 const nsString& aTitle,
michael@0 38 int64_t aFrecency,
michael@0 39 const VisitsArray& aVisits)
michael@0 40 : mId(aId)
michael@0 41 , mGUID(aGUID)
michael@0 42 , mURI(aURI)
michael@0 43 , mTitle(aTitle)
michael@0 44 , mFrecency(aFrecency)
michael@0 45 , mVisits(aVisits)
michael@0 46 , mVisitsAvailable(true)
michael@0 47 {
michael@0 48 NS_PRECONDITION(mURI, "Must provide a non-null uri!");
michael@0 49 }
michael@0 50
michael@0 51 ////////////////////////////////////////////////////////////////////////////////
michael@0 52 //// mozIPlaceInfo
michael@0 53
michael@0 54 NS_IMETHODIMP
michael@0 55 PlaceInfo::GetPlaceId(int64_t* _placeId)
michael@0 56 {
michael@0 57 *_placeId = mId;
michael@0 58 return NS_OK;
michael@0 59 }
michael@0 60
michael@0 61 NS_IMETHODIMP
michael@0 62 PlaceInfo::GetGuid(nsACString& _guid)
michael@0 63 {
michael@0 64 _guid = mGUID;
michael@0 65 return NS_OK;
michael@0 66 }
michael@0 67
michael@0 68 NS_IMETHODIMP
michael@0 69 PlaceInfo::GetUri(nsIURI** _uri)
michael@0 70 {
michael@0 71 NS_ADDREF(*_uri = mURI);
michael@0 72 return NS_OK;
michael@0 73 }
michael@0 74
michael@0 75 NS_IMETHODIMP
michael@0 76 PlaceInfo::GetTitle(nsAString& _title)
michael@0 77 {
michael@0 78 _title = mTitle;
michael@0 79 return NS_OK;
michael@0 80 }
michael@0 81
michael@0 82 NS_IMETHODIMP
michael@0 83 PlaceInfo::GetFrecency(int64_t* _frecency)
michael@0 84 {
michael@0 85 *_frecency = mFrecency;
michael@0 86 return NS_OK;
michael@0 87 }
michael@0 88
michael@0 89 NS_IMETHODIMP
michael@0 90 PlaceInfo::GetVisits(JSContext* aContext,
michael@0 91 JS::MutableHandle<JS::Value> _visits)
michael@0 92 {
michael@0 93 // If the visits data was not provided, return null rather
michael@0 94 // than an empty array to distinguish this case from the case
michael@0 95 // of a place without any visit.
michael@0 96 if (!mVisitsAvailable) {
michael@0 97 _visits.setNull();
michael@0 98 return NS_OK;
michael@0 99 }
michael@0 100
michael@0 101 // TODO bug 625913 when we use this in situations that have more than one
michael@0 102 // visit here, we will likely want to make this cache the value.
michael@0 103 JS::Rooted<JSObject*> visits(aContext,
michael@0 104 JS_NewArrayObject(aContext, 0));
michael@0 105 NS_ENSURE_TRUE(visits, NS_ERROR_OUT_OF_MEMORY);
michael@0 106
michael@0 107 JS::Rooted<JSObject*> global(aContext, JS::CurrentGlobalOrNull(aContext));
michael@0 108 NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
michael@0 109
michael@0 110 nsCOMPtr<nsIXPConnect> xpc = mozilla::services::GetXPConnect();
michael@0 111
michael@0 112 for (VisitsArray::size_type idx = 0; idx < mVisits.Length(); idx++) {
michael@0 113 nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
michael@0 114 nsresult rv = xpc->WrapNative(aContext, global, mVisits[idx],
michael@0 115 NS_GET_IID(mozIVisitInfo),
michael@0 116 getter_AddRefs(wrapper));
michael@0 117 NS_ENSURE_SUCCESS(rv, rv);
michael@0 118
michael@0 119 JS::Rooted<JSObject*> jsobj(aContext, wrapper->GetJSObject());
michael@0 120 NS_ENSURE_STATE(jsobj);
michael@0 121
michael@0 122 bool rc = JS_SetElement(aContext, visits, idx, jsobj);
michael@0 123 NS_ENSURE_TRUE(rc, NS_ERROR_UNEXPECTED);
michael@0 124 }
michael@0 125
michael@0 126 _visits.setObject(*visits);
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130 ////////////////////////////////////////////////////////////////////////////////
michael@0 131 //// nsISupports
michael@0 132
michael@0 133 NS_IMPL_ISUPPORTS(
michael@0 134 PlaceInfo
michael@0 135 , mozIPlaceInfo
michael@0 136 )
michael@0 137
michael@0 138 } // namespace places
michael@0 139 } // namespace mozilla

mercurial