summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorckehoe <ckehoe@chromium.org>2015-04-20 15:11:36 -0700
committerCommit bot <commit-bot@chromium.org>2015-04-20 22:12:41 +0000
commit6de2caba0ee5c91724dd2ff7092fa588ac0f60fd (patch)
tree5aa369d1c51c4bd60b10ee20ac384ffcf4fc4ef5 /chrome/browser
parent21d4d15a81b030f522fef29a0429f08a70220f68 (diff)
downloadchromium_src-6de2caba0ee5c91724dd2ff7092fa588ac0f60fd.zip
chromium_src-6de2caba0ee5c91724dd2ff7092fa588ac0f60fd.tar.gz
chromium_src-6de2caba0ee5c91724dd2ff7092fa588ac0f60fd.tar.bz2
Fixing the AudioModemAPI to support multiple profiles correctly.
BUG=478123 Review URL: https://codereview.chromium.org/1092333003 Cr-Commit-Position: refs/heads/master@{#325920}
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/copresence/chrome_whispernet_client.cc13
-rw-r--r--chrome/browser/extensions/api/audio_modem/audio_modem_api.h1
-rw-r--r--chrome/browser/extensions/api/copresence_private/copresence_private_api.cc85
-rw-r--r--chrome/browser/extensions/api/copresence_private/copresence_private_api.h40
4 files changed, 95 insertions, 44 deletions
diff --git a/chrome/browser/copresence/chrome_whispernet_client.cc b/chrome/browser/copresence/chrome_whispernet_client.cc
index 2023196..116e69e 100644
--- a/chrome/browser/copresence/chrome_whispernet_client.cc
+++ b/chrome/browser/copresence/chrome_whispernet_client.cc
@@ -40,7 +40,6 @@ namespace OnEncodeTokenRequest =
extensions::api::copresence_private::OnEncodeTokenRequest;
using extensions::Event;
-using extensions::copresence_private::RegisterWhispernetClient;
namespace {
@@ -105,18 +104,20 @@ void ChromeWhispernetClient::Initialize(
DCHECK(!init_callback.is_null());
init_callback_ = init_callback;
- extensions::ComponentLoader* loader =
- extensions::ExtensionSystem::Get(browser_context_)
- ->extension_service()->component_loader();
- DCHECK(loader);
+ ExtensionService* extension_service =
+ extensions::ExtensionSystem::Get(browser_context_)->extension_service();
+ CHECK(extension_service);
+ extensions::ComponentLoader* loader = extension_service->component_loader();
+ CHECK(loader);
if (!loader->Exists(kWhispernetProxyExtensionId)) {
DVLOG(3) << "Loading Whispernet proxy.";
loader->Add(IDR_WHISPERNET_PROXY_MANIFEST,
base::FilePath(FILE_PATH_LITERAL("whispernet_proxy")));
}
- client_id_ = RegisterWhispernetClient(this);
+ client_id_ = extensions::CopresencePrivateService::GetFactoryInstance()
+ ->Get(browser_context_)->RegisterWhispernetClient(this);
AudioConfiguration(GetDefaultAudioConfig());
}
diff --git a/chrome/browser/extensions/api/audio_modem/audio_modem_api.h b/chrome/browser/extensions/api/audio_modem/audio_modem_api.h
index 557ecef..dcccbfe 100644
--- a/chrome/browser/extensions/api/audio_modem/audio_modem_api.h
+++ b/chrome/browser/extensions/api/audio_modem/audio_modem_api.h
@@ -80,6 +80,7 @@ class AudioModemAPI final : public BrowserContextKeyedAPI {
std::map<std::string, base::OneShotTimer<AudioModemAPI>*> receive_timers_[2];
// BrowserContextKeyedAPI implementation.
+ static const bool kServiceIsCreatedWithBrowserContext = false;
static const char* service_name() { return "AudioModemAPI"; }
DISALLOW_COPY_AND_ASSIGN(AudioModemAPI);
diff --git a/chrome/browser/extensions/api/copresence_private/copresence_private_api.cc b/chrome/browser/extensions/api/copresence_private/copresence_private_api.cc
index 9467f12..b693843 100644
--- a/chrome/browser/extensions/api/copresence_private/copresence_private_api.cc
+++ b/chrome/browser/extensions/api/copresence_private/copresence_private_api.cc
@@ -19,21 +19,16 @@
using audio_modem::WhispernetClient;
using content::BrowserThread;
-namespace {
+namespace extensions {
-// TODO(ckehoe): Move these globals into a proper CopresencePrivateService.
+namespace SendFound = api::copresence_private::SendFound;
+namespace SendSamples = api::copresence_private::SendSamples;
+namespace SendInitialized = api::copresence_private::SendInitialized;
-base::LazyInstance<std::map<std::string, WhispernetClient*>>
-g_whispernet_clients = LAZY_INSTANCE_INITIALIZER;
+namespace {
-// To avoid concurrency issues, only use this on the UI thread.
-static bool g_initialized = false;
-
-WhispernetClient* GetWhispernetClient(const std::string& id) {
- WhispernetClient* client = g_whispernet_clients.Get()[id];
- DCHECK(client);
- return client;
-}
+base::LazyInstance<BrowserContextKeyedAPIFactory<CopresencePrivateService>>
+ g_factory = LAZY_INSTANCE_INITIALIZER;
void RunInitCallback(WhispernetClient* client, bool status) {
DCHECK(client);
@@ -45,26 +40,52 @@ void RunInitCallback(WhispernetClient* client, bool status) {
} // namespace
-namespace extensions {
-
-namespace SendFound = api::copresence_private::SendFound;
-namespace SendSamples = api::copresence_private::SendSamples;
-namespace SendInitialized = api::copresence_private::SendInitialized;
+CopresencePrivateService::CopresencePrivateService(
+ content::BrowserContext* context)
+ : initialized_(false) {}
-namespace copresence_private {
+CopresencePrivateService::~CopresencePrivateService() {}
-const std::string RegisterWhispernetClient(WhispernetClient* client) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- if (g_initialized)
+const std::string CopresencePrivateService::RegisterWhispernetClient(
+ WhispernetClient* client) {
+ if (initialized_)
RunInitCallback(client, true);
std::string id = base::GenerateGUID();
- g_whispernet_clients.Get()[id] = client;
+ whispernet_clients_[id] = client;
return id;
}
-} // namespace copresence_private
+void CopresencePrivateService::OnWhispernetInitialized(bool success) {
+ if (success)
+ initialized_ = true;
+
+ DVLOG(2) << "Notifying " << whispernet_clients_.size()
+ << " clients that initialization is complete.";
+ for (auto client_entry : whispernet_clients_)
+ RunInitCallback(client_entry.second, success);
+}
+
+WhispernetClient* CopresencePrivateService::GetWhispernetClient(
+ const std::string& id) {
+ WhispernetClient* client = whispernet_clients_[id];
+ DCHECK(client);
+ return client;
+}
+
+// static
+BrowserContextKeyedAPIFactory<CopresencePrivateService>*
+CopresencePrivateService::GetFactoryInstance() {
+ return g_factory.Pointer();
+}
+
+template <>
+void BrowserContextKeyedAPIFactory<CopresencePrivateService>
+ ::DeclareFactoryDependencies() {
+ DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
+}
+
// Copresence Private functions.
@@ -73,7 +94,9 @@ ExtensionFunction::ResponseAction CopresencePrivateSendFoundFunction::Run() {
scoped_ptr<SendFound::Params> params(SendFound::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
- WhispernetClient* whispernet_client = GetWhispernetClient(params->client_id);
+ WhispernetClient* whispernet_client =
+ CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
+ ->GetWhispernetClient(params->client_id);
if (whispernet_client->GetTokensCallback().is_null())
return RespondNow(NoArguments());
@@ -91,7 +114,9 @@ ExtensionFunction::ResponseAction CopresencePrivateSendSamplesFunction::Run() {
scoped_ptr<SendSamples::Params> params(SendSamples::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
- WhispernetClient* whispernet_client = GetWhispernetClient(params->client_id);
+ WhispernetClient* whispernet_client =
+ CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
+ ->GetWhispernetClient(params->client_id);
if (whispernet_client->GetSamplesCallback().is_null())
return RespondNow(NoArguments());
@@ -115,14 +140,8 @@ CopresencePrivateSendInitializedFunction::Run() {
SendInitialized::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- if (params->success)
- g_initialized = true;
-
- DVLOG(2) << "Notifying " << g_whispernet_clients.Get().size()
- << " clients that initialization is complete.";
- for (auto client_entry : g_whispernet_clients.Get())
- RunInitCallback(client_entry.second, params->success);
+ CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
+ ->OnWhispernetInitialized(params->success);
return RespondNow(NoArguments());
}
diff --git a/chrome/browser/extensions/api/copresence_private/copresence_private_api.h b/chrome/browser/extensions/api/copresence_private/copresence_private_api.h
index d4c2098..e783e09 100644
--- a/chrome/browser/extensions/api/copresence_private/copresence_private_api.h
+++ b/chrome/browser/extensions/api/copresence_private/copresence_private_api.h
@@ -7,7 +7,10 @@
#include <string>
+#include "base/macros.h"
+#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/extension_function.h"
+#include "extensions/browser/extension_function_histogram_value.h"
namespace audio_modem {
class WhispernetClient;
@@ -15,13 +18,40 @@ class WhispernetClient;
namespace extensions {
-namespace copresence_private {
+class CopresencePrivateService final : public BrowserContextKeyedAPI {
+ public:
+ explicit CopresencePrivateService(content::BrowserContext* context);
+ ~CopresencePrivateService() override;
+
+ // Registers a client to receive events from Whispernet.
+ const std::string
+ RegisterWhispernetClient(audio_modem::WhispernetClient* client);
+
+ // Gets the whispernet client by ID.
+ audio_modem::WhispernetClient* GetWhispernetClient(const std::string& id);
+
+ // Called from the whispernet_proxy extension when it has initialized.
+ void OnWhispernetInitialized(bool success);
+
+ // BrowserContextKeyedAPI implementation.
+ static BrowserContextKeyedAPIFactory<CopresencePrivateService>*
+ GetFactoryInstance();
-// Register a client to receive events from Whispernet.
-const std::string
-RegisterWhispernetClient(audio_modem::WhispernetClient* client);
+ private:
+ friend class BrowserContextKeyedAPIFactory<CopresencePrivateService>;
+
+ // BrowserContextKeyedAPI implementation.
+ static const char* service_name() { return "CopresencePrivateService"; }
+
+ bool initialized_;
+ std::map<std::string, audio_modem::WhispernetClient*> whispernet_clients_;
+
+ DISALLOW_COPY_AND_ASSIGN(CopresencePrivateService);
+};
-} // namespace copresence_private
+template<>
+void BrowserContextKeyedAPIFactory<CopresencePrivateService>
+ ::DeclareFactoryDependencies();
class CopresencePrivateSendFoundFunction : public UIThreadExtensionFunction {
public: