| Index: ui/aura/mus/window_tree_client.cc
 | 
| diff --git a/ui/aura/mus/window_tree_client.cc b/ui/aura/mus/window_tree_client.cc
 | 
| index 934261571eae24565c6abb99a6eb7cb4782885f0..7bff8fe99ca75d7065345728447e709aeb767cc9 100644
 | 
| --- a/ui/aura/mus/window_tree_client.cc
 | 
| +++ b/ui/aura/mus/window_tree_client.cc
 | 
| @@ -185,6 +185,7 @@ WindowTreeClient::WindowTreeClient(
 | 
|        binding_(this),
 | 
|        tree_(nullptr),
 | 
|        in_destructor_(false),
 | 
| +      in_external_window_mode_(false),
 | 
|        weak_factory_(this) {
 | 
|    DCHECK(delegate_);
 | 
|    // Allow for a null request in tests.
 | 
| @@ -234,6 +235,22 @@ void WindowTreeClient::ConnectViaWindowTreeFactory() {
 | 
|    SetWindowTree(std::move(window_tree));
 | 
|  }
 | 
|  
 | 
| +void WindowTreeClient::ConnectViaWindowTreeHostFactory() {
 | 
| +  // The client id doesn't really matter, we use 101 purely for debugging.
 | 
| +  client_id_ = 101;
 | 
| +
 | 
| +  ui::mojom::WindowTreeHostFactoryRegistrarPtr host_factory_registrar;
 | 
| +  connector_->BindInterface(ui::mojom::kServiceName, &host_factory_registrar);
 | 
| +
 | 
| +  ui::mojom::WindowTreePtr window_tree;
 | 
| +  host_factory_registrar->Register(MakeRequest(&window_tree_host_factory_ptr_),
 | 
| +                                   MakeRequest(&window_tree),
 | 
| +                                   binding_.CreateInterfacePtrAndBind());
 | 
| +  SetWindowTree(std::move(window_tree));
 | 
| +
 | 
| +  in_external_window_mode_ = true;
 | 
| +}
 | 
| +
 | 
|  void WindowTreeClient::ConnectAsWindowManager() {
 | 
|    DCHECK(window_manager_delegate_);
 | 
|  
 | 
| @@ -245,6 +262,12 @@ void WindowTreeClient::ConnectAsWindowManager() {
 | 
|    SetWindowTree(std::move(window_tree));
 | 
|  }
 | 
|  
 | 
| +void WindowTreeClient::CreateHost(ui::mojom::WindowTreeHostRequest host,
 | 
| +                                  uint32_t client_id) {
 | 
| +  window_tree_host_factory_ptr_->CreatePlatformWindow(std::move(host),
 | 
| +                                                      client_id);
 | 
| +}
 | 
| +
 | 
|  void WindowTreeClient::SetCanFocus(Window* window, bool can_focus) {
 | 
|    DCHECK(tree_);
 | 
|    DCHECK(window);
 | 
| @@ -421,6 +444,14 @@ std::unique_ptr<WindowTreeHostMus> WindowTreeClient::CreateWindowTreeHost(
 | 
|        base::MakeUnique<WindowTreeHostMus>(std::move(window_port), this,
 | 
|                                            display_id);
 | 
|    window_tree_host->InitHost();
 | 
| +
 | 
| +  ConfigureWindowDataFromServer(window_tree_host.get(), window_data);
 | 
| +  return window_tree_host;
 | 
| +}
 | 
| +
 | 
| +void WindowTreeClient::ConfigureWindowDataFromServer(
 | 
| +    WindowTreeHostMus* window_tree_host,
 | 
| +    const ui::mojom::WindowData& window_data) {
 | 
|    SetLocalPropertiesFromServerProperties(
 | 
|        WindowMus::Get(window_tree_host->window()), window_data);
 | 
|    if (window_data.visible) {
 | 
| @@ -429,7 +460,6 @@ std::unique_ptr<WindowTreeHostMus> WindowTreeClient::CreateWindowTreeHost(
 | 
|    }
 | 
|    SetWindowBoundsFromServer(WindowMus::Get(window_tree_host->window()),
 | 
|                              window_data.bounds);
 | 
| -  return window_tree_host;
 | 
|  }
 | 
|  
 | 
|  WindowMus* WindowTreeClient::NewWindowFromWindowData(
 | 
| @@ -847,7 +877,25 @@ void WindowTreeClient::OnEmbed(ClientSpecificId client_id,
 | 
|                                 int64_t display_id,
 | 
|                                 Id focused_window_id,
 | 
|                                 bool drawn) {
 | 
| +  if (in_external_window_mode_) {
 | 
| +    // No need to set 'tree_ptr_' whether it was already set during
 | 
| +    // ConnectViaWindowManagerHostFactory.
 | 
| +    DCHECK(tree_ptr_);
 | 
| +    DCHECK(!tree);
 | 
| +
 | 
| +    auto it = windows_.find(focused_window_id);
 | 
| +    DCHECK(it != windows_.end());
 | 
| +
 | 
| +    WindowTreeHostMus* window_tree_host = GetWindowTreeHostMus(it->second);
 | 
| +    window_tree_host->InitHost();
 | 
| +    ConfigureWindowDataFromServer(window_tree_host, *root_data);
 | 
| +
 | 
| +    delegate_->OnEmbed(nullptr);
 | 
| +    return;
 | 
| +  }
 | 
| +
 | 
|    DCHECK(!tree_ptr_);
 | 
| +  DCHECK(tree);
 | 
|    tree_ptr_ = std::move(tree);
 | 
|  
 | 
|    is_from_embed_ = true;
 | 
| @@ -1719,8 +1767,12 @@ void WindowTreeClient::OnWindowTreeHostCancelWindowMove(
 | 
|  
 | 
|  std::unique_ptr<WindowPortMus> WindowTreeClient::CreateWindowPortForTopLevel(
 | 
|      const std::map<std::string, std::vector<uint8_t>>* properties) {
 | 
| +  WindowMusType window_type = in_external_window_mode_
 | 
| +                                  ? WindowMusType::EMBED
 | 
| +                                  : WindowMusType::TOP_LEVEL;
 | 
| +
 | 
|    std::unique_ptr<WindowPortMus> window_port =
 | 
| -      base::MakeUnique<WindowPortMus>(this, WindowMusType::TOP_LEVEL);
 | 
| +      base::MakeUnique<WindowPortMus>(this, window_type);
 | 
|    roots_.insert(window_port.get());
 | 
|  
 | 
|    window_port->set_server_id(MakeTransportId(client_id_, next_window_id_++));
 | 
| @@ -1732,11 +1784,17 @@ std::unique_ptr<WindowPortMus> WindowTreeClient::CreateWindowPortForTopLevel(
 | 
|        transport_properties[property_pair.first] = property_pair.second;
 | 
|    }
 | 
|  
 | 
| -  const uint32_t change_id =
 | 
| -      ScheduleInFlightChange(base::MakeUnique<CrashInFlightChange>(
 | 
| -          window_port.get(), ChangeType::NEW_TOP_LEVEL_WINDOW));
 | 
| -  tree_->NewTopLevelWindow(change_id, window_port->server_id(),
 | 
| -                           transport_properties);
 | 
| +  if (in_external_window_mode_) {
 | 
| +    ui::mojom::WindowTreeHostPtr host;
 | 
| +    CreateHost(MakeRequest(&host), window_port->server_id());
 | 
| +  } else {
 | 
| +    const uint32_t change_id =
 | 
| +        ScheduleInFlightChange(base::MakeUnique<CrashInFlightChange>(
 | 
| +            window_port.get(), ChangeType::NEW_TOP_LEVEL_WINDOW));
 | 
| +    tree_->NewTopLevelWindow(change_id, window_port->server_id(),
 | 
| +                             transport_properties);
 | 
| +  }
 | 
| +
 | 
|    return window_port;
 | 
|  }
 | 
|  
 | 
| 
 |