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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_AUDIO_MODEM_AUDIO_MODEM_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_AUDIO_MODEM_AUDIO_MODEM_API_H_
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/common/extensions/api/audio_modem.h"
14 #include "components/audio_modem/public/modem.h"
15 #include "extensions/browser/browser_context_keyed_api_factory.h"
16 #include "extensions/browser/extension_function.h"
17 #include "extensions/browser/extension_function_histogram_value.h"
19 namespace extensions
{
21 // Implementation of the chrome.audioModem API.
22 class AudioModemAPI final
: public BrowserContextKeyedAPI
{
24 // Default constructor.
25 explicit AudioModemAPI(content::BrowserContext
* context
);
27 // Testing constructor: pass in dependencies.
28 AudioModemAPI(content::BrowserContext
* context
,
29 scoped_ptr
<audio_modem::WhispernetClient
> whispernet_client
,
30 scoped_ptr
<audio_modem::Modem
> modem
);
32 ~AudioModemAPI() override
;
34 // Starts transmitting a token, and returns the associated API status.
35 // Fails if another app is already transmitting the same AudioType.
36 api::audio_modem::Status
StartTransmit(
37 const std::string
& app_id
,
38 const api::audio_modem::RequestParams
& params
,
39 const std::string
& token
);
41 // Stops an in-progress transmit, and returns the associated API status.
42 // Fails if the specified app is not currently transmitting.
43 api::audio_modem::Status
StopTransmit(const std::string
& app_id
,
44 audio_modem::AudioType audio_type
);
46 // Starts receiving for the specified app.
47 // Multiple apps may receive the same AudioType simultaneously.
48 void StartReceive(const std::string
& app_id
,
49 const api::audio_modem::RequestParams
& params
);
51 // Stops receiving for the specified app, and returns the associated
52 // API status. Fails if that app is not currently receiving.
53 api::audio_modem::Status
StopReceive(const std::string
& app_id
,
54 audio_modem::AudioType audio_type
);
56 bool init_failed() const { return init_failed_
; }
58 // BrowserContextKeyedAPI implementation.
59 static BrowserContextKeyedAPIFactory
<AudioModemAPI
>* GetFactoryInstance();
62 friend class BrowserContextKeyedAPIFactory
<AudioModemAPI
>;
64 void WhispernetInitComplete(bool success
);
65 void TokensReceived(const std::vector
<audio_modem::AudioToken
>& tokens
);
67 content::BrowserContext
* const browser_context_
;
68 scoped_ptr
<audio_modem::WhispernetClient
> whispernet_client_
;
69 scoped_ptr
<audio_modem::Modem
> modem_
;
72 // IDs for the currently transmitting app (if any), indexed by AudioType.
73 std::string transmitters_
[2];
75 // Timeouts for the currently active transmits, indexed by AudioType.
76 base::OneShotTimer
<AudioModemAPI
> transmit_timers_
[2];
78 // Maps of currently receiving app ID => timeouts. Indexed by AudioType.
79 // We own all of these pointers. Do not remove them without calling delete.
80 std::map
<std::string
, base::OneShotTimer
<AudioModemAPI
>*> receive_timers_
[2];
82 // BrowserContextKeyedAPI implementation.
83 static const bool kServiceIsCreatedWithBrowserContext
= false;
84 static const char* service_name() { return "AudioModemAPI"; }
86 DISALLOW_COPY_AND_ASSIGN(AudioModemAPI
);
90 void BrowserContextKeyedAPIFactory
<AudioModemAPI
>::DeclareFactoryDependencies();
92 class AudioModemTransmitFunction
: public UIThreadExtensionFunction
{
94 DECLARE_EXTENSION_FUNCTION("audioModem.transmit", AUDIOMODEM_TRANSMIT
);
97 ~AudioModemTransmitFunction() override
{}
98 ExtensionFunction::ResponseAction
Run() override
;
101 class AudioModemStopTransmitFunction
: public UIThreadExtensionFunction
{
103 DECLARE_EXTENSION_FUNCTION("audioModem.stopTransmit",
104 AUDIOMODEM_STOPTRANSMIT
);
107 ~AudioModemStopTransmitFunction() override
{}
108 ExtensionFunction::ResponseAction
Run() override
;
111 class AudioModemReceiveFunction
: public UIThreadExtensionFunction
{
113 DECLARE_EXTENSION_FUNCTION("audioModem.receive", AUDIOMODEM_RECEIVE
);
116 ~AudioModemReceiveFunction() override
{}
117 ExtensionFunction::ResponseAction
Run() override
;
120 class AudioModemStopReceiveFunction
: public UIThreadExtensionFunction
{
122 DECLARE_EXTENSION_FUNCTION("audioModem.stopReceive", AUDIOMODEM_STOPRECEIVE
);
125 ~AudioModemStopReceiveFunction() override
{}
126 ExtensionFunction::ResponseAction
Run() override
;
129 } // namespace extensions
131 #endif // CHROME_BROWSER_EXTENSIONS_API_AUDIO_MODEM_AUDIO_MODEM_API_H_