1// Copyright (c) 2011 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 CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_LIST_H_ 6#define CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_LIST_H_ 7#pragma once 8 9#include <string> 10#include <vector> 11 12#include "base/basictypes.h" 13#include "base/observer_list.h" 14#include "chrome/browser/sync/js_arg_list.h" 15#include "chrome/browser/sync/js_event_router.h" 16#include "chrome/browser/sync/js_frontend.h" 17 18namespace browser_sync { 19 20class JsBackend; 21class JsEventHandler; 22 23// A beefed-up ObserverList<JsEventHandler> that transparently handles 24// the communication between the handlers and a backend. 25class JsEventHandlerList : public JsFrontend, public JsEventRouter { 26 public: 27 JsEventHandlerList(); 28 29 virtual ~JsEventHandlerList(); 30 31 // Sets the backend to route all messages to. Should be called only 32 // if a backend has not already been set. 33 void SetBackend(JsBackend* backend); 34 35 // Removes any existing backend. 36 void RemoveBackend(); 37 38 // JsFrontend implementation. Routes messages to any attached 39 // backend; if there is none, queues up the message for processing 40 // when the next backend is attached. 41 virtual void AddHandler(JsEventHandler* handler); 42 virtual void RemoveHandler(JsEventHandler* handler); 43 virtual void ProcessMessage( 44 const std::string& name, const JsArgList& args, 45 const JsEventHandler* sender); 46 47 // JsEventRouter implementation. Routes the event to the 48 // appropriate handler(s). 49 virtual void RouteJsEvent(const std::string& name, 50 const JsArgList& args, 51 const JsEventHandler* target); 52 53 private: 54 // A struct used to hold the arguments to ProcessMessage() for 55 // future invocation. 56 struct PendingMessage { 57 std::string name; 58 JsArgList args; 59 const JsEventHandler* sender; 60 61 PendingMessage(const std::string& name, const JsArgList& args, 62 const JsEventHandler* sender); 63 }; 64 65 typedef std::vector<PendingMessage> PendingMessageList; 66 67 JsBackend* backend_; 68 ObserverList<JsEventHandler> handlers_; 69 PendingMessageList pending_messages_; 70 71 DISALLOW_COPY_AND_ASSIGN(JsEventHandlerList); 72}; 73 74} // namespace browser_sync 75 76#endif // CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_LIST_H_ 77