Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / browser / api / sockets_tcp / tcp_socket_event_dispatcher.h
blob886cb2842f4e3d9a953d8f08c6f9d5518d415d4c
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_SOCKETS_TCP_TCP_SOCKET_EVENT_DISPATCHER_H_
6 #define EXTENSIONS_BROWSER_API_SOCKETS_TCP_TCP_SOCKET_EVENT_DISPATCHER_H_
8 #include <string>
10 #include "extensions/browser/api/api_resource_manager.h"
11 #include "extensions/browser/api/sockets_tcp/sockets_tcp_api.h"
13 namespace content {
14 class BrowserContext;
17 namespace extensions {
18 struct Event;
19 class ResumableTCPSocket;
22 namespace extensions {
23 namespace api {
25 // Dispatch events related to "sockets.tcp" sockets from callback on native
26 // socket instances. There is one instance per profile.
27 class TCPSocketEventDispatcher
28 : public BrowserContextKeyedAPI,
29 public base::SupportsWeakPtr<TCPSocketEventDispatcher> {
30 public:
31 explicit TCPSocketEventDispatcher(content::BrowserContext* context);
32 ~TCPSocketEventDispatcher() override;
34 // Socket is active, start receving from it.
35 void OnSocketConnect(const std::string& extension_id, int socket_id);
37 // Socket is active again, start receiving data from it.
38 void OnSocketResume(const std::string& extension_id, int socket_id);
40 // BrowserContextKeyedAPI implementation.
41 static BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>*
42 GetFactoryInstance();
44 // Convenience method to get the SocketEventDispatcher for a profile.
45 static TCPSocketEventDispatcher* Get(content::BrowserContext* context);
47 private:
48 typedef ApiResourceManager<ResumableTCPSocket>::ApiResourceData SocketData;
49 friend class BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>;
50 // BrowserContextKeyedAPI implementation.
51 static const char* service_name() { return "TCPSocketEventDispatcher"; }
52 static const bool kServiceHasOwnInstanceInIncognito = true;
53 static const bool kServiceIsNULLWhileTesting = true;
55 // base::Bind supports methods with up to 6 parameters. ReadParams is used
56 // as a workaround that limitation for invoking StartReceive.
57 struct ReadParams {
58 ReadParams();
59 ~ReadParams();
61 content::BrowserThread::ID thread_id;
62 void* browser_context_id;
63 std::string extension_id;
64 scoped_refptr<SocketData> sockets;
65 int socket_id;
68 // Start a receive and register a callback.
69 void StartSocketRead(const std::string& extension_id, int socket_id);
71 // Start a receive and register a callback.
72 static void StartRead(const ReadParams& params);
74 // Called when socket receive data.
75 static void ReadCallback(const ReadParams& params,
76 int bytes_read,
77 scoped_refptr<net::IOBuffer> io_buffer);
79 // Post an extension event from IO to UI thread
80 static void PostEvent(const ReadParams& params, scoped_ptr<Event> event);
82 // Dispatch an extension event on to EventRouter instance on UI thread.
83 static void DispatchEvent(void* browser_context_id,
84 const std::string& extension_id,
85 scoped_ptr<Event> event);
87 // Usually IO thread (except for unit testing).
88 content::BrowserThread::ID thread_id_;
89 content::BrowserContext* const browser_context_;
90 scoped_refptr<SocketData> sockets_;
93 } // namespace api
94 } // namespace extensions
96 #endif // EXTENSIONS_BROWSER_API_SOCKETS_TCP_TCP_SOCKET_EVENT_DISPATCHER_H_