| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/extensions/api/audio_modem/audio_modem_api.h" | 5 #include "chrome/browser/extensions/api/audio_modem/audio_modem_api.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 if (success) { | 219 if (success) { |
| 220 VLOG(2) << "Whispernet initialized successfully."; | 220 VLOG(2) << "Whispernet initialized successfully."; |
| 221 } else { | 221 } else { |
| 222 LOG(ERROR) << "Failed to initialize Whispernet!"; | 222 LOG(ERROR) << "Failed to initialize Whispernet!"; |
| 223 init_failed_ = true; | 223 init_failed_ = true; |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 | 226 |
| 227 void AudioModemAPI::TokensReceived(const std::vector<AudioToken>& tokens) { | 227 void AudioModemAPI::TokensReceived(const std::vector<AudioToken>& tokens) { |
| 228 // Distribute the tokens to the appropriate app(s). | 228 // Distribute the tokens to the appropriate app(s). |
| 229 std::map<std::string, std::vector<linked_ptr<ReceivedToken>>> tokens_by_app; | 229 std::list<ReceivedToken> all_tokens; |
| 230 std::map<std::string, std::vector<ReceivedToken*>> tokens_by_app; |
| 230 for (const AudioToken& token : tokens) { | 231 for (const AudioToken& token : tokens) { |
| 231 linked_ptr<ReceivedToken> api_token(new ReceivedToken); | 232 ReceivedToken api_token; |
| 232 const std::string& raw_token = DecodeBase64Token(token.token); | 233 const std::string& raw_token = DecodeBase64Token(token.token); |
| 233 api_token->token.assign(raw_token.c_str(), | 234 api_token.token.assign(raw_token.c_str(), |
| 234 raw_token.c_str() + raw_token.size()); | 235 raw_token.c_str() + raw_token.size()); |
| 235 api_token->band = token.audible ? AUDIOBAND_AUDIBLE : AUDIOBAND_INAUDIBLE; | 236 api_token.band = token.audible ? AUDIOBAND_AUDIBLE : AUDIOBAND_INAUDIBLE; |
| 237 all_tokens.push_back(std::move(api_token)); |
| 236 for (const auto& receiver : | 238 for (const auto& receiver : |
| 237 receive_timers_[token.audible ? AUDIBLE : INAUDIBLE]) { | 239 receive_timers_[token.audible ? AUDIBLE : INAUDIBLE]) { |
| 238 tokens_by_app[receiver.first].push_back(api_token); | 240 tokens_by_app[receiver.first].push_back(&all_tokens.back()); |
| 239 } | 241 } |
| 240 } | 242 } |
| 241 | 243 |
| 242 // Send events to the appropriate app(s). | 244 // Send events to the appropriate app(s). |
| 243 for (const auto& app_entry : tokens_by_app) { | 245 for (const auto& app_entry : tokens_by_app) { |
| 244 const std::string& app_id = app_entry.first; | 246 const std::string& app_id = app_entry.first; |
| 245 const auto& tokens = app_entry.second; | 247 const auto& app_tokens = app_entry.second; |
| 246 if (app_id.empty()) | 248 if (app_id.empty()) |
| 247 continue; | 249 continue; |
| 248 | 250 |
| 251 // Construct the event arguments by hand because a given token can be |
| 252 // present for multiple listeners, so constructing a |
| 253 // std::vector<ReceivedToken> for each is inefficient. |
| 254 scoped_ptr<base::ListValue> tokens_value(new base::ListValue()); |
| 255 for (const ReceivedToken* token : app_tokens) |
| 256 tokens_value->Append(token->ToValue()); |
| 257 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 258 args->Append(std::move(tokens_value)); |
| 259 |
| 249 EventRouter::Get(browser_context_) | 260 EventRouter::Get(browser_context_) |
| 250 ->DispatchEventToExtension( | 261 ->DispatchEventToExtension( |
| 251 app_id, make_scoped_ptr(new Event(events::AUDIO_MODEM_ON_RECEIVED, | 262 app_id, make_scoped_ptr(new Event(events::AUDIO_MODEM_ON_RECEIVED, |
| 252 OnReceived::kEventName, | 263 OnReceived::kEventName, |
| 253 OnReceived::Create(tokens)))); | 264 std::move(args)))); |
| 254 } | 265 } |
| 255 } | 266 } |
| 256 | 267 |
| 257 | 268 |
| 258 // Functions outside the API scope. | 269 // Functions outside the API scope. |
| 259 | 270 |
| 260 template <> | 271 template <> |
| 261 void | 272 void |
| 262 BrowserContextKeyedAPIFactory<AudioModemAPI>::DeclareFactoryDependencies() { | 273 BrowserContextKeyedAPIFactory<AudioModemAPI>::DeclareFactoryDependencies() { |
| 263 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 274 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 ExtensionFunction::ResponseAction AudioModemStopReceiveFunction::Run() { | 353 ExtensionFunction::ResponseAction AudioModemStopReceiveFunction::Run() { |
| 343 scoped_ptr<StopReceive::Params> params(StopReceive::Params::Create(*args_)); | 354 scoped_ptr<StopReceive::Params> params(StopReceive::Params::Create(*args_)); |
| 344 EXTENSION_FUNCTION_VALIDATE(params.get()); | 355 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 345 | 356 |
| 346 Status status = AudioModemAPI::GetFactoryInstance()->Get(browser_context()) | 357 Status status = AudioModemAPI::GetFactoryInstance()->Get(browser_context()) |
| 347 ->StopReceive(extension_id(), AudioTypeForBand(params->band)); | 358 ->StopReceive(extension_id(), AudioTypeForBand(params->band)); |
| 348 return RespondNow(ArgumentList(StopReceive::Results::Create(status))); | 359 return RespondNow(ArgumentList(StopReceive::Results::Create(status))); |
| 349 } | 360 } |
| 350 | 361 |
| 351 } // namespace extensions | 362 } // namespace extensions |
| OLD | NEW |