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 133 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 WebIntentsRegistry* registry_; | |
groby-ooo-7-16
2012/07/27 00:11:26
Please document variable and ownership. (I.e. I as
Steve McKay
2012/07/27 00:54:46
Done.
| |
157 | |
155 // Underlying data query. | 158 // Underlying data query. |
156 WebDataService::Handle pending_query_; | 159 WebDataService::Handle query_handle_; |
157 | 160 |
158 // The callback for this particular query. | 161 // The callback for this particular query. |
159 QueryCallback callback_; | 162 QueryCallback callback_; |
160 | 163 |
161 // Callback for a query for defaults. | 164 // Callback for a query for defaults. |
162 DefaultQueryCallback default_callback_; | 165 DefaultQueryCallback default_callback_; |
163 | 166 |
164 // The particular action to filter for while searching through extensions. | 167 // The particular action to filter for while searching through extensions. |
165 // If |action_| is empty, return all extension-provided services. | 168 // If |action_| is empty, return all extension-provided services. |
166 string16 action_; | 169 string16 action_; |
167 | 170 |
168 // The MIME type that was requested for this service query. | 171 // The MIME type that was requested for this service query. |
169 // Suppports wild cards. | 172 // Suppports wild cards. |
170 string16 type_; | 173 string16 type_; |
171 | 174 |
172 // The url of the invoking page. | 175 // The url of the invoking page. |
173 GURL url_; | 176 GURL url_; |
174 | 177 |
175 // Create a new IntentsQuery for services with the specified action/type. | 178 // Create a new IntentsQuery for services with the specified action/type. |
176 IntentsQuery(const QueryCallback& callback, | 179 IntentsQuery(WebIntentsRegistry* registry, |
180 const QueryCallback& callback, | |
177 const string16& action, const string16& type) | 181 const string16& action, const string16& type) |
178 : callback_(callback), action_(action), type_(type) {} | 182 : registry_(registry), callback_(callback), action_(action), |
183 type_(type) {} | |
179 | 184 |
180 // Create a new IntentsQuery for all intent services or for existence checks. | 185 // Create a new IntentsQuery for all intent services or for existence checks. |
181 explicit IntentsQuery(const QueryCallback callback) | 186 IntentsQuery(WebIntentsRegistry* registry, |
182 : callback_(callback), type_(ASCIIToUTF16("*")) {} | 187 const QueryCallback callback) |
188 : registry_(registry), callback_(callback), type_(ASCIIToUTF16("*")) {} | |
183 | 189 |
184 // Create a new IntentsQuery for default services. | 190 // Create a new IntentsQuery for default services. |
185 IntentsQuery(const DefaultQueryCallback& callback, | 191 IntentsQuery(WebIntentsRegistry* registry, |
192 const DefaultQueryCallback& callback, | |
186 const string16& action, const string16& type, const GURL& url) | 193 const string16& action, const string16& type, const GURL& url) |
187 : default_callback_(callback), action_(action), type_(type), url_(url) {} | 194 : registry_(registry), default_callback_(callback), action_(action), |
195 type_(type), url_(url) {} | |
196 | |
197 void OnWebDataServiceRequestDone( | |
198 WebDataService::Handle h, | |
199 const WDTypedResult* result) OVERRIDE { | |
200 | |
201 // dispatch the request | |
202 if (result->GetType() == WEB_INTENTS_RESULT) { | |
groby-ooo-7-16
2012/07/27 00:11:26
Why is this not a callback? This way we can save o
Greg Billock
2012/07/27 00:41:29
We discussed doing that. With that design, we'd ne
Steve McKay
2012/07/27 00:54:46
IntentsQuery will be decomposed into sub-classes i
| |
203 registry_->OnWebIntentsResultReceived(this, h, result); | |
204 } else if (result->GetType() == WEB_INTENTS_DEFAULTS_RESULT) { | |
205 registry_->OnWebIntentsDefaultsResultReceived(this, h, result); | |
206 } else { | |
207 NOTREACHED(); | |
208 } | |
209 } | |
188 }; | 210 }; |
189 | 211 |
190 WebIntentsRegistry::WebIntentsRegistry() {} | 212 WebIntentsRegistry::WebIntentsRegistry() {} |
191 | 213 |
192 WebIntentsRegistry::~WebIntentsRegistry() { | 214 WebIntentsRegistry::~WebIntentsRegistry() { |
215 | |
193 // Cancel all pending queries, since we can't handle them any more. | 216 // Cancel all pending queries, since we can't handle them any more. |
194 for (QueryMap::iterator it(queries_.begin()); it != queries_.end(); ++it) { | 217 for (QueryVector::iterator it = pending_queries_.begin(); |
195 wds_->CancelRequest(it->first); | 218 it != pending_queries_.end(); ++it) { |
196 delete it->second; | 219 IntentsQuery* query = *it; |
220 wds_->CancelRequest(query->query_handle_); | |
221 delete query; | |
197 } | 222 } |
198 } | 223 } |
199 | 224 |
200 void WebIntentsRegistry::Initialize( | 225 void WebIntentsRegistry::Initialize( |
201 scoped_refptr<WebDataService> wds, | 226 scoped_refptr<WebDataService> wds, |
202 ExtensionServiceInterface* extension_service) { | 227 ExtensionServiceInterface* extension_service) { |
203 wds_ = wds; | 228 wds_ = wds; |
204 extension_service_ = extension_service; | 229 extension_service_ = extension_service; |
205 } | 230 } |
206 | 231 |
207 void WebIntentsRegistry::OnWebDataServiceRequestDone( | 232 void WebIntentsRegistry::OnWebIntentsResultReceived( |
233 IntentsQuery* query, | |
208 WebDataService::Handle h, | 234 WebDataService::Handle h, |
Greg Billock
2012/07/27 00:41:29
Can get rid of handle param
Steve McKay
2012/07/27 00:54:46
Done.
| |
209 const WDTypedResult* result) { | 235 const WDTypedResult* result) { |
236 DCHECK(query); | |
210 DCHECK(result); | 237 DCHECK(result); |
211 if (result->GetType() == WEB_INTENTS_DEFAULTS_RESULT) { | |
212 OnWebDataServiceDefaultsRequestDone(h, result); | |
213 return; | |
214 } | |
215 DCHECK(result->GetType() == WEB_INTENTS_RESULT); | 238 DCHECK(result->GetType() == WEB_INTENTS_RESULT); |
216 | 239 |
217 QueryMap::iterator it = queries_.find(h); | 240 bool query_found = ClaimQuery(query); |
218 DCHECK(it != queries_.end()); | 241 DCHECK(query_found); |
219 | |
220 IntentsQuery* query(it->second); | |
221 DCHECK(query); | |
222 queries_.erase(it); | |
223 | 242 |
224 IntentServiceList matching_services = static_cast< | 243 IntentServiceList matching_services = static_cast< |
225 const WDResult<IntentServiceList>*>(result)->GetValue(); | 244 const WDResult<IntentServiceList>*>(result)->GetValue(); |
226 | 245 |
227 // Loop over all services in all extensions, collect ones | 246 // Loop over all services in all extensions, collect ones |
228 // matching the query. | 247 // matching the query. |
229 if (extension_service_) { | 248 if (extension_service_) { |
230 const ExtensionSet* extensions = extension_service_->extensions(); | 249 const ExtensionSet* extensions = extension_service_->extensions(); |
231 if (extensions) { | 250 if (extensions) { |
232 for (ExtensionSet::const_iterator i(extensions->begin()); | 251 for (ExtensionSet::const_iterator i(extensions->begin()); |
233 i != extensions->end(); ++i) { | 252 i != extensions->end(); ++i) { |
234 AddMatchingServicesForExtension(**i, query->action_, | 253 AddMatchingServicesForExtension(**i, query->action_, |
235 &matching_services); | 254 &matching_services); |
236 } | 255 } |
237 } | 256 } |
238 } | 257 } |
239 | 258 |
240 // Filter out all services not matching the query type. | 259 // Filter out all services not matching the query type. |
241 FilterServicesByMimetype(query->type_, &matching_services); | 260 FilterServicesByMimetype(query->type_, &matching_services); |
242 | 261 |
243 // Collapse intents that are equivalent for all but |type|. | 262 // Collapse intents that are equivalent for all but |type|. |
244 CollapseIntents(&matching_services); | 263 CollapseIntents(&matching_services); |
245 | 264 |
246 query->callback_.Run(matching_services); | 265 query->callback_.Run(matching_services); |
247 delete query; | 266 delete query; |
248 } | 267 } |
249 | 268 |
250 const Extension* WebIntentsRegistry::ExtensionForURL(const std::string& url) { | 269 void WebIntentsRegistry::OnWebIntentsDefaultsResultReceived( |
251 const ExtensionSet* extensions = extension_service_->extensions(); | 270 IntentsQuery* query, |
252 if (!extensions) | |
253 return NULL; | |
254 | |
255 // Use the unsafe ExtensionURLInfo constructor: we don't care if the extension | |
256 // is running or not. | |
257 GURL gurl(url); | |
258 ExtensionURLInfo info(gurl); | |
259 return extensions->GetExtensionOrAppByURL(info); | |
260 } | |
261 | |
262 void WebIntentsRegistry::OnWebDataServiceDefaultsRequestDone( | |
263 WebDataService::Handle h, | 271 WebDataService::Handle h, |
Greg Billock
2012/07/27 00:41:29
We don't need the handle anymore, right?
Steve McKay
2012/07/27 00:54:46
Done.
| |
264 const WDTypedResult* result) { | 272 const WDTypedResult* result) { |
265 QueryMap::iterator it = queries_.find(h); | 273 DCHECK(query); |
266 DCHECK(it != queries_.end()); | 274 DCHECK(result); |
275 DCHECK(result->GetType() == WEB_INTENTS_DEFAULTS_RESULT); | |
267 | 276 |
268 IntentsQuery* query(it->second); | 277 bool query_found = ClaimQuery(query); |
269 DCHECK(query); | 278 DCHECK(query_found); |
270 queries_.erase(it); | |
271 | 279 |
272 std::vector<DefaultWebIntentService> services = static_cast< | 280 std::vector<DefaultWebIntentService> services = static_cast< |
273 const WDResult<std::vector<DefaultWebIntentService> >*>(result)-> | 281 const WDResult<std::vector<DefaultWebIntentService> >*>(result)-> |
274 GetValue(); | 282 GetValue(); |
275 | 283 |
276 DefaultWebIntentService default_service; | 284 DefaultWebIntentService default_service; |
277 std::vector<DefaultWebIntentService>::iterator iter(services.begin()); | 285 std::vector<DefaultWebIntentService>::iterator iter(services.begin()); |
278 for (; iter != services.end(); ++iter) { | 286 for (; iter != services.end(); ++iter) { |
279 if (!WebIntentsTypesMatch(iter->type, query->type_)) { | 287 if (!WebIntentsTypesMatch(iter->type, query->type_)) { |
280 continue; | 288 continue; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 break; | 326 break; |
319 } | 327 } |
320 } | 328 } |
321 } | 329 } |
322 | 330 |
323 query->default_callback_.Run(default_service); | 331 query->default_callback_.Run(default_service); |
324 delete query; | 332 delete query; |
325 } | 333 } |
326 | 334 |
327 void WebIntentsRegistry::GetIntentServices( | 335 void WebIntentsRegistry::GetIntentServices( |
328 const string16& action, const string16& mimetype, | 336 const string16& action, const string16& type, |
329 const QueryCallback& callback) { | 337 const QueryCallback& callback) { |
330 DCHECK(wds_.get()); | 338 DCHECK(wds_.get()); |
331 DCHECK(!callback.is_null()); | 339 DCHECK(!callback.is_null()); |
332 | 340 |
333 IntentsQuery* query = new IntentsQuery(callback, action, mimetype); | 341 IntentsQuery* query = new IntentsQuery(this, callback, action, type); |
334 query->pending_query_ = wds_->GetWebIntentServices(action, this); | 342 query->query_handle_ = wds_->GetWebIntentServices(action, query); |
335 queries_[query->pending_query_] = query; | 343 TrackQuery(query); |
336 } | 344 } |
337 | 345 |
338 void WebIntentsRegistry::GetAllIntentServices( | 346 void WebIntentsRegistry::GetAllIntentServices( |
339 const QueryCallback& callback) { | 347 const QueryCallback& callback) { |
340 DCHECK(wds_.get()); | 348 DCHECK(wds_.get()); |
341 DCHECK(!callback.is_null()); | 349 DCHECK(!callback.is_null()); |
342 | 350 |
343 IntentsQuery* query = new IntentsQuery(callback); | 351 IntentsQuery* query = new IntentsQuery(this, callback); |
344 query->pending_query_ = wds_->GetAllWebIntentServices(this); | 352 query->query_handle_ = wds_->GetAllWebIntentServices(query); |
345 queries_[query->pending_query_] = query; | 353 TrackQuery(query); |
346 } | 354 } |
347 | 355 |
348 void WebIntentsRegistry::IntentServiceExists( | 356 void WebIntentsRegistry::IntentServiceExists( |
349 const WebIntentServiceData& service, | 357 const WebIntentServiceData& service, |
350 const base::Callback<void(bool)>& callback) { | 358 const base::Callback<void(bool)>& callback) { |
351 DCHECK(!callback.is_null()); | 359 DCHECK(!callback.is_null()); |
352 | 360 |
353 IntentsQuery* query = new IntentsQuery( | 361 IntentsQuery* query = new IntentsQuery( |
354 base::Bind(&ExistenceCallback, service, callback)); | 362 this, base::Bind(&ExistenceCallback, service, callback)); |
355 query->pending_query_ = wds_->GetWebIntentServicesForURL( | 363 query->query_handle_ = wds_->GetWebIntentServicesForURL( |
356 UTF8ToUTF16(service.service_url.spec()), this); | 364 UTF8ToUTF16(service.service_url.spec()), query); |
357 queries_[query->pending_query_] = query; | 365 TrackQuery(query); |
358 } | 366 } |
359 | 367 |
360 void WebIntentsRegistry::GetIntentServicesForExtensionFilter( | 368 void WebIntentsRegistry::GetIntentServicesForExtensionFilter( |
361 const string16& action, | 369 const string16& action, |
362 const string16& mimetype, | 370 const string16& mimetype, |
363 const std::string& extension_id, | 371 const std::string& extension_id, |
364 const QueryCallback& callback) { | 372 const QueryCallback& callback) { |
365 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 373 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
366 DCHECK(!callback.is_null()); | 374 DCHECK(!callback.is_null()); |
367 | 375 |
376 // This isn't a WDS query, so we don't track it, | |
377 // or claim the query later. | |
368 scoped_ptr<IntentsQuery> query( | 378 scoped_ptr<IntentsQuery> query( |
369 new IntentsQuery(callback, action, mimetype)); | 379 new IntentsQuery(this, callback, action, mimetype)); |
370 content::BrowserThread::PostTask( | 380 content::BrowserThread::PostTask( |
371 content::BrowserThread::UI, | 381 content::BrowserThread::UI, |
372 FROM_HERE, | 382 FROM_HERE, |
373 base::Bind(&WebIntentsRegistry::DoGetIntentServicesForExtensionFilter, | 383 base::Bind(&WebIntentsRegistry::DoGetIntentServicesForExtensionFilter, |
374 base::Unretained(this), | 384 base::Unretained(this), |
375 base::Passed(&query), extension_id)); | 385 base::Passed(&query), extension_id)); |
376 } | 386 } |
377 | 387 |
378 void WebIntentsRegistry::DoGetIntentServicesForExtensionFilter( | 388 void WebIntentsRegistry::DoGetIntentServicesForExtensionFilter( |
379 scoped_ptr<IntentsQuery> query, | 389 scoped_ptr<IntentsQuery> query, |
(...skipping 25 matching lines...) Expand all Loading... | |
405 } | 415 } |
406 | 416 |
407 void WebIntentsRegistry::GetDefaultIntentService( | 417 void WebIntentsRegistry::GetDefaultIntentService( |
408 const string16& action, | 418 const string16& action, |
409 const string16& type, | 419 const string16& type, |
410 const GURL& invoking_url, | 420 const GURL& invoking_url, |
411 const DefaultQueryCallback& callback) { | 421 const DefaultQueryCallback& callback) { |
412 DCHECK(!callback.is_null()); | 422 DCHECK(!callback.is_null()); |
413 | 423 |
414 IntentsQuery* query = | 424 IntentsQuery* query = |
415 new IntentsQuery(callback, action, type, invoking_url); | 425 new IntentsQuery(this, callback, action, type, invoking_url); |
416 query->pending_query_ = | 426 query->query_handle_ = |
417 wds_->GetDefaultWebIntentServicesForAction(action, this); | 427 wds_->GetDefaultWebIntentServicesForAction(action, query); |
418 queries_[query->pending_query_] = query; | 428 TrackQuery(query); |
419 } | 429 } |
420 | 430 |
421 void WebIntentsRegistry::RegisterIntentService( | 431 void WebIntentsRegistry::RegisterIntentService( |
422 const WebIntentServiceData& service) { | 432 const WebIntentServiceData& service) { |
423 DCHECK(wds_.get()); | 433 DCHECK(wds_.get()); |
424 wds_->AddWebIntentService(service); | 434 wds_->AddWebIntentService(service); |
425 } | 435 } |
426 | 436 |
427 void WebIntentsRegistry::UnregisterIntentService( | 437 void WebIntentsRegistry::UnregisterIntentService( |
428 const WebIntentServiceData& service) { | 438 const WebIntentServiceData& service) { |
(...skipping 24 matching lines...) Expand all Loading... | |
453 if (write_iter != read_iter) | 463 if (write_iter != read_iter) |
454 *write_iter = *read_iter; | 464 *write_iter = *read_iter; |
455 } | 465 } |
456 ++read_iter; | 466 ++read_iter; |
457 } | 467 } |
458 | 468 |
459 // Cut off everything after the last intent copied to the list. | 469 // Cut off everything after the last intent copied to the list. |
460 if (++write_iter != services->end()) | 470 if (++write_iter != services->end()) |
461 services->erase(write_iter, services->end()); | 471 services->erase(write_iter, services->end()); |
462 } | 472 } |
473 | |
474 const Extension* WebIntentsRegistry::ExtensionForURL(const std::string& url) { | |
groby-ooo-7-16
2012/07/27 00:11:26
Technically, functions should be in order of decla
Greg Billock
2012/07/27 00:41:29
Yeah, update the header.
On 2012/07/27 00:11:26,
Steve McKay
2012/07/27 00:54:46
Done.
| |
475 const ExtensionSet* extensions = extension_service_->extensions(); | |
476 if (!extensions) | |
477 return NULL; | |
478 | |
479 // Use the unsafe ExtensionURLInfo constructor: we don't care if the extension | |
480 // is running or not. | |
481 GURL gurl(url); | |
482 ExtensionURLInfo info(gurl); | |
483 return extensions->GetExtensionOrAppByURL(info); | |
484 } | |
485 | |
486 void WebIntentsRegistry::TrackQuery(IntentsQuery* query) { | |
487 DCHECK(query); | |
488 pending_queries_.push_back(query); | |
489 } | |
490 | |
491 bool WebIntentsRegistry::ClaimQuery(IntentsQuery* query) { | |
492 DCHECK(query); | |
493 QueryVector::iterator it = std::find( | |
494 pending_queries_.begin(), pending_queries_.end(), query); | |
495 if (it != pending_queries_.end()) { | |
496 pending_queries_.erase(it); | |
497 return true; | |
498 } else { | |
499 return false; | |
500 } | |
501 } | |
OLD | NEW |