NetworkProxyImport.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2009 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
30 #include "nspr.h"
31 #include "nsCOMPtr.h"
32 
33 #include <nsNetUtil.h>
34 #include <nsIPrefService.h>
35 #include <nsIMutableArray.h>
36 
37 #include "NetworkProxyImport.h"
38 
39 #ifdef XP_WIN
40 #include <windows.h>
41 #include "nsIWindowsRegKey.h"
42 #endif
43 
44 //-----------------------------------------------------------------------------
46  nsresult (*ImportProxySettingsFunction)(PRBool *);
47  const char *sourceName;
48 };
49 
51 #ifdef XP_WIN
52  { ImportProxySettings_IE, "Internet Explorer" },
53 #endif
54  { ImportProxySettings_Auto, "Automatic" },
55 };
56 
58  char* prefix;
59  PRInt32 prefixLength;
61  char* hostPref;
62  char* portPref;
63 };
64 
65 //-----------------------------------------------------------------------------
67 
68 //-----------------------------------------------------------------------------
70 {
71 } //ctor
72 
73 //-----------------------------------------------------------------------------
75 {
76 } //dtor
77 
78 //-----------------------------------------------------------------------------
79 // Import proxy settings from the specified source
80 NS_IMETHODIMP CNetworkProxyImport::ImportProxySettings(const nsAString &aSource,
81  PRBool *_retval)
82 {
83  NS_ENSURE_ARG_POINTER(_retval);
84  *_retval = PR_FALSE;
85 
86  for (unsigned int i=0;i<NS_ARRAY_LENGTH(networkProxyImportSources);i++) {
87  if (aSource.
88  Equals(NS_ConvertASCIItoUTF16(networkProxyImportSources[i].sourceName))) {
89  return networkProxyImportSources[i].ImportProxySettingsFunction(_retval);
90  }
91  }
92 
93  return NS_ERROR_INVALID_ARG;
94 }
95 
96 //-----------------------------------------------------------------------------
97 // Return an array of nsISupportsString containing the proxy import source IDs
98 NS_IMETHODIMP CNetworkProxyImport::GetImportSources(nsIArray **_retval)
99 {
100  NS_ENSURE_ARG_POINTER(_retval);
101 
102  nsresult rv;
103  nsCOMPtr<nsIMutableArray> array =
104  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
105  NS_ENSURE_SUCCESS(rv, rv);
106 
107  for (unsigned int i=0;i<NS_ARRAY_LENGTH(networkProxyImportSources);i++) {
108  nsCOMPtr<nsISupportsString> source =
109  do_CreateInstance("@mozilla.org/supports-string;1", &rv);
110  source->
111  SetData(NS_ConvertASCIItoUTF16(networkProxyImportSources[i].sourceName));
112  rv = array->AppendElement(source, PR_FALSE);
113  }
114 
115  return CallQueryInterface(array, _retval);
116 }
117 
118 //-----------------------------------------------------------------------------
119 void CNetworkProxyImport::SetUnicharPref(const char* aPref,
120  const nsAString& aValue,
121  nsIPrefBranch* aPrefs)
122 {
123  nsCOMPtr<nsISupportsString> supportsString =
124  do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
125  if (supportsString) {
126  supportsString->SetData(aValue);
127  aPrefs->SetComplexValue(aPref, NS_GET_IID(nsISupportsString),
128  supportsString);
129  }
130 }
131 
132 //-----------------------------------------------------------------------------
133 void CNetworkProxyImport::ParseOverrideServers(const nsAString& aServers,
134  nsIPrefBranch* aBranch)
135 {
136  // Windows (and Opera) formats its proxy override list in the form:
137  // server;server;server where server is a server name or ip address,
138  // or "<local>". Mozilla's format is server,server,server, and <local>
139  // must be translated to "localhost,127.0.0.1"
140  nsAutoString override(aServers);
141  PRInt32 left = 0, right = 0;
142  for (;;) {
143  right = override.FindChar(';', right);
144  const nsAString& host =
145  Substring(override, left,
146  (right < 0 ? override.Length() : right) - left);
147  if (host.EqualsLiteral("<local>"))
148  override.Replace(left, 7, NS_LITERAL_STRING("localhost,127.0.0.1"));
149  if (right < 0)
150  break;
151  left = right + 1;
152  override.Replace(right, 1, NS_LITERAL_STRING(","));
153  }
154  CNetworkProxyImport::SetUnicharPref("network.proxy.no_proxies_on",
155  override,
156  aBranch);
157 }
158 
159 //-----------------------------------------------------------------------------
160 void CNetworkProxyImport::SetProxyPref(const nsAString& aHostPort,
161  const char* aPref,
162  const char* aPortPref,
163  nsIPrefBranch* aPrefs)
164 {
165  nsCOMPtr<nsIURI> uri;
166  nsCAutoString host;
167  PRInt32 portValue;
168 
169  // try parsing it as a URI first
170  if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), aHostPort))
171  && NS_SUCCEEDED(uri->GetHost(host))
172  && !host.IsEmpty()
173  && NS_SUCCEEDED(uri->GetPort(&portValue))) {
175  NS_ConvertUTF8toUTF16(host),
176  aPrefs);
177  aPrefs->SetIntPref(aPortPref, portValue);
178  } else {
179  nsAutoString hostPort(aHostPort);
180  PRInt32 portDelimOffset = hostPort.RFindChar(':');
181  if (portDelimOffset > 0) {
183  SetUnicharPref(aPref,
184  Substring(hostPort, 0, portDelimOffset),
185  aPrefs);
186  nsAutoString port(Substring(hostPort, portDelimOffset + 1));
187  nsresult stringErr;
188  portValue = port.ToInteger(&stringErr);
189  if (NS_SUCCEEDED(stringErr))
190  aPrefs->SetIntPref(aPortPref, portValue);
191  } else {
192  CNetworkProxyImport::SetUnicharPref(aPref, hostPort, aPrefs);
193  }
194  }
195 }
196 
197 //-----------------------------------------------------------------------------
198 // This "import" source simply sets the proxy settings to "automatic detection",
199 // it should be the last source in the list as it is the least likely to succeed
200 nsresult ImportProxySettings_Auto(PRBool *_retval)
201 {
202  nsCOMPtr<nsIPrefBranch> prefs;
203  nsCOMPtr<nsIPrefService> pserve(do_GetService(NS_PREFSERVICE_CONTRACTID));
204  if (pserve)
205  pserve->GetBranch("", getter_AddRefs(prefs));
206  if (!prefs) {
207  *_retval = PR_FALSE;
208  return NS_ERROR_FAILURE;
209  }
210 
211  prefs->SetIntPref("network.proxy.type", 4);
212 
213  *_retval = PR_TRUE;
214 
215  return NS_OK;
216 }
217 
218 //-----------------------------------------------------------------------------
219 // Import settings from IE (code from mozilla IE migration service).
220 // This does not support the "automatic detection" setting (if that mode is set
221 // in IE, the settings will be imported as "no proxy").
222 #ifdef XP_WIN
223 nsresult ImportProxySettings_IE(PRBool *_retval)
224 {
225  nsCOMPtr<nsIPrefBranch> prefs;
226  nsCOMPtr<nsIPrefService> pserve(do_GetService(NS_PREFSERVICE_CONTRACTID));
227  if (pserve)
228  pserve->GetBranch("", getter_AddRefs(prefs));
229  if (!prefs)
230  return NS_ERROR_FAILURE;
231 
232  nsCOMPtr<nsIWindowsRegKey> regKey =
233  do_CreateInstance("@mozilla.org/windows-registry-key;1");
234  NS_NAMED_LITERAL_STRING(key,
235  "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
236  if (regKey &&
237  NS_SUCCEEDED(regKey->Open(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER,
239  nsAutoString buf;
240 
241  PRUint32 proxyType = 0;
242  // If there's an autoconfig URL specified in the registry at all,
243  // it is being used.
244  if (NS_SUCCEEDED(regKey->
245  ReadStringValue(NS_LITERAL_STRING("AutoConfigURL"), buf))) {
246  // make this future-proof (MS IE will support IDN eventually and
247  // 'URL' will contain more than ASCII characters)
248  CNetworkProxyImport::SetUnicharPref("network.proxy.autoconfig_url",
249  buf,
250  prefs);
251  proxyType = 2;
252  }
253 
254  // ProxyEnable
255  PRUint32 enabled;
256  if (NS_SUCCEEDED(regKey->
257  ReadIntValue(NS_LITERAL_STRING("ProxyEnable"), &enabled))) {
258  if (enabled & 0x1)
259  proxyType = 1;
260  }
261 
262  prefs->SetIntPref("network.proxy.type", proxyType);
263 
264  if (NS_SUCCEEDED(regKey->
265  ReadStringValue(NS_LITERAL_STRING("ProxyOverride"), buf)))
267 
268  if (NS_SUCCEEDED(regKey->
269  ReadStringValue(NS_LITERAL_STRING("ProxyServer"), buf))) {
270 
271  NetworkProxyData data[] = {
272  { "ftp=", 4, PR_FALSE, "network.proxy.ftp",
273  "network.proxy.ftp_port" },
274  { "gopher=", 7, PR_FALSE, "network.proxy.gopher",
275  "network.proxy.gopher_port" },
276  { "http=", 5, PR_FALSE, "network.proxy.http",
277  "network.proxy.http_port" },
278  { "https=", 6, PR_FALSE, "network.proxy.ssl",
279  "network.proxy.ssl_port" },
280  { "socks=", 6, PR_FALSE, "network.proxy.socks",
281  "network.proxy.socks_port" },
282  };
283 
284  PRInt32 startIndex = 0, count = 0;
285  PRBool foundSpecificProxy = PR_FALSE;
286  for (PRUint32 i = 0; i < NS_ARRAY_LENGTH(data); ++i) {
287  PRInt32 offset = buf.Find(NS_ConvertASCIItoUTF16(data[i].prefix));
288  if (offset >= 0) {
289  foundSpecificProxy = PR_TRUE;
290 
291  data[i].proxyConfigured = PR_TRUE;
292 
293  startIndex = offset + data[i].prefixLength;
294 
295  PRInt32 terminal = buf.FindChar(';', offset);
296  count = terminal > startIndex ? terminal - startIndex :
297  buf.Length() - startIndex;
298 
299  // hostPort now contains host:port
300  CNetworkProxyImport::SetProxyPref(Substring(buf, startIndex, count),
301  data[i].hostPref,
302  data[i].portPref, prefs);
303  }
304  }
305 
306  if (!foundSpecificProxy) {
307  // No proxy config for any specific type was found, assume
308  // the ProxyServer value is of the form host:port and that
309  // it applies to all protocols.
310  for (PRUint32 i = 0; i < 5; ++i)
312  data[i].hostPref,
313  data[i].portPref,
314  prefs);
315  prefs->SetBoolPref("network.proxy.share_proxy_settings", PR_TRUE);
316  }
317  }
318  *_retval = PR_TRUE;
319  }
320  return NS_OK;
321 }
322 #endif
323 
return NS_OK
onPageChanged aValue
Definition: FeedWriter.js:1395
inArray array
static void SetProxyPref(const nsAString &aHostPort, const char *aPref, const char *aPortPref, nsIPrefBranch *aPrefs)
const NS_PREFSERVICE_CONTRACTID
static void ParseOverrideServers(const nsAString &aServers, nsIPrefBranch *aBranch)
function right(ele) rect(ele).right
const nsIPrefBranch
NS_IMPL_ISUPPORTS1(CNetworkProxyImport, sbINetworkProxyImport)
Songbird NetworkProxyImport Component Definition.
PRUint32 & offset
var count
Definition: test_bug7406.js:32
nsresult ImportProxySettings_Auto(PRBool *_retval)
nsresult(* ImportProxySettingsFunction)(PRBool *)
static void SetUnicharPref(const char *aPref, const nsAString &aValue, nsIPrefBranch *aPrefs)
var uri
Definition: FeedWriter.js:1135
var prefs
Definition: FeedWriter.js:1169
NS_DECL_ISUPPORTS NS_DECL_SBINETWORKPROXYIMPORT CNetworkProxyImport()
A service to import network proxy settings from several sources.
NetworkProxyImportSource networkProxyImportSources[]
const nsISupportsString
observe data
Definition: FeedWriter.js:1329
const ACCESS_READ
Definition: pageInfo.js:194
_getSelectedPageStyle s i