summaryrefslogtreecommitdiffstats
path: root/toolkit/components/sessionstore/RestoreTabContentObserver.cpp
blob: b0dda66ff03b549fe50a057ada89731e87888c5d (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/WindowGlobalChild.h"
#include "mozilla/dom/WindowGlobalParent.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsPIDOMWindow.h"
#include "nsReadableUtils.h"
#include "nsThreadUtils.h"
#include "RestoreTabContentObserver.h"

using namespace mozilla::dom;

NS_IMPL_ISUPPORTS(RestoreTabContentObserver, nsIObserver)

const char* const kAboutReaderTopic = "AboutReader:Ready";
const char* const kContentDocumentLoaded = "content-document-loaded";
const char* const kChromeDocumentLoaded = "chrome-document-loaded";

/* static */
void RestoreTabContentObserver::Initialize() {
  MOZ_ASSERT(!gRestoreTabContentObserver);
  MOZ_ASSERT(NS_IsMainThread());

  RefPtr<RestoreTabContentObserver> observer = new RestoreTabContentObserver();

  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
  if (NS_WARN_IF(!obs)) {
    return;
  }

  obs->AddObserver(observer, kAboutReaderTopic, false);
  obs->AddObserver(observer, kContentDocumentLoaded, false);
  obs->AddObserver(observer, kChromeDocumentLoaded, false);

  gRestoreTabContentObserver = observer;
}

/* static */
void RestoreTabContentObserver::Shutdown() {
  MOZ_ASSERT(NS_IsMainThread());

  if (!gRestoreTabContentObserver) {
    return;
  }

  RefPtr<RestoreTabContentObserver> observer = gRestoreTabContentObserver;
  gRestoreTabContentObserver = nullptr;

  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
  if (!obs) {
    return;
  }

  obs->RemoveObserver(observer, kAboutReaderTopic);
  obs->RemoveObserver(observer, kContentDocumentLoaded);
  obs->RemoveObserver(observer, kChromeDocumentLoaded);
}

NS_IMETHODIMP
RestoreTabContentObserver::Observe(nsISupports* aSubject, const char* aTopic,
                                   const char16_t* aData) {
  nsCOMPtr<nsPIDOMWindowInner> inner;
  if (!strcmp(aTopic, kAboutReaderTopic)) {
    inner = do_QueryInterface(aSubject);
  } else if (!strcmp(aTopic, kContentDocumentLoaded) ||
             !strcmp(aTopic, kChromeDocumentLoaded)) {
    nsCOMPtr<Document> doc = do_QueryInterface(aSubject);
    inner = doc ? doc->GetInnerWindow() : nullptr;
  }
  if (!inner) {
    return NS_OK;
  }

  nsCOMPtr<nsIURI> uri = inner->GetDocumentURI();
  if (!uri) {
    return NS_OK;
  }

  // We'll handle loading about:reader with "AboutReader:Ready"
  // rather than "content-document-loaded".
  if (uri->SchemeIs("about") &&
      StringBeginsWith(uri->GetSpecOrDefault(), "about:reader"_ns) &&
      strcmp(aTopic, kAboutReaderTopic) != 0) {
    return NS_OK;
  }

  RefPtr<BrowsingContext> bc = inner->GetBrowsingContext();
  if (!bc || !bc->Top()->GetHasRestoreData()) {
    return NS_OK;
  }
  if (XRE_IsParentProcess()) {
    if (WindowGlobalParent* wgp = bc->Canonical()->GetCurrentWindowGlobal()) {
      bc->Canonical()->Top()->RequestRestoreTabContent(wgp);
    }
  } else if (WindowContext* windowContext = bc->GetCurrentWindowContext()) {
    if (WindowGlobalChild* wgc = windowContext->GetWindowGlobalChild()) {
      wgc->SendRequestRestoreTabContent();
    }
  }
  return NS_OK;
}

mozilla::StaticRefPtr<RestoreTabContentObserver>
    RestoreTabContentObserver::gRestoreTabContentObserver;