OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/intents/web_intents_registry.h" | 5 #include "chrome/browser/intents/web_intents_registry.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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 const string16& action, | 77 const string16& action, |
78 IntentServiceList* matching_services) { | 78 IntentServiceList* matching_services) { |
79 const IntentServiceList& services = extension.intents_services(); | 79 const IntentServiceList& services = extension.intents_services(); |
80 for (IntentServiceList::const_iterator i = services.begin(); | 80 for (IntentServiceList::const_iterator i = services.begin(); |
81 i != services.end(); ++i) { | 81 i != services.end(); ++i) { |
82 if (action.empty() || action == i->action) | 82 if (action.empty() || action == i->action) |
83 matching_services->push_back(*i); | 83 matching_services->push_back(*i); |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 // Removes all services from |matching_services| that do not match |mimetype|. | 87 // Removes all services from |matching_services| that do not match |type|. |
88 // Wildcards are supported, of the form '<type>/*' or '*'. | 88 // Wildcards are supported, of the form '<type>/*' or '*'. |
89 void FilterServicesByMimetype(const string16& mimetype, | 89 void FilterServicesByType(const string16& type, |
90 IntentServiceList* matching_services) { | 90 IntentServiceList* matching_services) { |
91 // Filter out all services not matching the query type. | 91 // Filter out all services not matching the query type. |
92 IntentServiceList::iterator iter(matching_services->begin()); | 92 IntentServiceList::iterator iter(matching_services->begin()); |
93 while (iter != matching_services->end()) { | 93 while (iter != matching_services->end()) { |
94 if (WebIntentsTypesMatch(iter->type, mimetype)) | 94 if (WebIntentsTypesMatch(iter->type, type)) |
95 ++iter; | 95 ++iter; |
96 else | 96 else |
97 iter = matching_services->erase(iter); | 97 iter = matching_services->erase(iter); |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 // Callback for existence checks. Converts a callback for a list of services | 101 // Callback for existence checks. Converts a callback for a list of services |
102 // into a callback that returns true if the list contains a specific service. | 102 // into a callback that returns true if the list contains a specific service. |
103 void ExistenceCallback(const webkit_glue::WebIntentServiceData& service, | 103 void ExistenceCallback(const webkit_glue::WebIntentServiceData& service, |
104 const base::Callback<void(bool)>& callback, | 104 const base::Callback<void(bool)>& callback, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 (lhs.action != rhs.action) || | 144 (lhs.action != rhs.action) || |
145 (lhs.title != rhs.title) || | 145 (lhs.title != rhs.title) || |
146 (lhs.disposition != rhs.disposition)); | 146 (lhs.disposition != rhs.disposition)); |
147 } | 147 } |
148 | 148 |
149 } // namespace | 149 } // namespace |
150 | 150 |
151 using webkit_glue::WebIntentServiceData; | 151 using webkit_glue::WebIntentServiceData; |
152 | 152 |
153 // Internal object representing all data associated with a single query. | 153 // Internal object representing all data associated with a single query. |
154 struct WebIntentsRegistry::IntentsQuery { | 154 struct WebIntentsRegistry::IntentsQuery : public WebDataServiceConsumer { |
155 | |
156 // Handle so we can call back into the WebIntentsRegistry when | |
157 // processing query results. The registry is guaranteed to be | |
158 // valid for the life of this object. We do not own this object. | |
159 WebIntentsRegistry* registry_; | |
160 | |
155 // Underlying data query. | 161 // Underlying data query. |
156 WebDataService::Handle pending_query_; | 162 WebDataService::Handle query_handle_; |
157 | 163 |
158 // The callback for this particular query. | 164 // The callback for this particular query. |
159 QueryCallback callback_; | 165 QueryCallback callback_; |
160 | 166 |
161 // Callback for a query for defaults. | 167 // Callback for a query for defaults. |
162 DefaultQueryCallback default_callback_; | 168 DefaultQueryCallback default_callback_; |
163 | 169 |
164 // The particular action to filter for while searching through extensions. | 170 // The particular action to filter for while searching through extensions. |
165 // If |action_| is empty, return all extension-provided services. | 171 // If |action_| is empty, return all extension-provided services. |
166 string16 action_; | 172 string16 action_; |
167 | 173 |
168 // The MIME type that was requested for this service query. | 174 // The MIME type that was requested for this service query. |
169 // Suppports wild cards. | 175 // Suppports wild cards. |
170 string16 type_; | 176 string16 type_; |
171 | 177 |
172 // The url of the invoking page. | 178 // The url of the invoking page. |
173 GURL url_; | 179 GURL url_; |
174 | 180 |
175 // Create a new IntentsQuery for services with the specified action/type. | 181 // Create a new IntentsQuery for services with the specified action/type. |
176 IntentsQuery(const QueryCallback& callback, | 182 IntentsQuery(WebIntentsRegistry* registry, |
183 const QueryCallback& callback, | |
177 const string16& action, const string16& type) | 184 const string16& action, const string16& type) |
178 : callback_(callback), action_(action), type_(type) {} | 185 : registry_(registry), callback_(callback), action_(action), |
186 type_(type) {} | |
179 | 187 |
180 // Create a new IntentsQuery for all intent services or for existence checks. | 188 // Create a new IntentsQuery for all intent services or for existence checks. |
181 explicit IntentsQuery(const QueryCallback callback) | 189 IntentsQuery(WebIntentsRegistry* registry, |
182 : callback_(callback), type_(ASCIIToUTF16("*")) {} | 190 const QueryCallback callback) |
191 : registry_(registry), callback_(callback), type_(ASCIIToUTF16("*")) {} | |
183 | 192 |
184 // Create a new IntentsQuery for default services. | 193 // Create a new IntentsQuery for default services. |
185 IntentsQuery(const DefaultQueryCallback& callback, | 194 IntentsQuery(WebIntentsRegistry* registry, |
195 const DefaultQueryCallback& callback, | |
186 const string16& action, const string16& type, const GURL& url) | 196 const string16& action, const string16& type, const GURL& url) |
187 : default_callback_(callback), action_(action), type_(type), url_(url) {} | 197 : registry_(registry), default_callback_(callback), action_(action), |
198 type_(type), url_(url) {} | |
199 | |
200 void OnWebDataServiceRequestDone( | |
201 WebDataService::Handle h, | |
202 const WDTypedResult* result) OVERRIDE { | |
203 | |
204 // dispatch the request | |
205 if (result->GetType() == WEB_INTENTS_RESULT) { | |
206 registry_->OnWebIntentsResultReceived(this, result); | |
207 } else if (result->GetType() == WEB_INTENTS_DEFAULTS_RESULT) { | |
208 registry_->OnWebIntentsDefaultsResultReceived(this, result); | |
209 } else { | |
210 NOTREACHED(); | |
211 } | |
212 } | |
188 }; | 213 }; |
189 | 214 |
190 WebIntentsRegistry::WebIntentsRegistry() {} | 215 WebIntentsRegistry::WebIntentsRegistry() {} |
191 | 216 |
192 WebIntentsRegistry::~WebIntentsRegistry() { | 217 WebIntentsRegistry::~WebIntentsRegistry() { |
218 | |
193 // Cancel all pending queries, since we can't handle them any more. | 219 // Cancel all pending queries, since we can't handle them any more. |
194 for (QueryMap::iterator it(queries_.begin()); it != queries_.end(); ++it) { | 220 for (QueryVector::iterator it = pending_queries_.begin(); |
195 wds_->CancelRequest(it->first); | 221 it != pending_queries_.end(); ++it) { |
196 delete it->second; | 222 IntentsQuery* query = *it; |
223 wds_->CancelRequest(query->query_handle_); | |
224 delete query; | |
197 } | 225 } |
198 } | 226 } |
199 | 227 |
200 void WebIntentsRegistry::Initialize( | 228 void WebIntentsRegistry::Initialize( |
201 scoped_refptr<WebDataService> wds, | 229 scoped_refptr<WebDataService> wds, |
202 ExtensionServiceInterface* extension_service) { | 230 ExtensionServiceInterface* extension_service) { |
203 wds_ = wds; | 231 wds_ = wds; |
204 extension_service_ = extension_service; | 232 extension_service_ = extension_service; |
205 } | 233 } |
206 | 234 |
207 void WebIntentsRegistry::OnWebDataServiceRequestDone( | 235 void WebIntentsRegistry::OnWebIntentsResultReceived( |
208 WebDataService::Handle h, | 236 IntentsQuery* query, |
209 const WDTypedResult* result) { | 237 const WDTypedResult* result) { |
238 DCHECK(query); | |
210 DCHECK(result); | 239 DCHECK(result); |
211 if (result->GetType() == WEB_INTENTS_DEFAULTS_RESULT) { | |
212 OnWebDataServiceDefaultsRequestDone(h, result); | |
213 return; | |
214 } | |
215 DCHECK(result->GetType() == WEB_INTENTS_RESULT); | 240 DCHECK(result->GetType() == WEB_INTENTS_RESULT); |
216 | 241 |
217 QueryMap::iterator it = queries_.find(h); | 242 bool released = ReleaseQuery(query); |
218 DCHECK(it != queries_.end()); | 243 DCHECK(released); |
219 | |
220 IntentsQuery* query(it->second); | |
221 DCHECK(query); | |
222 queries_.erase(it); | |
223 | 244 |
224 IntentServiceList matching_services = static_cast< | 245 IntentServiceList matching_services = static_cast< |
225 const WDResult<IntentServiceList>*>(result)->GetValue(); | 246 const WDResult<IntentServiceList>*>(result)->GetValue(); |
226 | 247 |
227 // Loop over all services in all extensions, collect ones | 248 // Loop over all services in all extensions, collect ones |
228 // matching the query. | 249 // matching the query. |
229 if (extension_service_) { | 250 if (extension_service_) { |
230 const ExtensionSet* extensions = extension_service_->extensions(); | 251 const ExtensionSet* extensions = extension_service_->extensions(); |
231 if (extensions) { | 252 if (extensions) { |
232 for (ExtensionSet::const_iterator i(extensions->begin()); | 253 for (ExtensionSet::const_iterator i(extensions->begin()); |
233 i != extensions->end(); ++i) { | 254 i != extensions->end(); ++i) { |
234 AddMatchingServicesForExtension(**i, query->action_, | 255 AddMatchingServicesForExtension(**i, query->action_, |
235 &matching_services); | 256 &matching_services); |
236 } | 257 } |
237 } | 258 } |
238 } | 259 } |
239 | 260 |
240 // Filter out all services not matching the query type. | 261 // Filter out all services not matching the query type. |
241 FilterServicesByMimetype(query->type_, &matching_services); | 262 FilterServicesByType(query->type_, &matching_services); |
242 | 263 |
243 // Collapse intents that are equivalent for all but |type|. | 264 // Collapse intents that are equivalent for all but |type|. |
244 CollapseIntents(&matching_services); | 265 CollapseIntents(&matching_services); |
245 | 266 |
246 query->callback_.Run(matching_services); | 267 query->callback_.Run(matching_services); |
247 delete query; | 268 delete query; |
248 } | 269 } |
249 | 270 |
250 const Extension* WebIntentsRegistry::ExtensionForURL(const std::string& url) { | 271 void WebIntentsRegistry::OnWebIntentsDefaultsResultReceived( |
251 const ExtensionSet* extensions = extension_service_->extensions(); | 272 IntentsQuery* query, |
252 if (!extensions) | 273 const WDTypedResult* result) { |
253 return NULL; | 274 DCHECK(query); |
275 DCHECK(result); | |
276 DCHECK(result->GetType() == WEB_INTENTS_DEFAULTS_RESULT); | |
254 | 277 |
255 // Use the unsafe ExtensionURLInfo constructor: we don't care if the extension | 278 bool released = ReleaseQuery(query); |
256 // is running or not. | 279 DCHECK(released); |
257 GURL gurl(url); | |
258 ExtensionURLInfo info(gurl); | |
259 return extensions->GetExtensionOrAppByURL(info); | |
260 } | |
261 | |
262 void WebIntentsRegistry::OnWebDataServiceDefaultsRequestDone( | |
263 WebDataService::Handle h, | |
264 const WDTypedResult* result) { | |
265 QueryMap::iterator it = queries_.find(h); | |
266 DCHECK(it != queries_.end()); | |
267 | |
268 IntentsQuery* query(it->second); | |
269 DCHECK(query); | |
270 queries_.erase(it); | |
271 | 280 |
272 std::vector<DefaultWebIntentService> services = static_cast< | 281 std::vector<DefaultWebIntentService> services = static_cast< |
273 const WDResult<std::vector<DefaultWebIntentService> >*>(result)-> | 282 const WDResult<std::vector<DefaultWebIntentService> >*>(result)-> |
274 GetValue(); | 283 GetValue(); |
275 | 284 |
276 DefaultWebIntentService default_service; | 285 DefaultWebIntentService default_service; |
277 std::vector<DefaultWebIntentService>::iterator iter(services.begin()); | 286 std::vector<DefaultWebIntentService>::iterator iter(services.begin()); |
278 for (; iter != services.end(); ++iter) { | 287 for (; iter != services.end(); ++iter) { |
279 if (!WebIntentsTypesMatch(iter->type, query->type_)) { | 288 if (!WebIntentsTypesMatch(iter->type, query->type_)) { |
280 continue; | 289 continue; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 break; | 327 break; |
319 } | 328 } |
320 } | 329 } |
321 } | 330 } |
322 | 331 |
323 query->default_callback_.Run(default_service); | 332 query->default_callback_.Run(default_service); |
324 delete query; | 333 delete query; |
325 } | 334 } |
326 | 335 |
327 void WebIntentsRegistry::GetIntentServices( | 336 void WebIntentsRegistry::GetIntentServices( |
328 const string16& action, const string16& mimetype, | 337 const string16& action, const string16& type, |
329 const QueryCallback& callback) { | 338 const QueryCallback& callback) { |
330 DCHECK(wds_.get()); | 339 DCHECK(wds_.get()); |
331 DCHECK(!callback.is_null()); | 340 DCHECK(!callback.is_null()); |
332 | 341 |
333 IntentsQuery* query = new IntentsQuery(callback, action, mimetype); | 342 IntentsQuery* query = new IntentsQuery(this, callback, action, type); |
334 query->pending_query_ = wds_->GetWebIntentServices(action, this); | 343 query->query_handle_ = wds_->GetWebIntentServices(action, query); |
335 queries_[query->pending_query_] = query; | 344 TrackQuery(query); |
336 } | 345 } |
337 | 346 |
338 void WebIntentsRegistry::GetAllIntentServices( | 347 void WebIntentsRegistry::GetAllIntentServices( |
339 const QueryCallback& callback) { | 348 const QueryCallback& callback) { |
340 DCHECK(wds_.get()); | 349 DCHECK(wds_.get()); |
341 DCHECK(!callback.is_null()); | 350 DCHECK(!callback.is_null()); |
342 | 351 |
343 IntentsQuery* query = new IntentsQuery(callback); | 352 IntentsQuery* query = new IntentsQuery(this, callback); |
344 query->pending_query_ = wds_->GetAllWebIntentServices(this); | 353 query->query_handle_ = wds_->GetAllWebIntentServices(query); |
345 queries_[query->pending_query_] = query; | 354 TrackQuery(query); |
346 } | 355 } |
347 | 356 |
348 void WebIntentsRegistry::IntentServiceExists( | 357 void WebIntentsRegistry::IntentServiceExists( |
349 const WebIntentServiceData& service, | 358 const WebIntentServiceData& service, |
350 const base::Callback<void(bool)>& callback) { | 359 const base::Callback<void(bool)>& callback) { |
351 DCHECK(!callback.is_null()); | 360 DCHECK(!callback.is_null()); |
352 | 361 |
353 IntentsQuery* query = new IntentsQuery( | 362 IntentsQuery* query = new IntentsQuery( |
354 base::Bind(&ExistenceCallback, service, callback)); | 363 this, base::Bind(&ExistenceCallback, service, callback)); |
355 query->pending_query_ = wds_->GetWebIntentServicesForURL( | 364 query->query_handle_ = wds_->GetWebIntentServicesForURL( |
356 UTF8ToUTF16(service.service_url.spec()), this); | 365 UTF8ToUTF16(service.service_url.spec()), query); |
357 queries_[query->pending_query_] = query; | 366 TrackQuery(query); |
358 } | 367 } |
359 | 368 |
360 void WebIntentsRegistry::GetIntentServicesForExtensionFilter( | 369 void WebIntentsRegistry::GetIntentServicesForExtensionFilter( |
361 const string16& action, | 370 const string16& action, |
362 const string16& mimetype, | 371 const string16& type, |
363 const std::string& extension_id, | 372 const std::string& extension_id, |
364 const QueryCallback& callback) { | 373 const QueryCallback& callback) { |
365 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 374 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
366 DCHECK(!callback.is_null()); | 375 DCHECK(!callback.is_null()); |
367 | 376 |
377 // This isn't a WDS query, so we don't track it, | |
378 // or claim the query later. | |
368 scoped_ptr<IntentsQuery> query( | 379 scoped_ptr<IntentsQuery> query( |
369 new IntentsQuery(callback, action, mimetype)); | 380 new IntentsQuery(this, callback, action, type)); |
370 content::BrowserThread::PostTask( | 381 content::BrowserThread::PostTask( |
371 content::BrowserThread::UI, | 382 content::BrowserThread::UI, |
372 FROM_HERE, | 383 FROM_HERE, |
373 base::Bind(&WebIntentsRegistry::DoGetIntentServicesForExtensionFilter, | 384 base::Bind(&WebIntentsRegistry::DoGetIntentServicesForExtensionFilter, |
374 base::Unretained(this), | 385 base::Unretained(this), |
375 base::Passed(&query), extension_id)); | 386 base::Passed(&query), extension_id)); |
376 } | 387 } |
377 | 388 |
378 void WebIntentsRegistry::DoGetIntentServicesForExtensionFilter( | 389 void WebIntentsRegistry::DoGetIntentServicesForExtensionFilter( |
379 scoped_ptr<IntentsQuery> query, | 390 scoped_ptr<IntentsQuery> query, |
380 const std::string& extension_id) { | 391 const std::string& extension_id) { |
381 IntentServiceList matching_services; | 392 IntentServiceList matching_services; |
382 | 393 |
383 if (extension_service_) { | 394 if (extension_service_) { |
384 const Extension* extension = | 395 const Extension* extension = |
385 extension_service_->GetExtensionById(extension_id, false); | 396 extension_service_->GetExtensionById(extension_id, false); |
386 AddMatchingServicesForExtension(*extension, | 397 AddMatchingServicesForExtension(*extension, |
387 query->action_, | 398 query->action_, |
388 &matching_services); | 399 &matching_services); |
389 FilterServicesByMimetype(query->type_, &matching_services); | 400 FilterServicesByType(query->type_, &matching_services); |
390 } | 401 } |
391 | 402 |
392 query->callback_.Run(matching_services); | 403 query->callback_.Run(matching_services); |
393 } | 404 } |
394 | 405 |
395 void WebIntentsRegistry::RegisterDefaultIntentService( | 406 void WebIntentsRegistry::RegisterDefaultIntentService( |
396 const DefaultWebIntentService& default_service) { | 407 const DefaultWebIntentService& default_service) { |
397 DCHECK(wds_.get()); | 408 DCHECK(wds_.get()); |
398 wds_->AddDefaultWebIntentService(default_service); | 409 wds_->AddDefaultWebIntentService(default_service); |
399 } | 410 } |
400 | 411 |
401 void WebIntentsRegistry::UnregisterDefaultIntentService( | 412 void WebIntentsRegistry::UnregisterDefaultIntentService( |
402 const DefaultWebIntentService& default_service) { | 413 const DefaultWebIntentService& default_service) { |
403 DCHECK(wds_.get()); | 414 DCHECK(wds_.get()); |
404 wds_->RemoveDefaultWebIntentService(default_service); | 415 wds_->RemoveDefaultWebIntentService(default_service); |
405 } | 416 } |
406 | 417 |
407 void WebIntentsRegistry::GetDefaultIntentService( | 418 void WebIntentsRegistry::GetDefaultIntentService( |
408 const string16& action, | 419 const string16& action, |
409 const string16& type, | 420 const string16& type, |
410 const GURL& invoking_url, | 421 const GURL& invoking_url, |
411 const DefaultQueryCallback& callback) { | 422 const DefaultQueryCallback& callback) { |
412 DCHECK(!callback.is_null()); | 423 DCHECK(!callback.is_null()); |
413 | 424 |
414 IntentsQuery* query = | 425 IntentsQuery* query = |
415 new IntentsQuery(callback, action, type, invoking_url); | 426 new IntentsQuery(this, callback, action, type, invoking_url); |
416 query->pending_query_ = | 427 query->query_handle_ = |
417 wds_->GetDefaultWebIntentServicesForAction(action, this); | 428 wds_->GetDefaultWebIntentServicesForAction(action, query); |
418 queries_[query->pending_query_] = query; | 429 TrackQuery(query); |
419 } | 430 } |
420 | 431 |
421 void WebIntentsRegistry::RegisterIntentService( | 432 void WebIntentsRegistry::RegisterIntentService( |
422 const WebIntentServiceData& service) { | 433 const WebIntentServiceData& service) { |
423 DCHECK(wds_.get()); | 434 DCHECK(wds_.get()); |
424 wds_->AddWebIntentService(service); | 435 wds_->AddWebIntentService(service); |
425 } | 436 } |
426 | 437 |
427 void WebIntentsRegistry::UnregisterIntentService( | 438 void WebIntentsRegistry::UnregisterIntentService( |
428 const WebIntentServiceData& service) { | 439 const WebIntentServiceData& service) { |
(...skipping 24 matching lines...) Expand all Loading... | |
453 if (write_iter != read_iter) | 464 if (write_iter != read_iter) |
454 *write_iter = *read_iter; | 465 *write_iter = *read_iter; |
455 } | 466 } |
456 ++read_iter; | 467 ++read_iter; |
457 } | 468 } |
458 | 469 |
459 // Cut off everything after the last intent copied to the list. | 470 // Cut off everything after the last intent copied to the list. |
460 if (++write_iter != services->end()) | 471 if (++write_iter != services->end()) |
461 services->erase(write_iter, services->end()); | 472 services->erase(write_iter, services->end()); |
462 } | 473 } |
474 | |
475 const Extension* WebIntentsRegistry::ExtensionForURL(const std::string& url) { | |
476 const ExtensionSet* extensions = extension_service_->extensions(); | |
477 if (!extensions) | |
478 return NULL; | |
479 | |
480 // Use the unsafe ExtensionURLInfo constructor: we don't care if the extension | |
481 // is running or not. | |
482 GURL gurl(url); | |
483 ExtensionURLInfo info(gurl); | |
484 return extensions->GetExtensionOrAppByURL(info); | |
485 } | |
486 | |
487 void WebIntentsRegistry::TrackQuery(IntentsQuery* query) { | |
488 DCHECK(query); | |
489 pending_queries_.push_back(query); | |
490 } | |
491 | |
492 bool WebIntentsRegistry::ReleaseQuery(IntentsQuery* query) { | |
493 DCHECK(query); | |
groby-ooo-7-16
2012/07/27 01:17:04
Also, no need to DCHECK. This works fine with NULL
Steve McKay
2012/07/27 01:30:42
Done.
| |
494 QueryVector::iterator it = std::find( | |
495 pending_queries_.begin(), pending_queries_.end(), query); | |
496 if (it != pending_queries_.end()) { | |
497 pending_queries_.erase(it); | |
498 return true; | |
499 } else { | |
500 return false; | |
501 } | |
502 } | |
OLD | NEW |