summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/copresence/copresence_api.h
blob: 5776fd79d12654929cda55498774ca5d161d6f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_

#include <map>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/api/copresence/copresence_translations.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/common/extensions/api/copresence.h"
#include "components/copresence/public/copresence_delegate.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"

namespace copresence {
class CopresenceManager;
class WhispernetClient;
}

namespace extensions {

class CopresenceService : public BrowserContextKeyedAPI,
                          public copresence::CopresenceDelegate {
 public:
  explicit CopresenceService(content::BrowserContext* context);
  virtual ~CopresenceService();

  // BrowserContextKeyedAPI implementation.
  virtual void Shutdown() override;

  // These accessors will always return an object (except during shutdown).
  // If the object doesn't exist, they will create one first.
  copresence::CopresenceManager* manager();
  copresence::WhispernetClient* whispernet_client();

  // A registry containing the app id's associated with every subscription.
  SubscriptionToAppMap& apps_by_subscription_id() {
    return apps_by_subscription_id_;
  }

  void set_api_key(const std::string& api_key) { api_key_ = api_key; }

  // Manager override for testing.
  void set_manager_for_testing(
      scoped_ptr<copresence::CopresenceManager> manager);

  // BrowserContextKeyedAPI implementation.
  static BrowserContextKeyedAPIFactory<CopresenceService>* GetFactoryInstance();

 private:
  friend class BrowserContextKeyedAPIFactory<CopresenceService>;

  // CopresenceDelegate implementation
  virtual void HandleMessages(
      const std::string& app_id,
      const std::string& subscription_id,
      const std::vector<copresence::Message>& message) override;
  virtual net::URLRequestContextGetter* GetRequestContext() const override;
  virtual const std::string GetPlatformVersionString() const override;
  virtual const std::string GetAPIKey() const override;
  virtual copresence::WhispernetClient* GetWhispernetClient() override;

  // BrowserContextKeyedAPI implementation.
  static const char* service_name() { return "CopresenceService"; }

  bool is_shutting_down_;
  std::map<std::string, std::string> apps_by_subscription_id_;

  content::BrowserContext* const browser_context_;
  std::string api_key_;

  scoped_ptr<copresence::CopresenceManager> manager_;
  scoped_ptr<copresence::WhispernetClient> whispernet_client_;

  DISALLOW_COPY_AND_ASSIGN(CopresenceService);
};

template <>
void BrowserContextKeyedAPIFactory<
    CopresenceService>::DeclareFactoryDependencies();

class CopresenceExecuteFunction : public ChromeUIThreadExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("copresence.execute", COPRESENCE_EXECUTE);

 protected:
  virtual ~CopresenceExecuteFunction() {}
  virtual ExtensionFunction::ResponseAction Run() override;

 private:
  void SendResult(copresence::CopresenceStatus status);
};

class CopresenceSetApiKeyFunction : public ChromeUIThreadExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("copresence.setApiKey", COPRESENCE_SETAPIKEY);

 protected:
  virtual ~CopresenceSetApiKeyFunction() {}
  virtual ExtensionFunction::ResponseAction Run() override;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_