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_
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
;
28 namespace extensions
{
30 class CopresencePeerResource
;
31 class CopresenceSocketResource
;
33 class CopresenceSocketFunction
: public UIThreadExtensionFunction
{
35 CopresenceSocketFunction();
37 void DispatchOnConnectedEvent(
39 scoped_ptr
<copresence_sockets::CopresenceSocket
> socket
);
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
,
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
;
69 void OnDataReceived(int socket_id
,
70 const scoped_refptr
<net::IOBuffer
>& buffer
,
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
{
80 DECLARE_EXTENSION_FUNCTION("copresenceSocket.createPeer",
81 COPRESENCESOCKET_CREATEPEER
);
84 ~CopresenceSocketCreatePeerFunction() override
{}
85 ExtensionFunction::ResponseAction
Execute() override
;
88 void OnCreated(int peer_id
, const std::string
& locator
);
91 class CopresenceSocketDestroyPeerFunction
: public CopresenceSocketFunction
{
93 DECLARE_EXTENSION_FUNCTION("copresenceSocket.destroyPeer",
94 COPRESENCESOCKET_DESTROYPEER
);
97 ~CopresenceSocketDestroyPeerFunction() override
{}
98 ExtensionFunction::ResponseAction
Execute() override
;
101 class CopresenceSocketSendFunction
: public CopresenceSocketFunction
{
103 DECLARE_EXTENSION_FUNCTION("copresenceSocket.send", COPRESENCESOCKET_SEND
);
106 ~CopresenceSocketSendFunction() override
{}
107 ExtensionFunction::ResponseAction
Execute() override
;
110 class CopresenceSocketDisconnectFunction
: public CopresenceSocketFunction
{
112 DECLARE_EXTENSION_FUNCTION("copresenceSocket.disconnect",
113 COPRESENCESOCKET_DISCONNECT
);
116 ~CopresenceSocketDisconnectFunction() override
{}
117 ExtensionFunction::ResponseAction
Execute() override
;
120 } // namespace extensions
122 #endif // EXTENSIONS_BROWSER_API_COPRESENCE_SOCKET_COPRESENCE_SOCKET_API_H_