OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "examples/ui/tile/tile_view.h" | 6 #include "examples/ui/tile/tile_view.h" |
7 #include "mojo/services/geometry/cpp/geometry_util.h" | 7 #include "mojo/services/geometry/cpp/geometry_util.h" |
8 | 8 |
9 namespace examples { | 9 namespace examples { |
10 | 10 |
11 namespace { | 11 namespace { |
12 constexpr uint32_t kViewResourceIdBase = 100; | 12 constexpr uint32_t kViewResourceIdBase = 100; |
13 constexpr uint32_t kViewResourceIdSpacing = 100; | 13 constexpr uint32_t kViewResourceIdSpacing = 100; |
14 | 14 |
15 constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId; | 15 constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId; |
16 constexpr uint32_t kViewNodeIdBase = 100; | 16 constexpr uint32_t kViewNodeIdBase = 100; |
17 constexpr uint32_t kViewNodeIdSpacing = 100; | 17 constexpr uint32_t kViewNodeIdSpacing = 100; |
18 constexpr uint32_t kViewSceneNodeIdOffset = 1; | 18 constexpr uint32_t kViewSceneNodeIdOffset = 1; |
19 constexpr uint32_t kViewFallbackNodeIdOffset = 2; | 19 constexpr uint32_t kViewFallbackNodeIdOffset = 2; |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 TileView::TileView(mojo::ApplicationImpl* app_impl, | 22 TileView::TileView( |
23 const std::vector<std::string>& view_urls, | 23 mojo::ApplicationImpl* app_impl, |
24 const mojo::ui::ViewProvider::CreateViewCallback& callback) | 24 mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request, |
25 : BaseView(app_impl, "Tile", callback), view_urls_(view_urls) { | 25 const std::vector<std::string>& view_urls) |
| 26 : BaseView(app_impl, view_owner_request.Pass(), "Tile"), |
| 27 view_urls_(view_urls) { |
26 ConnectViews(); | 28 ConnectViews(); |
27 } | 29 } |
28 | 30 |
29 TileView::~TileView() {} | 31 TileView::~TileView() {} |
30 | 32 |
31 void TileView::ConnectViews() { | 33 void TileView::ConnectViews() { |
32 uint32_t child_key = 0; | 34 uint32_t child_key = 0; |
33 for (const auto& url : view_urls_) { | 35 for (const auto& url : view_urls_) { |
34 // Start connecting to the view provider. | 36 // Start connecting to the view provider. |
35 mojo::ui::ViewProviderPtr provider; | 37 mojo::ui::ViewProviderPtr provider; |
36 app_impl()->ConnectToService(url, &provider); | 38 app_impl()->ConnectToService(url, &provider); |
37 provider.set_connection_error_handler( | |
38 base::Bind(&TileView::OnChildConnectionError, base::Unretained(this), | |
39 child_key, url)); | |
40 | 39 |
41 // Create the view. | |
42 // We include the provider reference in the callback so that the | |
43 // binding will be kept alive until the callback completes. | |
44 LOG(INFO) << "Connecting to view: child_key=" << child_key | 40 LOG(INFO) << "Connecting to view: child_key=" << child_key |
45 << ", url=" << url; | 41 << ", url=" << url; |
46 provider->CreateView( | 42 mojo::ui::ViewOwnerPtr child_view_owner; |
47 nullptr, nullptr, | 43 provider->CreateView(mojo::GetProxy(&child_view_owner), nullptr, nullptr); |
48 base::Bind(&TileView::OnChildCreated, base::Unretained(this), child_key, | 44 |
49 url, base::Passed(provider.Pass()))); | 45 view_host()->AddChild(child_key, child_view_owner.Pass()); |
| 46 views_.emplace(std::make_pair( |
| 47 child_key, std::unique_ptr<ViewData>(new ViewData(url, child_key)))); |
| 48 |
50 child_key++; | 49 child_key++; |
51 } | 50 } |
52 } | 51 } |
53 | 52 |
54 void TileView::OnChildConnectionError(uint32_t child_key, | |
55 const std::string& url) { | |
56 LOG(ERROR) << "Could not connect to view: child_key=" << child_key | |
57 << ", url=" << url; | |
58 } | |
59 | |
60 void TileView::OnChildCreated(uint32_t child_key, | |
61 const std::string& url, | |
62 mojo::ui::ViewProviderPtr provider, | |
63 mojo::ui::ViewTokenPtr token) { | |
64 DCHECK(views_.find(child_key) == views_.end()); | |
65 LOG(INFO) << "View created: child_key=" << child_key << ", url=" << url; | |
66 | |
67 view_host()->AddChild(child_key, token.Pass()); | |
68 views_.emplace(std::make_pair( | |
69 child_key, std::unique_ptr<ViewData>(new ViewData(url, child_key)))); | |
70 | |
71 // Note that the view provider will be destroyed once this function | |
72 // returns which is fine now that we are done creating the view. | |
73 } | |
74 | |
75 void TileView::OnChildUnavailable(uint32_t child_key, | 53 void TileView::OnChildUnavailable(uint32_t child_key, |
76 const OnChildUnavailableCallback& callback) { | 54 const OnChildUnavailableCallback& callback) { |
77 auto it = views_.find(child_key); | 55 auto it = views_.find(child_key); |
78 DCHECK(it != views_.end()); | 56 DCHECK(it != views_.end()); |
79 LOG(ERROR) << "View died unexpectedly: child_key=" << child_key | 57 LOG(ERROR) << "View died unexpectedly: child_key=" << child_key |
80 << ", url=" << it->second->url; | 58 << ", url=" << it->second->url; |
81 | 59 |
82 std::unique_ptr<ViewData> view_data = std::move(it->second); | 60 std::unique_ptr<ViewData> view_data = std::move(it->second); |
83 views_.erase(it); | 61 views_.erase(it); |
84 | 62 |
85 view_host()->RemoveChild(child_key); | 63 view_host()->RemoveChild(child_key, nullptr); |
86 | 64 |
87 if (view_data->layout_pending) { | 65 if (view_data->layout_pending) { |
88 DCHECK(pending_child_layout_count_); | 66 DCHECK(pending_child_layout_count_); |
89 pending_child_layout_count_--; | 67 pending_child_layout_count_--; |
90 FinishLayout(); | 68 FinishLayout(); |
91 } | 69 } |
92 | 70 |
93 callback.Run(); | 71 callback.Run(); |
94 } | 72 } |
95 | 73 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 pending_layout_callback_.Run(info.Pass()); | 242 pending_layout_callback_.Run(info.Pass()); |
265 pending_layout_callback_.reset(); | 243 pending_layout_callback_.reset(); |
266 } | 244 } |
267 | 245 |
268 TileView::ViewData::ViewData(const std::string& url, uint32_t key) | 246 TileView::ViewData::ViewData(const std::string& url, uint32_t key) |
269 : url(url), key(key), layout_pending(false) {} | 247 : url(url), key(key), layout_pending(false) {} |
270 | 248 |
271 TileView::ViewData::~ViewData() {} | 249 TileView::ViewData::~ViewData() {} |
272 | 250 |
273 } // namespace examples | 251 } // namespace examples |
OLD | NEW |