OLD | NEW |
| (Empty) |
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_SYNC_JS_CONTROLLER_H_ | |
6 #define CHROME_BROWSER_SYNC_SYNC_JS_CONTROLLER_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/observer_list.h" | |
16 #include "chrome/browser/sync/js/js_arg_list.h" | |
17 #include "chrome/browser/sync/js/js_controller.h" | |
18 #include "chrome/browser/sync/js/js_event_handler.h" | |
19 #include "chrome/browser/sync/util/weak_handle.h" | |
20 | |
21 namespace browser_sync { | |
22 | |
23 class JsBackend; | |
24 | |
25 // A class that mediates between the sync JsEventHandlers and the sync | |
26 // JsBackend. | |
27 class SyncJsController | |
28 : public JsController, public JsEventHandler, | |
29 public base::SupportsWeakPtr<SyncJsController> { | |
30 public: | |
31 SyncJsController(); | |
32 | |
33 virtual ~SyncJsController(); | |
34 | |
35 // Sets the backend to route all messages to (if initialized). | |
36 // Sends any queued-up messages if |backend| is initialized. | |
37 void AttachJsBackend(const WeakHandle<JsBackend>& js_backend); | |
38 | |
39 // JsController implementation. | |
40 virtual void AddJsEventHandler(JsEventHandler* event_handler) OVERRIDE; | |
41 virtual void RemoveJsEventHandler(JsEventHandler* event_handler) OVERRIDE; | |
42 // Queues up any messages that are sent when there is no attached | |
43 // initialized backend. | |
44 virtual void ProcessJsMessage( | |
45 const std::string& name, const JsArgList& args, | |
46 const WeakHandle<JsReplyHandler>& reply_handler) OVERRIDE; | |
47 | |
48 // JsEventHandler implementation. | |
49 virtual void HandleJsEvent(const std::string& name, | |
50 const JsEventDetails& details) OVERRIDE; | |
51 | |
52 private: | |
53 // A struct used to hold the arguments to ProcessJsMessage() for | |
54 // future invocation. | |
55 struct PendingJsMessage { | |
56 std::string name; | |
57 JsArgList args; | |
58 WeakHandle<JsReplyHandler> reply_handler; | |
59 | |
60 PendingJsMessage(const std::string& name, const JsArgList& args, | |
61 const WeakHandle<JsReplyHandler>& reply_handler); | |
62 | |
63 ~PendingJsMessage(); | |
64 }; | |
65 | |
66 typedef std::vector<PendingJsMessage> PendingJsMessageList; | |
67 | |
68 // Sets |js_backend_|'s event handler depending on how many | |
69 // underlying event handlers we have. | |
70 void UpdateBackendEventHandler(); | |
71 | |
72 WeakHandle<JsBackend> js_backend_; | |
73 ObserverList<JsEventHandler> js_event_handlers_; | |
74 PendingJsMessageList pending_js_messages_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(SyncJsController); | |
77 }; | |
78 | |
79 } // namespace browser_sync | |
80 | |
81 #endif // CHROME_BROWSER_SYNC_SYNC_JS_CONTROLLER_H_ | |
OLD | NEW |