OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "chrome/browser/extensions/api/copresence/copresence_translations.h" | |
15 #include "chrome/browser/extensions/chrome_extension_function.h" | |
16 #include "chrome/common/extensions/api/copresence.h" | |
17 #include "components/copresence/public/copresence_delegate.h" | |
18 #include "extensions/browser/browser_context_keyed_api_factory.h" | |
19 | |
20 class ChromeWhispernetClient; | |
21 | |
22 namespace audio_modem { | |
23 class WhispernetClient; | |
24 } | |
25 | |
26 namespace copresence { | |
27 class CopresenceManager; | |
28 } | |
29 | |
30 namespace gcm { | |
31 class GCMDriver; | |
32 } | |
33 | |
34 namespace user_prefs { | |
35 class PrefRegistrySyncable; | |
36 } | |
37 | |
38 namespace extensions { | |
39 | |
40 class CopresenceService final : public BrowserContextKeyedAPI, | |
41 public copresence::CopresenceDelegate { | |
42 public: | |
43 explicit CopresenceService(content::BrowserContext* context); | |
44 ~CopresenceService() override; | |
45 | |
46 // BrowserContextKeyedAPI implementation. | |
47 static const bool kServiceHasOwnInstanceInIncognito = true; | |
48 void Shutdown() override; | |
49 | |
50 // These accessors will always return an object (except during shutdown). | |
51 // If the object doesn't exist, they will create one first. | |
52 copresence::CopresenceManager* manager(); | |
53 | |
54 // A registry containing the app id's associated with every subscription. | |
55 SubscriptionToAppMap& apps_by_subscription_id() { | |
56 return apps_by_subscription_id_; | |
57 } | |
58 | |
59 std::string auth_token(const std::string& app_id) const; | |
60 | |
61 void set_api_key(const std::string& app_id, | |
62 const std::string& api_key); | |
63 | |
64 void set_auth_token(const std::string& app_id, | |
65 const std::string& token); | |
66 | |
67 // Delete all current copresence data, including stored device IDs. | |
68 void ResetState(); | |
69 | |
70 // Manager override for testing. | |
71 void set_manager_for_testing( | |
72 std::unique_ptr<copresence::CopresenceManager> manager); | |
73 | |
74 // Registers the preference for saving our device IDs. | |
75 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
76 | |
77 // BrowserContextKeyedAPI implementation. | |
78 static BrowserContextKeyedAPIFactory<CopresenceService>* GetFactoryInstance(); | |
79 | |
80 private: | |
81 friend class BrowserContextKeyedAPIFactory<CopresenceService>; | |
82 | |
83 // CopresenceDelegate implementation | |
84 void HandleMessages(const std::string& app_id, | |
85 const std::string& subscription_id, | |
86 const std::vector<copresence::Message>& message) override; | |
87 void HandleStatusUpdate(copresence::CopresenceStatus status) override; | |
88 net::URLRequestContextGetter* GetRequestContext() const override; | |
89 std::string GetPlatformVersionString() const override; | |
90 std::string GetAPIKey(const std::string& app_id) const override; | |
91 audio_modem::WhispernetClient* GetWhispernetClient() override; | |
92 gcm::GCMDriver* GetGCMDriver() override; | |
93 std::string GetDeviceId(bool authenticated) override; | |
94 void SaveDeviceId(bool authenticated, const std::string& device_id) override; | |
95 | |
96 // BrowserContextKeyedAPI implementation. | |
97 static const char* service_name() { return "CopresenceService"; } | |
98 | |
99 PrefService* GetPrefService(); | |
100 | |
101 bool is_shutting_down_; | |
102 content::BrowserContext* const browser_context_; | |
103 | |
104 std::map<std::string, std::string> apps_by_subscription_id_; | |
105 | |
106 std::map<std::string, std::string> api_keys_by_app_; | |
107 std::map<std::string, std::string> auth_tokens_by_app_; | |
108 | |
109 std::unique_ptr<audio_modem::WhispernetClient> whispernet_client_; | |
110 std::unique_ptr<copresence::CopresenceManager> manager_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(CopresenceService); | |
113 }; | |
114 | |
115 template <> | |
116 void BrowserContextKeyedAPIFactory< | |
117 CopresenceService>::DeclareFactoryDependencies(); | |
118 | |
119 class CopresenceExecuteFunction : public ChromeUIThreadExtensionFunction { | |
120 public: | |
121 DECLARE_EXTENSION_FUNCTION("copresence.execute", COPRESENCE_EXECUTE); | |
122 | |
123 protected: | |
124 ~CopresenceExecuteFunction() override {} | |
125 ExtensionFunction::ResponseAction Run() override; | |
126 | |
127 private: | |
128 void SendResult(copresence::CopresenceStatus status); | |
129 }; | |
130 | |
131 // TODO(ckehoe): Remove this function. | |
132 class CopresenceSetApiKeyFunction : public ChromeUIThreadExtensionFunction { | |
133 public: | |
134 DECLARE_EXTENSION_FUNCTION("copresence.setApiKey", COPRESENCE_SETAPIKEY); | |
135 | |
136 protected: | |
137 ~CopresenceSetApiKeyFunction() override {} | |
138 ExtensionFunction::ResponseAction Run() override; | |
139 }; | |
140 | |
141 class CopresenceSetAuthTokenFunction : public ChromeUIThreadExtensionFunction { | |
142 public: | |
143 DECLARE_EXTENSION_FUNCTION("copresence.setAuthToken", | |
144 COPRESENCE_SETAUTHTOKEN); | |
145 | |
146 protected: | |
147 ~CopresenceSetAuthTokenFunction() override {} | |
148 ExtensionFunction::ResponseAction Run() override; | |
149 }; | |
150 | |
151 } // namespace extensions | |
152 | |
153 #endif // CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_ | |
OLD | NEW |