Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/windowwatcher/nsAutoWindowStateHelper.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsAutoWindowStateHelper.h"
8
9
#include "mozilla/dom/Event.h"
10
#include "nsIDocument.h"
11
#include "nsIDOMWindow.h"
12
#include "nsPIDOMWindow.h"
13
#include "nsString.h"
14
15
using namespace mozilla;
16
using namespace mozilla::dom;
17
18
/****************************************************************
19
 ****************** nsAutoWindowStateHelper *********************
20
 ****************************************************************/
21
22
nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsPIDOMWindowOuter* aWindow)
23
  : mWindow(aWindow)
24
  , mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog"))
25
0
{
26
0
  if (mWindow) {
27
0
    mWindow->EnterModalState();
28
0
  }
29
0
}
30
31
nsAutoWindowStateHelper::~nsAutoWindowStateHelper()
32
0
{
33
0
  if (mWindow) {
34
0
    mWindow->LeaveModalState();
35
0
  }
36
0
37
0
  if (mDefaultEnabled) {
38
0
    DispatchEventToChrome("DOMModalDialogClosed");
39
0
  }
40
0
}
41
42
bool
43
nsAutoWindowStateHelper::DispatchEventToChrome(const char* aEventName)
44
0
{
45
0
  // XXXbz should we skip dispatching the event if the inner changed?
46
0
  // That is, should we store both the inner and the outer?
47
0
  if (!mWindow) {
48
0
    return true;
49
0
  }
50
0
51
0
  // The functions of nsContentUtils do not provide the required behavior,
52
0
  // so the following is inlined.
53
0
  nsIDocument* doc = mWindow->GetExtantDoc();
54
0
  if (!doc) {
55
0
    return true;
56
0
  }
57
0
58
0
  ErrorResult rv;
59
0
  RefPtr<Event> event = doc->CreateEvent(NS_LITERAL_STRING("Events"),
60
0
                                         CallerType::System, rv);
61
0
  if (rv.Failed()) {
62
0
    rv.SuppressException();
63
0
    return false;
64
0
  }
65
0
  event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true);
66
0
  event->SetTrusted(true);
67
0
  event->WidgetEventPtr()->mFlags.mOnlyChromeDispatch = true;
68
0
69
0
  nsCOMPtr<EventTarget> target = do_QueryInterface(mWindow);
70
0
  bool defaultActionEnabled =
71
0
    target->DispatchEvent(*event, CallerType::System, IgnoreErrors());
72
0
  return defaultActionEnabled;
73
0
}