EmbedTouchManager.cpp 4.14 KB
Newer Older
Tatiana Meshkova's avatar
Tatiana Meshkova committed
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 "EmbedTouchManager"
#include "mozilla/embedlite/EmbedLog.h"

#include "EmbedTouchManager.h"
#include "EmbedTouchListener.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"

EmbedTouchManager::EmbedTouchManager()
  : mWindowCounter(0)
{
}

EmbedTouchManager::~EmbedTouchManager()
{
}

41
NS_IMPL_ISUPPORTS(EmbedTouchManager, nsIObserver, nsISupportsWeakReference)
Tatiana Meshkova's avatar
Tatiana Meshkova committed
42 43 44 45 46 47 48 49 50 51 52 53 54

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

    if (observerService) {
        rv = observerService->AddObserver(this,
                                          "domwindowopened",
                                          true);
        NS_ENSURE_SUCCESS(rv, rv);
55 56 57 58
        rv = observerService->AddObserver(this,
                                          "embedliteviewcreated",
                                          true);
        NS_ENSURE_SUCCESS(rv, rv);
Tatiana Meshkova's avatar
Tatiana Meshkova committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        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
EmbedTouchManager::Observe(nsISupports *aSubject,
                             const char *aTopic,
74
                             const char16_t *aData)
Tatiana Meshkova's avatar
Tatiana Meshkova committed
75 76
{
    nsresult rv;
77
    if (!strcmp(aTopic, "embedliteviewcreated")) {
Tatiana Meshkova's avatar
Tatiana Meshkova committed
78 79 80
        nsCOMPtr<nsIDOMWindow> win = do_QueryInterface(aSubject, &rv);
        NS_ENSURE_SUCCESS(rv, NS_OK);
        WindowCreated(win);
81
    } else if (!strcmp(aTopic, "domwindowclosed")) {
Tatiana Meshkova's avatar
Tatiana Meshkova committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        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
EmbedTouchManager::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<EmbedTouchListener> listener = new EmbedTouchListener(aWin);
    mArray.AppendObject(listener);
    mWindowCounter++;
    if (!mService) {
        mService = do_GetService("@mozilla.org/embedlite-app-service;1");
    }
106 107 108 109
    uint32_t id = 0;
    mService->GetIDByWindow(aWin, &id);
    LOGT("id for window: %u", id);
    mService->AddContentListener(id, listener);
Tatiana Meshkova's avatar
Tatiana Meshkova committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}

void
EmbedTouchManager::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<EmbedTouchListener> listener;
    int i = 0;
    for (i = 0; i < mArray.Count(); ++i) {
        if (mArray[i]->DOMWindow.get() == aWin) {
            listener = mArray[i];
            break;
        }
    }
    mArray.RemoveObjectAt(i);
    mWindowCounter--;
130 131
    uint32_t id = 0;
    mService->GetIDByWindow(aWin, &id);
132
    mService->RemoveContentListener(id, listener);
Tatiana Meshkova's avatar
Tatiana Meshkova committed
133 134 135 136 137
    if (!mWindowCounter) {
        mService = nullptr;
    }
}