// Copyright (c) 2012 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. // P2PSocketDispatcher is a per-renderer object that dispatchers all // P2P messages received from the browser and relays all P2P messages // sent to the browser. P2PSocketClient instances register themselves // with the dispatcher using RegisterClient() and UnregisterClient(). // // Relationship of classes. // // P2PSocketHost P2PSocketClient // ^ ^ // | | // v IPC v // P2PSocketDispatcherHost <---------> P2PSocketDispatcher // // P2PSocketDispatcher receives and dispatches messages on the // renderer thread. #ifndef CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_ #define CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_ #include #include "base/callback_forward.h" #include "base/compiler_specific.h" #include "base/id_map.h" #include "base/observer_list_threadsafe.h" #include "base/synchronization/lock.h" #include "content/common/content_export.h" #include "content/common/p2p_sockets.h" #include "content/public/renderer/render_view_observer.h" #include "net/base/net_util.h" class RenderViewImpl; namespace base { class MessageLoopProxy; } // namespace base namespace net { class IPEndPoint; } // namespace net namespace webkit_glue { class NetworkListObserver; } // webkit_glue namespace content { class P2PHostAddressRequest; class P2PSocketClient; // Callback interface that allows the implementor to be notified before a // P2PSocketDispatcher is destroyed. // P2PSocketDispatcher requires that all NetworkListObservers are // unregistered when SocketDispatcherGone is called. class P2PSocketDispatcherDestructionObserver { public: virtual void OnSocketDispatcherDestroyed() = 0; protected: virtual ~P2PSocketDispatcherDestructionObserver() {} }; class CONTENT_EXPORT P2PSocketDispatcher : public content::RenderViewObserver { public: explicit P2PSocketDispatcher(RenderViewImpl* render_view); virtual ~P2PSocketDispatcher(); // Add a new network list observer. Each observer is called // immidiately after it is registered and then later whenever // network configuration changes. Can be called on any thread. The // observer is always called on the thread it was added. void AddNetworkListObserver( webkit_glue::NetworkListObserver* network_list_observer); // Removes network list observer. Must be called on the thread on // which the observer was added. void RemoveNetworkListObserver( webkit_glue::NetworkListObserver* network_list_observer); void AddDestructionObserver(P2PSocketDispatcherDestructionObserver* observer); void RemoveDestructionObserver( P2PSocketDispatcherDestructionObserver* observer); // RenderViewObserver overrides. virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; private: friend class P2PHostAddressRequest; friend class P2PSocketClient; class AsyncMessageSender; base::MessageLoopProxy* message_loop(); // Called by P2PSocketClient. int RegisterClient(P2PSocketClient* client); void UnregisterClient(int id); void SendP2PMessage(IPC::Message* msg); // Called by DnsRequest. int RegisterHostAddressRequest(P2PHostAddressRequest* request); void UnregisterHostAddressRequest(int id); // Incoming message handlers. void OnNetworkListChanged(const net::NetworkInterfaceList& networks); void OnGetHostAddressResult(int32 request_id, const net::IPAddressNumber& address); void OnSocketCreated(int socket_id, const net::IPEndPoint& address); void OnIncomingTcpConnection(int socket_id, const net::IPEndPoint& address); void OnError(int socket_id); void OnDataReceived(int socket_id, const net::IPEndPoint& address, const std::vector& data); P2PSocketClient* GetClient(int socket_id); scoped_refptr message_loop_; IDMap clients_; IDMap host_address_requests_; bool network_notifications_started_; scoped_refptr > network_list_observers_; scoped_refptr async_message_sender_; ObserverList destruction_observer_; DISALLOW_COPY_AND_ASSIGN(P2PSocketDispatcher); }; } // namespace content #endif // CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_