OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/printing/cloud_print/privet_notifications.h" | 5 #include "chrome/browser/printing/cloud_print/privet_notifications.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 privet_http_factory_.swap(privet_http_factory); | 84 privet_http_factory_.swap(privet_http_factory); |
85 } | 85 } |
86 | 86 |
87 PrivetNotificationsListener::~PrivetNotificationsListener() { | 87 PrivetNotificationsListener::~PrivetNotificationsListener() { |
88 } | 88 } |
89 | 89 |
90 void PrivetNotificationsListener::DeviceChanged( | 90 void PrivetNotificationsListener::DeviceChanged( |
91 const std::string& name, | 91 const std::string& name, |
92 const DeviceDescription& description) { | 92 const DeviceDescription& description) { |
93 ReportPrivetUmaEvent(PRIVET_DEVICE_CHANGED); | 93 ReportPrivetUmaEvent(PRIVET_DEVICE_CHANGED); |
94 DeviceContextMap::iterator found = devices_seen_.find(name); | 94 DeviceContextMap::iterator it = devices_seen_.find(name); |
95 if (found != devices_seen_.end()) { | 95 if (it != devices_seen_.end()) { |
96 if (!description.id.empty() && // Device is registered | 96 if (!description.id.empty() && // Device is registered |
97 found->second->notification_may_be_active) { | 97 it->second->notification_may_be_active) { |
98 found->second->notification_may_be_active = false; | 98 it->second->notification_may_be_active = false; |
99 NotifyDeviceRemoved(); | 99 NotifyDeviceRemoved(); |
100 } | 100 } |
101 return; // Already saw this device. | 101 return; // Already saw this device. |
102 } | 102 } |
103 | 103 |
104 std::unique_ptr<DeviceContext>& device_context = devices_seen_[name]; | 104 std::unique_ptr<DeviceContext>& device_context = devices_seen_[name]; |
105 device_context.reset(new DeviceContext); | 105 device_context.reset(new DeviceContext); |
106 device_context->notification_may_be_active = false; | 106 device_context->notification_may_be_active = false; |
107 device_context->registered = !description.id.empty(); | 107 device_context->registered = !description.id.empty(); |
108 | 108 |
109 if (!device_context->registered) { | 109 if (!device_context->registered) { |
110 device_context->privet_http_resolution = | 110 device_context->privet_http_resolution = |
111 privet_http_factory_->CreatePrivetHTTP(name); | 111 privet_http_factory_->CreatePrivetHTTP(name); |
112 device_context->privet_http_resolution->Start( | 112 device_context->privet_http_resolution->Start( |
113 description.address, | 113 description.address, |
114 base::Bind(&PrivetNotificationsListener::CreateInfoOperation, | 114 base::Bind(&PrivetNotificationsListener::CreateInfoOperation, |
115 base::Unretained(this))); | 115 base::Unretained(this))); |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 void PrivetNotificationsListener::CreateInfoOperation( | 119 void PrivetNotificationsListener::CreateInfoOperation( |
120 std::unique_ptr<PrivetHTTPClient> http_client) { | 120 std::unique_ptr<PrivetHTTPClient> http_client) { |
121 if (!http_client) { | 121 // Do nothing if resolution fails. |
122 // Do nothing if resolution fails. | 122 if (!http_client) |
123 return; | 123 return; |
124 } | |
125 | 124 |
126 std::string name = http_client->GetName(); | 125 std::string name = http_client->GetName(); |
127 DeviceContextMap::iterator device_iter = devices_seen_.find(name); | 126 DeviceContextMap::iterator it = devices_seen_.find(name); |
128 if (device_iter == devices_seen_.end()) | 127 if (it == devices_seen_.end()) |
129 return; | 128 return; |
130 DeviceContext* device = device_iter->second.get(); | 129 |
130 DeviceContext* device = it->second.get(); | |
131 device->privet_http.swap(http_client); | 131 device->privet_http.swap(http_client); |
132 device->info_operation = device->privet_http->CreateInfoOperation( | 132 device->info_operation = device->privet_http->CreateInfoOperation( |
133 base::Bind(&PrivetNotificationsListener::OnPrivetInfoDone, | 133 base::Bind(&PrivetNotificationsListener::OnPrivetInfoDone, |
134 base::Unretained(this), | 134 base::Unretained(this), |
135 device)); | 135 device)); |
136 device->info_operation->Start(); | 136 device->info_operation->Start(); |
137 } | 137 } |
138 | 138 |
139 void PrivetNotificationsListener::OnPrivetInfoDone( | 139 void PrivetNotificationsListener::OnPrivetInfoDone( |
140 DeviceContext* device, | 140 DeviceContext* device, |
141 const base::DictionaryValue* json_value) { | 141 const base::DictionaryValue* json_value) { |
142 int uptime; | 142 int uptime; |
143 | 143 |
144 if (!json_value || | 144 if (!json_value || |
145 !json_value->GetInteger(kPrivetInfoKeyUptime, &uptime) || | 145 !json_value->GetInteger(kPrivetInfoKeyUptime, &uptime) || |
146 uptime > kTenMinutesInSeconds) { | 146 uptime > kTenMinutesInSeconds) { |
147 return; | 147 return; |
148 } | 148 } |
149 | 149 |
150 DCHECK(!device->notification_may_be_active); | 150 DCHECK(!device->notification_may_be_active); |
151 device->notification_may_be_active = true; | 151 device->notification_may_be_active = true; |
152 devices_active_++; | 152 devices_active_++; |
153 delegate_->PrivetNotify(devices_active_, true); | 153 delegate_->PrivetNotify(devices_active_, true); |
154 } | 154 } |
155 | 155 |
156 void PrivetNotificationsListener::DeviceRemoved(const std::string& name) { | 156 void PrivetNotificationsListener::DeviceRemoved(const std::string& name) { |
157 DeviceContextMap::iterator device_iter = devices_seen_.find(name); | 157 DeviceContextMap::iterator it = devices_seen_.find(name); |
158 if (device_iter == devices_seen_.end()) | 158 if (it == devices_seen_.end()) |
159 return; | 159 return; |
160 DeviceContext* device = device_iter->second.get(); | |
161 | 160 |
161 DeviceContext* device = it->second.get(); | |
162 device->info_operation.reset(); | 162 device->info_operation.reset(); |
163 device->privet_http_resolution.reset(); | 163 device->privet_http_resolution.reset(); |
164 device->notification_may_be_active = false; | 164 device->notification_may_be_active = false; |
165 NotifyDeviceRemoved(); | 165 NotifyDeviceRemoved(); |
166 } | 166 } |
167 | 167 |
168 void PrivetNotificationsListener::DeviceCacheFlushed() { | 168 void PrivetNotificationsListener::DeviceCacheFlushed() { |
169 for (DeviceContextMap::iterator i = devices_seen_.begin(); | 169 for (const auto& it : devices_seen_) { |
170 i != devices_seen_.end(); ++i) { | 170 DeviceContext* device = it.second.get(); |
171 DeviceContext* device = i->second.get(); | |
172 | 171 |
173 device->info_operation.reset(); | 172 device->info_operation.reset(); |
174 device->privet_http_resolution.reset(); | 173 device->privet_http_resolution.reset(); |
175 if (device->notification_may_be_active) { | 174 if (device->notification_may_be_active) { |
Lei Zhang
2016/06/01 02:15:17
This is an unneeded check, removed in the next pat
| |
176 device->notification_may_be_active = false; | 175 device->notification_may_be_active = false; |
177 } | 176 } |
178 } | 177 } |
179 | 178 |
180 devices_active_ = 0; | 179 devices_active_ = 0; |
181 delegate_->PrivetRemoveNotification(); | 180 delegate_->PrivetRemoveNotification(); |
182 } | 181 } |
183 | 182 |
184 void PrivetNotificationsListener::NotifyDeviceRemoved() { | 183 void PrivetNotificationsListener::NotifyDeviceRemoved() { |
185 devices_active_--; | 184 devices_active_--; |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 | 383 |
385 void PrivetNotificationDelegate::DisableNotifications() { | 384 void PrivetNotificationDelegate::DisableNotifications() { |
386 Profile* profile_obj = Profile::FromBrowserContext(profile_); | 385 Profile* profile_obj = Profile::FromBrowserContext(profile_); |
387 | 386 |
388 profile_obj->GetPrefs()->SetBoolean( | 387 profile_obj->GetPrefs()->SetBoolean( |
389 prefs::kLocalDiscoveryNotificationsEnabled, | 388 prefs::kLocalDiscoveryNotificationsEnabled, |
390 false); | 389 false); |
391 } | 390 } |
392 | 391 |
393 } // namespace cloud_print | 392 } // namespace cloud_print |
OLD | NEW |