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 "services/ui/view_manager/view_associate_table.h" | 5 #include "services/ui/view_manager/view_associate_table.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "mojo/public/cpp/application/connect.h" | 11 #include "mojo/public/cpp/application/connect.h" |
12 #include "mojo/services/ui/views/cpp/formatting.h" | 12 #include "mojo/services/ui/views/cpp/formatting.h" |
13 | 13 |
14 namespace view_manager { | 14 namespace view_manager { |
15 | 15 |
16 template <typename T> | 16 template <typename T> |
17 static bool Contains(const mojo::Array<T>& array, const T& value) { | 17 static bool Contains(const mojo::Array<T>& array, const T& value) { |
18 return std::find(array.storage().cbegin(), array.storage().cend(), value) != | 18 return std::find(array.storage().cbegin(), array.storage().cend(), value) != |
19 array.storage().cend(); | 19 array.storage().cend(); |
20 } | 20 } |
21 | 21 |
22 ViewAssociateTable::ViewAssociateTable() {} | 22 ViewAssociateTable::ViewAssociateTable() {} |
23 | 23 |
24 ViewAssociateTable::~ViewAssociateTable() {} | 24 ViewAssociateTable::~ViewAssociateTable() {} |
25 | 25 |
26 void ViewAssociateTable::ConnectAssociates( | 26 void ViewAssociateTable::RegisterViewAssociate( |
27 mojo::ApplicationImpl* app_impl, | |
28 mojo::ui::ViewInspector* inspector, | 27 mojo::ui::ViewInspector* inspector, |
29 const std::vector<std::string>& urls, | 28 mojo::ui::ViewAssociatePtr associate, |
30 const AssociateConnectionErrorCallback& connection_error_callback) { | 29 const AssociateConnectionErrorCallback& connection_error_callback, |
31 DCHECK(app_impl); | 30 const mojo::String& label) { |
32 DCHECK(inspector); | 31 DCHECK(inspector); |
32 DCHECK(associate.is_bound()); | |
33 | 33 |
34 for (auto& url : urls) { | 34 associates_.emplace_back( |
35 DVLOG(1) << "Connecting to view associate: url=" << url; | 35 new AssociateData(label, associate.Pass(), inspector)); |
36 associates_.emplace_back(new AssociateData(url, inspector)); | 36 AssociateData* data = associates_.back().get(); |
37 AssociateData* data = associates_.back().get(); | |
38 | 37 |
39 mojo::ConnectToService(app_impl->shell(), url, GetProxy(&data->associate)); | 38 if (connection_error_callback.is_null()) { |
39 // Set it to use our error handler. | |
40 data->associate.set_connection_error_handler( | 40 data->associate.set_connection_error_handler( |
41 base::Bind(connection_error_callback, url)); | 41 base::Bind(&ViewAssociateTable::OnAssociateConnectionError, |
42 base::Unretained(this), data)); | |
43 } | |
42 | 44 |
43 mojo::ui::ViewInspectorPtr inspector; | 45 // Connect the associate to our view inspector. |
44 data->inspector_binding.Bind(GetProxy(&inspector)); | 46 mojo::ui::ViewInspectorPtr inspector_ptr; |
45 data->associate->Connect( | 47 data->inspector_binding.Bind(GetProxy(&inspector_ptr)); |
46 inspector.Pass(), | 48 data->associate->Connect( |
47 base::Bind(&ViewAssociateTable::OnConnected, base::Unretained(this), | 49 inspector_ptr.Pass(), |
48 pending_connection_count_)); | 50 base::Bind(&ViewAssociateTable::OnConnected, base::Unretained(this), |
51 pending_connection_count_)); | |
49 | 52 |
50 pending_connection_count_++; | 53 // Wait for the associate to connect to our view inspector. |
51 } | 54 pending_connection_count_++; |
55 } | |
56 | |
57 void ViewAssociateTable::RegisterViewAssociateWatcher( | |
58 mojo::ui::ViewAssociateWatcherPtr view_associate_watcher) { | |
59 associate_watchers_.push_back(view_associate_watcher.Pass()); | |
jeffbrown
2016/05/17 00:44:49
prefer emplace_back where possible
| |
52 } | 60 } |
53 | 61 |
54 void ViewAssociateTable::ConnectToViewService( | 62 void ViewAssociateTable::ConnectToViewService( |
55 mojo::ui::ViewTokenPtr view_token, | 63 mojo::ui::ViewTokenPtr view_token, |
56 const mojo::String& service_name, | 64 const mojo::String& service_name, |
57 mojo::ScopedMessagePipeHandle client_handle) { | 65 mojo::ScopedMessagePipeHandle client_handle) { |
58 if (pending_connection_count_) { | 66 if (pending_connection_count_) { |
59 deferred_work_.push_back( | 67 deferred_work_.push_back( |
60 base::Bind(&ViewAssociateTable::ConnectToViewService, | 68 base::Bind(&ViewAssociateTable::ConnectToViewService, |
61 base::Unretained(this), base::Passed(view_token.Pass()), | 69 base::Unretained(this), base::Passed(view_token.Pass()), |
62 service_name, base::Passed(client_handle.Pass()))); | 70 service_name, base::Passed(client_handle.Pass()))); |
63 return; | 71 return; |
64 } | 72 } |
65 | 73 |
66 for (auto& data : associates_) { | 74 for (auto& data : associates_) { |
67 DCHECK(data->info); | 75 DCHECK(data->info); |
68 if (Contains(data->info->view_service_names, service_name)) { | 76 if (Contains(data->info->view_service_names, service_name)) { |
69 DVLOG(2) << "Connecting to view service: view_token=" << view_token | 77 DVLOG(2) << "Connecting to view service: view_token=" << view_token |
70 << ", service_name=" << service_name | 78 << ", service_name=" << service_name |
71 << ", associate_url=" << data->url; | 79 << ", associate_label=" << data->label; |
72 DCHECK(data->associate); | 80 DCHECK(data->associate); |
73 data->associate->ConnectToViewService(view_token.Pass(), service_name, | 81 data->associate->ConnectToViewService(view_token.Pass(), service_name, |
74 client_handle.Pass()); | 82 client_handle.Pass()); |
75 return; | 83 return; |
76 } | 84 } |
77 } | 85 } |
78 | 86 |
79 DVLOG(2) << "Requested view service not available: view_token=" << view_token | 87 DVLOG(2) << "Requested view service not available: view_token=" << view_token |
80 << ", service_name=" << service_name; | 88 << ", service_name=" << service_name; |
81 // Allow pipe to be closed as an indication of failure. | 89 // Allow pipe to be closed as an indication of failure. |
82 } | 90 } |
83 | 91 |
92 void ViewAssociateTable::OnAssociateConnectionError( | |
93 AssociateData* associate_data) { | |
94 // Remove associate from our list. | |
95 std::string label; | |
96 for (auto it = associates_.begin(); it != associates_.end(); it++) { | |
97 AssociateData* data = it->get(); | |
98 if (associate_data == data) { | |
99 DVLOG(2) << "ViewAssociate disconnected, removing from table" | |
100 << ", associate_label=" << data->label; | |
101 label = data->label; | |
102 associates_.erase(it); | |
103 break; | |
104 } | |
105 } | |
106 for (auto& view_associate_watcher : associate_watchers_) { | |
107 // TODO(mikejurka) do we even need this callback? | |
108 mojo::Callback<void()> null_callback; | |
109 view_associate_watcher->OnViewAssociateConnectionError(label, | |
110 null_callback); | |
111 } | |
112 } | |
113 | |
84 void ViewAssociateTable::ConnectToViewTreeService( | 114 void ViewAssociateTable::ConnectToViewTreeService( |
85 mojo::ui::ViewTreeTokenPtr view_tree_token, | 115 mojo::ui::ViewTreeTokenPtr view_tree_token, |
86 const mojo::String& service_name, | 116 const mojo::String& service_name, |
87 mojo::ScopedMessagePipeHandle client_handle) { | 117 mojo::ScopedMessagePipeHandle client_handle) { |
88 if (pending_connection_count_) { | 118 if (pending_connection_count_) { |
89 deferred_work_.push_back( | 119 deferred_work_.push_back( |
90 base::Bind(&ViewAssociateTable::ConnectToViewTreeService, | 120 base::Bind(&ViewAssociateTable::ConnectToViewTreeService, |
91 base::Unretained(this), base::Passed(view_tree_token.Pass()), | 121 base::Unretained(this), base::Passed(view_tree_token.Pass()), |
92 service_name, base::Passed(client_handle.Pass()))); | 122 service_name, base::Passed(client_handle.Pass()))); |
93 return; | 123 return; |
94 } | 124 } |
95 | 125 |
96 for (auto& data : associates_) { | 126 for (auto& data : associates_) { |
97 DCHECK(data->info); | 127 DCHECK(data->info); |
98 if (Contains(data->info->view_tree_service_names, service_name)) { | 128 if (Contains(data->info->view_tree_service_names, service_name)) { |
99 DVLOG(2) << "Connecting to view tree service: view_tree_token=" | 129 DVLOG(2) << "Connecting to view tree service: view_tree_token=" |
100 << view_tree_token << ", service_name=" << service_name | 130 << view_tree_token << ", service_name=" << service_name |
101 << ", associate_url=" << data->url; | 131 << ", associate_label=" << data->label; |
102 DCHECK(data->associate); | 132 DCHECK(data->associate); |
103 data->associate->ConnectToViewTreeService( | 133 data->associate->ConnectToViewTreeService( |
104 view_tree_token.Pass(), service_name, client_handle.Pass()); | 134 view_tree_token.Pass(), service_name, client_handle.Pass()); |
105 return; | 135 return; |
106 } | 136 } |
107 } | 137 } |
108 | 138 |
109 DVLOG(2) << "Requested view tree service not available: view_tree_token=" | 139 DVLOG(2) << "Requested view tree service not available: view_tree_token=" |
110 << view_tree_token << ", service_name=" << service_name; | 140 << view_tree_token << ", service_name=" << service_name; |
111 // Allow pipe to be closed as an indication of failure. | 141 // Allow pipe to be closed as an indication of failure. |
112 } | 142 } |
113 | 143 |
114 void ViewAssociateTable::OnConnected(uint32_t index, | 144 void ViewAssociateTable::OnConnected(uint32_t index, |
115 mojo::ui::ViewAssociateInfoPtr info) { | 145 mojo::ui::ViewAssociateInfoPtr info) { |
116 DCHECK(info); | 146 DCHECK(info); |
117 DCHECK(pending_connection_count_); | 147 DCHECK(pending_connection_count_); |
118 DCHECK(!associates_[index]->info); | 148 DCHECK(!associates_[index]->info); |
119 | 149 |
120 DVLOG(1) << "Connected to view associate: url=" << associates_[index]->url | 150 DVLOG(1) << "Connected to view associate: label=" << associates_[index]->label |
121 << ", info=" << info; | 151 << ", info=" << info; |
122 associates_[index]->info = info.Pass(); | 152 associates_[index]->info = info.Pass(); |
123 | 153 |
124 pending_connection_count_--; | 154 pending_connection_count_--; |
125 if (!pending_connection_count_) | 155 if (!pending_connection_count_) |
126 CompleteDeferredWork(); | 156 CompleteDeferredWork(); |
127 } | 157 } |
128 | 158 |
129 void ViewAssociateTable::CompleteDeferredWork() { | 159 void ViewAssociateTable::CompleteDeferredWork() { |
130 DCHECK(!pending_connection_count_); | 160 DCHECK(!pending_connection_count_); |
131 | 161 |
132 for (auto& work : deferred_work_) | 162 for (auto& work : deferred_work_) |
133 work.Run(); | 163 work.Run(); |
134 deferred_work_.clear(); | 164 deferred_work_.clear(); |
135 } | 165 } |
136 | 166 |
167 size_t ViewAssociateTable::associate_count() { | |
168 return associates_.size(); | |
169 } | |
170 | |
137 ViewAssociateTable::AssociateData::AssociateData( | 171 ViewAssociateTable::AssociateData::AssociateData( |
138 const std::string& url, | 172 const std::string& label, |
173 mojo::ui::ViewAssociatePtr associate, | |
139 mojo::ui::ViewInspector* inspector) | 174 mojo::ui::ViewInspector* inspector) |
140 : url(url), inspector_binding(inspector) {} | 175 : label(label), associate(associate.Pass()), inspector_binding(inspector) {} |
141 | 176 |
142 ViewAssociateTable::AssociateData::~AssociateData() {} | 177 ViewAssociateTable::AssociateData::~AssociateData() {} |
143 | 178 |
144 } // namespace view_manager | 179 } // namespace view_manager |
OLD | NEW |