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
20namespace copresence {
21class CopresenceManager;
22class WhispernetClient;
23}
24
25namespace extensions {
26
27class CopresenceService : public BrowserContextKeyedAPI,
28                          public copresence::CopresenceDelegate {
29 public:
30  explicit CopresenceService(content::BrowserContext* context);
31  virtual ~CopresenceService();
32
33  // BrowserContextKeyedAPI implementation.
34  virtual void Shutdown() OVERRIDE;
35
36  // These accessors will always return an object (except during shutdown).
37  // If the object doesn't exist, they will create one first.
38  copresence::CopresenceManager* manager();
39  copresence::WhispernetClient* whispernet_client();
40
41  // A registry containing the app id's associated with every subscription.
42  SubscriptionToAppMap& apps_by_subscription_id() {
43    return apps_by_subscription_id_;
44  }
45
46  void set_api_key(const std::string& api_key) { api_key_ = api_key; }
47
48  // Manager override for testing.
49  void set_manager_for_testing(
50      scoped_ptr<copresence::CopresenceManager> manager);
51
52  // BrowserContextKeyedAPI implementation.
53  static BrowserContextKeyedAPIFactory<CopresenceService>* GetFactoryInstance();
54
55 private:
56  friend class BrowserContextKeyedAPIFactory<CopresenceService>;
57
58  // CopresenceDelegate implementation
59  virtual void HandleMessages(
60      const std::string& app_id,
61      const std::string& subscription_id,
62      const std::vector<copresence::Message>& message) OVERRIDE;
63  virtual net::URLRequestContextGetter* GetRequestContext() const OVERRIDE;
64  virtual const std::string GetPlatformVersionString() const OVERRIDE;
65  virtual const std::string GetAPIKey() const OVERRIDE;
66  virtual copresence::WhispernetClient* GetWhispernetClient() OVERRIDE;
67
68  // BrowserContextKeyedAPI implementation.
69  static const char* service_name() { return "CopresenceService"; }
70
71  bool is_shutting_down_;
72  std::map<std::string, std::string> apps_by_subscription_id_;
73
74  content::BrowserContext* const browser_context_;
75  std::string api_key_;
76
77  scoped_ptr<copresence::CopresenceManager> manager_;
78  scoped_ptr<copresence::WhispernetClient> whispernet_client_;
79
80  DISALLOW_COPY_AND_ASSIGN(CopresenceService);
81};
82
83template <>
84void BrowserContextKeyedAPIFactory<
85    CopresenceService>::DeclareFactoryDependencies();
86
87class CopresenceExecuteFunction : public ChromeUIThreadExtensionFunction {
88 public:
89  DECLARE_EXTENSION_FUNCTION("copresence.execute", COPRESENCE_EXECUTE);
90
91 protected:
92  virtual ~CopresenceExecuteFunction() {}
93  virtual ExtensionFunction::ResponseAction Run() OVERRIDE;
94
95 private:
96  void SendResult(copresence::CopresenceStatus status);
97};
98
99class CopresenceSetApiKeyFunction : public ChromeUIThreadExtensionFunction {
100 public:
101  DECLARE_EXTENSION_FUNCTION("copresence.setApiKey", COPRESENCE_SETAPIKEY);
102
103 protected:
104  virtual ~CopresenceSetApiKeyFunction() {}
105  virtual ExtensionFunction::ResponseAction Run() OVERRIDE;
106};
107
108}  // namespace extensions
109
110#endif  // CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_
111