runtime_holder.h 4.6 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_CONTENT_HANDLER_RUNTIME_HOLDER_H_
#define FLUTTER_CONTENT_HANDLER_RUNTIME_HOLDER_H_

8 9
#include <mx/channel.h>

10 11
#include <unordered_set>

12
#include "application/lib/app/application_context.h"
13 14
#include "application/services/application_environment.fidl.h"
#include "application/services/service_provider.fidl.h"
15
#include "apps/mozart/lib/flutter/sdk_ext/src/natives.h"
A
Adam Barth 已提交
16
#include "apps/mozart/services/input/input_connection.fidl.h"
17
#include "apps/mozart/services/input/text_input.fidl.h"
A
Adam Barth 已提交
18
#include "apps/mozart/services/views/view_manager.fidl.h"
19 20
#include "flutter/assets/unzipper_provider.h"
#include "flutter/assets/zip_asset_store.h"
21
#include "flutter/flow/layers/layer_tree.h"
22
#include "flutter/lib/ui/window/viewport_metrics.h"
23 24
#include "flutter/runtime/runtime_controller.h"
#include "flutter/runtime/runtime_delegate.h"
25
#include "lib/fidl/cpp/bindings/binding.h"
26 27 28
#include "lib/ftl/functional/closure.h"
#include "lib/ftl/macros.h"
#include "lib/ftl/memory/weak_ptr.h"
29

A
Adam Barth 已提交
30
namespace flutter_runner {
31 32
class Rasterizer;

33
class RuntimeHolder : public blink::RuntimeDelegate,
34
                      public mozart::NativesDelegate,
35
                      public mozart::ViewListener,
36 37
                      public mozart::InputListener,
                      public mozart::InputMethodEditorClient {
38 39 40 41
 public:
  RuntimeHolder();
  ~RuntimeHolder();

42
  void Init(std::unique_ptr<app::ApplicationContext> context,
43
            fidl::InterfaceRequest<app::ServiceProvider> outgoing_services,
A
Adam Barth 已提交
44
            std::vector<char> bundle);
J
Jeff Brown 已提交
45
  void CreateView(const std::string& script_uri,
A
Adam Barth 已提交
46
                  fidl::InterfaceRequest<mozart::ViewOwner> view_owner_request,
47
                  fidl::InterfaceRequest<app::ServiceProvider> services);
48

49 50 51
  Dart_Port GetUIIsolateMainPort();
  std::string GetUIIsolateName();

52 53
 private:
  // |blink::RuntimeDelegate| implementation:
54
  std::string DefaultRouteName() override;
55 56
  void ScheduleFrame() override;
  void Render(std::unique_ptr<flow::LayerTree> layer_tree) override;
57
  void UpdateSemantics(std::vector<blink::SemanticsNode> update) override;
58 59
  void HandlePlatformMessage(
      ftl::RefPtr<blink::PlatformMessage> message) override;
60
  void DidCreateMainIsolate(Dart_Isolate isolate) override;
61

62 63 64
  // |mozart::NativesDelegate| implementation:
  mozart::View* GetMozartView() override;

65
  // |mozart::InputListener| implementation:
66
  void OnEvent(mozart::InputEventPtr event,
67 68
               const OnEventCallback& callback) override;

J
Jeff Brown 已提交
69
  // |mozart::ViewListener| implementation:
70 71 72
  void OnPropertiesChanged(
      mozart::ViewPropertiesPtr properties,
      const OnPropertiesChangedCallback& callback) override;
J
Jeff Brown 已提交
73

74 75 76
  // |mozart::InputMethodEditorClient| implementation:
  void DidUpdateState(mozart::TextInputStatePtr state,
                      mozart::InputEventPtr event) override;
77
  void OnAction(mozart::InputMethodAction action) override;
78

79 80
  ftl::WeakPtr<RuntimeHolder> GetWeakPtr();

81 82
  void InitRootBundle(std::vector<char> bundle);
  blink::UnzipperProvider GetUnzipperProviderForRootBundle();
A
Adam Barth 已提交
83 84
  bool HandleAssetPlatformMessage(blink::PlatformMessage* message);
  bool HandleTextInputPlatformMessage(blink::PlatformMessage* message);
85

86
  void InitFidlInternal();
87
  void InitMozartInternal();
88

89
  void PostBeginFrame();
90 91
  void BeginFrame();
  void OnFrameComplete();
92
  void OnRedrawFrame();
J
Jeff Brown 已提交
93
  void Invalidate();
94

95
  std::unique_ptr<app::ApplicationContext> context_;
96
  fidl::InterfaceRequest<app::ServiceProvider> outgoing_services_;
97 98
  std::vector<char> root_bundle_data_;
  ftl::RefPtr<blink::ZipAssetStore> asset_store_;
R
Ryan Macnak 已提交
99
  void* dylib_handle_ = nullptr;
100 101
  std::unique_ptr<Rasterizer> rasterizer_;
  std::unique_ptr<blink::RuntimeController> runtime_;
102
  blink::ViewportMetrics viewport_metrics_;
J
Jeff Brown 已提交
103
  mozart::ViewManagerPtr view_manager_;
A
Adam Barth 已提交
104 105
  fidl::Binding<mozart::ViewListener> view_listener_binding_;
  fidl::Binding<mozart::InputListener> input_listener_binding_;
106
  mozart::InputConnectionPtr input_connection_;
J
Jeff Brown 已提交
107
  mozart::ViewPtr view_;
A
Adam Barth 已提交
108
  std::unordered_set<int> down_pointers_;
109 110 111
  mozart::InputMethodEditorPtr input_method_editor_;
  fidl::Binding<mozart::InputMethodEditorClient> text_input_binding_;
  int current_text_input_client_ = 0;
112
  ftl::TimePoint last_begin_frame_time_;
113 114 115
  bool frame_outstanding_ = false;
  bool frame_scheduled_ = false;
  bool frame_rendering_ = false;
116

117 118 119 120 121
  ftl::WeakPtrFactory<RuntimeHolder> weak_factory_;

  FTL_DISALLOW_COPY_AND_ASSIGN(RuntimeHolder);
};

A
Adam Barth 已提交
122
}  // namespace flutter_runner
123 124

#endif  // FLUTTER_CONTENT_HANDLER_RUNTIME_HOLDER_H_