diff --git a/dom/base/nsWindowMemoryReporter.cpp b/dom/base/nsWindowMemoryReporter.cpp index bbf68fc1ac8c..364835aa3c09 100644 --- a/dom/base/nsWindowMemoryReporter.cpp +++ b/dom/base/nsWindowMemoryReporter.cpp @@ -116,60 +116,64 @@ nsWindowMemoryReporter* nsWindowMemoryReporter::Get() { return sWindowReporter; } -static nsCString GetWindowURISpec(nsGlobalWindowInner* aWindow) { - NS_ENSURE_TRUE(aWindow, NS_LITERAL_CSTRING("")); +static already_AddRefed GetWindowURI(nsGlobalWindowInner* aWindow) { + NS_ENSURE_TRUE(aWindow, nullptr); nsCOMPtr doc = aWindow->GetExtantDoc(); - if (doc) { - nsCOMPtr uri; - uri = doc->GetDocumentURI(); - return uri->GetSpecOrDefault(); - } - nsCOMPtr scriptObjPrincipal = - do_QueryObject(aWindow); - NS_ENSURE_TRUE(scriptObjPrincipal, NS_LITERAL_CSTRING("")); + nsCOMPtr uri; - // GetPrincipal() will print a warning if the window does not have an outer - // window, so check here for an outer window first. This code is - // functionally correct if we leave out the GetOuterWindow() check, but we - // end up printing a lot of warnings during debug mochitests. - if (!aWindow->GetOuterWindow()) { - return NS_LITERAL_CSTRING(""); + if (doc) { + uri = doc->GetDocumentURI(); } - nsIPrincipal* principal = scriptObjPrincipal->GetPrincipal(); - if (!principal) { - return NS_LITERAL_CSTRING(""); + + if (!uri) { + nsCOMPtr scriptObjPrincipal = + do_QueryObject(aWindow); + NS_ENSURE_TRUE(scriptObjPrincipal, nullptr); + + // GetPrincipal() will print a warning if the window does not have an outer + // window, so check here for an outer window first. This code is + // functionally correct if we leave out the GetOuterWindow() check, but we + // end up printing a lot of warnings during debug mochitests. + if (aWindow->GetOuterWindow()) { + nsIPrincipal* principal = scriptObjPrincipal->GetPrincipal(); + if (principal) { + principal->GetURI(getter_AddRefs(uri)); + } + } } - nsCString spec; - principal->GetAsciiSpec(spec); - return spec; + + return uri.forget(); } // Forward to the inner window if we need to when getting the window's URI. -static nsCString GetWindowURISpec(nsGlobalWindowOuter* aWindow) { - NS_ENSURE_TRUE(aWindow, NS_LITERAL_CSTRING("")); - return GetWindowURISpec(aWindow->GetCurrentInnerWindowInternal()); +static already_AddRefed GetWindowURI(nsGlobalWindowOuter* aWindow) { + NS_ENSURE_TRUE(aWindow, nullptr); + return GetWindowURI(aWindow->GetCurrentInnerWindowInternal()); } static void AppendWindowURI(nsGlobalWindowInner* aWindow, nsACString& aStr, bool aAnonymize) { - nsCString spec = GetWindowURISpec(aWindow); + nsCOMPtr uri = GetWindowURI(aWindow); - if (spec.IsEmpty()) { + if (uri) { + if (aAnonymize && !aWindow->IsChromeWindow()) { + aStr.AppendPrintf("", aWindow->WindowID()); + } else { + nsCString spec = uri->GetSpecOrDefault(); + + // A hack: replace forward slashes with '\\' so they aren't + // treated as path separators. Users of the reporters + // (such as about:memory) have to undo this change. + spec.ReplaceChar('/', '\\'); + + aStr += spec; + } + } else { // If we're unable to find a URI, we're dealing with a chrome window with // no document in it (or somesuch), so we call this a "system window". aStr += NS_LITERAL_CSTRING("[system]"); - return; } - if (aAnonymize && !aWindow->IsChromeWindow()) { - aStr.AppendPrintf("", aWindow->WindowID()); - return; - } - // A hack: replace forward slashes with '\\' so they aren't - // treated as path separators. Users of the reporters - // (such as about:memory) have to undo this change. - spec.ReplaceChar('/', '\\'); - aStr += spec; } MOZ_DEFINE_MALLOC_SIZE_OF(WindowsMallocSizeOf) @@ -223,11 +227,18 @@ static void CollectWindowReports(nsGlobalWindowInner* aWindow, // Avoid calling aWindow->GetInProcessTop() if there's no outer window. It // will work just fine, but will spew a lot of warnings. nsGlobalWindowOuter* top = nullptr; + nsCOMPtr location; if (aWindow->GetOuterWindow()) { // Our window should have a null top iff it has a null docshell. MOZ_ASSERT(!!aWindow->GetInProcessTopInternal() == !!aWindow->GetDocShell()); top = aWindow->GetInProcessTopInternal(); + if (top) { + location = GetWindowURI(top); + } + } + if (!location) { + location = GetWindowURI(aWindow); } windowPath += NS_LITERAL_CSTRING("window-objects/");