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" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 void ViewAssociateTable::ConnectAssociates( | 26 void ViewAssociateTable::ConnectAssociates( |
27 mojo::ApplicationImpl* app_impl, | 27 mojo::ApplicationImpl* app_impl, |
28 mojo::ui::ViewInspector* inspector, | 28 mojo::ui::ViewInspector* inspector, |
29 const std::vector<std::string>& urls, | 29 const std::vector<std::string>& urls, |
30 const AssociateConnectionErrorCallback& connection_error_callback) { | 30 const AssociateConnectionErrorCallback& connection_error_callback) { |
31 DCHECK(app_impl); | 31 DCHECK(app_impl); |
32 DCHECK(inspector); | 32 DCHECK(inspector); |
33 | 33 |
34 for (auto& url : urls) { | 34 for (auto& url : urls) { |
35 DVLOG(1) << "Connecting to view associate: url=" << url; | 35 DVLOG(1) << "Connecting to view associate: url=" << url; |
36 associates_.emplace_back(new AssociateData(url, inspector)); | |
37 AssociateData* data = associates_.back().get(); | |
38 | 36 |
39 mojo::ConnectToService(app_impl->shell(), url, GetProxy(&data->associate)); | 37 mojo::ui::ViewAssociatePtr associate; |
40 data->associate.set_connection_error_handler( | 38 mojo::ConnectToService(app_impl->shell(), url, GetProxy(&associate)); |
41 base::Bind(connection_error_callback, url)); | |
42 | 39 |
43 mojo::ui::ViewInspectorPtr inspector; | 40 // Wire up the associate to us |
44 data->inspector_binding.Bind(GetProxy(&inspector)); | 41 RegisterViewAssociate(inspector, associate.Pass(), |
45 data->associate->Connect( | 42 connection_error_callback); |
46 inspector.Pass(), | |
47 base::Bind(&ViewAssociateTable::OnConnected, base::Unretained(this), | |
48 pending_connection_count_)); | |
49 | |
50 pending_connection_count_++; | |
51 } | 43 } |
52 } | 44 } |
53 | 45 |
46 void ViewAssociateTable::RegisterViewAssociate( | |
47 mojo::ui::ViewInspector* inspector, | |
48 mojo::ui::ViewAssociatePtr associate, | |
49 const AssociateConnectionErrorCallback& connection_error_callback) { | |
50 DCHECK(inspector); | |
51 DCHECK(associate.is_bound()); | |
52 | |
53 associates_.emplace_back(new AssociateData(associate.Pass(), inspector)); | |
jeffbrown
2016/05/11 23:44:19
for debugging, it might be useful if each associat
mikejurka
2016/05/16 23:35:18
how would we get/generate the label? should it be
| |
54 AssociateData* data = associates_.back().get(); | |
55 | |
56 if (connection_error_callback.is_null()) { | |
jeffbrown
2016/05/11 23:44:20
I think this check is going to go away when we mov
mikejurka
2016/05/16 23:35:18
follow-up since the existing code should still wor
| |
57 // set it to use our error handler | |
58 data->associate.set_connection_error_handler( | |
59 base::Bind(&ViewAssociateTable::OnAssociateConnectionError, | |
60 base::Unretained(this), data)); | |
61 } | |
62 | |
63 // connect the associate to our view inspector | |
jeffbrown
2016/05/11 23:44:19
style nit: Treat comments as complete sentences.
mikejurka
2016/05/16 23:35:18
Done.
| |
64 mojo::ui::ViewInspectorPtr inspector_ptr; | |
65 data->inspector_binding.Bind(GetProxy(&inspector_ptr)); | |
66 data->associate->Connect( | |
67 inspector_ptr.Pass(), | |
68 base::Bind(&ViewAssociateTable::OnConnected, base::Unretained(this), | |
69 pending_connection_count_)); | |
70 | |
71 // wait for the associate to connect to our view inspector | |
jeffbrown
2016/05/11 23:44:16
Technically we're waiting for it to give us back i
| |
72 pending_connection_count_++; | |
73 } | |
74 | |
54 void ViewAssociateTable::ConnectToViewService( | 75 void ViewAssociateTable::ConnectToViewService( |
55 mojo::ui::ViewTokenPtr view_token, | 76 mojo::ui::ViewTokenPtr view_token, |
56 const mojo::String& service_name, | 77 const mojo::String& service_name, |
57 mojo::ScopedMessagePipeHandle client_handle) { | 78 mojo::ScopedMessagePipeHandle client_handle) { |
58 if (pending_connection_count_) { | 79 if (pending_connection_count_) { |
59 deferred_work_.push_back( | 80 deferred_work_.push_back( |
60 base::Bind(&ViewAssociateTable::ConnectToViewService, | 81 base::Bind(&ViewAssociateTable::ConnectToViewService, |
61 base::Unretained(this), base::Passed(view_token.Pass()), | 82 base::Unretained(this), base::Passed(view_token.Pass()), |
62 service_name, base::Passed(client_handle.Pass()))); | 83 service_name, base::Passed(client_handle.Pass()))); |
63 return; | 84 return; |
64 } | 85 } |
65 | 86 |
66 for (auto& data : associates_) { | 87 for (auto& data : associates_) { |
67 DCHECK(data->info); | 88 DCHECK(data->info); |
68 if (Contains(data->info->view_service_names, service_name)) { | 89 if (Contains(data->info->view_service_names, service_name)) { |
69 DVLOG(2) << "Connecting to view service: view_token=" << view_token | 90 DVLOG(2) << "Connecting to view service: view_token=" << view_token |
70 << ", service_name=" << service_name | 91 << ", service_name=" << service_name; |
71 << ", associate_url=" << data->url; | |
72 DCHECK(data->associate); | 92 DCHECK(data->associate); |
73 data->associate->ConnectToViewService(view_token.Pass(), service_name, | 93 data->associate->ConnectToViewService(view_token.Pass(), service_name, |
74 client_handle.Pass()); | 94 client_handle.Pass()); |
95 | |
75 return; | 96 return; |
76 } | 97 } |
77 } | 98 } |
78 | 99 |
79 DVLOG(2) << "Requested view service not available: view_token=" << view_token | 100 DVLOG(2) << "Requested view service not available: view_token=" << view_token |
80 << ", service_name=" << service_name; | 101 << ", service_name=" << service_name; |
81 // Allow pipe to be closed as an indication of failure. | 102 // Allow pipe to be closed as an indication of failure. |
82 } | 103 } |
83 | 104 |
105 void ViewAssociateTable::OnAssociateConnectionError( | |
106 AssociateData* associate_data) { | |
107 // Remove associate from our list | |
108 for (auto it = associates_.begin(); it != associates_.end(); it++) { | |
109 AssociateData* data = it->get(); | |
110 if (associate_data == data) { | |
111 DVLOG(2) << "ViewAssociate disconnected, removing from table"; | |
112 associates_.erase(it); | |
113 break; | |
114 } | |
115 } | |
116 } | |
117 | |
84 void ViewAssociateTable::ConnectToViewTreeService( | 118 void ViewAssociateTable::ConnectToViewTreeService( |
85 mojo::ui::ViewTreeTokenPtr view_tree_token, | 119 mojo::ui::ViewTreeTokenPtr view_tree_token, |
86 const mojo::String& service_name, | 120 const mojo::String& service_name, |
87 mojo::ScopedMessagePipeHandle client_handle) { | 121 mojo::ScopedMessagePipeHandle client_handle) { |
88 if (pending_connection_count_) { | 122 if (pending_connection_count_) { |
89 deferred_work_.push_back( | 123 deferred_work_.push_back( |
90 base::Bind(&ViewAssociateTable::ConnectToViewTreeService, | 124 base::Bind(&ViewAssociateTable::ConnectToViewTreeService, |
91 base::Unretained(this), base::Passed(view_tree_token.Pass()), | 125 base::Unretained(this), base::Passed(view_tree_token.Pass()), |
92 service_name, base::Passed(client_handle.Pass()))); | 126 service_name, base::Passed(client_handle.Pass()))); |
93 return; | 127 return; |
94 } | 128 } |
95 | 129 |
96 for (auto& data : associates_) { | 130 for (auto& data : associates_) { |
97 DCHECK(data->info); | 131 DCHECK(data->info); |
98 if (Contains(data->info->view_tree_service_names, service_name)) { | 132 if (Contains(data->info->view_tree_service_names, service_name)) { |
99 DVLOG(2) << "Connecting to view tree service: view_tree_token=" | 133 DVLOG(2) << "Connecting to view tree service: view_tree_token=" |
100 << view_tree_token << ", service_name=" << service_name | 134 << view_tree_token << ", service_name=" << service_name; |
101 << ", associate_url=" << data->url; | |
jeffbrown
2016/05/11 23:44:20
Yeah, it would be really useful to be able to prin
| |
102 DCHECK(data->associate); | 135 DCHECK(data->associate); |
103 data->associate->ConnectToViewTreeService( | 136 data->associate->ConnectToViewTreeService( |
104 view_tree_token.Pass(), service_name, client_handle.Pass()); | 137 view_tree_token.Pass(), service_name, client_handle.Pass()); |
105 return; | 138 return; |
106 } | 139 } |
107 } | 140 } |
108 | 141 |
109 DVLOG(2) << "Requested view tree service not available: view_tree_token=" | 142 DVLOG(2) << "Requested view tree service not available: view_tree_token=" |
110 << view_tree_token << ", service_name=" << service_name; | 143 << view_tree_token << ", service_name=" << service_name; |
111 // Allow pipe to be closed as an indication of failure. | 144 // Allow pipe to be closed as an indication of failure. |
112 } | 145 } |
113 | 146 |
114 void ViewAssociateTable::OnConnected(uint32_t index, | 147 void ViewAssociateTable::OnConnected(uint32_t index, |
115 mojo::ui::ViewAssociateInfoPtr info) { | 148 mojo::ui::ViewAssociateInfoPtr info) { |
116 DCHECK(info); | 149 DCHECK(info); |
117 DCHECK(pending_connection_count_); | 150 DCHECK(pending_connection_count_); |
118 DCHECK(!associates_[index]->info); | 151 DCHECK(!associates_[index]->info); |
119 | 152 |
120 DVLOG(1) << "Connected to view associate: url=" << associates_[index]->url | 153 DVLOG(1) << "Connected to view associate, info=" << info; |
121 << ", info=" << info; | |
122 associates_[index]->info = info.Pass(); | 154 associates_[index]->info = info.Pass(); |
123 | 155 |
124 pending_connection_count_--; | 156 pending_connection_count_--; |
125 if (!pending_connection_count_) | 157 if (!pending_connection_count_) |
126 CompleteDeferredWork(); | 158 CompleteDeferredWork(); |
127 } | 159 } |
128 | 160 |
129 void ViewAssociateTable::CompleteDeferredWork() { | 161 void ViewAssociateTable::CompleteDeferredWork() { |
130 DCHECK(!pending_connection_count_); | 162 DCHECK(!pending_connection_count_); |
131 | 163 |
132 for (auto& work : deferred_work_) | 164 for (auto& work : deferred_work_) |
133 work.Run(); | 165 work.Run(); |
134 deferred_work_.clear(); | 166 deferred_work_.clear(); |
135 } | 167 } |
136 | 168 |
137 ViewAssociateTable::AssociateData::AssociateData( | 169 ViewAssociateTable::AssociateData::AssociateData( |
138 const std::string& url, | 170 mojo::ui::ViewAssociatePtr associate, |
139 mojo::ui::ViewInspector* inspector) | 171 mojo::ui::ViewInspector* inspector) |
140 : url(url), inspector_binding(inspector) {} | 172 : associate(associate.Pass()), inspector_binding(inspector) {} |
141 | 173 |
142 ViewAssociateTable::AssociateData::~AssociateData() {} | 174 ViewAssociateTable::AssociateData::~AssociateData() {} |
143 | 175 |
144 } // namespace view_manager | 176 } // namespace view_manager |
OLD | NEW |