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