widget/android/nsAppShell.h
author Mike Hommey <mh+mozilla@glandium.org>
Tue, 07 Jun 2016 13:49:14 +0900
changeset 301919 08093ab05dd463215a7ab14c73fa0905a7ef3ff9
parent 282584 9d7a310cb701aff7072c1ec8aae853fb125ce319
child 305912 56b6362b516e71447964b615b7c1fb8f9889f590
permissions -rw-r--r--
Bug 1278415 - Also write a mozinfo for js standalone builds. r=gps Historically, a mozinfo for js standalone build has not been necessary, but with the move towards a) having things work with mach and b) buildconfig using the MozbuildObject.from_environment in next patch, mozinfo becomes necessary (it's required for MozbuildObject.from_environment to find the directory is an objdir). Interestingly, hazard builds do both a js standalone build and a desktop Firefox build at the same time, both of which are done with MOZCONFIG set in the environment... with the Firefox mozconfig. The result of now writing a mozinfo for js standalone builds is that in that case, they end up with a reference to the mozconfig, which the build system then reads, and finds a MOZ_OBJDIR, which then doesn't match the js standalone build objdir. So for those builds, reset MOZCONFIG.

/* -*- Mode: c++; tab-width: 40; 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/. */

#ifndef nsAppShell_h__
#define nsAppShell_h__

#include "mozilla/HangMonitor.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Monitor.h"
#include "mozilla/Move.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/unused.h"
#include "mozilla/jni/Natives.h"
#include "nsBaseAppShell.h"
#include "nsCOMPtr.h"
#include "nsTArray.h"
#include "nsInterfaceHashtable.h"
#include "nsIAndroidBridge.h"

namespace mozilla {
class AndroidGeckoEvent;
bool ProcessNextEvent();
void NotifyEvent();
}

class nsWindow;

class nsAppShell :
    public nsBaseAppShell
{
public:
    struct Event : mozilla::LinkedListElement<Event>
    {
        typedef mozilla::HangMonitor::ActivityType Type;

        bool HasSameTypeAs(const Event* other) const
        {
            // Compare vtable addresses to determine same type.
            return *reinterpret_cast<const uintptr_t*>(this)
                    == *reinterpret_cast<const uintptr_t*>(other);
        }

        virtual ~Event() {}
        virtual void Run() = 0;

        virtual void PostTo(mozilla::LinkedList<Event>& queue)
        {
            queue.insertBack(this);
        }

        virtual Type ActivityType() const
        {
            return Type::kGeneralActivity;
        }
    };

    class LegacyGeckoEvent;

    template<typename T>
    class LambdaEvent : public Event
    {
    protected:
        T lambda;

    public:
        LambdaEvent(T&& l) : lambda(mozilla::Move(l)) {}
        void Run() override { return lambda(); }
    };

    class ProxyEvent : public Event
    {
    protected:
        mozilla::UniquePtr<Event> baseEvent;

    public:
        ProxyEvent(mozilla::UniquePtr<Event>&& event)
            : baseEvent(mozilla::Move(event))
        {}

        void PostTo(mozilla::LinkedList<Event>& queue) override
        {
            baseEvent->PostTo(queue);
        }

        void Run() override
        {
            baseEvent->Run();
        }
    };

    static nsAppShell* Get()
    {
        MOZ_ASSERT(NS_IsMainThread());
        return sAppShell;
    }

    nsAppShell();

    NS_DECL_ISUPPORTS_INHERITED
    NS_DECL_NSIOBSERVER

    nsresult Init();

    void NotifyNativeEvent();
    bool ProcessNextNativeEvent(bool mayWait) override;

    // Post a subclass of Event.
    // e.g. PostEvent(mozilla::MakeUnique<MyEvent>());
    template<typename T, typename D>
    static void PostEvent(mozilla::UniquePtr<T, D>&& event)
    {
        mozilla::MutexAutoLock lock(*sAppShellLock);
        if (!sAppShell) {
            return;
        }
        sAppShell->mEventQueue.Post(mozilla::Move(event));
    }

    // Post a event that will call a lambda
    // e.g. PostEvent([=] { /* do something */ });
    template<typename T>
    static void PostEvent(T&& lambda)
    {
        mozilla::MutexAutoLock lock(*sAppShellLock);
        if (!sAppShell) {
            return;
        }
        sAppShell->mEventQueue.Post(mozilla::MakeUnique<LambdaEvent<T>>(
                mozilla::Move(lambda)));
    }

    static void PostEvent(mozilla::AndroidGeckoEvent* event);

    // Post a event and wait for it to finish running on the Gecko thread.
    static void SyncRunEvent(Event&& event,
                             mozilla::UniquePtr<Event>(*eventFactory)(
                                    mozilla::UniquePtr<Event>&&) = nullptr);

    void SetBrowserApp(nsIAndroidBrowserApp* aBrowserApp) {
        mBrowserApp = aBrowserApp;
    }

    void GetBrowserApp(nsIAndroidBrowserApp* *aBrowserApp) {
        *aBrowserApp = mBrowserApp;
    }

protected:
    static nsAppShell* sAppShell;
    static mozilla::StaticAutoPtr<mozilla::Mutex> sAppShellLock;

    virtual ~nsAppShell();

    nsresult AddObserver(const nsAString &aObserverKey, nsIObserver *aObserver);

    class NativeCallbackEvent : public Event
    {
        // Capturing the nsAppShell instance is safe because if the app
        // shell is detroyed, this lambda will not be called either.
        nsAppShell* const appShell;

    public:
        NativeCallbackEvent(nsAppShell* as) : appShell(as) {}
        void Run() override { appShell->NativeEventCallback(); }
    };

    void ScheduleNativeEventCallback() override
    {
        mEventQueue.Post(mozilla::MakeUnique<NativeCallbackEvent>(this));
    }

    class Queue
    {
    private:
        mozilla::Monitor mMonitor;
        mozilla::LinkedList<Event> mQueue;

    public:
        Queue() : mMonitor("nsAppShell.Queue")
        {}

        void Signal()
        {
            mozilla::MonitorAutoLock lock(mMonitor);
            lock.NotifyAll();
        }

        void Post(mozilla::UniquePtr<Event>&& event)
        {
            MOZ_ASSERT(event && !event->isInList());

            mozilla::MonitorAutoLock lock(mMonitor);
            event->PostTo(mQueue);
            if (event->isInList()) {
                // Ownership of event object transfers to the queue.
                mozilla::Unused << event.release();
            }
            lock.NotifyAll();
        }

        mozilla::UniquePtr<Event> Pop(bool mayWait)
        {
            mozilla::MonitorAutoLock lock(mMonitor);

            if (mayWait && mQueue.isEmpty()) {
                lock.Wait();
            }
            // Ownership of event object transfers to the return value.
            return mozilla::UniquePtr<Event>(mQueue.popFirst());
        }

    } mEventQueue;

    mozilla::CondVar mSyncRunFinished;
    bool mSyncRunQuit;

    bool mAllowCoalescingTouches;

    nsCOMPtr<nsIAndroidBrowserApp> mBrowserApp;
    nsInterfaceHashtable<nsStringHashKey, nsIObserver> mObserversHash;
};

// Class that implement native JNI methods can inherit from
// UsesGeckoThreadProxy to have the native call forwarded
// automatically to the Gecko thread.
class UsesGeckoThreadProxy : public mozilla::jni::UsesNativeCallProxy
{
public:
    template<class Functor>
    static void OnNativeCall(Functor&& call)
    {
        nsAppShell::PostEvent(mozilla::Move(call));
    }
};

#endif // nsAppShell_h__