OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_AUDIO_MODEM_AUDIO_MODEM_API_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_AUDIO_MODEM_AUDIO_MODEM_API_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "chrome/common/extensions/api/audio_modem.h" | |
15 #include "components/audio_modem/public/modem.h" | |
16 #include "extensions/browser/browser_context_keyed_api_factory.h" | |
17 #include "extensions/browser/extension_function.h" | |
18 #include "extensions/browser/extension_function_histogram_value.h" | |
19 | |
20 namespace extensions { | |
21 | |
22 // Implementation of the chrome.audioModem API. | |
23 class AudioModemAPI final : public BrowserContextKeyedAPI { | |
24 public: | |
25 // Default constructor. | |
26 explicit AudioModemAPI(content::BrowserContext* context); | |
27 | |
28 // Testing constructor: pass in dependencies. | |
29 AudioModemAPI( | |
30 content::BrowserContext* context, | |
31 std::unique_ptr<audio_modem::WhispernetClient> whispernet_client, | |
32 std::unique_ptr<audio_modem::Modem> modem); | |
33 | |
34 ~AudioModemAPI() override; | |
35 | |
36 // Starts transmitting a token, and returns the associated API status. | |
37 // Fails if another app is already transmitting the same AudioType. | |
38 api::audio_modem::Status StartTransmit( | |
39 const std::string& app_id, | |
40 const api::audio_modem::RequestParams& params, | |
41 const std::string& token); | |
42 | |
43 // Stops an in-progress transmit, and returns the associated API status. | |
44 // Fails if the specified app is not currently transmitting. | |
45 api::audio_modem::Status StopTransmit(const std::string& app_id, | |
46 audio_modem::AudioType audio_type); | |
47 | |
48 // Starts receiving for the specified app. | |
49 // Multiple apps may receive the same AudioType simultaneously. | |
50 void StartReceive(const std::string& app_id, | |
51 const api::audio_modem::RequestParams& params); | |
52 | |
53 // Stops receiving for the specified app, and returns the associated | |
54 // API status. Fails if that app is not currently receiving. | |
55 api::audio_modem::Status StopReceive(const std::string& app_id, | |
56 audio_modem::AudioType audio_type); | |
57 | |
58 bool init_failed() const { return init_failed_; } | |
59 | |
60 // BrowserContextKeyedAPI implementation. | |
61 static BrowserContextKeyedAPIFactory<AudioModemAPI>* GetFactoryInstance(); | |
62 | |
63 private: | |
64 friend class BrowserContextKeyedAPIFactory<AudioModemAPI>; | |
65 | |
66 void WhispernetInitComplete(bool success); | |
67 void TokensReceived(const std::vector<audio_modem::AudioToken>& tokens); | |
68 | |
69 content::BrowserContext* const browser_context_; | |
70 std::unique_ptr<audio_modem::WhispernetClient> whispernet_client_; | |
71 std::unique_ptr<audio_modem::Modem> modem_; | |
72 bool init_failed_; | |
73 | |
74 // IDs for the currently transmitting app (if any), indexed by AudioType. | |
75 std::string transmitters_[2]; | |
76 | |
77 // Timeouts for the currently active transmits, indexed by AudioType. | |
78 base::OneShotTimer transmit_timers_[2]; | |
79 | |
80 // Maps of currently receiving app ID => timeouts. Indexed by AudioType. | |
81 // We own all of these pointers. Do not remove them without calling delete. | |
82 std::map<std::string, base::OneShotTimer*> receive_timers_[2]; | |
83 | |
84 // BrowserContextKeyedAPI implementation. | |
85 static const bool kServiceIsCreatedWithBrowserContext = false; | |
86 static const char* service_name() { return "AudioModemAPI"; } | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(AudioModemAPI); | |
89 }; | |
90 | |
91 template<> | |
92 void BrowserContextKeyedAPIFactory<AudioModemAPI>::DeclareFactoryDependencies(); | |
93 | |
94 class AudioModemTransmitFunction : public UIThreadExtensionFunction { | |
95 public: | |
96 DECLARE_EXTENSION_FUNCTION("audioModem.transmit", AUDIOMODEM_TRANSMIT); | |
97 | |
98 protected: | |
99 ~AudioModemTransmitFunction() override {} | |
100 ExtensionFunction::ResponseAction Run() override; | |
101 }; | |
102 | |
103 class AudioModemStopTransmitFunction : public UIThreadExtensionFunction { | |
104 public: | |
105 DECLARE_EXTENSION_FUNCTION("audioModem.stopTransmit", | |
106 AUDIOMODEM_STOPTRANSMIT); | |
107 | |
108 protected: | |
109 ~AudioModemStopTransmitFunction() override {} | |
110 ExtensionFunction::ResponseAction Run() override; | |
111 }; | |
112 | |
113 class AudioModemReceiveFunction : public UIThreadExtensionFunction { | |
114 public: | |
115 DECLARE_EXTENSION_FUNCTION("audioModem.receive", AUDIOMODEM_RECEIVE); | |
116 | |
117 protected: | |
118 ~AudioModemReceiveFunction() override {} | |
119 ExtensionFunction::ResponseAction Run() override; | |
120 }; | |
121 | |
122 class AudioModemStopReceiveFunction : public UIThreadExtensionFunction { | |
123 public: | |
124 DECLARE_EXTENSION_FUNCTION("audioModem.stopReceive", AUDIOMODEM_STOPRECEIVE); | |
125 | |
126 protected: | |
127 ~AudioModemStopReceiveFunction() override {} | |
128 ExtensionFunction::ResponseAction Run() override; | |
129 }; | |
130 | |
131 } // namespace extensions | |
132 | |
133 #endif // CHROME_BROWSER_EXTENSIONS_API_AUDIO_MODEM_AUDIO_MODEM_API_H_ | |
OLD | NEW |