Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / copresence / copresence_api.h
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 <string>
10 #include <vector>
11
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.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 copresence {
23 class CopresenceManager;
24 class WhispernetClient;
25 }
26
27 namespace gcm {
28 class GCMDriver;
29 }
30
31 namespace extensions {
32
33 class CopresenceService : public BrowserContextKeyedAPI,
34                           public copresence::CopresenceDelegate {
35  public:
36   explicit CopresenceService(content::BrowserContext* context);
37   ~CopresenceService() override;
38
39   // BrowserContextKeyedAPI implementation.
40   void Shutdown() override;
41
42   // These accessors will always return an object (except during shutdown).
43   // If the object doesn't exist, they will create one first.
44   copresence::CopresenceManager* manager();
45   copresence::WhispernetClient* whispernet_client();
46
47   // A registry containing the app id's associated with every subscription.
48   SubscriptionToAppMap& apps_by_subscription_id() {
49     return apps_by_subscription_id_;
50   }
51
52   void set_api_key(const std::string& app_id,
53                    const std::string& api_key);
54
55   std::string auth_token() const {
56     return auth_token_;
57   }
58
59   void set_auth_token(const std::string& token);
60
61   // Manager override for testing.
62   void set_manager_for_testing(
63       scoped_ptr<copresence::CopresenceManager> manager);
64
65   // BrowserContextKeyedAPI implementation.
66   static BrowserContextKeyedAPIFactory<CopresenceService>* GetFactoryInstance();
67
68  private:
69   friend class BrowserContextKeyedAPIFactory<CopresenceService>;
70
71   // CopresenceDelegate implementation
72   void HandleMessages(const std::string& app_id,
73                       const std::string& subscription_id,
74                       const std::vector<copresence::Message>& message) override;
75   void HandleStatusUpdate(copresence::CopresenceStatus status) override;
76   net::URLRequestContextGetter* GetRequestContext() const override;
77   const std::string GetPlatformVersionString() const override;
78   const std::string GetAPIKey(const std::string& app_id) const override;
79   copresence::WhispernetClient* GetWhispernetClient() override;
80   gcm::GCMDriver* GetGCMDriver() override;
81
82   // BrowserContextKeyedAPI implementation.
83   static const char* service_name() { return "CopresenceService"; }
84
85   bool is_shutting_down_;
86   std::map<std::string, std::string> apps_by_subscription_id_;
87
88   content::BrowserContext* const browser_context_;
89   std::map<std::string, std::string> api_keys_by_app_;
90
91   // TODO(ckehoe): This is a temporary hack.
92   // Auth tokens from different apps needs to be separated properly.
93   std::string auth_token_;
94
95   scoped_ptr<copresence::CopresenceManager> manager_;
96   scoped_ptr<ChromeWhispernetClient> whispernet_client_;
97
98   DISALLOW_COPY_AND_ASSIGN(CopresenceService);
99 };
100
101 template <>
102 void BrowserContextKeyedAPIFactory<
103     CopresenceService>::DeclareFactoryDependencies();
104
105 class CopresenceExecuteFunction : public ChromeUIThreadExtensionFunction {
106  public:
107   DECLARE_EXTENSION_FUNCTION("copresence.execute", COPRESENCE_EXECUTE);
108
109  protected:
110   ~CopresenceExecuteFunction() override {}
111   ExtensionFunction::ResponseAction Run() override;
112
113  private:
114   void SendResult(copresence::CopresenceStatus status);
115 };
116
117 class CopresenceSetApiKeyFunction : public ChromeUIThreadExtensionFunction {
118  public:
119   DECLARE_EXTENSION_FUNCTION("copresence.setApiKey", COPRESENCE_SETAPIKEY);
120
121  protected:
122   ~CopresenceSetApiKeyFunction() override {}
123   ExtensionFunction::ResponseAction Run() override;
124 };
125
126 class CopresenceSetAuthTokenFunction : public ChromeUIThreadExtensionFunction {
127  public:
128   DECLARE_EXTENSION_FUNCTION("copresence.setAuthToken",
129                              COPRESENCE_SETAUTHTOKEN);
130
131  protected:
132   virtual ~CopresenceSetAuthTokenFunction() {}
133   ExtensionFunction::ResponseAction Run() override;
134 };
135
136 }  // namespace extensions
137
138 #endif  // CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_