toolkit/components/places/PlaceInfo.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/PlaceInfo.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "PlaceInfo.h"
     1.9 +#include "VisitInfo.h"
    1.10 +#include "nsIURI.h"
    1.11 +#include "nsServiceManagerUtils.h"
    1.12 +#include "nsIXPConnect.h"
    1.13 +#include "mozilla/Services.h"
    1.14 +#include "jsapi.h"
    1.15 +
    1.16 +namespace mozilla {
    1.17 +namespace places {
    1.18 +
    1.19 +////////////////////////////////////////////////////////////////////////////////
    1.20 +//// PlaceInfo
    1.21 +
    1.22 +PlaceInfo::PlaceInfo(int64_t aId,
    1.23 +                     const nsCString& aGUID,
    1.24 +                     already_AddRefed<nsIURI> aURI,
    1.25 +                     const nsString& aTitle,
    1.26 +                     int64_t aFrecency)
    1.27 +: mId(aId)
    1.28 +, mGUID(aGUID)
    1.29 +, mURI(aURI)
    1.30 +, mTitle(aTitle)
    1.31 +, mFrecency(aFrecency)
    1.32 +, mVisitsAvailable(false)
    1.33 +{
    1.34 +  NS_PRECONDITION(mURI, "Must provide a non-null uri!");
    1.35 +}
    1.36 +
    1.37 +PlaceInfo::PlaceInfo(int64_t aId,
    1.38 +                     const nsCString& aGUID,
    1.39 +                     already_AddRefed<nsIURI> aURI,
    1.40 +                     const nsString& aTitle,
    1.41 +                     int64_t aFrecency,
    1.42 +                     const VisitsArray& aVisits)
    1.43 +: mId(aId)
    1.44 +, mGUID(aGUID)
    1.45 +, mURI(aURI)
    1.46 +, mTitle(aTitle)
    1.47 +, mFrecency(aFrecency)
    1.48 +, mVisits(aVisits)
    1.49 +, mVisitsAvailable(true)
    1.50 +{
    1.51 +  NS_PRECONDITION(mURI, "Must provide a non-null uri!");
    1.52 +}
    1.53 +
    1.54 +////////////////////////////////////////////////////////////////////////////////
    1.55 +//// mozIPlaceInfo
    1.56 +
    1.57 +NS_IMETHODIMP
    1.58 +PlaceInfo::GetPlaceId(int64_t* _placeId)
    1.59 +{
    1.60 +  *_placeId = mId;
    1.61 +  return NS_OK;
    1.62 +}
    1.63 +
    1.64 +NS_IMETHODIMP
    1.65 +PlaceInfo::GetGuid(nsACString& _guid)
    1.66 +{
    1.67 +  _guid = mGUID;
    1.68 +  return NS_OK;
    1.69 +}
    1.70 +
    1.71 +NS_IMETHODIMP
    1.72 +PlaceInfo::GetUri(nsIURI** _uri)
    1.73 +{
    1.74 +  NS_ADDREF(*_uri = mURI);
    1.75 +  return NS_OK;
    1.76 +}
    1.77 +
    1.78 +NS_IMETHODIMP
    1.79 +PlaceInfo::GetTitle(nsAString& _title)
    1.80 +{
    1.81 +  _title = mTitle;
    1.82 +  return NS_OK;
    1.83 +}
    1.84 +
    1.85 +NS_IMETHODIMP
    1.86 +PlaceInfo::GetFrecency(int64_t* _frecency)
    1.87 +{
    1.88 +  *_frecency = mFrecency;
    1.89 +  return NS_OK;
    1.90 +}
    1.91 +
    1.92 +NS_IMETHODIMP
    1.93 +PlaceInfo::GetVisits(JSContext* aContext,
    1.94 +                     JS::MutableHandle<JS::Value> _visits)
    1.95 +{
    1.96 +  // If the visits data was not provided, return null rather
    1.97 +  // than an empty array to distinguish this case from the case
    1.98 +  // of a place without any visit.
    1.99 +  if (!mVisitsAvailable) {
   1.100 +    _visits.setNull();
   1.101 +    return NS_OK;
   1.102 +  }
   1.103 +
   1.104 +  // TODO bug 625913 when we use this in situations that have more than one
   1.105 +  // visit here, we will likely want to make this cache the value.
   1.106 +  JS::Rooted<JSObject*> visits(aContext,
   1.107 +                               JS_NewArrayObject(aContext, 0));
   1.108 +  NS_ENSURE_TRUE(visits, NS_ERROR_OUT_OF_MEMORY);
   1.109 +
   1.110 +  JS::Rooted<JSObject*> global(aContext, JS::CurrentGlobalOrNull(aContext));
   1.111 +  NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
   1.112 +
   1.113 +  nsCOMPtr<nsIXPConnect> xpc = mozilla::services::GetXPConnect();
   1.114 +
   1.115 +  for (VisitsArray::size_type idx = 0; idx < mVisits.Length(); idx++) {
   1.116 +    nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
   1.117 +    nsresult rv = xpc->WrapNative(aContext, global, mVisits[idx],
   1.118 +                                  NS_GET_IID(mozIVisitInfo),
   1.119 +                                  getter_AddRefs(wrapper));
   1.120 +    NS_ENSURE_SUCCESS(rv, rv);
   1.121 +
   1.122 +    JS::Rooted<JSObject*> jsobj(aContext, wrapper->GetJSObject());
   1.123 +    NS_ENSURE_STATE(jsobj);
   1.124 +
   1.125 +    bool rc = JS_SetElement(aContext, visits, idx, jsobj);
   1.126 +    NS_ENSURE_TRUE(rc, NS_ERROR_UNEXPECTED);
   1.127 +  }
   1.128 +
   1.129 +  _visits.setObject(*visits);
   1.130 +  return NS_OK;
   1.131 +}
   1.132 +
   1.133 +////////////////////////////////////////////////////////////////////////////////
   1.134 +//// nsISupports
   1.135 +
   1.136 +NS_IMPL_ISUPPORTS(
   1.137 +  PlaceInfo
   1.138 +, mozIPlaceInfo
   1.139 +)
   1.140 +
   1.141 +} // namespace places
   1.142 +} // namespace mozilla

mercurial