EmbedChromeManager.cpp 4.8 KB
Newer Older
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * 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/. */

#define LOG_COMPONENT "EmbedChromeManager"
#include "mozilla/embedlite/EmbedLog.h"

#include "EmbedChromeManager.h"
#include "EmbedChromeListener.h"

#include "nsServiceManagerUtils.h"
#include "nsIObserverService.h"

#include "nsStringGlue.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDOMWindow.h"
#include "nsIDOMEventTarget.h"
#include "nsIDOMEvent.h"
#include "nsPIDOMWindow.h"
#include "nsIEmbedLiteJSON.h"
#include "nsComponentManagerUtils.h"
#include "nsIVariant.h"
#include "nsHashPropertyBag.h"
#include "nsIDOMWindowUtils.h"
#include "nsIDOMHTMLLinkElement.h"
#include "nsIFocusManager.h"
#include "nsIDocShellTreeItem.h"
#include "nsIWebNavigation.h"

EmbedChromeManager::EmbedChromeManager()
  : mWindowCounter(0)
{
}

EmbedChromeManager::~EmbedChromeManager()
{
}

41
NS_IMPL_ISUPPORTS(EmbedChromeManager, nsIObserver, nsISupportsWeakReference)
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

nsresult
EmbedChromeManager::Init()
{
    nsresult rv;
    nsCOMPtr<nsIObserverService> observerService =
        do_GetService(NS_OBSERVERSERVICE_CONTRACTID);

    if (observerService) {
        rv = observerService->AddObserver(this,
                                          "domwindowopened",
                                          true);
        NS_ENSURE_SUCCESS(rv, rv);
        rv = observerService->AddObserver(this,
                                          "domwindowclosed",
                                          true);
        NS_ENSURE_SUCCESS(rv, rv);
        rv = observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
                                          false);
        NS_ENSURE_SUCCESS(rv, rv);
    }

    return rv;
}

NS_IMETHODIMP
EmbedChromeManager::Observe(nsISupports *aSubject,
                             const char *aTopic,
70
                             const char16_t *aData)
71 72 73 74 75 76
{
    nsresult rv;
    if (!strcmp(aTopic, "domwindowopened")) {
        nsCOMPtr<nsIDOMWindow> win = do_QueryInterface(aSubject, &rv);
        NS_ENSURE_SUCCESS(rv, NS_OK);
        WindowCreated(win);
77
    } else if (!strcmp(aTopic, "domwindowclosed")) {
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
        nsCOMPtr<nsIDOMWindow> win = do_QueryInterface(aSubject, &rv);
        NS_ENSURE_SUCCESS(rv, NS_OK);
        WindowDestroyed(win);
    } else {
        LOGT("obj:%p, top:%s", aSubject, aTopic);
    }

    return NS_OK;
}

void
EmbedChromeManager::WindowCreated(nsIDOMWindow* aWin)
{
    LOGT("WindowOpened: %p", aWin);
    nsCOMPtr<nsPIDOMWindow> pidomWindow = do_GetInterface(aWin);
    NS_ENSURE_TRUE(pidomWindow, );
    nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(pidomWindow->GetChromeEventHandler());
    NS_ENSURE_TRUE(target, );
    nsCOMPtr<EmbedChromeListener> listener = new EmbedChromeListener(aWin);
    target->AddEventListener(NS_LITERAL_STRING(MOZ_DOMContentLoaded), listener,  PR_FALSE);
    target->AddEventListener(NS_LITERAL_STRING(MOZ_DOMLinkAdded), listener,  PR_FALSE);
    target->AddEventListener(NS_LITERAL_STRING(MOZ_DOMWillOpenModalDialog), listener,  PR_FALSE);
    target->AddEventListener(NS_LITERAL_STRING(MOZ_DOMModalDialogClosed), listener,  PR_FALSE);
    target->AddEventListener(NS_LITERAL_STRING(MOZ_DOMWindowClose), listener,  PR_FALSE);
    target->AddEventListener(NS_LITERAL_STRING(MOZ_DOMMetaAdded), listener,  PR_FALSE);
    mArray.AppendObject(listener);
    mWindowCounter++;
    if (!mService) {
        mService = do_GetService("@mozilla.org/embedlite-app-service;1");
    }
}

void
EmbedChromeManager::WindowDestroyed(nsIDOMWindow* aWin)
{
    LOGT("WindowClosed: %p", aWin);
    nsCOMPtr<nsPIDOMWindow> pidomWindow = do_GetInterface(aWin);
    NS_ENSURE_TRUE(pidomWindow, );
    nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(pidomWindow->GetChromeEventHandler());
    NS_ENSURE_TRUE(target, );
    nsCOMPtr<EmbedChromeListener> listener;
    int i = 0;
    for (i = 0; i < mArray.Count(); ++i) {
        if (mArray[i]->DOMWindow.get() == aWin) {
            listener = mArray[i];
            break;
        }
    }
    mArray.RemoveObjectAt(i);
    target->RemoveEventListener(NS_LITERAL_STRING(MOZ_DOMContentLoaded), listener,  PR_FALSE);
    target->RemoveEventListener(NS_LITERAL_STRING(MOZ_DOMLinkAdded), listener,  PR_FALSE);
    target->RemoveEventListener(NS_LITERAL_STRING(MOZ_DOMWillOpenModalDialog), listener,  PR_FALSE);
    target->RemoveEventListener(NS_LITERAL_STRING(MOZ_DOMModalDialogClosed), listener,  PR_FALSE);
    target->RemoveEventListener(NS_LITERAL_STRING(MOZ_DOMWindowClose), listener,  PR_FALSE);
    target->RemoveEventListener(NS_LITERAL_STRING(MOZ_DOMMetaAdded), listener,  PR_FALSE);
    mWindowCounter--;
    if (!mWindowCounter) {
        mService = nullptr;
    }
}