/src/mozilla-central/dom/base/IntlUtils.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "IntlUtils.h" |
8 | | |
9 | | #include "mozilla/dom/ToJSValue.h" |
10 | | #include "mozIMozIntl.h" |
11 | | #include "nsContentUtils.h" |
12 | | |
13 | | namespace mozilla { |
14 | | namespace dom { |
15 | | |
16 | | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(IntlUtils, mWindow) |
17 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(IntlUtils) |
18 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(IntlUtils) |
19 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IntlUtils) |
20 | 0 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
21 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
22 | 0 | NS_INTERFACE_MAP_END |
23 | | |
24 | | IntlUtils::IntlUtils(nsPIDOMWindowInner* aWindow) |
25 | | : mWindow(aWindow) |
26 | 0 | { |
27 | 0 | } |
28 | | |
29 | | IntlUtils::~IntlUtils() |
30 | 0 | { |
31 | 0 | } |
32 | | |
33 | | JSObject* |
34 | | IntlUtils::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
35 | 0 | { |
36 | 0 | return IntlUtils_Binding::Wrap(aCx, this, aGivenProto); |
37 | 0 | } |
38 | | |
39 | | void |
40 | | IntlUtils::GetDisplayNames(const Sequence<nsString>& aLocales, |
41 | | const DisplayNameOptions& aOptions, |
42 | | DisplayNameResult& aResult, ErrorResult& aError) |
43 | 0 | { |
44 | 0 | MOZ_ASSERT(nsContentUtils::IsCallerChrome() || |
45 | 0 | nsContentUtils::IsCallerContentXBL()); |
46 | 0 |
|
47 | 0 | nsCOMPtr<mozIMozIntl> mozIntl = do_GetService("@mozilla.org/mozintl;1"); |
48 | 0 | if (!mozIntl) { |
49 | 0 | aError.Throw(NS_ERROR_UNEXPECTED); |
50 | 0 | return; |
51 | 0 | } |
52 | 0 | |
53 | 0 | aError.MightThrowJSException(); |
54 | 0 |
|
55 | 0 | // Need to enter privileged junk scope since mozIntl implementation is in |
56 | 0 | // chrome JS and passing XBL JS there shouldn't work. |
57 | 0 | AutoJSAPI jsapi; |
58 | 0 | if (!jsapi.Init(xpc::PrivilegedJunkScope())) { |
59 | 0 | aError.Throw(NS_ERROR_FAILURE); |
60 | 0 | return; |
61 | 0 | } |
62 | 0 | JSContext* cx = jsapi.cx(); |
63 | 0 |
|
64 | 0 | // Prepare parameter for getDisplayNames(). |
65 | 0 | JS::Rooted<JS::Value> locales(cx); |
66 | 0 | if (!ToJSValue(cx, aLocales, &locales)) { |
67 | 0 | aError.StealExceptionFromJSContext(cx); |
68 | 0 | return; |
69 | 0 | } |
70 | 0 | |
71 | 0 | JS::Rooted<JS::Value> options(cx); |
72 | 0 | if (!ToJSValue(cx, aOptions, &options)) { |
73 | 0 | aError.StealExceptionFromJSContext(cx); |
74 | 0 | return; |
75 | 0 | } |
76 | 0 | |
77 | 0 | // Now call the method. |
78 | 0 | JS::Rooted<JS::Value> retVal(cx); |
79 | 0 | nsresult rv = mozIntl->GetDisplayNames(locales, options, &retVal); |
80 | 0 | if (NS_FAILED(rv)) { |
81 | 0 | aError.Throw(rv); |
82 | 0 | return; |
83 | 0 | } |
84 | 0 | |
85 | 0 | if (!retVal.isObject() || !JS_WrapValue(cx, &retVal)) { |
86 | 0 | aError.Throw(NS_ERROR_FAILURE); |
87 | 0 | return; |
88 | 0 | } |
89 | 0 | |
90 | 0 | // Return the result as DisplayNameResult. |
91 | 0 | if (!aResult.Init(cx, retVal)) { |
92 | 0 | aError.Throw(NS_ERROR_FAILURE); |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | | void |
97 | | IntlUtils::GetLocaleInfo(const Sequence<nsString>& aLocales, |
98 | | LocaleInfo& aResult, ErrorResult& aError) |
99 | 0 | { |
100 | 0 | MOZ_ASSERT(nsContentUtils::IsCallerChrome() || |
101 | 0 | nsContentUtils::IsCallerContentXBL()); |
102 | 0 |
|
103 | 0 | nsCOMPtr<mozIMozIntl> mozIntl = do_GetService("@mozilla.org/mozintl;1"); |
104 | 0 | if (!mozIntl) { |
105 | 0 | aError.Throw(NS_ERROR_UNEXPECTED); |
106 | 0 | return; |
107 | 0 | } |
108 | 0 | |
109 | 0 | AutoJSAPI jsapi; |
110 | 0 | if (!jsapi.Init(xpc::PrivilegedJunkScope())) { |
111 | 0 | aError.Throw(NS_ERROR_FAILURE); |
112 | 0 | return; |
113 | 0 | } |
114 | 0 | JSContext* cx = jsapi.cx(); |
115 | 0 |
|
116 | 0 | // Prepare parameter for getLocaleInfo(). |
117 | 0 | JS::Rooted<JS::Value> locales(cx); |
118 | 0 | if (!ToJSValue(cx, aLocales, &locales)) { |
119 | 0 | aError.StealExceptionFromJSContext(cx); |
120 | 0 | return; |
121 | 0 | } |
122 | 0 | |
123 | 0 | // Now call the method. |
124 | 0 | JS::Rooted<JS::Value> retVal(cx); |
125 | 0 | nsresult rv = mozIntl->GetLocaleInfo(locales, &retVal); |
126 | 0 | if (NS_FAILED(rv)) { |
127 | 0 | aError.Throw(rv); |
128 | 0 | return; |
129 | 0 | } |
130 | 0 | |
131 | 0 | if (!retVal.isObject() || !JS_WrapValue(cx, &retVal)) { |
132 | 0 | aError.Throw(NS_ERROR_FAILURE); |
133 | 0 | return; |
134 | 0 | } |
135 | 0 | |
136 | 0 | // Return the result as LocaleInfo. |
137 | 0 | if (!aResult.Init(cx, retVal)) { |
138 | 0 | aError.Throw(NS_ERROR_FAILURE); |
139 | 0 | } |
140 | 0 | } |
141 | | |
142 | | } // dom namespace |
143 | | } // mozilla namespace |