DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (409f3966645a)

VCS Links

Line Code
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 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/* -*- 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 <time.h>

#include "mozilla/BackgroundHangMonitor.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Monitor.h"
#include "mozilla/Move.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/TypeTraits.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 {
bool ProcessNextEvent();
void NotifyEvent();
}

class nsWindow;

class nsAppShell :
    public nsBaseAppShell
{
public:
    struct Event : mozilla::LinkedListElement<Event>
    {
        static uint64_t GetTime()
        {
            timespec time;
            if (clock_gettime(CLOCK_MONOTONIC, &time)) {
                return 0ull;
            }
            return uint64_t(time.tv_sec) * 1000000000ull + time.tv_nsec;
        }

        uint64_t mPostTime{ 0 };

        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 bool IsUIEvent() const
        {
            return false;
        }
    };

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

    public:
        explicit LambdaEvent(T&& l) : lambda(std::move(l)) {}
        void Run() override { return lambda(); }
    };

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

    public:
        explicit ProxyEvent(mozilla::UniquePtr<Event>&& event)
            : baseEvent(std::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(std::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>>(
                std::move(lambda)));
    }

    // 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);

    template<typename T> static
    typename mozilla::EnableIf<!mozilla::IsBaseOf<Event, T>::value, void>::Type
    SyncRunEvent(T&& lambda)
    {
        SyncRunEvent(LambdaEvent<T>(std::forward<T>(lambda)));
    }

    static already_AddRefed<nsIURI> ResolveURI(const nsCString& aUriStr);

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

    nsIAndroidBrowserApp* GetBrowserApp() {
        return mBrowserApp;
    }

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

    static void RecordLatencies();

    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:
        explicit 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:
        enum {
            LATENCY_UI,
            LATENCY_OTHER,
            LATENCY_COUNT
        };
        static uint32_t sLatencyCount[LATENCY_COUNT];
        static uint64_t sLatencyTime[LATENCY_COUNT];

        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()) {
                event->mPostTime = Event::GetTime();
                // 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()) {
#ifdef EARLY_BETA_OR_EARLIER
                // Record latencies when we're about to be idle.
                nsAppShell::RecordLatencies();
#endif
                lock.Wait();
            }

            // Ownership of event object transfers to the return value.
            mozilla::UniquePtr<Event> event(mQueue.popFirst());
            if (!event || !event->mPostTime) {
                return event;
            }

#ifdef EARLY_BETA_OR_EARLIER
            const size_t latencyType = event->IsUIEvent() ? LATENCY_UI : LATENCY_OTHER;
            const uint64_t latency = Event::GetTime() - event->mPostTime;

            sLatencyCount[latencyType]++;
            sLatencyTime[latencyType] += latency;
#endif
            return event;
        }

    } mEventQueue;

private:

    mozilla::CondVar mSyncRunFinished;
    bool mSyncRunQuit;

    bool mAllowCoalescingTouches;

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

#endif // nsAppShell_h__