OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
| 7 #include "base/callback.h" |
| 8 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/webdata/web_data_service.h" | 9 #include "chrome/browser/webdata/web_data_service.h" |
7 | 10 |
8 // Internal object representing all data associated with a single query. | 11 // Internal object representing all data associated with a single query. |
9 struct WebIntentsRegistry::IntentsQuery { | 12 struct WebIntentsRegistry::IntentsQuery { |
10 // Unique query identifier. | 13 // Unique query identifier. |
11 QueryID query_id_; | 14 QueryID query_id_; |
12 | 15 |
13 // Underlying data query. | 16 // Underlying data query. |
14 WebDataService::Handle pending_query_; | 17 WebDataService::Handle pending_query_; |
15 | 18 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 104 |
102 IntentsQuery* query = new IntentsQuery; | 105 IntentsQuery* query = new IntentsQuery; |
103 query->query_id_ = next_query_id_++; | 106 query->query_id_ = next_query_id_++; |
104 query->consumer_ = consumer; | 107 query->consumer_ = consumer; |
105 query->pending_query_ = wds_->GetAllWebIntents(this); | 108 query->pending_query_ = wds_->GetAllWebIntents(this); |
106 queries_[query->pending_query_] = query; | 109 queries_[query->pending_query_] = query; |
107 | 110 |
108 return query->query_id_; | 111 return query->query_id_; |
109 } | 112 } |
110 | 113 |
| 114 // Trampoline consumer for calls to IntentProviderExists. Forwards existence |
| 115 // of the provided |service| to the provided |callback|. |
| 116 class ProviderCheckConsumer : public WebIntentsRegistry::Consumer { |
| 117 public: |
| 118 ProviderCheckConsumer(const WebIntentServiceData& service, |
| 119 const base::Callback<void(bool)>& callback) |
| 120 : callback_(callback), |
| 121 service_(service) {} |
| 122 virtual ~ProviderCheckConsumer() {} |
| 123 |
| 124 // Gets the list of all providers for a particular action. Check them all |
| 125 // to see if |provider_| is already registered. |
| 126 virtual void OnIntentsQueryDone( |
| 127 WebIntentsRegistry::QueryID id, |
| 128 const WebIntentsRegistry::IntentList& list) OVERRIDE { |
| 129 scoped_ptr<ProviderCheckConsumer> self_deleter(this); |
| 130 |
| 131 for (WebIntentsRegistry::IntentList::const_iterator i = list.begin(); |
| 132 i != list.end(); ++i) { |
| 133 if (*i == service_) { |
| 134 callback_.Run(true); |
| 135 return; |
| 136 } |
| 137 } |
| 138 |
| 139 callback_.Run(false); |
| 140 } |
| 141 |
| 142 private: |
| 143 base::Callback<void(bool)> callback_; |
| 144 WebIntentServiceData service_; |
| 145 }; |
| 146 |
| 147 WebIntentsRegistry::QueryID WebIntentsRegistry::IntentProviderExists( |
| 148 const WebIntentServiceData& provider, |
| 149 const base::Callback<void(bool)>& callback) { |
| 150 IntentsQuery* query = new IntentsQuery; |
| 151 query->query_id_ = next_query_id_++; |
| 152 query->consumer_ = new ProviderCheckConsumer(provider, callback); |
| 153 query->pending_query_ = wds_->GetWebIntentsForURL( |
| 154 UTF8ToUTF16(provider.service_url.spec()), this); |
| 155 queries_[query->pending_query_] = query; |
| 156 |
| 157 return query->query_id_; |
| 158 } |
| 159 |
111 void WebIntentsRegistry::RegisterIntentProvider( | 160 void WebIntentsRegistry::RegisterIntentProvider( |
112 const WebIntentServiceData& service) { | 161 const WebIntentServiceData& service) { |
113 DCHECK(wds_.get()); | 162 DCHECK(wds_.get()); |
114 wds_->AddWebIntent(service); | 163 wds_->AddWebIntent(service); |
115 } | 164 } |
116 | 165 |
117 void WebIntentsRegistry::UnregisterIntentProvider( | 166 void WebIntentsRegistry::UnregisterIntentProvider( |
118 const WebIntentServiceData& service) { | 167 const WebIntentServiceData& service) { |
119 DCHECK(wds_.get()); | 168 DCHECK(wds_.get()); |
120 wds_->RemoveWebIntent(service); | 169 wds_->RemoveWebIntent(service); |
121 } | 170 } |
OLD | NEW |