| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef FLUTTER_FML_MESSAGE_LOOP_H_ |
| 6 | #define FLUTTER_FML_MESSAGE_LOOP_H_ |
| 7 | |
| 8 | #include "flutter/fml/macros.h" |
| 9 | #include "flutter/fml/task_runner.h" |
| 10 | |
| 11 | namespace fml { |
| 12 | |
| 13 | class TaskRunner; |
| 14 | class MessageLoopImpl; |
| 15 | |
| 16 | /// An event loop associated with a thread. |
| 17 | /// |
| 18 | /// This class is the generic front-end to the MessageLoop, differences in |
| 19 | /// implementation based on the running platform are in the subclasses of |
| 20 | /// flutter::MessageLoopImpl (ex flutter::MessageLoopAndroid). |
| 21 | /// |
| 22 | /// For scheduling events on the message loop see flutter::TaskRunner. |
| 23 | /// |
| 24 | /// \see fml::TaskRunner |
| 25 | /// \see fml::MessageLoopImpl |
| 26 | /// \see fml::MessageLoopTaskQueues |
| 27 | /// \see fml::Wakeable |
| 28 | class MessageLoop { |
| 29 | public: |
| 30 | FML_EMBEDDER_ONLY |
| 31 | static MessageLoop& GetCurrent(); |
| 32 | |
| 33 | void Run(); |
| 34 | |
| 35 | void Terminate(); |
| 36 | |
| 37 | void AddTaskObserver(intptr_t key, const fml::closure& callback); |
| 38 | |
| 39 | void RemoveTaskObserver(intptr_t key); |
| 40 | |
| 41 | fml::RefPtr<fml::TaskRunner> GetTaskRunner() const; |
| 42 | |
| 43 | // Exposed for the embedder shell which allows clients to poll for events |
| 44 | // instead of dedicating a thread to the message loop. |
| 45 | void RunExpiredTasksNow(); |
| 46 | |
| 47 | static void EnsureInitializedForCurrentThread(); |
| 48 | |
| 49 | /// Returns true if \p EnsureInitializedForCurrentThread has been called on |
| 50 | /// this thread already. |
| 51 | static bool IsInitializedForCurrentThread(); |
| 52 | |
| 53 | ~MessageLoop(); |
| 54 | |
| 55 | /// Gets the unique identifier for the TaskQueue associated with the current |
| 56 | /// thread. |
| 57 | /// \see fml::MessageLoopTaskQueues |
| 58 | static TaskQueueId GetCurrentTaskQueueId(); |
| 59 | |
| 60 | private: |
| 61 | friend class TaskRunner; |
| 62 | friend class MessageLoopImpl; |
| 63 | |
| 64 | fml::RefPtr<MessageLoopImpl> loop_; |
| 65 | fml::RefPtr<fml::TaskRunner> task_runner_; |
| 66 | |
| 67 | MessageLoop(); |
| 68 | |
| 69 | fml::RefPtr<MessageLoopImpl> GetLoopImpl() const; |
| 70 | |
| 71 | FML_DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
| 72 | }; |
| 73 | |
| 74 | } // namespace fml |
| 75 | |
| 76 | #endif // FLUTTER_FML_MESSAGE_LOOP_H_ |
| 77 | |