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 14 matching lines...) Expand all Loading... |
25 | 25 |
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 ConnectAssociate(app_impl, inspector, url); |
36 associates_.emplace_back(new AssociateData(url, inspector)); | 36 } |
37 AssociateData* data = associates_.back().get(); | 37 } |
38 | 38 |
39 mojo::ConnectToService(app_impl->shell(), url, GetProxy(&data->associate)); | 39 void ViewAssociateTable::ConnectAssociate(mojo::ApplicationImpl* app_impl, |
40 data->associate.set_connection_error_handler( | 40 mojo::ui::ViewInspector* inspector, |
41 base::Bind(connection_error_callback, url)); | 41 const std::string& url) { |
| 42 DCHECK(app_impl); |
| 43 DCHECK(inspector); |
42 | 44 |
43 mojo::ui::ViewInspectorPtr inspector; | 45 DVLOG(1) << "Connecting to view associate: url=" << url; |
44 data->inspector_binding.Bind(GetProxy(&inspector)); | |
45 data->associate->Connect( | |
46 inspector.Pass(), | |
47 base::Bind(&ViewAssociateTable::OnConnected, base::Unretained(this), | |
48 pending_connection_count_)); | |
49 | 46 |
50 pending_connection_count_++; | 47 mojo::ui::ViewAssociatePtr associate; |
51 } | 48 mojo::ConnectToService(app_impl->shell(), url, GetProxy(&associate)); |
| 49 |
| 50 // Wire up the associate to us |
| 51 RegisterViewAssociate(inspector, associate.Pass()); |
| 52 } |
| 53 |
| 54 void ViewAssociateTable::RegisterViewAssociate( |
| 55 mojo::ui::ViewInspector* inspector, |
| 56 mojo::ui::ViewAssociatePtr associate) { |
| 57 DCHECK(inspector); |
| 58 DCHECK(associate.is_bound()); |
| 59 |
| 60 associates_.emplace_back(new AssociateData(associate.Pass(), inspector)); |
| 61 AssociateData* data = associates_.back().get(); |
| 62 |
| 63 // set it to use our error handler |
| 64 data->associate.set_connection_error_handler( |
| 65 base::Bind(&ViewAssociateTable::OnAssociateConnectionError, |
| 66 base::Unretained(this), data)); |
| 67 |
| 68 // connect the associate to our view inspector |
| 69 mojo::ui::ViewInspectorPtr inspector_ptr; |
| 70 data->inspector_binding.Bind(GetProxy(&inspector_ptr)); |
| 71 data->associate->Connect( |
| 72 inspector_ptr.Pass(), |
| 73 base::Bind(&ViewAssociateTable::OnConnected, base::Unretained(this), |
| 74 pending_connection_count_)); |
| 75 |
| 76 // wait for the associate to connect to our view inspector |
| 77 pending_connection_count_++; |
52 } | 78 } |
53 | 79 |
54 void ViewAssociateTable::ConnectToViewService( | 80 void ViewAssociateTable::ConnectToViewService( |
55 mojo::ui::ViewTokenPtr view_token, | 81 mojo::ui::ViewTokenPtr view_token, |
56 const mojo::String& service_name, | 82 const mojo::String& service_name, |
57 mojo::ScopedMessagePipeHandle client_handle) { | 83 mojo::ScopedMessagePipeHandle client_handle) { |
58 if (pending_connection_count_) { | 84 if (pending_connection_count_) { |
59 deferred_work_.push_back( | 85 deferred_work_.push_back( |
60 base::Bind(&ViewAssociateTable::ConnectToViewService, | 86 base::Bind(&ViewAssociateTable::ConnectToViewService, |
61 base::Unretained(this), base::Passed(view_token.Pass()), | 87 base::Unretained(this), base::Passed(view_token.Pass()), |
62 service_name, base::Passed(client_handle.Pass()))); | 88 service_name, base::Passed(client_handle.Pass()))); |
63 return; | 89 return; |
64 } | 90 } |
65 | 91 |
66 for (auto& data : associates_) { | 92 for (auto& data : associates_) { |
67 DCHECK(data->info); | 93 DCHECK(data->info); |
68 if (Contains(data->info->view_service_names, service_name)) { | 94 if (Contains(data->info->view_service_names, service_name)) { |
69 DVLOG(2) << "Connecting to view service: view_token=" << view_token | 95 DVLOG(2) << "Connecting to view service: view_token=" << view_token |
70 << ", service_name=" << service_name | 96 << ", service_name=" << service_name; |
71 << ", associate_url=" << data->url; | |
72 DCHECK(data->associate); | 97 DCHECK(data->associate); |
73 data->associate->ConnectToViewService(view_token.Pass(), service_name, | 98 data->associate->ConnectToViewService(view_token.Pass(), service_name, |
74 client_handle.Pass()); | 99 client_handle.Pass()); |
| 100 |
75 return; | 101 return; |
76 } | 102 } |
77 } | 103 } |
78 | 104 |
79 DVLOG(2) << "Requested view service not available: view_token=" << view_token | 105 DVLOG(2) << "Requested view service not available: view_token=" << view_token |
80 << ", service_name=" << service_name; | 106 << ", service_name=" << service_name; |
81 // Allow pipe to be closed as an indication of failure. | 107 // Allow pipe to be closed as an indication of failure. |
82 } | 108 } |
83 | 109 |
| 110 void ViewAssociateTable::OnAssociateConnectionError( |
| 111 AssociateData* associate_data) { |
| 112 DVLOG(1) << "Exiting due to associate connection error."; |
| 113 |
| 114 // la, la de daaa..... |
| 115 for (auto it = associates_.begin(); it != associates_.end(); it++) { |
| 116 AssociateData* data = it->get(); |
| 117 // DCHECK(data->info); |
| 118 if (associate_data == data) { |
| 119 DVLOG(2) << "found it."; |
| 120 associates_.erase(it); |
| 121 break; |
| 122 } |
| 123 } |
| 124 } |
| 125 |
84 void ViewAssociateTable::ConnectToViewTreeService( | 126 void ViewAssociateTable::ConnectToViewTreeService( |
85 mojo::ui::ViewTreeTokenPtr view_tree_token, | 127 mojo::ui::ViewTreeTokenPtr view_tree_token, |
86 const mojo::String& service_name, | 128 const mojo::String& service_name, |
87 mojo::ScopedMessagePipeHandle client_handle) { | 129 mojo::ScopedMessagePipeHandle client_handle) { |
88 if (pending_connection_count_) { | 130 if (pending_connection_count_) { |
89 deferred_work_.push_back( | 131 deferred_work_.push_back( |
90 base::Bind(&ViewAssociateTable::ConnectToViewTreeService, | 132 base::Bind(&ViewAssociateTable::ConnectToViewTreeService, |
91 base::Unretained(this), base::Passed(view_tree_token.Pass()), | 133 base::Unretained(this), base::Passed(view_tree_token.Pass()), |
92 service_name, base::Passed(client_handle.Pass()))); | 134 service_name, base::Passed(client_handle.Pass()))); |
93 return; | 135 return; |
94 } | 136 } |
95 | 137 |
96 for (auto& data : associates_) { | 138 for (auto& data : associates_) { |
97 DCHECK(data->info); | 139 DCHECK(data->info); |
98 if (Contains(data->info->view_tree_service_names, service_name)) { | 140 if (Contains(data->info->view_tree_service_names, service_name)) { |
99 DVLOG(2) << "Connecting to view tree service: view_tree_token=" | 141 DVLOG(2) << "Connecting to view tree service: view_tree_token=" |
100 << view_tree_token << ", service_name=" << service_name | 142 << view_tree_token << ", service_name=" << service_name; |
101 << ", associate_url=" << data->url; | |
102 DCHECK(data->associate); | 143 DCHECK(data->associate); |
103 data->associate->ConnectToViewTreeService( | 144 data->associate->ConnectToViewTreeService( |
104 view_tree_token.Pass(), service_name, client_handle.Pass()); | 145 view_tree_token.Pass(), service_name, client_handle.Pass()); |
105 return; | 146 return; |
106 } | 147 } |
107 } | 148 } |
108 | 149 |
109 DVLOG(2) << "Requested view tree service not available: view_tree_token=" | 150 DVLOG(2) << "Requested view tree service not available: view_tree_token=" |
110 << view_tree_token << ", service_name=" << service_name; | 151 << view_tree_token << ", service_name=" << service_name; |
111 // Allow pipe to be closed as an indication of failure. | 152 // Allow pipe to be closed as an indication of failure. |
112 } | 153 } |
113 | 154 |
114 void ViewAssociateTable::OnConnected(uint32_t index, | 155 void ViewAssociateTable::OnConnected(uint32_t index, |
115 mojo::ui::ViewAssociateInfoPtr info) { | 156 mojo::ui::ViewAssociateInfoPtr info) { |
116 DCHECK(info); | 157 DCHECK(info); |
117 DCHECK(pending_connection_count_); | 158 DCHECK(pending_connection_count_); |
118 DCHECK(!associates_[index]->info); | 159 DCHECK(!associates_[index]->info); |
119 | 160 |
120 DVLOG(1) << "Connected to view associate: url=" << associates_[index]->url | 161 DVLOG(1) << "Connected to view associate, info=" << info; |
121 << ", info=" << info; | |
122 associates_[index]->info = info.Pass(); | 162 associates_[index]->info = info.Pass(); |
123 | 163 |
124 pending_connection_count_--; | 164 pending_connection_count_--; |
125 if (!pending_connection_count_) | 165 if (!pending_connection_count_) |
126 CompleteDeferredWork(); | 166 CompleteDeferredWork(); |
127 } | 167 } |
128 | 168 |
129 void ViewAssociateTable::CompleteDeferredWork() { | 169 void ViewAssociateTable::CompleteDeferredWork() { |
130 DCHECK(!pending_connection_count_); | 170 DCHECK(!pending_connection_count_); |
131 | 171 |
132 for (auto& work : deferred_work_) | 172 for (auto& work : deferred_work_) |
133 work.Run(); | 173 work.Run(); |
134 deferred_work_.clear(); | 174 deferred_work_.clear(); |
135 } | 175 } |
136 | 176 |
137 ViewAssociateTable::AssociateData::AssociateData( | 177 ViewAssociateTable::AssociateData::AssociateData( |
138 const std::string& url, | 178 mojo::ui::ViewAssociatePtr associate, |
139 mojo::ui::ViewInspector* inspector) | 179 mojo::ui::ViewInspector* inspector) |
140 : url(url), inspector_binding(inspector) {} | 180 : associate(associate.Pass()), inspector_binding(inspector) {} |
141 | 181 |
142 ViewAssociateTable::AssociateData::~AssociateData() {} | 182 ViewAssociateTable::AssociateData::~AssociateData() {} |
143 | 183 |
144 } // namespace view_manager | 184 } // namespace view_manager |
OLD | NEW |