Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / copresence_socket / copresence_socket_api.h
blob71db9ed294688bf8965159d12f01aa75a64643e7
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.
5 #ifndef EXTENSIONS_BROWSER_API_COPRESENCE_SOCKET_COPRESENCE_SOCKET_API_H_
6 #define EXTENSIONS_BROWSER_API_COPRESENCE_SOCKET_COPRESENCE_SOCKET_API_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "extensions/browser/api/api_resource_manager.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"
20 namespace copresence_sockets {
21 class CopresenceSocket;
24 namespace net {
25 class IOBuffer;
28 namespace extensions {
30 class CopresencePeerResource;
31 class CopresenceSocketResource;
33 class CopresenceSocketFunction : public UIThreadExtensionFunction {
34 public:
35 CopresenceSocketFunction();
37 void DispatchOnConnectedEvent(
38 int peer_id,
39 scoped_ptr<copresence_sockets::CopresenceSocket> socket);
41 protected:
42 ~CopresenceSocketFunction() override;
44 // Override this and do actual work here.
45 virtual ExtensionFunction::ResponseAction Execute() = 0;
47 // Takes ownership of peer.
48 int AddPeer(CopresencePeerResource* peer);
49 // Takes ownership of socket.
50 int AddSocket(CopresenceSocketResource* socket);
52 // Takes ownership of peer.
53 void ReplacePeer(const std::string& extension_id,
54 int peer_id,
55 CopresencePeerResource* peer);
57 CopresencePeerResource* GetPeer(int peer_id);
58 CopresenceSocketResource* GetSocket(int socket_id);
60 void RemovePeer(int peer_id);
61 void RemoveSocket(int socket_id);
63 // ExtensionFunction overrides:
64 ExtensionFunction::ResponseAction Run() override;
66 private:
67 void Initialize();
69 void OnDataReceived(int socket_id,
70 const scoped_refptr<net::IOBuffer>& buffer,
71 int size);
72 void DispatchOnReceiveEvent(int socket_id, const std::string& data);
74 ApiResourceManager<CopresencePeerResource>* peers_manager_;
75 ApiResourceManager<CopresenceSocketResource>* sockets_manager_;
78 class CopresenceSocketCreatePeerFunction : public CopresenceSocketFunction {
79 public:
80 DECLARE_EXTENSION_FUNCTION("copresenceSocket.createPeer",
81 COPRESENCESOCKET_CREATEPEER);
83 protected:
84 ~CopresenceSocketCreatePeerFunction() override {}
85 ExtensionFunction::ResponseAction Execute() override;
87 private:
88 void OnCreated(int peer_id, const std::string& locator);
91 class CopresenceSocketDestroyPeerFunction : public CopresenceSocketFunction {
92 public:
93 DECLARE_EXTENSION_FUNCTION("copresenceSocket.destroyPeer",
94 COPRESENCESOCKET_DESTROYPEER);
96 protected:
97 ~CopresenceSocketDestroyPeerFunction() override {}
98 ExtensionFunction::ResponseAction Execute() override;
101 class CopresenceSocketSendFunction : public CopresenceSocketFunction {
102 public:
103 DECLARE_EXTENSION_FUNCTION("copresenceSocket.send", COPRESENCESOCKET_SEND);
105 protected:
106 ~CopresenceSocketSendFunction() override {}
107 ExtensionFunction::ResponseAction Execute() override;
110 class CopresenceSocketDisconnectFunction : public CopresenceSocketFunction {
111 public:
112 DECLARE_EXTENSION_FUNCTION("copresenceSocket.disconnect",
113 COPRESENCESOCKET_DISCONNECT);
115 protected:
116 ~CopresenceSocketDisconnectFunction() override {}
117 ExtensionFunction::ResponseAction Execute() override;
120 } // namespace extensions
122 #endif // EXTENSIONS_BROWSER_API_COPRESENCE_SOCKET_COPRESENCE_SOCKET_API_H_