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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
// Copyright (c) 2012 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/intents/web_intents_registry.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/intents/default_web_intent_service.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_set.h"
#include "googleurl/src/gurl.h"
#include "net/base/mime_util.h"
namespace {
typedef WebIntentsRegistry::IntentServiceList IntentServiceList;
// Compares two mime types for equality. Supports wild cards in both
// |type1| and |type2|. Wild cards are of the form '<type>/*' or '*'.
bool MimeTypesAreEqual(const string16& type1, const string16& type2) {
// We don't have a MIME matcher that allows patterns on both sides
// Instead, we do two comparisons, treating each type in turn as a
// pattern. If either one matches, we consider this a MIME match.
if (net::MatchesMimeType(UTF16ToUTF8(type1), UTF16ToUTF8(type2)))
return true;
return net::MatchesMimeType(UTF16ToUTF8(type2), UTF16ToUTF8(type1));
}
// Adds any intent services of |extension| that match |action| to
// |matching_services|.
void AddMatchingServicesForExtension(const Extension& extension,
const string16& action,
IntentServiceList* matching_services) {
const IntentServiceList& services = extension.intents_services();
for (IntentServiceList::const_iterator i = services.begin();
i != services.end(); ++i) {
if (action.empty() || action == i->action)
matching_services->push_back(*i);
}
}
// Removes all services from |matching_services| that do not match |mimetype|.
// Wildcards are supported, of the form '<type>/*' or '*'.
void FilterServicesByMimetype(const string16& mimetype,
IntentServiceList* matching_services) {
// Filter out all services not matching the query type.
IntentServiceList::iterator iter(matching_services->begin());
while (iter != matching_services->end()) {
if (MimeTypesAreEqual(iter->type, mimetype))
++iter;
else
iter = matching_services->erase(iter);
}
}
} // namespace
using webkit_glue::WebIntentServiceData;
// Internal object representing all data associated with a single query.
struct WebIntentsRegistry::IntentsQuery {
// Unique query identifier.
QueryID query_id_;
// Underlying data query.
WebDataService::Handle pending_query_;
// The consumer for this particular query.
Consumer* consumer_;
// The particular action to filter for while searching through extensions.
// If |action_| is empty, return all extension-provided services.
string16 action_;
// The MIME type that was requested for this service query.
// Suppports wild cards.
string16 type_;
// The url of the invoking page.
GURL url_;
// Create a new IntentsQuery for services with the specified action/type.
IntentsQuery(QueryID id, Consumer* consumer,
const string16& action, const string16& type)
: query_id_(id), consumer_(consumer), action_(action), type_(type) {}
// Create a new IntentsQuery for all intent services or for existence checks.
IntentsQuery(QueryID id, Consumer* consumer)
: query_id_(id), consumer_(consumer), type_(ASCIIToUTF16("*")) {}
// Create a new IntentsQuery for default services.
IntentsQuery(QueryID id, Consumer* consumer,
const string16& action, const string16& type, const GURL& url)
: query_id_(id), consumer_(consumer),
action_(action), type_(type), url_(url) {}
};
WebIntentsRegistry::WebIntentsRegistry() : next_query_id_(0) {}
WebIntentsRegistry::~WebIntentsRegistry() {
// Cancel all pending queries, since we can't handle them any more.
for (QueryMap::iterator it(queries_.begin()); it != queries_.end(); ++it) {
wds_->CancelRequest(it->first);
delete it->second;
}
}
void WebIntentsRegistry::Initialize(
scoped_refptr<WebDataService> wds,
ExtensionServiceInterface* extension_service) {
wds_ = wds;
extension_service_ = extension_service;
}
void WebIntentsRegistry::OnWebDataServiceRequestDone(
WebDataService::Handle h,
const WDTypedResult* result) {
DCHECK(result);
if (result->GetType() == WEB_INTENTS_DEFAULTS_RESULT) {
OnWebDataServiceDefaultsRequestDone(h, result);
return;
}
DCHECK(result->GetType() == WEB_INTENTS_RESULT);
QueryMap::iterator it = queries_.find(h);
DCHECK(it != queries_.end());
IntentsQuery* query(it->second);
DCHECK(query);
queries_.erase(it);
IntentServiceList matching_services = static_cast<
const WDResult<IntentServiceList>*>(result)->GetValue();
// Loop over all services in all extensions, collect ones
// matching the query.
if (extension_service_) {
const ExtensionSet* extensions = extension_service_->extensions();
if (extensions) {
for (ExtensionSet::const_iterator i(extensions->begin());
i != extensions->end(); ++i) {
AddMatchingServicesForExtension(**i, query->action_,
&matching_services);
}
}
}
// Filter out all services not matching the query type.
FilterServicesByMimetype(query->type_, &matching_services);
query->consumer_->OnIntentsQueryDone(query->query_id_, matching_services);
delete query;
}
const Extension* WebIntentsRegistry::ExtensionForURL(const std::string& url) {
const ExtensionSet* extensions = extension_service_->extensions();
if (!extensions)
return NULL;
// Use the unsafe ExtensionURLInfo constructor: we don't care if the extension
// is running or not.
GURL gurl(url);
ExtensionURLInfo info(gurl);
return extensions->GetExtensionOrAppByURL(info);
}
void WebIntentsRegistry::OnWebDataServiceDefaultsRequestDone(
WebDataService::Handle h,
const WDTypedResult* result) {
QueryMap::iterator it = queries_.find(h);
DCHECK(it != queries_.end());
IntentsQuery* query(it->second);
DCHECK(query);
queries_.erase(it);
std::vector<DefaultWebIntentService> services = static_cast<
const WDResult<std::vector<DefaultWebIntentService> >*>(result)->
GetValue();
DefaultWebIntentService default_service;
std::vector<DefaultWebIntentService>::iterator iter(services.begin());
for (; iter != services.end(); ++iter) {
if (!MimeTypesAreEqual(iter->type, query->type_)) {
continue;
}
if (!iter->url_pattern.MatchesURL(query->url_)) {
continue;
}
const Extension* extension = ExtensionForURL(iter->service_url);
if (extension != NULL &&
!extension_service_->IsExtensionEnabled(extension->id())) {
continue;
}
// Found a match. If it is better than default_service, use it.
// Currently the metric is that if the new value is user-set,
// prefer it. If the present value is suppressed, prefer it.
// If the new value has a more specific pattern, prefer it.
if (default_service.user_date <= 0 && iter->user_date >= 0)
default_service = *iter;
else if (default_service.suppression > 0 && iter->suppression <= 0)
default_service = *iter;
else if (default_service.url_pattern.match_all_urls() &&
!iter->url_pattern.match_all_urls())
default_service = *iter;
else if (iter->url_pattern < default_service.url_pattern)
default_service = *iter;
}
query->consumer_->OnIntentsDefaultsQueryDone(query->query_id_,
default_service);
delete query;
}
WebIntentsRegistry::QueryID WebIntentsRegistry::GetIntentServices(
const string16& action, const string16& mimetype, Consumer* consumer) {
DCHECK(consumer);
DCHECK(wds_.get());
IntentsQuery* query =
new IntentsQuery(next_query_id_++, consumer, action, mimetype);
query->pending_query_ = wds_->GetWebIntentServices(action, this);
queries_[query->pending_query_] = query;
return query->query_id_;
}
WebIntentsRegistry::QueryID WebIntentsRegistry::GetAllIntentServices(
Consumer* consumer) {
DCHECK(consumer);
DCHECK(wds_.get());
IntentsQuery* query = new IntentsQuery(next_query_id_++, consumer);
query->pending_query_ = wds_->GetAllWebIntentServices(this);
queries_[query->pending_query_] = query;
return query->query_id_;
}
// Trampoline consumer for calls to IntentServiceExists. Forwards existence
// of the provided |service| to the provided |callback|.
class ServiceCheckConsumer : public WebIntentsRegistry::Consumer {
public:
ServiceCheckConsumer(const WebIntentServiceData& service,
const base::Callback<void(bool)>& callback)
: callback_(callback),
service_(service) {}
virtual ~ServiceCheckConsumer() {}
// Gets the list of all services for a particular action. Check them all
// to see if |service_| is already registered.
virtual void OnIntentsQueryDone(
WebIntentsRegistry::QueryID id,
const WebIntentsRegistry::IntentServiceList& list) OVERRIDE {
scoped_ptr<ServiceCheckConsumer> self_deleter(this);
for (WebIntentsRegistry::IntentServiceList::const_iterator i = list.begin();
i != list.end(); ++i) {
if (*i == service_) {
callback_.Run(true);
return;
}
}
callback_.Run(false);
}
virtual void OnIntentsDefaultsQueryDone(
WebIntentsRegistry::QueryID query_id,
const DefaultWebIntentService& default_service) {}
private:
base::Callback<void(bool)> callback_;
WebIntentServiceData service_;
};
WebIntentsRegistry::QueryID WebIntentsRegistry::IntentServiceExists(
const WebIntentServiceData& service,
const base::Callback<void(bool)>& callback) {
IntentsQuery* query = new IntentsQuery(
next_query_id_++, new ServiceCheckConsumer(service, callback));
query->pending_query_ = wds_->GetWebIntentServicesForURL(
UTF8ToUTF16(service.service_url.spec()), this);
queries_[query->pending_query_] = query;
return query->query_id_;
}
WebIntentsRegistry::QueryID
WebIntentsRegistry::GetIntentServicesForExtensionFilter(
const string16& action,
const string16& mimetype,
const std::string& extension_id,
Consumer* consumer) {
DCHECK(consumer);
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
scoped_ptr<IntentsQuery> query(
new IntentsQuery(next_query_id_++, consumer, action, mimetype));
int query_id = query->query_id_;
content::BrowserThread::PostTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&WebIntentsRegistry::DoGetIntentServicesForExtensionFilter,
base::Unretained(this),
base::Passed(&query), extension_id));
return query_id;
}
void WebIntentsRegistry::DoGetIntentServicesForExtensionFilter(
scoped_ptr<IntentsQuery> query,
const std::string& extension_id) {
IntentServiceList matching_services;
if (extension_service_) {
const Extension* extension =
extension_service_->GetExtensionById(extension_id, false);
AddMatchingServicesForExtension(*extension,
query->action_,
&matching_services);
FilterServicesByMimetype(query->type_, &matching_services);
}
query->consumer_->OnIntentsQueryDone(query->query_id_, matching_services);
}
void WebIntentsRegistry::RegisterDefaultIntentService(
const DefaultWebIntentService& default_service) {
DCHECK(wds_.get());
wds_->AddDefaultWebIntentService(default_service);
}
void WebIntentsRegistry::UnregisterDefaultIntentService(
const DefaultWebIntentService& default_service) {
DCHECK(wds_.get());
wds_->RemoveDefaultWebIntentService(default_service);
}
WebIntentsRegistry::QueryID WebIntentsRegistry::GetDefaultIntentService(
const string16& action,
const string16& type,
const GURL& invoking_url,
Consumer* consumer) {
IntentsQuery* query =
new IntentsQuery(next_query_id_++, consumer, action, type, invoking_url);
query->pending_query_ =
wds_->GetDefaultWebIntentServicesForAction(action, this);
queries_[query->pending_query_] = query;
return query->query_id_;
}
void WebIntentsRegistry::RegisterIntentService(
const WebIntentServiceData& service) {
DCHECK(wds_.get());
wds_->AddWebIntentService(service);
}
void WebIntentsRegistry::UnregisterIntentService(
const WebIntentServiceData& service) {
DCHECK(wds_.get());
wds_->RemoveWebIntentService(service);
}
|