1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is PlaceInfo object.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * the Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Shawn Wilsher <me@shawnwilsher.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "PlaceInfo.h"
39 : #include "VisitInfo.h"
40 : #include "nsIURI.h"
41 : #include "nsServiceManagerUtils.h"
42 : #include "nsIXPConnect.h"
43 : #include "mozilla/Services.h"
44 :
45 : namespace mozilla {
46 : namespace places {
47 :
48 : ////////////////////////////////////////////////////////////////////////////////
49 : //// PlaceInfo
50 :
51 438 : PlaceInfo::PlaceInfo(PRInt64 aId,
52 : const nsCString& aGUID,
53 : already_AddRefed<nsIURI> aURI,
54 : const nsString& aTitle,
55 : PRInt64 aFrecency,
56 : const VisitsArray& aVisits)
57 : : mId(aId)
58 : , mGUID(aGUID)
59 : , mURI(aURI)
60 : , mTitle(aTitle)
61 : , mFrecency(aFrecency)
62 438 : , mVisits(aVisits)
63 : {
64 438 : NS_PRECONDITION(mURI, "Must provide a non-null uri!");
65 438 : }
66 :
67 : ////////////////////////////////////////////////////////////////////////////////
68 : //// mozIPlaceInfo
69 :
70 : NS_IMETHODIMP
71 10 : PlaceInfo::GetPlaceId(PRInt64* _placeId)
72 : {
73 10 : *_placeId = mId;
74 10 : return NS_OK;
75 : }
76 :
77 : NS_IMETHODIMP
78 10 : PlaceInfo::GetGuid(nsACString& _guid)
79 : {
80 10 : _guid = mGUID;
81 10 : return NS_OK;
82 : }
83 :
84 : NS_IMETHODIMP
85 65 : PlaceInfo::GetUri(nsIURI** _uri)
86 : {
87 65 : NS_ADDREF(*_uri = mURI);
88 65 : return NS_OK;
89 : }
90 :
91 : NS_IMETHODIMP
92 33 : PlaceInfo::GetTitle(nsAString& _title)
93 : {
94 33 : _title = mTitle;
95 33 : return NS_OK;
96 : }
97 :
98 : NS_IMETHODIMP
99 8 : PlaceInfo::GetFrecency(PRInt64* _frecency)
100 : {
101 8 : *_frecency = mFrecency;
102 8 : return NS_OK;
103 : }
104 :
105 : NS_IMETHODIMP
106 26 : PlaceInfo::GetVisits(JSContext* aContext,
107 : jsval* _visits)
108 : {
109 : // TODO bug 625913 when we use this in situations that have more than one
110 : // visit here, we will likely want to make this cache the value.
111 26 : JSObject* visits = JS_NewArrayObject(aContext, 0, NULL);
112 26 : NS_ENSURE_TRUE(visits, NS_ERROR_OUT_OF_MEMORY);
113 :
114 26 : JSObject* global = JS_GetGlobalForScopeChain(aContext);
115 26 : NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
116 :
117 52 : nsCOMPtr<nsIXPConnect> xpc = mozilla::services::GetXPConnect();
118 :
119 52 : for (VisitsArray::size_type idx = 0; idx < mVisits.Length(); idx++) {
120 52 : nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
121 52 : nsresult rv = xpc->WrapNative(aContext, global, mVisits[idx],
122 : NS_GET_IID(mozIVisitInfo),
123 52 : getter_AddRefs(wrapper));
124 26 : NS_ENSURE_SUCCESS(rv, rv);
125 :
126 : JSObject* jsobj;
127 26 : rv = wrapper->GetJSObject(&jsobj);
128 26 : NS_ENSURE_SUCCESS(rv, rv);
129 26 : jsval wrappedVisit = OBJECT_TO_JSVAL(jsobj);
130 :
131 26 : JSBool rc = JS_SetElement(aContext, visits, idx, &wrappedVisit);
132 26 : NS_ENSURE_TRUE(rc, NS_ERROR_UNEXPECTED);
133 : }
134 :
135 26 : *_visits = OBJECT_TO_JSVAL(visits);
136 :
137 26 : return NS_OK;
138 : }
139 :
140 : ////////////////////////////////////////////////////////////////////////////////
141 : //// nsISupports
142 :
143 7668 : NS_IMPL_ISUPPORTS1(
144 : PlaceInfo
145 : , mozIPlaceInfo
146 : )
147 :
148 : } // namespace places
149 : } // namespace mozilla
|