Flutter Engine
runner.h
Go to the documentation of this file.
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_SHELL_PLATFORM_FUCHSIA_RUNNER_H_
6 #define FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNNER_H_
7 
8 #include <memory>
9 #include <unordered_map>
10 
11 #include <fuchsia/component/runner/cpp/fidl.h>
12 #include <fuchsia/sys/cpp/fidl.h>
13 #include <lib/async-loop/cpp/loop.h>
14 #include <lib/sys/cpp/component_context.h>
15 #include <lib/trace-engine/instrumentation.h>
16 #include <lib/trace/observer.h>
17 
18 #include "component_v1.h"
19 #include "component_v2.h"
20 #include "flutter/fml/macros.h"
21 #include "fml/memory/ref_ptr.h"
22 #include "fml/task_runner.h"
23 #include "lib/fidl/cpp/binding_set.h"
25 
26 namespace flutter_runner {
27 
28 /// Publishes the CF v1 and CF v2 runner services.
29 ///
30 /// Each component will be run on a separate thread dedicated to that component.
31 ///
32 /// TODO(fxb/50694): Add unit tests for CF v2.
33 class Runner final
34  : public fuchsia::sys::Runner /* CF v1 */,
35  public fuchsia::component::runner::ComponentRunner /* CF v2 */ {
36  public:
37  // Does not take ownership of context.
39  sys::ComponentContext* context);
40 
41  ~Runner();
42 
43  private:
44  // CF v1 lifecycle methods.
45  // TODO(fxb/50694) Deprecate these once all Flutter components have been
46  // ported to CF v2.
47 
48  // |fuchsia::sys::Runner|
49  void StartComponent(fuchsia::sys::Package package,
50  fuchsia::sys::StartupInfo startup_info,
51  fidl::InterfaceRequest<fuchsia::sys::ComponentController>
52  controller) override;
53 
54  /// Registers a new CF v1 component with this runner, binding the component
55  /// to this runner.
56  void RegisterComponentV1(
57  fidl::InterfaceRequest<fuchsia::sys::Runner> request);
58 
59  /// Callback that should be fired when a registered CF v2 component is
60  /// terminated.
61  void OnComponentV1Terminate(const ComponentV1* component);
62 
63  // CF v2 lifecycle methods.
64 
65  // |fuchsia::component::runner::ComponentRunner|
66  void Start(
67  fuchsia::component::runner::ComponentStartInfo start_info,
68  fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
69  controller) override;
70 
71  /// Registers a new CF v2 component with this runner, binding the component
72  /// to this runner.
73  void RegisterComponentV2(
74  fidl::InterfaceRequest<fuchsia::component::runner::ComponentRunner>
75  request);
76 
77  /// Callback that should be fired when a registered CF v2 component is
78  /// terminated.
79  void OnComponentV2Terminate(const ComponentV2* component);
80 
81  void SetupICU();
82 
83 #if !defined(DART_PRODUCT)
84  void SetupTraceObserver();
85 #endif // !defined(DART_PRODUCT)
86 
87  // Called from SetupICU, for testing only. Returns false on error.
88  static bool SetupICUInternal();
89  // Called from SetupICU, for testing only. Returns false on error.
90  static bool SetupTZDataInternal();
91 #if defined(FRIEND_TEST)
92  FRIEND_TEST(RunnerTest, TZData);
93  FRIEND_TEST(RunnerTZDataTest, LoadsWithoutTZDataPresent);
94 #endif // defined(FRIEND_TEST)
95 
96  fml::RefPtr<fml::TaskRunner> task_runner_;
97 
98  sys::ComponentContext* context_;
99 
100  // CF v1 component state.
101  fidl::BindingSet<fuchsia::sys::Runner> active_components_v1_bindings_;
102  std::unordered_map<const ComponentV1*, ActiveComponentV1>
103  active_components_v1_;
104 
105  // CF v2 component state.
106 
107  /// The components that are currently bound to this runner.
108  fidl::BindingSet<fuchsia::component::runner::ComponentRunner>
109  active_components_v2_bindings_;
110 
111  /// The components that are currently actively running on threads.
112  std::unordered_map<const ComponentV2*, ActiveComponentV2>
113  active_components_v2_;
114 
115 #if !defined(DART_PRODUCT)
116  // The connection between the Dart VM service and The Hub.
117  std::unique_ptr<dart_utils::VMServiceObject> vmservice_object_;
118 
119  std::unique_ptr<trace::TraceObserver> trace_observer_;
120  trace_prolonged_context_t* prolonged_context_;
121 #endif // !defined(DART_PRODUCT)
122 
123  FML_DISALLOW_COPY_AND_ASSIGN(Runner);
124 };
125 
126 } // namespace flutter_runner
127 
128 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNNER_H_
Runner(fml::RefPtr< fml::TaskRunner > task_runner, sys::ComponentContext *context)
Definition: runner.cc:152