| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_SYNC_INTERNALS_MESSAGE_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_SYNC_INTERNALS_MESSAGE_HANDLER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_SYNC_INTERNALS_MESSAGE_HANDLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_SYNC_INTERNALS_MESSAGE_HANDLER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "components/sync/driver/sync_service_observer.h" | 16 #include "components/sync/driver/sync_service_observer.h" |
| 17 #include "components/sync/engine/cycle/type_debug_info_observer.h" | 17 #include "components/sync/engine/cycle/type_debug_info_observer.h" |
| 18 #include "components/sync/engine/events/protocol_event_observer.h" | 18 #include "components/sync/engine/events/protocol_event_observer.h" |
| 19 #include "components/sync/js/js_controller.h" | 19 #include "components/sync/js/js_controller.h" |
| 20 #include "components/sync/js/js_event_handler.h" | 20 #include "components/sync/js/js_event_handler.h" |
| 21 #include "components/version_info/channel.h" |
| 21 #include "content/public/browser/web_ui_message_handler.h" | 22 #include "content/public/browser/web_ui_message_handler.h" |
| 22 | 23 |
| 23 class SigninManagerBase; | |
| 24 | |
| 25 namespace browser_sync { | 24 namespace browser_sync { |
| 26 class ProfileSyncService; | 25 class ProfileSyncService; |
| 27 } // namespace browser_sync | 26 } // namespace browser_sync |
| 28 | 27 |
| 29 namespace syncer { | 28 namespace syncer { |
| 30 class SyncService; | 29 class SyncService; |
| 31 } // namespace syncer | 30 } // namespace syncer |
| 32 | 31 |
| 33 // Interface to abstract away the creation of the about-sync value dictionary. | |
| 34 class AboutSyncDataExtractor { | |
| 35 public: | |
| 36 // Given state about sync, extracts various interesting fields and populates | |
| 37 // a tree of base::Value objects. | |
| 38 virtual std::unique_ptr<base::DictionaryValue> ConstructAboutInformation( | |
| 39 syncer::SyncService* service, | |
| 40 SigninManagerBase* signin) = 0; | |
| 41 virtual ~AboutSyncDataExtractor() {} | |
| 42 }; | |
| 43 | |
| 44 // The implementation for the chrome://sync-internals page. | 32 // The implementation for the chrome://sync-internals page. |
| 45 class SyncInternalsMessageHandler : public content::WebUIMessageHandler, | 33 class SyncInternalsMessageHandler : public content::WebUIMessageHandler, |
| 46 public syncer::JsEventHandler, | 34 public syncer::JsEventHandler, |
| 47 public syncer::SyncServiceObserver, | 35 public syncer::SyncServiceObserver, |
| 48 public syncer::ProtocolEventObserver, | 36 public syncer::ProtocolEventObserver, |
| 49 public syncer::TypeDebugInfoObserver { | 37 public syncer::TypeDebugInfoObserver { |
| 50 public: | 38 public: |
| 51 SyncInternalsMessageHandler(); | 39 SyncInternalsMessageHandler(); |
| 52 ~SyncInternalsMessageHandler() override; | 40 ~SyncInternalsMessageHandler() override; |
| 53 | 41 |
| 42 // content::WebUIMessageHandler implementation. |
| 43 void OnJavascriptDisallowed() override; |
| 54 void RegisterMessages() override; | 44 void RegisterMessages() override; |
| 55 | 45 |
| 56 // Sets up observers to receive events and forward them to the UI. | 46 // Sets up observers to receive events and forward them to the UI. |
| 57 void HandleRegisterForEvents(const base::ListValue* args); | 47 void HandleRegisterForEvents(const base::ListValue* args); |
| 58 | 48 |
| 59 // Sets up observers to receive per-type counters and forward them to the UI. | 49 // Sets up observers to receive per-type counters and forward them to the UI. |
| 60 void HandleRegisterForPerTypeCounters(const base::ListValue* args); | 50 void HandleRegisterForPerTypeCounters(const base::ListValue* args); |
| 61 | 51 |
| 62 // Fires an event to send updated info back to the page. | 52 // Fires an event to send updated info back to the page. |
| 63 void HandleRequestUpdatedAboutInfo(const base::ListValue* args); | 53 void HandleRequestUpdatedAboutInfo(const base::ListValue* args); |
| 64 | 54 |
| 65 // Fires and event to send the list of types back to the page. | 55 // Fires and event to send the list of types back to the page. |
| 66 void HandleRequestListOfTypes(const base::ListValue* args); | 56 void HandleRequestListOfTypes(const base::ListValue* args); |
| 67 | 57 |
| 68 // Handler for getAllNodes message. Needs a |request_id| argument. | 58 // Handler for getAllNodes message. Needs a |request_id| argument. |
| 69 void HandleGetAllNodes(const base::ListValue* args); | 59 void HandleGetAllNodes(const base::ListValue* args); |
| 70 | 60 |
| 71 // syncer::JsEventHandler implementation. | 61 // syncer::JsEventHandler implementation. |
| 72 void HandleJsEvent(const std::string& name, | 62 void HandleJsEvent(const std::string& name, |
| 73 const syncer::JsEventDetails& details) override; | 63 const syncer::JsEventDetails& details) override; |
| 74 | 64 |
| 75 // Callback used in GetAllNodes. | 65 // Callback used in GetAllNodes. |
| 76 void OnReceivedAllNodes(int request_id, | 66 void OnReceivedAllNodes(int request_id, |
| 77 std::unique_ptr<base::ListValue> nodes); | 67 std::unique_ptr<base::ListValue> nodes); |
| 78 | 68 |
| 79 // syncer::SyncServiceObserver implementation. | 69 // syncer::SyncServiceObserver implementation. |
| 80 void OnStateChanged(syncer::SyncService* sync) override; | 70 void OnStateChanged(syncer::SyncService* sync) override; |
| 81 | 71 |
| 82 // ProtocolEventObserver implementation. | 72 // syncer::ProtocolEventObserver implementation. |
| 83 void OnProtocolEvent(const syncer::ProtocolEvent& e) override; | 73 void OnProtocolEvent(const syncer::ProtocolEvent& e) override; |
| 84 | 74 |
| 85 // TypeDebugInfoObserver implementation. | 75 // syncer::TypeDebugInfoObserver implementation. |
| 86 void OnCommitCountersUpdated(syncer::ModelType type, | 76 void OnCommitCountersUpdated(syncer::ModelType type, |
| 87 const syncer::CommitCounters& counters) override; | 77 const syncer::CommitCounters& counters) override; |
| 88 void OnUpdateCountersUpdated(syncer::ModelType type, | 78 void OnUpdateCountersUpdated(syncer::ModelType type, |
| 89 const syncer::UpdateCounters& counters) override; | 79 const syncer::UpdateCounters& counters) override; |
| 90 void OnStatusCountersUpdated(syncer::ModelType type, | 80 void OnStatusCountersUpdated(syncer::ModelType type, |
| 91 const syncer::StatusCounters& counters) override; | 81 const syncer::StatusCounters& counters) override; |
| 92 | 82 |
| 93 // Helper to emit counter updates. | 83 // Helper to emit counter updates. |
| 94 // | 84 // |
| 95 // Used in implementation of On*CounterUpdated methods. Emits the given | 85 // Used in implementation of On*CounterUpdated methods. Emits the given |
| 96 // dictionary value with additional data to specify the model type and | 86 // dictionary value with additional data to specify the model type and |
| 97 // counter type. | 87 // counter type. |
| 98 void EmitCounterUpdate(syncer::ModelType type, | 88 void EmitCounterUpdate(syncer::ModelType type, |
| 99 const std::string& counter_type, | 89 const std::string& counter_type, |
| 100 std::unique_ptr<base::DictionaryValue> value); | 90 std::unique_ptr<base::DictionaryValue> value); |
| 101 | 91 |
| 102 protected: | 92 protected: |
| 103 // Constructor used for unit testing to override the about sync info. | 93 using SyncServiceProvider = base::RepeatingCallback<syncer::SyncService*()>; |
| 104 SyncInternalsMessageHandler( | 94 |
| 105 std::unique_ptr<AboutSyncDataExtractor> about_sync_data_extractor); | 95 using AboutSyncDataDelegate = |
| 96 base::RepeatingCallback<std::unique_ptr<base::DictionaryValue>( |
| 97 syncer::SyncService* service, |
| 98 version_info::Channel channel)>; |
| 99 |
| 100 // Constructor used for unit testing to override dependencies. |
| 101 SyncInternalsMessageHandler(SyncServiceProvider sync_service_provider, |
| 102 AboutSyncDataDelegate about_sync_data_delegate); |
| 106 | 103 |
| 107 private: | 104 private: |
| 108 // Fetches updated aboutInfo and sends it to the page in the form of an | 105 // Fetches updated aboutInfo and sends it to the page in the form of an |
| 109 // onAboutInfoUpdated event. | 106 // onAboutInfoUpdated event. |
| 110 void SendAboutInfo(); | 107 void SendAboutInfo(); |
| 111 | 108 |
| 112 browser_sync::ProfileSyncService* GetProfileSyncService(); | 109 // Gets the ProfileSyncService of the underlying original profile. May return |
| 110 // nullptr (e.g., if sync is disabled on the command line). Shouldn't be |
| 111 // called directly, but instead through |sync_service_provider_|. |
| 112 syncer::SyncService* BindForSyncServiceProvider(); |
| 113 |
| 114 // Sends a dispatch event to the UI. Javascript must be enabled. |
| 115 void DispatchEvent(const std::string& name, const base::Value& details_value); |
| 116 |
| 117 // Unregisters for notifications from all notifications coming from the sync |
| 118 // machinery. Leaves notifications hooked into the UI alone. |
| 119 void UnregisterModelNotifications(); |
| 113 | 120 |
| 114 base::WeakPtr<syncer::JsController> js_controller_; | 121 base::WeakPtr<syncer::JsController> js_controller_; |
| 115 | 122 |
| 116 // A flag used to prevent double-registration with ProfileSyncService. | 123 // A flag used to prevent double-registration with ProfileSyncService. |
| 117 bool is_registered_ = false; | 124 bool is_registered_ = false; |
| 118 | 125 |
| 119 // A flag used to prevent double-registration as TypeDebugInfoObserver with | 126 // A flag used to prevent double-registration as TypeDebugInfoObserver with |
| 120 // ProfileSyncService. | 127 // ProfileSyncService. |
| 121 bool is_registered_for_counters_ = false; | 128 bool is_registered_for_counters_ = false; |
| 122 | 129 |
| 130 // An abstraction of who provides the SyncService. |
| 131 SyncServiceProvider sync_service_provider_; |
| 132 |
| 123 // An abstraction of who creates the about sync info value map. | 133 // An abstraction of who creates the about sync info value map. |
| 124 std::unique_ptr<AboutSyncDataExtractor> about_sync_data_extractor_; | 134 AboutSyncDataDelegate about_sync_data_delegate_; |
| 125 | 135 |
| 126 base::WeakPtrFactory<SyncInternalsMessageHandler> weak_ptr_factory_; | 136 base::WeakPtrFactory<SyncInternalsMessageHandler> weak_ptr_factory_; |
| 127 | 137 |
| 128 DISALLOW_COPY_AND_ASSIGN(SyncInternalsMessageHandler); | 138 DISALLOW_COPY_AND_ASSIGN(SyncInternalsMessageHandler); |
| 129 }; | 139 }; |
| 130 | 140 |
| 131 #endif // CHROME_BROWSER_UI_WEBUI_SYNC_INTERNALS_MESSAGE_HANDLER_H_ | 141 #endif // CHROME_BROWSER_UI_WEBUI_SYNC_INTERNALS_MESSAGE_HANDLER_H_ |
| OLD | NEW |