summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/copresence/chrome_whispernet_client.cc71
-rw-r--r--chrome/browser/copresence/chrome_whispernet_client.h15
-rw-r--r--chrome/browser/copresence/chrome_whispernet_client_browsertest.cc3
-rw-r--r--chrome/browser/resources/whispernet_proxy/js/init.js2
-rw-r--r--components/copresence/copresence_manager_impl.cc10
-rw-r--r--components/copresence/handlers/directive_handler.h1
6 files changed, 51 insertions, 51 deletions
diff --git a/chrome/browser/copresence/chrome_whispernet_client.cc b/chrome/browser/copresence/chrome_whispernet_client.cc
index 0748371..37ac718 100644
--- a/chrome/browser/copresence/chrome_whispernet_client.cc
+++ b/chrome/browser/copresence/chrome_whispernet_client.cc
@@ -44,7 +44,11 @@ const char ChromeWhispernetClient::kWhispernetProxyExtensionId[] =
ChromeWhispernetClient::ChromeWhispernetClient(
content::BrowserContext* browser_context)
- : browser_context_(browser_context), extension_loaded_(false) {
+ : browser_context_(browser_context),
+ event_router_(extensions::EventRouter::Get(browser_context)),
+ extension_loaded_(false) {
+ DCHECK(browser_context_);
+ DCHECK(event_router_);
}
ChromeWhispernetClient::~ChromeWhispernetClient() {
@@ -74,6 +78,8 @@ void ChromeWhispernetClient::Initialize(
} else {
init_callback_.Run(true);
}
+
+ AudioConfiguration(GetDefaultAudioConfig());
}
void ChromeWhispernetClient::Shutdown() {
@@ -84,16 +90,8 @@ void ChromeWhispernetClient::Shutdown() {
db_callback_.Reset();
}
-void AudioConfiguration(const copresence::config::AudioParamData& data,
- const copresence::SuccessCallback& init_callback) {
-}
-
void ChromeWhispernetClient::EncodeToken(const std::string& token_str,
copresence::AudioType type) {
- DCHECK(extension_loaded_);
- DCHECK(browser_context_);
- DCHECK(extensions::EventRouter::Get(browser_context_));
-
extensions::api::copresence_private::EncodeTokenParameters params;
params.token.token = token_str;
params.token.audible = type == copresence::AUDIBLE;
@@ -102,17 +100,12 @@ void ChromeWhispernetClient::EncodeToken(const std::string& token_str,
extensions::api::copresence_private::OnEncodeTokenRequest::Create(params),
browser_context_));
- extensions::EventRouter::Get(browser_context_)
- ->DispatchEventToExtension(kWhispernetProxyExtensionId, event.Pass());
+ SendEventIfLoaded(event.Pass());
}
void ChromeWhispernetClient::DecodeSamples(copresence::AudioType type,
const std::string& samples,
const size_t token_length[2]) {
- DCHECK(extension_loaded_);
- DCHECK(browser_context_);
- DCHECK(extensions::EventRouter::Get(browser_context_));
-
extensions::api::copresence_private::DecodeSamplesParameters params;
params.samples.assign(samples.begin(), samples.end());
params.decode_audible =
@@ -128,22 +121,16 @@ void ChromeWhispernetClient::DecodeSamples(copresence::AudioType type,
params),
browser_context_));
- extensions::EventRouter::Get(browser_context_)
- ->DispatchEventToExtension(kWhispernetProxyExtensionId, event.Pass());
+ SendEventIfLoaded(event.Pass());
}
void ChromeWhispernetClient::DetectBroadcast() {
- DCHECK(extension_loaded_);
- DCHECK(browser_context_);
- DCHECK(extensions::EventRouter::Get(browser_context_));
-
scoped_ptr<extensions::Event> event(new extensions::Event(
extensions::api::copresence_private::OnDetectBroadcastRequest::kEventName,
make_scoped_ptr(new base::ListValue()),
browser_context_));
- extensions::EventRouter::Get(browser_context_)
- ->DispatchEventToExtension(kWhispernetProxyExtensionId, event.Pass());
+ SendEventIfLoaded(event.Pass());
}
void ChromeWhispernetClient::RegisterTokensCallback(
@@ -182,9 +169,6 @@ copresence::SuccessCallback ChromeWhispernetClient::GetInitializedCallback() {
void ChromeWhispernetClient::AudioConfiguration(
const copresence::config::AudioParamData& params) {
- DCHECK(browser_context_);
- DCHECK(extensions::EventRouter::Get(browser_context_));
-
extensions::api::copresence_private::AudioParameters audio_params;
// We serialize AudioConfigData to a string and send it to the whispernet
@@ -198,23 +182,34 @@ void ChromeWhispernetClient::AudioConfiguration(
extensions::api::copresence_private::OnConfigAudio::Create(audio_params),
browser_context_));
- extensions::EventRouter::Get(browser_context_)
- ->DispatchEventToExtension(kWhispernetProxyExtensionId, event.Pass());
+ SendEventIfLoaded(event.Pass());
}
-void ChromeWhispernetClient::OnExtensionLoaded(bool success) {
+void ChromeWhispernetClient::SendEventIfLoaded(
+ scoped_ptr<extensions::Event> event) {
if (extension_loaded_) {
- if (!init_callback_.is_null())
- init_callback_.Run(success);
- return;
+ event_router_->DispatchEventToExtension(kWhispernetProxyExtensionId,
+ event.Pass());
+ } else {
+ DVLOG(2) << "Queueing event: " << event->event_name;
+ queued_events_.push_back(event.release());
}
+}
- // Our extension just got loaded, initialize whispernet.
- extension_loaded_ = true;
+void ChromeWhispernetClient::OnExtensionLoaded(bool success) {
+ if (!init_callback_.is_null())
+ init_callback_.Run(success);
- // This will fire another OnExtensionLoaded call once the initialization is
- // done, which means we've initialized for realz, hence call the init
- // callback.
+ DVLOG(3) << "Sending " << queued_events_.size()
+ << " queued requests to whispernet";
- AudioConfiguration(GetDefaultAudioConfig());
+ // In this loop, ownership of each Event is passed to a scoped_ptr instead.
+ // Thus we can just discard the pointers at the end.
+ for (extensions::Event* event : queued_events_) {
+ event_router_->DispatchEventToExtension(kWhispernetProxyExtensionId,
+ make_scoped_ptr(event));
+ }
+ queued_events_.weak_clear();
+
+ extension_loaded_ = true;
}
diff --git a/chrome/browser/copresence/chrome_whispernet_client.h b/chrome/browser/copresence/chrome_whispernet_client.h
index 8b0e4f7..d629289 100644
--- a/chrome/browser/copresence/chrome_whispernet_client.h
+++ b/chrome/browser/copresence/chrome_whispernet_client.h
@@ -6,10 +6,10 @@
#define CHROME_BROWSER_COPRESENCE_CHROME_WHISPERNET_CLIENT_H_
#include <string>
-#include <vector>
#include "base/callback.h"
#include "base/macros.h"
+#include "base/memory/scoped_vector.h"
#include "components/copresence/public/copresence_constants.h"
#include "components/copresence/public/whispernet_client.h"
@@ -18,12 +18,17 @@ class BrowserContext;
}
namespace extensions {
+
+struct Event;
+class EventRouter;
+
namespace api {
namespace copresence_private {
struct AudioParameters;
}
}
-}
+
+} // namespace extensions
namespace media {
class AudioBusRefCounted;
@@ -64,11 +69,14 @@ class ChromeWhispernetClient final : public copresence::WhispernetClient {
// Fire an event to configure whispernet with the given audio parameters.
void AudioConfiguration(const copresence::config::AudioParamData& params);
+ void SendEventIfLoaded(scoped_ptr<extensions::Event> event);
+
// This gets called twice; once when the proxy extension loads, the second
// time when we have initialized the proxy extension's encoder and decoder.
void OnExtensionLoaded(bool success);
- content::BrowserContext* browser_context_;
+ content::BrowserContext* const browser_context_;
+ extensions::EventRouter* const event_router_;
copresence::SuccessCallback extension_loaded_callback_;
copresence::SuccessCallback init_callback_;
@@ -77,6 +85,7 @@ class ChromeWhispernetClient final : public copresence::WhispernetClient {
copresence::SamplesCallback samples_callback_;
copresence::SuccessCallback db_callback_;
+ ScopedVector<extensions::Event> queued_events_;
bool extension_loaded_;
DISALLOW_COPY_AND_ASSIGN(ChromeWhispernetClient);
diff --git a/chrome/browser/copresence/chrome_whispernet_client_browsertest.cc b/chrome/browser/copresence/chrome_whispernet_client_browsertest.cc
index 4a34f9a..2cc2cd4 100644
--- a/chrome/browser/copresence/chrome_whispernet_client_browsertest.cc
+++ b/chrome/browser/copresence/chrome_whispernet_client_browsertest.cc
@@ -54,7 +54,7 @@ std::string AudioBusToString(scoped_refptr<media::AudioBusRefCounted> source) {
} // namespace
class ChromeWhispernetClientTest : public ExtensionBrowserTest {
- public:
+ protected:
ChromeWhispernetClientTest()
: context_(NULL), expected_audible_(false), initialized_(false) {}
@@ -130,7 +130,6 @@ class ChromeWhispernetClientTest : public ExtensionBrowserTest {
run_loop_->Run();
}
- protected:
void InitCallback(bool success) {
EXPECT_TRUE(success);
initialized_ = true;
diff --git a/chrome/browser/resources/whispernet_proxy/js/init.js b/chrome/browser/resources/whispernet_proxy/js/init.js
index 97355a0..87abe26 100644
--- a/chrome/browser/resources/whispernet_proxy/js/init.js
+++ b/chrome/browser/resources/whispernet_proxy/js/init.js
@@ -31,8 +31,6 @@ function audioConfig(audioParams) {
whisperDecoder = new WhisperDecoder(audioParams.paramData, whispernetNacl);
whisperDecoder.setReceiveCallback(chrome.copresencePrivate.sendFound);
whisperDecoder.onDetectBroadcast(chrome.copresencePrivate.sendDetect);
-
- chrome.copresencePrivate.sendInitialized(true);
}
/**
diff --git a/components/copresence/copresence_manager_impl.cc b/components/copresence/copresence_manager_impl.cc
index 89d5e8a..851b193 100644
--- a/components/copresence/copresence_manager_impl.cc
+++ b/components/copresence/copresence_manager_impl.cc
@@ -82,6 +82,10 @@ CopresenceManagerImpl::CopresenceManagerImpl(CopresenceDelegate* delegate)
directive_handler_.get(),
gcm_handler_.get(),
messages_callback));
+
+ directive_handler_->Start(delegate_->GetWhispernetClient(),
+ base::Bind(&CopresenceManagerImpl::ReceivedTokens,
+ base::Unretained(this)));
}
CopresenceManagerImpl::~CopresenceManagerImpl() {
@@ -116,12 +120,6 @@ void CopresenceManagerImpl::ExecuteReportRequest(
void CopresenceManagerImpl::WhispernetInitComplete(bool success) {
if (success) {
DVLOG(3) << "Whispernet initialized successfully.";
-
- directive_handler_->Start(delegate_->GetWhispernetClient(),
- base::Bind(&CopresenceManagerImpl::ReceivedTokens,
- base::Unretained(this)));
-
- // Start up timers.
poll_timer_->Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kPollTimerIntervalMs),
base::Bind(&CopresenceManagerImpl::PollForMessages,
diff --git a/components/copresence/handlers/directive_handler.h b/components/copresence/handlers/directive_handler.h
index 24e1789..5c3f6ea7 100644
--- a/components/copresence/handlers/directive_handler.h
+++ b/components/copresence/handlers/directive_handler.h
@@ -26,6 +26,7 @@ class DirectiveHandler {
// |whispernet_client| is owned by the caller and must outlive the
// DirectiveHandler.
// |tokens_cb| is called for all audio tokens found in recorded audio.
+ // TODO(ckehoe): This is no longer needed. Merge into the constructor.
virtual void Start(WhispernetClient* whispernet_client,
const TokensCallback& tokens_cb) = 0;