1 // Copyright 2012 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 COMPONENTS_SYNC_JS_SYNC_JS_CONTROLLER_H_
6 #define COMPONENTS_SYNC_JS_SYNC_JS_CONTROLLER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/compiler_specific.h"
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "components/sync/base/weak_handle.h"
16 #include "components/sync/js/js_controller.h"
17 #include "components/sync/js/js_event_handler.h"
18 
19 namespace syncer {
20 
21 class JsBackend;
22 
23 // A class that mediates between the sync JsEventHandlers and the sync
24 // JsBackend.
25 class SyncJsController : public JsController,
26                          public JsEventHandler,
27                          public base::SupportsWeakPtr<SyncJsController> {
28  public:
29   SyncJsController();
30 
31   ~SyncJsController() override;
32 
33   // Sets the backend to route all messages to (if initialized).
34   // Sends any queued-up messages if |backend| is initialized.
35   void AttachJsBackend(const WeakHandle<JsBackend>& js_backend);
36 
37   // JsController implementation.
38   void AddJsEventHandler(JsEventHandler* event_handler) override;
39   void RemoveJsEventHandler(JsEventHandler* event_handler) override;
40 
41   // JsEventHandler implementation.
42   void HandleJsEvent(const std::string& name,
43                      const JsEventDetails& details) override;
44 
45  private:
46   // Sets |js_backend_|'s event handler depending on how many
47   // underlying event handlers we have.
48   void UpdateBackendEventHandler();
49 
50   WeakHandle<JsBackend> js_backend_;
51   base::ObserverList<JsEventHandler>::Unchecked js_event_handlers_;
52 
53   DISALLOW_COPY_AND_ASSIGN(SyncJsController);
54 };
55 
56 }  // namespace syncer
57 
58 #endif  // COMPONENTS_SYNC_JS_SYNC_JS_CONTROLLER_H_
59