Bumping gaia.json for 4 gaia revision(s) a=gaia-bump
[gecko.git] / widget / windows / LSPAnnotator.cpp
blob98ac8b88f6b18fd2c607c653ab384bc34e105a10
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /**
6 * LSPs are evil little bits of code that are allowed to inject into our
7 * networking stack by Windows. Once they have wormed into our process
8 * they gnaw at our innards until we crash. Here we force the buggers
9 * into the light by recording them in our crash information.
10 * We do the enumeration on a thread because I'm concerned about startup perf
11 * on machines with several LSPs.
14 #include "nsICrashReporter.h"
15 #include "nsISupportsImpl.h"
16 #include "nsServiceManagerUtils.h"
17 #include "nsThreadUtils.h"
18 #include <windows.h>
19 #include <ws2spi.h>
21 namespace mozilla {
22 namespace crashreporter {
24 class LSPAnnotationGatherer : public nsRunnable
26 ~LSPAnnotationGatherer() {}
28 public:
29 NS_DECL_THREADSAFE_ISUPPORTS
30 NS_DECL_NSIRUNNABLE
32 void Annotate();
33 nsCString mString;
34 nsCOMPtr<nsIThread> mThread;
37 NS_IMPL_ISUPPORTS(LSPAnnotationGatherer, nsIRunnable)
39 void
40 LSPAnnotationGatherer::Annotate()
42 nsCOMPtr<nsICrashReporter> cr =
43 do_GetService("@mozilla.org/toolkit/crash-reporter;1");
44 bool enabled;
45 if (cr && NS_SUCCEEDED(cr->GetEnabled(&enabled)) && enabled) {
46 cr->AnnotateCrashReport(NS_LITERAL_CSTRING("Winsock_LSP"), mString);
48 mThread->Shutdown();
51 NS_IMETHODIMP
52 LSPAnnotationGatherer::Run()
54 PR_SetCurrentThreadName("LSP Annotator");
56 mThread = NS_GetCurrentThread();
58 DWORD size = 0;
59 int err;
60 // Get the size of the buffer we need
61 if (SOCKET_ERROR != WSCEnumProtocols(nullptr, nullptr, &size, &err) ||
62 err != WSAENOBUFS) {
63 // Er, what?
64 NS_NOTREACHED("WSCEnumProtocols suceeded when it should have failed ...");
65 return NS_ERROR_FAILURE;
68 nsAutoArrayPtr<char> byteArray(new char[size]);
69 WSAPROTOCOL_INFOW* providers =
70 reinterpret_cast<WSAPROTOCOL_INFOW*>(byteArray.get());
72 int n = WSCEnumProtocols(nullptr, providers, &size, &err);
73 if (n == SOCKET_ERROR) {
74 // Lame. We provided the right size buffer; we'll just give up now.
75 NS_WARNING("Could not get LSP list");
76 return NS_ERROR_FAILURE;
79 nsCString str;
80 for (int i = 0; i < n; i++) {
81 AppendUTF16toUTF8(nsDependentString(providers[i].szProtocol), str);
82 str.AppendLiteral(" : ");
83 str.AppendInt(providers[i].iVersion);
84 str.AppendLiteral(" : ");
85 str.AppendInt(providers[i].iSocketType);
86 str.AppendLiteral(" : ");
88 wchar_t path[MAX_PATH];
89 int dummy;
90 if (!WSCGetProviderPath(&providers[i].ProviderId, path,
91 &dummy, &err)) {
92 AppendUTF16toUTF8(nsDependentString(path), str);
95 if (i + 1 != n) {
96 str.AppendLiteral(" \n ");
100 mString = str;
101 NS_DispatchToMainThread(NS_NewRunnableMethod(this, &LSPAnnotationGatherer::Annotate));
102 return NS_OK;
105 void LSPAnnotate()
107 nsCOMPtr<nsIThread> thread;
108 nsCOMPtr<nsIRunnable> runnable =
109 do_QueryObject(new LSPAnnotationGatherer());
110 NS_NewThread(getter_AddRefs(thread), runnable);
113 } // namespace crashreporter
114 } // namespace mozilla