| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/copresence/chrome_whispernet_client.h" | 5 #include "chrome/browser/copresence/chrome_whispernet_client.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "chrome/browser/extensions/api/copresence_private/copresence_private_ap
i.h" | 8 #include "chrome/browser/extensions/api/copresence_private/copresence_private_ap
i.h" |
| 9 #include "chrome/browser/extensions/component_loader.h" | 9 #include "chrome/browser/extensions/component_loader.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } // namespace | 37 } // namespace |
| 38 | 38 |
| 39 // static | 39 // static |
| 40 const char ChromeWhispernetClient::kWhispernetProxyExtensionId[] = | 40 const char ChromeWhispernetClient::kWhispernetProxyExtensionId[] = |
| 41 "bpfmnplchembfbdgieamdodgaencleal"; | 41 "bpfmnplchembfbdgieamdodgaencleal"; |
| 42 | 42 |
| 43 // Public: | 43 // Public: |
| 44 | 44 |
| 45 ChromeWhispernetClient::ChromeWhispernetClient( | 45 ChromeWhispernetClient::ChromeWhispernetClient( |
| 46 content::BrowserContext* browser_context) | 46 content::BrowserContext* browser_context) |
| 47 : browser_context_(browser_context), extension_loaded_(false) { | 47 : browser_context_(browser_context), |
| 48 event_router_(extensions::EventRouter::Get(browser_context)), |
| 49 extension_loaded_(false) { |
| 50 DCHECK(browser_context_); |
| 51 DCHECK(event_router_); |
| 48 } | 52 } |
| 49 | 53 |
| 50 ChromeWhispernetClient::~ChromeWhispernetClient() { | 54 ChromeWhispernetClient::~ChromeWhispernetClient() { |
| 51 } | 55 } |
| 52 | 56 |
| 53 void ChromeWhispernetClient::Initialize( | 57 void ChromeWhispernetClient::Initialize( |
| 54 const copresence::SuccessCallback& init_callback) { | 58 const copresence::SuccessCallback& init_callback) { |
| 55 DVLOG(3) << "Initializing whispernet proxy client."; | 59 DVLOG(3) << "Initializing whispernet proxy client."; |
| 56 init_callback_ = init_callback; | 60 init_callback_ = init_callback; |
| 57 | 61 |
| 58 extensions::ExtensionSystem* es = | 62 extensions::ExtensionSystem* es = |
| 59 extensions::ExtensionSystem::Get(browser_context_); | 63 extensions::ExtensionSystem::Get(browser_context_); |
| 60 DCHECK(es); | 64 DCHECK(es); |
| 61 ExtensionService* service = es->extension_service(); | 65 ExtensionService* service = es->extension_service(); |
| 62 DCHECK(service); | 66 DCHECK(service); |
| 63 extensions::ComponentLoader* loader = service->component_loader(); | 67 extensions::ComponentLoader* loader = service->component_loader(); |
| 64 DCHECK(loader); | 68 DCHECK(loader); |
| 65 | 69 |
| 66 // This callback is cancelled in Shutdown(). | 70 // This callback is cancelled in Shutdown(). |
| 67 extension_loaded_callback_ = base::Bind( | 71 extension_loaded_callback_ = base::Bind( |
| 68 &ChromeWhispernetClient::OnExtensionLoaded, base::Unretained(this)); | 72 &ChromeWhispernetClient::OnExtensionLoaded, base::Unretained(this)); |
| 69 | 73 |
| 70 if (!loader->Exists(kWhispernetProxyExtensionId)) { | 74 if (!loader->Exists(kWhispernetProxyExtensionId)) { |
| 71 DVLOG(3) << "Loading Whispernet proxy."; | 75 DVLOG(3) << "Loading Whispernet proxy."; |
| 72 loader->Add(IDR_WHISPERNET_PROXY_MANIFEST, | 76 loader->Add(IDR_WHISPERNET_PROXY_MANIFEST, |
| 73 base::FilePath(FILE_PATH_LITERAL("whispernet_proxy"))); | 77 base::FilePath(FILE_PATH_LITERAL("whispernet_proxy"))); |
| 74 } else { | 78 } else { |
| 75 init_callback_.Run(true); | 79 init_callback_.Run(true); |
| 76 } | 80 } |
| 81 |
| 82 AudioConfiguration(GetDefaultAudioConfig()); |
| 77 } | 83 } |
| 78 | 84 |
| 79 void ChromeWhispernetClient::Shutdown() { | 85 void ChromeWhispernetClient::Shutdown() { |
| 80 extension_loaded_callback_.Reset(); | 86 extension_loaded_callback_.Reset(); |
| 81 init_callback_.Reset(); | 87 init_callback_.Reset(); |
| 82 tokens_callback_.Reset(); | 88 tokens_callback_.Reset(); |
| 83 samples_callback_.Reset(); | 89 samples_callback_.Reset(); |
| 84 db_callback_.Reset(); | 90 db_callback_.Reset(); |
| 85 } | 91 } |
| 86 | 92 |
| 87 void AudioConfiguration(const copresence::config::AudioParamData& data, | |
| 88 const copresence::SuccessCallback& init_callback) { | |
| 89 } | |
| 90 | |
| 91 void ChromeWhispernetClient::EncodeToken(const std::string& token_str, | 93 void ChromeWhispernetClient::EncodeToken(const std::string& token_str, |
| 92 copresence::AudioType type) { | 94 copresence::AudioType type) { |
| 93 DCHECK(extension_loaded_); | |
| 94 DCHECK(browser_context_); | |
| 95 DCHECK(extensions::EventRouter::Get(browser_context_)); | |
| 96 | |
| 97 extensions::api::copresence_private::EncodeTokenParameters params; | 95 extensions::api::copresence_private::EncodeTokenParameters params; |
| 98 params.token.token = token_str; | 96 params.token.token = token_str; |
| 99 params.token.audible = type == copresence::AUDIBLE; | 97 params.token.audible = type == copresence::AUDIBLE; |
| 100 scoped_ptr<extensions::Event> event(new extensions::Event( | 98 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 101 extensions::api::copresence_private::OnEncodeTokenRequest::kEventName, | 99 extensions::api::copresence_private::OnEncodeTokenRequest::kEventName, |
| 102 extensions::api::copresence_private::OnEncodeTokenRequest::Create(params), | 100 extensions::api::copresence_private::OnEncodeTokenRequest::Create(params), |
| 103 browser_context_)); | 101 browser_context_)); |
| 104 | 102 |
| 105 extensions::EventRouter::Get(browser_context_) | 103 SendEventIfLoaded(event.Pass()); |
| 106 ->DispatchEventToExtension(kWhispernetProxyExtensionId, event.Pass()); | |
| 107 } | 104 } |
| 108 | 105 |
| 109 void ChromeWhispernetClient::DecodeSamples(copresence::AudioType type, | 106 void ChromeWhispernetClient::DecodeSamples(copresence::AudioType type, |
| 110 const std::string& samples, | 107 const std::string& samples, |
| 111 const size_t token_length[2]) { | 108 const size_t token_length[2]) { |
| 112 DCHECK(extension_loaded_); | |
| 113 DCHECK(browser_context_); | |
| 114 DCHECK(extensions::EventRouter::Get(browser_context_)); | |
| 115 | |
| 116 extensions::api::copresence_private::DecodeSamplesParameters params; | 109 extensions::api::copresence_private::DecodeSamplesParameters params; |
| 117 params.samples.assign(samples.begin(), samples.end()); | 110 params.samples.assign(samples.begin(), samples.end()); |
| 118 params.decode_audible = | 111 params.decode_audible = |
| 119 type == copresence::AUDIBLE || type == copresence::BOTH; | 112 type == copresence::AUDIBLE || type == copresence::BOTH; |
| 120 params.decode_inaudible = | 113 params.decode_inaudible = |
| 121 type == copresence::INAUDIBLE || type == copresence::BOTH; | 114 type == copresence::INAUDIBLE || type == copresence::BOTH; |
| 122 params.audible_token_length = token_length[copresence::AUDIBLE]; | 115 params.audible_token_length = token_length[copresence::AUDIBLE]; |
| 123 params.inaudible_token_length = token_length[copresence::INAUDIBLE]; | 116 params.inaudible_token_length = token_length[copresence::INAUDIBLE]; |
| 124 | 117 |
| 125 scoped_ptr<extensions::Event> event(new extensions::Event( | 118 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 126 extensions::api::copresence_private::OnDecodeSamplesRequest::kEventName, | 119 extensions::api::copresence_private::OnDecodeSamplesRequest::kEventName, |
| 127 extensions::api::copresence_private::OnDecodeSamplesRequest::Create( | 120 extensions::api::copresence_private::OnDecodeSamplesRequest::Create( |
| 128 params), | 121 params), |
| 129 browser_context_)); | 122 browser_context_)); |
| 130 | 123 |
| 131 extensions::EventRouter::Get(browser_context_) | 124 SendEventIfLoaded(event.Pass()); |
| 132 ->DispatchEventToExtension(kWhispernetProxyExtensionId, event.Pass()); | |
| 133 } | 125 } |
| 134 | 126 |
| 135 void ChromeWhispernetClient::DetectBroadcast() { | 127 void ChromeWhispernetClient::DetectBroadcast() { |
| 136 DCHECK(extension_loaded_); | |
| 137 DCHECK(browser_context_); | |
| 138 DCHECK(extensions::EventRouter::Get(browser_context_)); | |
| 139 | |
| 140 scoped_ptr<extensions::Event> event(new extensions::Event( | 128 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 141 extensions::api::copresence_private::OnDetectBroadcastRequest::kEventName, | 129 extensions::api::copresence_private::OnDetectBroadcastRequest::kEventName, |
| 142 make_scoped_ptr(new base::ListValue()), | 130 make_scoped_ptr(new base::ListValue()), |
| 143 browser_context_)); | 131 browser_context_)); |
| 144 | 132 |
| 145 extensions::EventRouter::Get(browser_context_) | 133 SendEventIfLoaded(event.Pass()); |
| 146 ->DispatchEventToExtension(kWhispernetProxyExtensionId, event.Pass()); | |
| 147 } | 134 } |
| 148 | 135 |
| 149 void ChromeWhispernetClient::RegisterTokensCallback( | 136 void ChromeWhispernetClient::RegisterTokensCallback( |
| 150 const copresence::TokensCallback& tokens_callback) { | 137 const copresence::TokensCallback& tokens_callback) { |
| 151 tokens_callback_ = tokens_callback; | 138 tokens_callback_ = tokens_callback; |
| 152 } | 139 } |
| 153 | 140 |
| 154 void ChromeWhispernetClient::RegisterSamplesCallback( | 141 void ChromeWhispernetClient::RegisterSamplesCallback( |
| 155 const copresence::SamplesCallback& samples_callback) { | 142 const copresence::SamplesCallback& samples_callback) { |
| 156 samples_callback_ = samples_callback; | 143 samples_callback_ = samples_callback; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 175 } | 162 } |
| 176 | 163 |
| 177 copresence::SuccessCallback ChromeWhispernetClient::GetInitializedCallback() { | 164 copresence::SuccessCallback ChromeWhispernetClient::GetInitializedCallback() { |
| 178 return extension_loaded_callback_; | 165 return extension_loaded_callback_; |
| 179 } | 166 } |
| 180 | 167 |
| 181 // Private: | 168 // Private: |
| 182 | 169 |
| 183 void ChromeWhispernetClient::AudioConfiguration( | 170 void ChromeWhispernetClient::AudioConfiguration( |
| 184 const copresence::config::AudioParamData& params) { | 171 const copresence::config::AudioParamData& params) { |
| 185 DCHECK(browser_context_); | |
| 186 DCHECK(extensions::EventRouter::Get(browser_context_)); | |
| 187 | |
| 188 extensions::api::copresence_private::AudioParameters audio_params; | 172 extensions::api::copresence_private::AudioParameters audio_params; |
| 189 | 173 |
| 190 // We serialize AudioConfigData to a string and send it to the whispernet | 174 // We serialize AudioConfigData to a string and send it to the whispernet |
| 191 // nacl wrapper. | 175 // nacl wrapper. |
| 192 const size_t params_size = sizeof(params); | 176 const size_t params_size = sizeof(params); |
| 193 audio_params.param_data.resize(params_size); | 177 audio_params.param_data.resize(params_size); |
| 194 memcpy(vector_as_array(&audio_params.param_data), ¶ms, params_size); | 178 memcpy(vector_as_array(&audio_params.param_data), ¶ms, params_size); |
| 195 | 179 |
| 196 scoped_ptr<extensions::Event> event(new extensions::Event( | 180 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 197 extensions::api::copresence_private::OnConfigAudio::kEventName, | 181 extensions::api::copresence_private::OnConfigAudio::kEventName, |
| 198 extensions::api::copresence_private::OnConfigAudio::Create(audio_params), | 182 extensions::api::copresence_private::OnConfigAudio::Create(audio_params), |
| 199 browser_context_)); | 183 browser_context_)); |
| 200 | 184 |
| 201 extensions::EventRouter::Get(browser_context_) | 185 SendEventIfLoaded(event.Pass()); |
| 202 ->DispatchEventToExtension(kWhispernetProxyExtensionId, event.Pass()); | 186 } |
| 187 |
| 188 void ChromeWhispernetClient::SendEventIfLoaded( |
| 189 scoped_ptr<extensions::Event> event) { |
| 190 if (extension_loaded_) { |
| 191 event_router_->DispatchEventToExtension(kWhispernetProxyExtensionId, |
| 192 event.Pass()); |
| 193 } else { |
| 194 DVLOG(2) << "Queueing event: " << event->event_name; |
| 195 queued_events_.push_back(event.release()); |
| 196 } |
| 203 } | 197 } |
| 204 | 198 |
| 205 void ChromeWhispernetClient::OnExtensionLoaded(bool success) { | 199 void ChromeWhispernetClient::OnExtensionLoaded(bool success) { |
| 206 if (extension_loaded_) { | 200 if (!init_callback_.is_null()) |
| 207 if (!init_callback_.is_null()) | 201 init_callback_.Run(success); |
| 208 init_callback_.Run(success); | 202 |
| 209 return; | 203 DVLOG(3) << "Sending " << queued_events_.size() |
| 204 << " queued requests to whispernet"; |
| 205 |
| 206 // In this loop, ownership of each Event is passed to a scoped_ptr instead. |
| 207 // Thus we can just discard the pointers at the end. |
| 208 for (extensions::Event* event : queued_events_) { |
| 209 event_router_->DispatchEventToExtension(kWhispernetProxyExtensionId, |
| 210 make_scoped_ptr(event)); |
| 210 } | 211 } |
| 212 queued_events_.weak_clear(); |
| 211 | 213 |
| 212 // Our extension just got loaded, initialize whispernet. | |
| 213 extension_loaded_ = true; | 214 extension_loaded_ = true; |
| 214 | |
| 215 // This will fire another OnExtensionLoaded call once the initialization is | |
| 216 // done, which means we've initialized for realz, hence call the init | |
| 217 // callback. | |
| 218 | |
| 219 AudioConfiguration(GetDefaultAudioConfig()); | |
| 220 } | 215 } |
| OLD | NEW |