summaryrefslogtreecommitdiffstats
path: root/mozilla-682832-proxy.patch
blob: 076b2a68d0875f303525b2b928e097c30757e624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
diff -up mozilla-release/toolkit/system/gnome/nsGSettingsService.cpp.682832 mozilla-release/toolkit/system/gnome/nsGSettingsService.cpp
--- mozilla-release/toolkit/system/gnome/nsGSettingsService.cpp.682832	2011-11-04 22:34:19.000000000 +0100
+++ mozilla-release/toolkit/system/gnome/nsGSettingsService.cpp	2011-12-07 11:36:29.302312405 +0100
@@ -42,6 +42,8 @@
 #include "nsMemory.h"
 #include "prlink.h"
 #include "nsComponentManagerUtils.h"
+#include "nsIMutableArray.h"
+#include "nsISupportsPrimitives.h"
 
 #include <glib.h>
 #include <glib-object.h>
@@ -56,6 +58,7 @@ typedef struct _GVariant GVariant;
 # define G_VARIANT_TYPE_STRING       ((const GVariantType *) "s")
 # define G_VARIANT_TYPE_OBJECT_PATH  ((const GVariantType *) "o")
 # define G_VARIANT_TYPE_SIGNATURE    ((const GVariantType *) "g")
+# define G_VARIANT_TYPE_STRING_ARRAY ((const GVariantType *) "as")
 #endif
 
 #define GSETTINGS_FUNCTIONS \
@@ -68,6 +71,7 @@ typedef struct _GVariant GVariant;
   FUNC(g_variant_get_int32, gint32, (GVariant* variant)) \
   FUNC(g_variant_get_boolean, gboolean, (GVariant* variant)) \
   FUNC(g_variant_get_string, const char *, (GVariant* value, gsize* length)) \
+  FUNC(g_variant_get_strv, const char **, (GVariant* value, gsize* length)) \
   FUNC(g_variant_is_of_type, gboolean, (GVariant* value, const GVariantType* type)) \
   FUNC(g_variant_new_int32, GVariant *, (gint32 value)) \
   FUNC(g_variant_new_boolean, GVariant *, (gboolean value)) \
@@ -91,6 +95,7 @@ GSETTINGS_FUNCTIONS
 #define g_variant_get_int32 _g_variant_get_int32
 #define g_variant_get_boolean _g_variant_get_boolean
 #define g_variant_get_string _g_variant_get_string
+#define g_variant_get_strv _g_variant_get_strv
 #define g_variant_is_of_type _g_variant_is_of_type
 #define g_variant_new_int32 _g_variant_new_int32
 #define g_variant_new_boolean _g_variant_new_boolean
@@ -263,6 +268,49 @@ nsGSettingsCollection::GetInt(const nsAC
   return NS_OK;
 }
 
+NS_IMETHODIMP
+nsGSettingsCollection::GetStringList(const nsACString& aKey, nsIArray** aResult)
+{
+  if (!KeyExists(aKey))
+    return NS_ERROR_INVALID_ARG;
+
+  nsCOMPtr<nsIMutableArray> items(do_CreateInstance(NS_ARRAY_CONTRACTID));
+  if (!items) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  GVariant *value = g_settings_get_value(mSettings,
+                                         PromiseFlatCString(aKey).get());
+
+  if (!g_variant_is_of_type(value, G_VARIANT_TYPE_STRING_ARRAY)) {
+    g_variant_unref(value);
+    return NS_ERROR_FAILURE;
+  }
+
+  const gchar ** gs_strings = g_variant_get_strv(value, NULL);
+  if (!gs_strings) {
+    // empty array
+    NS_ADDREF(*aResult = items);
+    g_variant_unref(value);
+    return NS_OK;
+  }
+
+  const gchar** p_gs_strings = gs_strings;
+  while (*p_gs_strings != NULL)
+  {
+    nsCOMPtr<nsISupportsCString> obj(do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID));
+    if (obj) {
+      obj->SetData(nsDependentCString(*p_gs_strings));
+      items->AppendElement(obj, false);
+    }
+    p_gs_strings++;
+  }
+  g_free(gs_strings);
+  NS_ADDREF(*aResult = items);
+  g_variant_unref(value);
+  return NS_OK;
+}
+
 nsresult
 nsGSettingsService::Init()
 {
diff -up mozilla-release/toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp.682832 mozilla-release/toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp
--- mozilla-release/toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp.682832	2011-11-04 22:34:19.000000000 +0100
+++ mozilla-release/toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp	2011-12-07 11:40:48.432502399 +0100
@@ -49,6 +49,7 @@
 #include "nsPrintfCString.h"
 #include "nsNetUtil.h"
 #include "nsISupportsPrimitives.h"
+#include "nsIGSettingsService.h"
 
 class nsUnixSystemProxySettings : public nsISystemProxySettings {
 public:
@@ -62,9 +63,12 @@ private:
   ~nsUnixSystemProxySettings() {}
   
   nsCOMPtr<nsIGConfService> mGConf;
+  nsCOMPtr<nsIGSettingsService> mGSettings;
   PRBool IsProxyMode(const char* aMode);
   nsresult SetProxyResultFromGConf(const char* aKeyBase, const char* aType, nsACString& aResult);
   nsresult GetProxyFromGConf(const nsACString& aScheme, const nsACString& aHost, PRInt32 aPort, nsACString& aResult);
+  nsresult GetProxyFromGSettings(const nsACString& aScheme, const nsACString& aHost, PRInt32 aPort, nsACString& aResult);
+  nsresult SetProxyResultFromGSettings(const char* aKeyBase, const char* aType, nsACString& aResult);
 };
 
 NS_IMPL_ISUPPORTS1(nsUnixSystemProxySettings, nsISystemProxySettings)
@@ -73,6 +77,7 @@ nsresult
 nsUnixSystemProxySettings::Init()
 {
   mGConf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
+  mGSettings = do_GetService(NS_GSETTINGSSERVICE_CONTRACTID);
   return NS_OK;
 }
 
@@ -87,14 +92,30 @@ nsUnixSystemProxySettings::IsProxyMode(c
 nsresult
 nsUnixSystemProxySettings::GetPACURI(nsACString& aResult)
 {
-  if (!mGConf || !IsProxyMode("auto")) {
-    // Return an empty string in this case
-    aResult.Truncate();
-    return NS_OK;
+  if (mGSettings) {
+    nsCOMPtr<nsIGSettingsCollection> proxy_settings;
+    mGSettings->GetCollectionForSchema(NS_LITERAL_CSTRING("org.gnome.system.proxy"), 
+                                       getter_AddRefs(proxy_settings));
+    if (proxy_settings) {
+      nsCString proxyMode;
+      // Check if mode is auto
+      nsresult rv = proxy_settings->GetString(NS_LITERAL_CSTRING("mode"), proxyMode);
+      if (rv == NS_OK && proxyMode.Equals("auto")) {
+        return proxy_settings->GetString(NS_LITERAL_CSTRING("autoconfig-url"), aResult);
+      }
+      /* The org.gnome.system.proxy schema has been found, but auto mode is not set.
+       * Don't try the GConf and return empty string. */
+      aResult.Truncate();
+      return NS_OK;
+    }
   }
-
-  return mGConf->GetString(NS_LITERAL_CSTRING("/system/proxy/autoconfig_url"),
-                           aResult);
+  if (mGConf && IsProxyMode("auto")) {
+    return mGConf->GetString(NS_LITERAL_CSTRING("/system/proxy/autoconfig_url"),
+                             aResult);
+  }
+  // Return an empty string when auto mode is not set.
+  aResult.Truncate();
+  return NS_OK;
 }
 
 static PRBool
@@ -231,7 +252,38 @@ nsUnixSystemProxySettings::SetProxyResul
   PRInt32 port;
   rv = mGConf->GetInt(portKey, &port);
   NS_ENSURE_SUCCESS(rv, rv);
+
+  /* When port is 0, proxy is not considered as enabled even if host is set. */
+  if (port == 0)
+    return NS_ERROR_FAILURE;
+
+  SetProxyResult(aType, host, port, aResult);
+  return NS_OK;
+}
+
+nsresult
+nsUnixSystemProxySettings::SetProxyResultFromGSettings(const char* aKeyBase, const char* aType,
+                                                       nsACString& aResult)
+{
+  nsCOMPtr<nsIGSettingsCollection> proxy_settings;
+  nsresult rv = mGSettings->GetCollectionForSchema(nsDependentCString(aKeyBase),
+                                                   getter_AddRefs(proxy_settings));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCAutoString host;
+  rv = proxy_settings->GetString(NS_LITERAL_CSTRING("host"), host);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (host.IsEmpty())
+    return NS_ERROR_FAILURE;
+  
+  PRInt32 port;
+  rv = proxy_settings->GetInt(NS_LITERAL_CSTRING("port"), &port);
+  NS_ENSURE_SUCCESS(rv, rv);
     
+  /* When port is 0, proxy is not considered as enabled even if host is set. */
+  if (port == 0)
+    return NS_ERROR_FAILURE;
+
   SetProxyResult(aType, host, port, aResult);
   return NS_OK;
 }
@@ -271,17 +323,17 @@ static PRBool ConvertToIPV6Addr(const ns
                                 PRIPv6Addr* aAddr)
 {
   PRNetAddr addr;
+  // try to convert hostname to IP
   if (PR_StringToNetAddr(PromiseFlatCString(aName).get(), &addr) != PR_SUCCESS)
     return PR_FALSE;
 
-  PRIPv6Addr ipv6;
   // convert parsed address to IPv6
   if (addr.raw.family == PR_AF_INET) {
     // convert to IPv4-mapped address
-    PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &ipv6);
+    PR_ConvertIPv4AddrToIPv6(addr.inet.ip, aAddr);
   } else if (addr.raw.family == PR_AF_INET6) {
     // copy the address
-    memcpy(&ipv6, &addr.ipv6.ip, sizeof(PRIPv6Addr));
+    memcpy(aAddr, &addr.ipv6.ip, sizeof(PRIPv6Addr));
   } else {
     return PR_FALSE;
   }
@@ -289,8 +341,8 @@ static PRBool ConvertToIPV6Addr(const ns
   return PR_TRUE;
 }
 
-static PRBool GConfIgnoreHost(const nsACString& aIgnore,
-                              const nsACString& aHost)
+static bool HostIgnoredByProxy(const nsACString& aIgnore,
+                               const nsACString& aHost)
 {
   if (aIgnore.Equals(aHost, nsCaseInsensitiveCStringComparator()))
     return PR_TRUE;
@@ -321,8 +373,9 @@ static PRBool GConfIgnoreHost(const nsAC
     slash = end;
   }
 
+  nsDependentCSubstring ignoreStripped(start, slash);
   PRIPv6Addr ignoreAddr, hostAddr;
-  if (!ConvertToIPV6Addr(aIgnore, &ignoreAddr) ||
+  if (!ConvertToIPV6Addr(ignoreStripped, &ignoreAddr) ||
       !ConvertToIPV6Addr(aHost, &hostAddr))
     return PR_FALSE;
 
@@ -355,7 +408,7 @@ nsUnixSystemProxySettings::GetProxyFromG
       if (str) {
         nsAutoString s;
         if (NS_SUCCEEDED(str->GetData(s)) && !s.IsEmpty()) {
-          if (GConfIgnoreHost(NS_ConvertUTF16toUTF8(s), aHost)) {
+          if (HostIgnoredByProxy(NS_ConvertUTF16toUTF8(s), aHost)) {
             aResult.AppendLiteral("DIRECT");
             return NS_OK;
           }
@@ -392,6 +445,71 @@ nsUnixSystemProxySettings::GetProxyFromG
 }
 
 nsresult
+nsUnixSystemProxySettings::GetProxyFromGSettings(const nsACString& aScheme,
+                                                 const nsACString& aHost,
+                                                 PRInt32 aPort,
+                                                 nsACString& aResult)
+{
+  nsCOMPtr<nsIGSettingsCollection> proxy_settings;
+  nsresult rv;
+
+  rv = mGSettings->GetCollectionForSchema(NS_LITERAL_CSTRING("org.gnome.system.proxy"),
+                                          getter_AddRefs(proxy_settings));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCString proxyMode; 
+  rv = proxy_settings->GetString(NS_LITERAL_CSTRING("mode"), proxyMode);
+  NS_ENSURE_SUCCESS(rv, rv);
+  
+  if (!proxyMode.Equals("manual")) {
+    aResult.AppendLiteral("DIRECT");
+    return NS_OK;
+  }
+
+  nsCOMPtr<nsIArray> ignoreList;
+  if (NS_SUCCEEDED(proxy_settings->GetStringList(NS_LITERAL_CSTRING("ignore-hosts"),
+                                                 getter_AddRefs(ignoreList))) && ignoreList) {
+    PRUint32 len = 0;
+    ignoreList->GetLength(&len);
+    for (PRUint32 i = 0; i < len; ++i) {
+      nsCOMPtr<nsISupportsCString> str = do_QueryElementAt(ignoreList, i);
+      if (str) {
+        nsCString s;
+        if (NS_SUCCEEDED(str->GetData(s)) && !s.IsEmpty()) {
+          if (HostIgnoredByProxy(s, aHost)) {
+            aResult.AppendLiteral("DIRECT");
+            return NS_OK;
+          }
+        }
+      }
+    }
+  }
+
+  if (aScheme.LowerCaseEqualsLiteral("http")) {
+    rv = SetProxyResultFromGSettings("org.gnome.system.proxy.http", "PROXY", aResult);
+  } else if (aScheme.LowerCaseEqualsLiteral("https")) {
+    rv = SetProxyResultFromGSettings("org.gnome.system.proxy.https", "PROXY", aResult);
+    /* Try to use HTTP proxy when HTTPS proxy is not explicitly defined */
+    if (rv != NS_OK) 
+      rv = SetProxyResultFromGSettings("org.gnome.system.proxy.http", "PROXY", aResult);
+  } else if (aScheme.LowerCaseEqualsLiteral("ftp")) {
+    rv = SetProxyResultFromGSettings("org.gnome.system.proxy.ftp", "PROXY", aResult);
+  } else {
+    rv = NS_ERROR_FAILURE;
+  }
+  if (rv != NS_OK) {
+     /* If proxy for scheme is not specified, use SOCKS proxy for all schemes */
+     rv = SetProxyResultFromGSettings("org.gnome.system.proxy.socks", "SOCKS", aResult);
+  }
+  
+  if (NS_FAILED(rv)) {
+    aResult.AppendLiteral("DIRECT");
+  }
+  
+  return NS_OK;
+}
+
+nsresult
 nsUnixSystemProxySettings::GetProxyForURI(nsIURI* aURI, nsACString& aResult)
 {
   nsCAutoString scheme;
@@ -406,10 +524,15 @@ nsUnixSystemProxySettings::GetProxyForUR
   rv = aURI->GetPort(&port);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!mGConf)
-    return GetProxyFromEnvironment(scheme, host, port, aResult);
+  if (mGSettings) {
+    rv = GetProxyFromGSettings(scheme, host, port, aResult);
+    if (rv == NS_OK)
+      return rv;
+  }
+  if (mGConf)
+    return GetProxyFromGConf(scheme, host, port, aResult);
 
-  return GetProxyFromGConf(scheme, host, port, aResult);
+  return GetProxyFromEnvironment(scheme, host, port, aResult);
 }
 
 #define NS_UNIXSYSTEMPROXYSERVICE_CID  /* 0fa3158c-d5a7-43de-9181-a285e74cf1d4 */\
diff -up mozilla-release/xpcom/system/nsIGSettingsService.idl.682832 mozilla-release/xpcom/system/nsIGSettingsService.idl
--- mozilla-release/xpcom/system/nsIGSettingsService.idl.682832	2011-11-04 22:34:23.000000000 +0100
+++ mozilla-release/xpcom/system/nsIGSettingsService.idl	2011-12-07 11:32:37.976256951 +0100
@@ -39,7 +39,7 @@
 #include "nsISupports.idl"
 #include "nsIArray.idl"
 
-[scriptable, uuid(09637d3c-3c07-40b4-aff9-1d2a0f046f3c)]
+[scriptable, uuid(16d5b0ed-e756-4f1b-a8ce-9132e869acd8)]
 interface nsIGSettingsCollection : nsISupports
 {
   void          setString(in AUTF8String key, in AUTF8String value);
@@ -48,6 +48,7 @@ interface nsIGSettingsCollection : nsISu
   AUTF8String   getString(in AUTF8String key);
   boolean       getBoolean(in AUTF8String key);
   long          getInt(in AUTF8String key);
+  nsIArray      getStringList(in AUTF8String key);
 };
 
 [scriptable, uuid(849c088b-57d1-4f24-b7b2-3dc4acb04c0a)]