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
#if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
14
# include <nsIComponentRegistrar.h>
15
# include <nsServiceManagerUtils.h>
17
# include <nsIServiceManagerUtils.h>
19
#include <nsIUnicodeEncoder.h>
21
#include "null_prompt_service.hpp"
22
#include "videolink.hpp"
23
#include "xpcom_support.hpp"
25
using xpcom_support::check;
29
NS_DEFINE_IID(prompt_service_iid, NS_IPROMPTSERVICE_IID);
31
class null_prompt_service_factory : public nsIFactory
37
NS_IMPL_ISUPPORTS1(null_prompt_service_factory, nsIFactory)
39
NS_IMETHODIMP null_prompt_service_factory::CreateInstance(
40
nsISupports *, const nsIID & iid, void ** result)
42
if (!iid.Equals(prompt_service_iid))
43
return NS_ERROR_NO_INTERFACE;
45
if (null_prompt_service * service =
46
new (std::nothrow) null_prompt_service)
54
return NS_ERROR_OUT_OF_MEMORY;
58
NS_IMETHODIMP null_prompt_service_factory::LockFactory(PRBool /*lock*/)
60
return NS_ERROR_NOT_IMPLEMENTED;
63
std::string native_error_string(const PRUnichar * text)
67
while (text[text_len])
70
nsCOMPtr<nsICharsetConverterManager> conv_manager;
71
nsCOMPtr<nsIUnicodeEncoder> encoder;
72
static const nsCID charset_converter_manager_cid =
73
NS_ICHARSETCONVERTERMANAGER_CID;
74
if (NS_SUCCEEDED(CallGetService<nsICharsetConverterManager>(
75
charset_converter_manager_cid,
76
getter_AddRefs(conv_manager)))
77
&& NS_SUCCEEDED(conv_manager->GetUnicodeEncoder(
79
getter_AddRefs(encoder))))
81
encoder->SetOutputErrorBehavior(
82
nsIUnicodeEncoder::kOnError_Replace, NULL, PRUnichar('?'));
84
char buf[1000]; // Hopefully long enough for an error message
87
PRInt32 out_len = sizeof(buf);
88
encoder->Convert(text, &text_len, out, &out_len);
91
out_len = sizeof(buf) - out_len;
92
encoder->Finish(out, &out_len);
95
result.assign(buf, out);
100
result.resize(text_len);
101
for (PRInt32 i = 0; i != text_len; ++i)
102
result[i] = (text[i] < 0x80) ? text[i] : '?';
109
NS_IMPL_ISUPPORTS1(null_prompt_service, nsIPromptService)
111
NS_IMETHODIMP null_prompt_service::Alert(
112
nsIDOMWindow *, const PRUnichar *, const PRUnichar * text)
114
fatal_error(native_error_string(text));
118
NS_IMETHODIMP null_prompt_service::AlertCheck(
119
nsIDOMWindow *, const PRUnichar *, const PRUnichar * text,
120
const PRUnichar *, PRBool *)
122
fatal_error(native_error_string(text));
126
NS_IMETHODIMP null_prompt_service::Confirm(
127
nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRBool * result)
134
NS_IMETHODIMP null_prompt_service::ConfirmCheck(
135
nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
136
const PRUnichar *, PRBool *, PRBool * result)
143
NS_IMETHODIMP null_prompt_service::ConfirmEx(
144
nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
145
PRUint32 flags, const PRUnichar *, const PRUnichar *, const PRUnichar *,
146
const PRUnichar *, PRBool *, PRInt32 * result)
148
// Accept the default
149
if (flags & BUTTON_POS_1_DEFAULT)
151
else if (flags & BUTTON_POS_2_DEFAULT)
158
NS_IMETHODIMP null_prompt_service::Prompt(
159
nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
160
const PRUnichar *, PRBool *, PRBool * result)
167
NS_IMETHODIMP null_prompt_service::PromptUsernameAndPassword(
168
nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
169
PRUnichar **, PRUnichar **, const PRUnichar *, PRBool *, PRBool * result)
176
NS_IMETHODIMP null_prompt_service::PromptPassword(
177
nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
178
const PRUnichar *, PRBool *, PRBool * result)
185
NS_IMETHODIMP null_prompt_service::Select(
186
nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUint32,
187
const PRUnichar **, PRInt32 *, PRBool * result)
194
void null_prompt_service::install()
196
static const nsCID prompt_service_cid = {
197
0xa2112d6a, 0x0e28, 0x421f,
198
{0xb4, 0x6a, 0x25, 0xc0, 0xb3, 0x8, 0xcb, 0xd0}
200
nsCOMPtr<nsIFactory> prompt_factory(new null_prompt_service_factory);
201
# if MOZ_VERSION_MAJOR > 1 \
202
|| (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
203
nsCOMPtr<nsIComponentRegistrar> comp_registrar;
204
check(NS_GetComponentRegistrar(getter_AddRefs(comp_registrar)));
205
check(comp_registrar->RegisterFactory(
208
"@mozilla.org/embedcomp/prompt-service;1",
211
check(nsComponentManager::RegisterFactory(
214
"@mozilla.org/embedcomp/prompt-service;1",
216
PR_TRUE)); // replace existing