[embedder] Additional bootstrapping.

- Finish moving sdk-integration to third_party.
- Add flutter/flutter as subrepository and use flutter tool from there.
- Update flutter/engine artifacts to match flutter/flutter revisions.
- Move flutter/engine artifacts into a subfolder.
- Run app in the session. This does not work yet because we do not
  have a ViewProvider.

Bug: 46971
Change-Id: If573fffd1a5ce3e4b2b5fb384d559bbf22d04582
diff --git a/.gitmodules b/.gitmodules
index d0ea17e..a9e9b1c 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,6 +1,9 @@
-[submodule "sdk-integration"]
+[submodule "third_party/sdk-integration"]
 	path = third_party/sdk-integration
 	url = https://fuchsia.googlesource.com/sdk-integration
 [submodule "third_party/googletest"]
 	path = third_party/googletest
 	url = https://fuchsia.googlesource.com/third_party/github.com/google/googletest/
+[submodule "third_party/dart-pkg/internal/flutter/flutter"]
+	path = third_party/dart-pkg/internal/flutter/flutter
+	url = https://flutter.googlesource.com/mirrors/flutter
diff --git a/README.md b/README.md
index 3048563..bc18aa3 100644
--- a/README.md
+++ b/README.md
@@ -29,16 +29,6 @@
     [[ -f "${HOME}/.ssh/fuchsia_authorized_keys" ]] || ssh-keygen -y -f "${HOME}/.ssh/fuchsia_ed25519" > "${HOME}/.ssh/fuchsia_authorized_keys"
    ```
 
-4. You will need [the Flutter tool](https://github.com/flutter/flutter/wiki/Setting-up-the-Framework-development-environment) in
-   your PATH. For example if your Flutter framework source checkout is at `~/flutter`, add the following to
-   your `~/.zprofile` for `zsh` or `~/.bash_profile` for `bash`:
-
-   ```sh
-   export PATH=~/flutter/bin:$PATH
-   ```
-
-   TODO(akbiggs): Get rid of the dependency on the Flutter tool by using Bazel build rules to build the Flutter app instead.
-
 ## Build and package the sample
 
 Now the repository is ready to build the sample.
@@ -92,6 +82,11 @@
 scripts/sync_engine_artifacts_to_revision.sh <ENGINE_COMMIT_SHA>
 ```
 
-A common workflow is to sync your Engine commit to your Flutter tool. If your Flutter tool
-lives at `~/flutter/bin/flutter`, you can do `cat ~/flutter/bin/internal/engine.version` to get
-your Flutter tool's Engine commit.
+A common workflow is to sync your Engine commit to the Flutter tool. To get your Flutter tool's Engine commit,
+you can do:
+
+```sh
+cat $FUCHSIA_EMBEDDER_DIR/third_party/dart-pkg/internal/flutter/flutter/bin/internal/engine.version
+```
+
+
diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel
index 657606b..837327f 100644
--- a/WORKSPACE.bazel
+++ b/WORKSPACE.bazel
@@ -5,7 +5,7 @@
 
 local_repository(
     name = "rules_fuchsia",
-    path = "sdk-integration/bazel_rules_fuchsia",
+    path = "third_party/sdk-integration/bazel_rules_fuchsia",
 )
 
 load(
@@ -26,10 +26,10 @@
 
 fuchsia_sdk_repository(
     name = "fuchsia_sdk",
-    cipd_tag = "version:8.20220502.3.1",
+    cipd_tag = "version:8.20220516.2.1",
     sha256 = {
-        "linux": "3e1fb302eef98d4fc36041b6e76227025c54aab3d2950d99eae2784e0faf6894",
-        "mac": "ad41128af40330dbbcae2ce4c1eb8167561fe338e539d53dc504b947ec0c1686",
+        "linux": "174fc1f1654a6201ff4080030b776a1b8b1aaaa3d0500ee3f3aa0068bb2a4de2",
+        "mac": "b7f932f25ea76f163a74707de25f5fb83de81bb92edb57ab29f433601e6a839c",
     },
 )
 
@@ -37,11 +37,11 @@
 
 fuchsia_clang_repository(
     name = "fuchsia_clang",
-    cipd_tag = "git_revision:ec2de7490813a7593dad59f210c7ec41f1a29002",
+    cipd_tag = "git_revision:80bebbc7cb77979ef9d229450b7ea84e3e9c6a5a",
     sdk_root_label = "@fuchsia_sdk",
     sha256 = {
-        "linux": "1a4ee70190a4e39c2804baa26c9aa58231a1ea67e16a5c5adc1dde2d7bada728",
-        "mac": "a5c681756e4f3db058ff451758962f9544f5993af38975c944eb82309222ce4f",
+        "linux": "3e1b7cacf697943aefc512f7cf4a4476e40a9dad937f268222ec5afd386d17a4",
+        "mac": "76d64ec3daaeb72c01061817affdce3bdac37a5ec47d931a8cefdc807d0fad90",
     },
 )
 
@@ -50,3 +50,14 @@
     name = "com_google_googletest",
     path = "third_party/googletest",
 )
+
+# Flutter.
+load(
+    "@rules_fuchsia//flutter:deps.bzl",
+    "flutter_register_toolchains",
+)
+
+flutter_register_toolchains(
+    dart_tag = "git_revision:e59c04ff550d5343f626a0fca4006e4b1e6c09da",
+    flutter_tag = "git_revision:b42972f3c1bea9762774bd3b142e1b37fe65a485",
+)
diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh
index d0552a5..9b439af 100755
--- a/scripts/bootstrap.sh
+++ b/scripts/bootstrap.sh
@@ -5,7 +5,7 @@
 # found in the LICENSE file.
 
 # TODO(https://fxbug.dev/90066): Delete this file once we don't need a generated
-# c++ toolchain from //sdk-integration/bazel_rules_generator/... anymore.
+# c++ toolchain from //third_party/sdk-integration/bazel_rules_generator/... anymore.
 
 set -e
 
@@ -46,7 +46,7 @@
 
   echo "Generating rules fuchsia!"
 
-  pushd "${REPO_ROOT}/sdk-integration/bazel_rules_generator/" > /dev/null || exit
+  pushd "${REPO_ROOT}/third_party/sdk-integration/bazel_rules_generator/" > /dev/null || exit
   ./generate.py \
     --directory "${REPO_ROOT}/sdk" \
     --output "${REPO_ROOT}/rules_fuchsia_transitional"
diff --git a/scripts/build.sh b/scripts/build.sh
index 143d033..6a0af99 100644
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -11,7 +11,7 @@
 # TODO(akbiggs): Set up infra.
 
 readonly REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/..
-readonly BAZELISK_TOOL="${REPO_ROOT}/sdk-integration/tools/bazel"
+readonly BAZELISK_TOOL="${REPO_ROOT}/third_party/sdk-integration/tools/bazel"
 
 main() {
   "$BAZELISK_TOOL" build --config=fuchsia_x64 //src/examples:examples_repository
diff --git a/scripts/build_and_run_sample_app.sh b/scripts/build_and_run_sample_app.sh
index 53fb37b..bfe0f3a 100755
--- a/scripts/build_and_run_sample_app.sh
+++ b/scripts/build_and_run_sample_app.sh
@@ -30,10 +30,10 @@
 
 echo-info "Building Flutter sample app bundle."
 cd "${FUCHSIA_EMBEDDER_DIR}"/src/examples/"${APP_NAME}"
-flutter build bundle
+"${FUCHSIA_EMBEDDER_DIR}"/tools/flutter build bundle
 
 echo-info "Running Flutter sample app."
 cd "${FUCHSIA_EMBEDDER_DIR}"
-bazel run --config=fuchsia_x64 //src/examples/"${APP_NAME}":"${APP_NAME}"_pkg.component
+bazel run --config=fuchsia_x64 //src/examples/"${APP_NAME}":"${APP_NAME}"_pkg.component -- --session
 
 popd
diff --git a/scripts/upload.sh b/scripts/upload.sh
index b98aa1c..5b3fa0f 100644
--- a/scripts/upload.sh
+++ b/scripts/upload.sh
@@ -11,7 +11,7 @@
 # TODO(akbiggs): Set up infra.
 
 readonly REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/..
-readonly BAZELISK_TOOL="${REPO_ROOT}/sdk-integration/tools/bazel"
+readonly BAZELISK_TOOL="${REPO_ROOT}/third_party/sdk-integration/tools/bazel"
 
 run_bazel() {
   local bazel="${REPO_ROOT}/tools/bazel"
diff --git a/sdk-integration b/sdk-integration
deleted file mode 160000
index 3dcbad9..0000000
--- a/sdk-integration
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 3dcbad97b1a1ab140caf5ba5b2102a82f3144d6e
diff --git a/sdk_version b/sdk_version
deleted file mode 100644
index fc4791d..0000000
--- a/sdk_version
+++ /dev/null
@@ -1 +0,0 @@
-7.20220330.2.1
diff --git a/src/embedder/BUILD.bazel b/src/embedder/BUILD.bazel
index c33d100..25c3a25 100644
--- a/src/embedder/BUILD.bazel
+++ b/src/embedder/BUILD.bazel
@@ -1,13 +1,13 @@
 # Copyright 2022 The Fuchsia Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
+#
 # Package for the Flutter embedder for Fuchsia.
 
 load(
     "@rules_fuchsia//fuchsia:defs.bzl",
     "fuchsia_cc_binary",
     "fuchsia_component_manifest",
-    "fuchsia_package_resource",
 )
 
 # ELF binary that takes Flutter app assets and runs a Flutter app.
@@ -15,12 +15,11 @@
     name = "embedder",
     visibility = ["//visibility:public"],
     srcs = [
-        # ABI-stable interface for the Flutter embedder platform. Copied from the Flutter Engine.
-        "embedder.h",
         "main.cc",
-        "libflutter_engine.so",
     ],
     deps = [
+        "//src/embedder/engine:embedder_header",
+        "//src/embedder/engine:libflutter_engine",
         "@fuchsia_sdk//pkg/async-loop-default",
         "@fuchsia_sdk//pkg/async-loop-cpp",
         "@fuchsia_sdk//pkg/syslog",
@@ -33,12 +32,3 @@
     src = "meta/embedder.cml",
     includes = ["@fuchsia_sdk//pkg/syslog:client"],
 )
-
-# The implementation of the embedder platform for the Flutter Engine.
-# TODO(akbiggs): This should come from a CIPD bucket instead.
-fuchsia_package_resource(
-    name = "flutter_engine",
-    visibility = ["//visibility:public"],
-    src = ":libflutter_engine.so",
-    dest = "lib/libflutter_engine.so",
-)
diff --git a/src/embedder/OWNERS b/src/embedder/OWNERS
deleted file mode 100644
index a8c7ae8..0000000
--- a/src/embedder/OWNERS
+++ /dev/null
@@ -1,2 +0,0 @@
-akbiggs@google.com
-dworsham@google.com
diff --git a/src/embedder/engine/BUILD.bazel b/src/embedder/engine/BUILD.bazel
new file mode 100644
index 0000000..80f8aa0
--- /dev/null
+++ b/src/embedder/engine/BUILD.bazel
@@ -0,0 +1,33 @@
+# Copyright 2022 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# Artifacts from the Flutter Engine repository: https://github.com/flutter/engine
+
+load(
+    "@rules_fuchsia//fuchsia:defs.bzl",
+    "fuchsia_package_resource",
+)
+
+# ABI-stable interface for the Flutter embedder platform. Copied from the Flutter Engine.
+cc_library(
+    name = "embedder_header",
+    hdrs = ["embedder.h"],
+    visibility = ["//src/embedder:__pkg__"],
+)
+
+# Shared library implementing the embedder.h header.
+#
+# TODO(akbiggs): This should come from a CIPD bucket instead.
+cc_library(
+    name = "libflutter_engine",
+    srcs = ["libflutter_engine.so"],
+    visibility = ["//src/embedder:__pkg__"],
+)
+
+fuchsia_package_resource(
+    name = "flutter_engine_pkg_resource",
+    visibility = ["//visibility:public"],
+    src = ":libflutter_engine.so",
+    dest = "lib/libflutter_engine.so",
+)
diff --git a/src/embedder/README.md b/src/embedder/engine/README.md
similarity index 68%
rename from src/embedder/README.md
rename to src/embedder/engine/README.md
index ef35819..239fc6d 100644
--- a/src/embedder/README.md
+++ b/src/embedder/engine/README.md
@@ -1,7 +1,6 @@
-# Flutter Embedder
+# `engine`
 
-The Flutter Embedder for Fuchsia is a new in-progress runtime for Flutter apps on Fuchsia. This runtime
-is built on top of Flutter's [embedder platform](https://github.com/flutter/flutter/wiki/Custom-Flutter-Engine-Embedders).
+This folder contains artifacts from the [Flutter Engine repository](https://github.com/flutter/engine).
 
 ## Code breakdown
 
@@ -18,10 +17,15 @@
 
 ### `embedder.h`
 
-This is a copy of the Flutter embedder platform header from the Engine repository: https://github.com/flutter/engine/blob/main/shell/platform/embedder/embedder.h
+This is a copy of the Flutter embedder platform header from the Engine repository:
+https://github.com/flutter/engine/blob/main/shell/platform/embedder/embedder.h
 
 ### `engine_revision`
 
 The git hash of [Flutter Engine](https://github.com/flutter/engine) that the Engine artifacts come from.
 Currently, this is purely for developer reference and is not used by any tooling.
 
+## Updating the artifacts
+
+See ["Syncing Engine Artifacts" in the top-level `README.md`](https://fuchsia.googlesource.com/flutter-embedder#syncing-engine-artifacts).
+
diff --git a/src/embedder/embedder.h b/src/embedder/engine/embedder.h
similarity index 100%
rename from src/embedder/embedder.h
rename to src/embedder/engine/embedder.h
diff --git a/src/embedder/engine/engine_revision b/src/embedder/engine/engine_revision
new file mode 100644
index 0000000..2c59ebd
--- /dev/null
+++ b/src/embedder/engine/engine_revision
@@ -0,0 +1 @@
+94a5895388f79e17b7bc8c91edebf461e57c2f01
diff --git a/src/embedder/engine/libflutter_engine.so b/src/embedder/engine/libflutter_engine.so
new file mode 100755
index 0000000..6e0da88
--- /dev/null
+++ b/src/embedder/engine/libflutter_engine.so
Binary files differ
diff --git a/src/embedder/engine_revision b/src/embedder/engine_revision
deleted file mode 100644
index 5226470..0000000
--- a/src/embedder/engine_revision
+++ /dev/null
@@ -1 +0,0 @@
-6710d67ead813c51e6e6cee4c8df7d208795dd85
diff --git a/src/embedder/libflutter_engine.so b/src/embedder/libflutter_engine.so
deleted file mode 100755
index 201dece..0000000
--- a/src/embedder/libflutter_engine.so
+++ /dev/null
Binary files differ
diff --git a/src/embedder/main.cc b/src/embedder/main.cc
index 7735348..14ce931 100644
--- a/src/embedder/main.cc
+++ b/src/embedder/main.cc
@@ -11,7 +11,7 @@
 #include <lib/syslog/global.h>
 #include <string>
 
-#include "embedder.h"
+#include "src/embedder/engine/embedder.h"
 
 namespace {
 
@@ -75,7 +75,6 @@
     FX_LOG(ERROR, kLogTag, "usage: executable <path to flutter bundle>");
     return EXIT_FAILURE;
   }
-
   const char *assets_path = argv[1];
 
   async::Loop loop(&kAsyncLoopConfigAttachToCurrentThread);
diff --git a/src/examples/flutter_sample_app/BUILD.bazel b/src/examples/flutter_sample_app/BUILD.bazel
index 6a5bc1d..cec1692 100644
--- a/src/examples/flutter_sample_app/BUILD.bazel
+++ b/src/examples/flutter_sample_app/BUILD.bazel
@@ -44,6 +44,6 @@
     visibility = ["//visibility:public"],
     deps = [
         ":component",
-        "//src/embedder:flutter_engine",
+        "//src/embedder/engine:flutter_engine_pkg_resource",
     ],
 )
diff --git a/src/examples/flutter_sample_app/pubspec.lock b/src/examples/flutter_sample_app/pubspec.lock
index 9cee3e9..2aa44f8 100644
--- a/src/examples/flutter_sample_app/pubspec.lock
+++ b/src/examples/flutter_sample_app/pubspec.lock
@@ -21,7 +21,7 @@
       name: characters
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "1.2.0"
+    version: "1.2.1"
   charcode:
     dependency: transitive
     description:
@@ -120,7 +120,7 @@
       name: source_span
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "1.8.2"
+    version: "1.9.0"
   stack_trace:
     dependency: transitive
     description:
diff --git a/third_party/dart-pkg/internal/flutter/flutter b/third_party/dart-pkg/internal/flutter/flutter
new file mode 160000
index 0000000..ac33330
--- /dev/null
+++ b/third_party/dart-pkg/internal/flutter/flutter
@@ -0,0 +1 @@
+Subproject commit ac3333094af2b4e51163167546777086e5bd7b79
diff --git a/third_party/sdk-integration b/third_party/sdk-integration
new file mode 160000
index 0000000..1f2296d
--- /dev/null
+++ b/third_party/sdk-integration
@@ -0,0 +1 @@
+Subproject commit 1f2296dc876d3de3f09c87b9e01fb1355b7fcec4
diff --git a/tools/dart b/tools/dart
new file mode 120000
index 0000000..6912b1e
--- /dev/null
+++ b/tools/dart
@@ -0,0 +1 @@
+../third_party/dart-pkg/internal/flutter/flutter/bin/dart
\ No newline at end of file
diff --git a/tools/flutter b/tools/flutter
new file mode 120000
index 0000000..fb80f8b
--- /dev/null
+++ b/tools/flutter
@@ -0,0 +1 @@
+../third_party/dart-pkg/internal/flutter/flutter/bin/flutter
\ No newline at end of file