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