OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/ui/demo/mus_demo_external.h" | 5 #include "services/ui/demo/mus_demo_external.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "services/service_manager/public/cpp/service_context.h" | 9 #include "services/service_manager/public/cpp/service_context.h" |
10 #include "services/ui/demo/window_tree_data.h" | 10 #include "services/ui/demo/window_tree_data.h" |
11 #include "services/ui/public/interfaces/constants.mojom.h" | 11 #include "services/ui/public/interfaces/constants.mojom.h" |
12 #include "services/ui/public/interfaces/window_tree_host.mojom.h" | 12 #include "services/ui/public/interfaces/window_tree_host.mojom.h" |
13 #include "ui/aura/mus/window_tree_client.h" | 13 #include "ui/aura/mus/window_tree_client.h" |
14 #include "ui/aura/mus/window_tree_host_mus.h" | 14 #include "ui/aura/mus/window_tree_host_mus.h" |
15 #include "ui/display/display.h" | 15 #include "ui/display/display.h" |
16 | 16 |
17 namespace ui { | 17 namespace ui { |
18 namespace demo { | 18 namespace demo { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 class WindowTreeDataExternal : public WindowTreeData { | 22 class WindowTreeDataExternal : public WindowTreeData { |
23 public: | 23 public: |
24 // Creates a new window tree host associated to the WindowTreeData. | 24 // Creates a new window tree host associated to the WindowTreeData. |
25 WindowTreeDataExternal(mojom::WindowTreeHostFactory* factory, | 25 WindowTreeDataExternal(aura::WindowTreeClient* window_tree_client, |
26 mojom::WindowTreeClientPtr tree_client, | |
27 int square_size) | 26 int square_size) |
28 : WindowTreeData(square_size) { | 27 : WindowTreeData(square_size) { |
29 // TODO(tonikitoo,fwang): Extend the API to allow creating WindowTreeHost | 28 SetWindowTreeHost( |
30 // using the WindowTreeClient. | 29 base::MakeUnique<aura::WindowTreeHostMus>(window_tree_client)); |
31 factory->CreateWindowTreeHost(MakeRequest(&host_), std::move(tree_client)); | |
32 } | 30 } |
33 | 31 |
34 private: | |
35 // Holds the Mojo pointer to the window tree host. | |
36 mojom::WindowTreeHostPtr host_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(WindowTreeDataExternal); | 32 DISALLOW_COPY_AND_ASSIGN(WindowTreeDataExternal); |
39 }; | 33 }; |
40 | 34 |
41 int GetSquareSizeForWindow(int window_index) { | 35 int GetSquareSizeForWindow(int window_index) { |
42 return 50 * window_index + 400; | 36 return 50 * window_index + 400; |
43 }; | 37 }; |
44 | 38 |
45 } // namespace | 39 } // namespace |
46 | 40 |
47 MusDemoExternal::MusDemoExternal() {} | 41 MusDemoExternal::MusDemoExternal() {} |
48 | 42 |
49 MusDemoExternal::~MusDemoExternal() {} | 43 MusDemoExternal::~MusDemoExternal() {} |
50 | 44 |
51 std::unique_ptr<aura::WindowTreeClient> | 45 std::unique_ptr<aura::WindowTreeClient> |
52 MusDemoExternal::CreateWindowTreeClient() { | 46 MusDemoExternal::CreateWindowTreeClient() { |
53 return base::MakeUnique<aura::WindowTreeClient>( | 47 return base::MakeUnique<aura::WindowTreeClient>(context()->connector(), this); |
54 context()->connector(), this, nullptr, | |
55 MakeRequest(&window_tree_client_mojo_)); | |
56 } | 48 } |
57 | 49 |
58 void MusDemoExternal::OnStartImpl() { | 50 void MusDemoExternal::OnStartImpl() { |
59 const base::CommandLine* command_line = | 51 const base::CommandLine* command_line = |
60 base::CommandLine::ForCurrentProcess(); | 52 base::CommandLine::ForCurrentProcess(); |
61 if (command_line->HasSwitch("external-window-count")) { | 53 if (command_line->HasSwitch("external-window-count")) { |
62 if (!base::StringToSizeT( | 54 if (!base::StringToSizeT( |
63 command_line->GetSwitchValueASCII("external-window-count"), | 55 command_line->GetSwitchValueASCII("external-window-count"), |
64 &number_of_windows_)) { | 56 &number_of_windows_)) { |
65 LOG(FATAL) << "Invalid value for \'external-window-count\'"; | 57 LOG(FATAL) << "Invalid value for \'external-window-count\'"; |
66 return; | 58 return; |
67 } | 59 } |
68 } | 60 } |
69 | 61 |
70 // TODO(tonikitoo,fwang): Extend the WindowTreeClient API to allow connection | 62 window_tree_client()->ConnectViaWindowTreeHostFactory(); |
71 // to the window tree host factory using window_tree_client(). | |
72 context()->connector()->BindInterface(ui::mojom::kServiceName, | |
73 &window_tree_host_factory_); | |
74 | 63 |
75 // TODO(tonikitoo,fwang): Implement management of displays in external mode. | 64 // TODO(tonikitoo,fwang): Implement management of displays in external mode. |
76 // For now, a fake display is created in order to work around an assertion in | 65 // For now, a fake display is created in order to work around an assertion in |
77 // aura::GetDeviceScaleFactorFromDisplay(). | 66 // aura::GetDeviceScaleFactorFromDisplay(). |
78 AddPrimaryDisplay(display::Display(0)); | 67 AddPrimaryDisplay(display::Display(0)); |
79 | 68 |
80 // The number of windows to open is specified by number_of_windows_. The | 69 // TODO(tonikitoo,fwang): New windows can be launched without need to wait |
81 // windows are opened sequentially (the first one here and the others after | 70 // the respective ::OnEmbed call of the previous instance. |
82 // each call to OnEmbed) to ensure that the WindowTreeHostMus passed to | |
83 // OnEmbed corresponds to the WindowTreeDataExternal::host_ created in | |
84 // OpenNewWindow. | |
85 OpenNewWindow(); | 71 OpenNewWindow(); |
86 } | 72 } |
87 | 73 |
88 void MusDemoExternal::OpenNewWindow() { | 74 void MusDemoExternal::OpenNewWindow() { |
89 // TODO(tonikitoo,fwang): Extend the WindowTreeClient API to allow creation | |
90 // of window tree host. Then pass window_tree_client() here and remove | |
91 // window_tree_host_factory_ and window_tree_client_mojo_. Currently | |
92 // window_tree_client_mojo_ is only initialized once so this is incorrect when | |
93 // kNumberOfWindows > 1. | |
94 AppendWindowTreeData(base::MakeUnique<WindowTreeDataExternal>( | 75 AppendWindowTreeData(base::MakeUnique<WindowTreeDataExternal>( |
95 window_tree_host_factory_.get(), std::move(window_tree_client_mojo_), | 76 window_tree_client(), |
96 GetSquareSizeForWindow(initialized_windows_count_))); | 77 GetSquareSizeForWindow(initialized_windows_count_))); |
97 } | 78 } |
98 | 79 |
99 void MusDemoExternal::OnEmbed( | 80 void MusDemoExternal::OnEmbed( |
100 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { | 81 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { |
101 InitWindowTreeData(std::move(window_tree_host)); | 82 DCHECK(!window_tree_host); |
| 83 |
| 84 // TODO: Clean up WindowTreeClientDelegate::OnEmbed API so that it passes |
| 85 // no ownership of WindowTreeHostMus instance. |
| 86 InitWindowTreeData(nullptr); |
102 initialized_windows_count_++; | 87 initialized_windows_count_++; |
103 | 88 |
104 // Open the next window until the requested number of windows is reached. | 89 // Open the next window until the requested number of windows is reached. |
105 if (initialized_windows_count_ < number_of_windows_) | 90 if (initialized_windows_count_ < number_of_windows_) |
106 OpenNewWindow(); | 91 OpenNewWindow(); |
107 } | 92 } |
108 | 93 |
109 void MusDemoExternal::OnEmbedRootDestroyed( | 94 void MusDemoExternal::OnEmbedRootDestroyed( |
110 aura::WindowTreeHostMus* window_tree_host) { | 95 aura::WindowTreeHostMus* window_tree_host) { |
111 RemoveWindowTreeData(window_tree_host); | 96 RemoveWindowTreeData(window_tree_host); |
112 } | 97 } |
113 | 98 |
114 } // namespace demo | 99 } // namespace demo |
115 } // namespace ui | 100 } // namespace ui |
OLD | NEW |