summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/js_event_handler_list.h
blob: 2d5cf9e068f91f846b40819414d57bacfe983710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2011 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.

#ifndef CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_LIST_H_
#define CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_LIST_H_
#pragma once

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/observer_list.h"
#include "chrome/browser/sync/js_arg_list.h"
#include "chrome/browser/sync/js_event_router.h"
#include "chrome/browser/sync/js_frontend.h"

namespace browser_sync {

class JsBackend;
class JsEventHandler;

// A beefed-up ObserverList<JsEventHandler> that transparently handles
// the communication between the handlers and a backend.
class JsEventHandlerList : public JsFrontend, public JsEventRouter {
 public:
  JsEventHandlerList();

  virtual ~JsEventHandlerList();

  // Sets the backend to route all messages to.  Should be called only
  // if a backend has not already been set.
  void SetBackend(JsBackend* backend);

  // Removes any existing backend.
  void RemoveBackend();

  // JsFrontend implementation.  Routes messages to any attached
  // backend; if there is none, queues up the message for processing
  // when the next backend is attached.
  virtual void AddHandler(JsEventHandler* handler);
  virtual void RemoveHandler(JsEventHandler* handler);
  virtual void ProcessMessage(
      const std::string& name, const JsArgList& args,
      const JsEventHandler* sender);

  // JsEventRouter implementation.  Routes the event to the
  // appropriate handler(s).
  virtual void RouteJsEvent(const std::string& name,
                            const JsArgList& args,
                            const JsEventHandler* target);

 private:
  // A struct used to hold the arguments to ProcessMessage() for
  // future invocation.
  struct PendingMessage {
    std::string name;
    JsArgList args;
    const JsEventHandler* sender;

    PendingMessage(const std::string& name, const JsArgList& args,
                   const JsEventHandler* sender);
  };

  typedef std::vector<PendingMessage> PendingMessageList;

  JsBackend* backend_;
  ObserverList<JsEventHandler> handlers_;
  PendingMessageList pending_messages_;

  DISALLOW_COPY_AND_ASSIGN(JsEventHandlerList);
};

}  // namespace browser_sync

#endif  // CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_LIST_H_