1 // Copyright 2006 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
10 #include <nsICharsetConverterManager.h>
11 #include <nsIComponentManager.h>
12 #include <nsIFactory.h>
13 #include <nsIServiceManagerUtils.h>
14 #include <nsIUnicodeEncoder.h>
16 #include "null_prompt_service.hpp"
18 #include "xpcom_support.hpp"
20 using xpcom_support::check;
24 NS_DEFINE_IID(prompt_service_iid, NS_IPROMPTSERVICE_IID);
26 class null_prompt_service_factory : public nsIFactory
32 NS_IMPL_ISUPPORTS1(null_prompt_service_factory, nsIFactory)
34 NS_IMETHODIMP null_prompt_service_factory::CreateInstance(
35 nsISupports *, const nsIID & iid, void ** result)
37 if (!iid.Equals(prompt_service_iid))
38 return NS_ERROR_NO_INTERFACE;
40 if (null_prompt_service * service =
41 new (std::nothrow) null_prompt_service)
49 return NS_ERROR_OUT_OF_MEMORY;
53 NS_IMETHODIMP null_prompt_service_factory::LockFactory(PRBool lock)
55 return NS_ERROR_NOT_IMPLEMENTED;
58 std::string native_error_string(const PRUnichar * text)
62 while (text[text_len])
65 nsCOMPtr<nsICharsetConverterManager> conv_manager;
66 nsCOMPtr<nsIUnicodeEncoder> encoder;
67 static const nsCID charset_converter_manager_cid =
68 NS_ICHARSETCONVERTERMANAGER_CID;
69 if (NS_SUCCEEDED(CallGetService<nsICharsetConverterManager>(
70 charset_converter_manager_cid,
71 getter_AddRefs(conv_manager)))
72 && NS_SUCCEEDED(conv_manager->GetUnicodeEncoder(
74 getter_AddRefs(encoder))))
76 encoder->SetOutputErrorBehavior(
77 nsIUnicodeEncoder::kOnError_Replace, NULL, PRUnichar('?'));
79 char buf[1000]; // Hopefully long enough for an error message
82 PRInt32 out_len = sizeof(buf);
83 encoder->Convert(text, &text_len, out, &out_len);
86 out_len = sizeof(buf) - out_len;
87 encoder->Finish(out, &out_len);
90 result.assign(buf, out);
95 result.resize(text_len);
96 for (PRInt32 i = 0; i != text_len; ++i)
97 result[i] = (text[i] < 0x80) ? text[i] : '?';
104 NS_IMPL_ISUPPORTS1(null_prompt_service, nsIPromptService)
106 NS_IMETHODIMP null_prompt_service::Alert(
107 nsIDOMWindow *, const PRUnichar *, const PRUnichar * text)
109 fatal_error(native_error_string(text));
113 NS_IMETHODIMP null_prompt_service::AlertCheck(
114 nsIDOMWindow *, const PRUnichar *, const PRUnichar * text,
115 const PRUnichar *, PRBool *)
117 fatal_error(native_error_string(text));
121 NS_IMETHODIMP null_prompt_service::Confirm(
122 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRBool * result)
129 NS_IMETHODIMP null_prompt_service::ConfirmCheck(
130 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
131 const PRUnichar *, PRBool *, PRBool * result)
138 NS_IMETHODIMP null_prompt_service::ConfirmEx(
139 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
140 PRUint32 flags, const PRUnichar *, const PRUnichar *, const PRUnichar *,
141 const PRUnichar *, PRBool *, PRInt32 * result)
143 // Accept the default
144 if (flags & BUTTON_POS_1_DEFAULT)
146 else if (flags & BUTTON_POS_2_DEFAULT)
153 NS_IMETHODIMP null_prompt_service::Prompt(
154 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
155 const PRUnichar *, PRBool *, PRBool * result)
162 NS_IMETHODIMP null_prompt_service::PromptUsernameAndPassword(
163 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
164 PRUnichar **, PRUnichar **, const PRUnichar *, PRBool *, PRBool * result)
171 NS_IMETHODIMP null_prompt_service::PromptPassword(
172 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
173 const PRUnichar *, PRBool *, PRBool * result)
180 NS_IMETHODIMP null_prompt_service::Select(
181 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUint32,
182 const PRUnichar **, PRInt32 *, PRBool * result)
189 void null_prompt_service::install()
191 static const nsCID prompt_service_cid = {
192 0xa2112d6a, 0x0e28, 0x421f,
193 {0xb4, 0x6a, 0x25, 0xc0, 0xb3, 0x8, 0xcb, 0xd0}
195 nsCOMPtr<nsIFactory> prompt_factory(new null_prompt_service_factory);
196 check(nsComponentManager::RegisterFactory(
199 "@mozilla.org/embedcomp/prompt-service;1",
201 PR_TRUE)); // replace existing