engine.h 5.0 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5 6
#ifndef SHELL_COMMON_ENGINE_H_
#define SHELL_COMMON_ENGINE_H_
7

8
#include "flutter/assets/zip_asset_store.h"
9
#include "flutter/lib/ui/window/platform_message.h"
10
#include "flutter/lib/ui/window/viewport_metrics.h"
11
#include "flutter/runtime/runtime_controller.h"
12
#include "flutter/runtime/runtime_delegate.h"
13
#include "flutter/shell/common/rasterizer.h"
14 15
#include "lib/fxl/macros.h"
#include "lib/fxl/memory/weak_ptr.h"
A
Adam Barth 已提交
16
#include "third_party/skia/include/core/SkPicture.h"
17

A
Adam Barth 已提交
18 19 20 21 22
namespace blink {
class DirectoryAssetBundle;
class ZipAssetBundle;
}  // namespace blink

23
namespace shell {
24
class PlatformView;
A
Adam Barth 已提交
25
class Animator;
26
using PointerDataPacket = blink::PointerDataPacket;
27

A
Adam Barth 已提交
28
class Engine : public blink::RuntimeDelegate {
29
 public:
30
  explicit Engine(PlatformView* platform_view);
A
Adam Barth 已提交
31

32 33
  ~Engine() override;

34
  fml::WeakPtr<Engine> GetWeakPtr();
35

36
  static void Init(const std::string& bundle_path);
37

38
  void RunBundle(const std::string& bundle_path,
39 40
                 const std::string& entrypoint = main_entrypoint_,
                 bool reuse_runtime_controller = false);
41 42 43 44 45

  // Uses the given snapshot instead of looking inside the bundle for the
  // snapshot. If |snapshot_override| is empty, this function looks for the
  // snapshot in the bundle itself.
  void RunBundleAndSnapshot(const std::string& bundle_path,
46
                            const std::string& snapshot_override,
47 48
                            const std::string& entrypoint = main_entrypoint_,
                            bool reuse_runtime_controller = false);
49

50
  // Uses the given source code instead of looking inside the bundle for the
51 52 53
  // source code.
  void RunBundleAndSource(const std::string& bundle_path,
                          const std::string& main,
54 55
                          const std::string& packages,
                          bool reuse_runtime_controller = false);
56

57
  void BeginFrame(fxl::TimePoint frame_time);
58
  void NotifyIdle(int64_t deadline);
59

60 61
  void RunFromSource(const std::string& main,
                     const std::string& packages,
62
                     const std::string& bundle);
63 64

  Dart_Port GetUIIsolateMainPort();
65
  std::string GetUIIsolateName();
66
  bool UIIsolateHasLivePorts();
67 68
  tonic::DartErrorHandleType GetUIIsolateLastError();
  tonic::DartErrorHandleType GetLoadScriptError();
69

70 71
  void OnOutputSurfaceCreated(const fxl::Closure& gpu_continuation);
  void OnOutputSurfaceDestroyed(const fxl::Closure& gpu_continuation);
72
  void SetViewportMetrics(const blink::ViewportMetrics& metrics);
73
  void DispatchPlatformMessage(fxl::RefPtr<blink::PlatformMessage> message);
74
  void DispatchPointerDataPacket(const PointerDataPacket& packet);
75
  void DispatchSemanticsAction(int id, blink::SemanticsAction action);
76
  void SetSemanticsEnabled(bool enabled);
77
  void ScheduleFrame(bool regenerate_layer_tree = true) override;
78

79
  void set_rasterizer(fml::WeakPtr<Rasterizer> rasterizer);
80

81
 private:
82
  // RuntimeDelegate methods:
83
  std::string DefaultRouteName() override;
A
Adam Barth 已提交
84
  void Render(std::unique_ptr<flow::LayerTree> layer_tree) override;
85
  void UpdateSemantics(std::vector<blink::SemanticsNode> update) override;
86
  void HandlePlatformMessage(
87
      fxl::RefPtr<blink::PlatformMessage> message) override;
88 89 90
  void DidCreateMainIsolate(Dart_Isolate isolate) override;
  void DidCreateSecondaryIsolate(Dart_Isolate isolate) override;

91 92 93
  void StopAnimator();
  void StartAnimatorIfPossible();

94
  void ConfigureAssetBundle(const std::string& path);
95 96
  void ConfigureRuntime(const std::string& script_uri,
                        bool reuse_runtime_controller = false);
97

98 99
  bool HandleLifecyclePlatformMessage(blink::PlatformMessage* message);
  bool HandleNavigationPlatformMessage(
100
      fxl::RefPtr<blink::PlatformMessage> message);
101
  bool HandleLocalizationPlatformMessage(blink::PlatformMessage* message);
102
  void HandleSettingsPlatformMessage(blink::PlatformMessage* message);
103

104
  void HandleAssetPlatformMessage(fxl::RefPtr<blink::PlatformMessage> message);
105
  bool GetAssetAsBuffer(const std::string& name, std::vector<uint8_t>* data);
A
Adam Barth 已提交
106

107 108
  static const std::string main_entrypoint_;

109
  std::weak_ptr<PlatformView> platform_view_;
110
  std::unique_ptr<Animator> animator_;
111
  std::unique_ptr<blink::RuntimeController> runtime_;
112
  tonic::DartErrorHandleType load_script_error_;
113
  std::string initial_route_;
114
  blink::ViewportMetrics viewport_metrics_;
115 116
  std::string language_code_;
  std::string country_code_;
117
  std::string user_settings_data_;
A
Adam Barth 已提交
118
  bool semantics_enabled_ = false;
A
Adam Barth 已提交
119
  // TODO(abarth): Unify these two behind a common interface.
120
  fxl::RefPtr<blink::ZipAssetStore> asset_store_;
A
Adam Barth 已提交
121
  std::unique_ptr<blink::DirectoryAssetBundle> directory_asset_bundle_;
122 123 124
  // TODO(eseidel): This should move into an AnimatorStateMachine.
  bool activity_running_;
  bool have_surface_;
125
  fml::WeakPtrFactory<Engine> weak_factory_;
126

127
  FXL_DISALLOW_COPY_AND_ASSIGN(Engine);
128 129 130 131
};

}  // namespace shell

132
#endif  // SHELL_COMMON_ENGINE_H_