1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/gcd_private/gcd_private_api.h"
#include "base/lazy_instance.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/local_discovery/cloud_device_list.h"
#include "chrome/browser/local_discovery/cloud_print_printer_list.h"
#include "chrome/browser/local_discovery/gcd_constants.h"
#include "chrome/browser/local_discovery/privet_device_lister_impl.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/signin/core/browser/signin_manager_base.h"
namespace extensions {
namespace gcd_private = api::gcd_private;
namespace {
scoped_ptr<Event> MakeCloudDeviceStateChangedEvent(
bool available,
const gcd_private::GCDDevice& device) {
scoped_ptr<base::ListValue> params =
gcd_private::OnCloudDeviceStateChanged::Create(available, device);
scoped_ptr<Event> event(new Event(
gcd_private::OnCloudDeviceStateChanged::kEventName, params.Pass()));
return event.Pass();
}
const int kNumRequestsNeeded = 2;
const char kIDPrefixCloudPrinter[] = "cloudprint:";
const char kIDPrefixGcd[] = "gcd:";
const char kIDPrefixMdns[] = "mdns:";
GcdPrivateAPI::GCDApiFlowFactoryForTests* g_gcd_api_flow_factory = NULL;
base::LazyInstance<BrowserContextKeyedAPIFactory<GcdPrivateAPI> > g_factory =
LAZY_INSTANCE_INITIALIZER;
scoped_ptr<local_discovery::GCDApiFlow> MakeGCDApiFlow(Profile* profile) {
if (g_gcd_api_flow_factory) {
return g_gcd_api_flow_factory->CreateGCDApiFlow();
}
ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
if (!token_service)
return scoped_ptr<local_discovery::GCDApiFlow>();
SigninManagerBase* signin_manager =
SigninManagerFactory::GetInstance()->GetForProfile(profile);
if (!signin_manager)
return scoped_ptr<local_discovery::GCDApiFlow>();
return local_discovery::GCDApiFlow::Create(
profile->GetRequestContext(),
token_service,
signin_manager->GetAuthenticatedAccountId());
}
} // namespace
GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context)
: num_device_listeners_(0), browser_context_(context) {
DCHECK(browser_context_);
EventRouter::Get(context)->RegisterObserver(
this, gcd_private::OnCloudDeviceStateChanged::kEventName);
}
GcdPrivateAPI::~GcdPrivateAPI() {
}
// static
BrowserContextKeyedAPIFactory<GcdPrivateAPI>*
GcdPrivateAPI::GetFactoryInstance() {
return g_factory.Pointer();
}
void GcdPrivateAPI::OnListenerAdded(const EventListenerInfo& details) {
num_device_listeners_++;
if (num_device_listeners_ == 1) {
service_discovery_client_ =
local_discovery::ServiceDiscoverySharedClient::GetInstance();
privet_device_lister_.reset(new local_discovery::PrivetDeviceListerImpl(
service_discovery_client_.get(), this));
privet_device_lister_->Start();
}
for (GCDDeviceMap::iterator i = known_devices_.begin();
i != known_devices_.end();
i++) {
EventRouter::Get(browser_context_)->DispatchEventToExtension(
details.extension_id,
MakeCloudDeviceStateChangedEvent(true, *i->second));
}
}
void GcdPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) {
num_device_listeners_--;
if (num_device_listeners_ == 0) {
privet_device_lister_.reset();
service_discovery_client_ = NULL;
}
}
void GcdPrivateAPI::DeviceChanged(
bool added,
const std::string& name,
const local_discovery::DeviceDescription& description) {
linked_ptr<gcd_private::GCDDevice> device(new gcd_private::GCDDevice);
device->setup_type = gcd_private::SETUP_TYPE_MDNS;
device->id_string = kIDPrefixMdns + name;
device->device_type = description.type;
device->device_name = description.name;
device->device_description = description.description;
if (!description.id.empty())
device->cloud_id.reset(new std::string(description.id));
known_devices_[device->id_string] = device;
EventRouter::Get(browser_context_)
->BroadcastEvent(MakeCloudDeviceStateChangedEvent(true, *device));
}
void GcdPrivateAPI::DeviceRemoved(const std::string& name) {
GCDDeviceMap::iterator found = known_devices_.find(kIDPrefixMdns + name);
linked_ptr<gcd_private::GCDDevice> device = found->second;
known_devices_.erase(found);
EventRouter::Get(browser_context_)
->BroadcastEvent(MakeCloudDeviceStateChangedEvent(false, *device));
}
void GcdPrivateAPI::DeviceCacheFlushed() {
for (GCDDeviceMap::iterator i = known_devices_.begin();
i != known_devices_.end();
i++) {
EventRouter::Get(browser_context_)
->BroadcastEvent(MakeCloudDeviceStateChangedEvent(false, *i->second));
}
known_devices_.clear();
}
// static
void GcdPrivateAPI::SetGCDApiFlowFactoryForTests(
GCDApiFlowFactoryForTests* factory) {
g_gcd_api_flow_factory = factory;
}
GcdPrivateGetCloudDeviceListFunction::GcdPrivateGetCloudDeviceListFunction() {
}
GcdPrivateGetCloudDeviceListFunction::~GcdPrivateGetCloudDeviceListFunction() {
}
bool GcdPrivateGetCloudDeviceListFunction::RunAsync() {
requests_succeeded_ = 0;
requests_failed_ = 0;
printer_list_ = MakeGCDApiFlow(GetProfile());
device_list_ = MakeGCDApiFlow(GetProfile());
if (!printer_list_ || !device_list_)
return false;
// Balanced in CheckListingDone()
AddRef();
printer_list_->Start(make_scoped_ptr<local_discovery::GCDApiFlow::Request>(
new local_discovery::CloudPrintPrinterList(this)));
device_list_->Start(make_scoped_ptr<local_discovery::GCDApiFlow::Request>(
new local_discovery::CloudDeviceList(this)));
return true;
}
void GcdPrivateGetCloudDeviceListFunction::OnDeviceListReady(
const DeviceList& devices) {
requests_succeeded_++;
devices_.insert(devices_.end(), devices.begin(), devices.end());
CheckListingDone();
}
void GcdPrivateGetCloudDeviceListFunction::OnDeviceListUnavailable() {
requests_failed_++;
CheckListingDone();
}
void GcdPrivateGetCloudDeviceListFunction::CheckListingDone() {
if (requests_failed_ + requests_succeeded_ != kNumRequestsNeeded)
return;
if (requests_succeeded_ == 0) {
SendResponse(false);
return;
}
std::vector<linked_ptr<gcd_private::GCDDevice> > devices;
for (DeviceList::iterator i = devices_.begin(); i != devices_.end(); i++) {
linked_ptr<gcd_private::GCDDevice> device(new gcd_private::GCDDevice);
device->setup_type = gcd_private::SETUP_TYPE_CLOUD;
if (i->type == local_discovery::kGCDTypePrinter) {
device->id_string = kIDPrefixCloudPrinter + i->id;
} else {
device->id_string = kIDPrefixGcd + i->id;
}
device->cloud_id.reset(new std::string(i->id));
device->device_type = i->type;
device->device_name = i->display_name;
device->device_description = i->description;
devices.push_back(device);
}
results_ = gcd_private::GetCloudDeviceList::Results::Create(devices);
SendResponse(true);
Release();
}
} // namespace extensions
|