| 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 #include "chrome/browser/ui/webui/sync_internals_message_handler.h" | 5 #include "chrome/browser/ui/webui/sync_internals_message_handler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 return integer; | 52 return integer; |
| 53 } | 53 } |
| 54 return 0; | 54 return 0; |
| 55 } | 55 } |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
| 59 SyncInternalsMessageHandler::SyncInternalsMessageHandler() | 59 SyncInternalsMessageHandler::SyncInternalsMessageHandler() |
| 60 : SyncInternalsMessageHandler( | 60 : SyncInternalsMessageHandler( |
| 61 base::BindRepeating( | 61 base::BindRepeating( |
| 62 &SyncInternalsMessageHandler::BindForSyncServiceProvider, | |
| 63 base::Unretained(this)), | |
| 64 base::BindRepeating( | |
| 65 &syncer::sync_ui_util::ConstructAboutInformation)) {} | 62 &syncer::sync_ui_util::ConstructAboutInformation)) {} |
| 66 | 63 |
| 67 SyncInternalsMessageHandler::SyncInternalsMessageHandler( | 64 SyncInternalsMessageHandler::SyncInternalsMessageHandler( |
| 68 SyncServiceProvider sync_service_provider, | |
| 69 AboutSyncDataDelegate about_sync_data_delegate) | 65 AboutSyncDataDelegate about_sync_data_delegate) |
| 70 : sync_service_provider_(std::move(sync_service_provider)), | 66 : about_sync_data_delegate_(std::move(about_sync_data_delegate)), |
| 71 about_sync_data_delegate_(std::move(about_sync_data_delegate)), | |
| 72 weak_ptr_factory_(this) {} | 67 weak_ptr_factory_(this) {} |
| 73 | 68 |
| 74 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { | 69 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { |
| 75 UnregisterModelNotifications(); | 70 UnregisterModelNotifications(); |
| 76 } | 71 } |
| 77 | 72 |
| 78 void SyncInternalsMessageHandler::OnJavascriptDisallowed() { | 73 void SyncInternalsMessageHandler::OnJavascriptDisallowed() { |
| 79 // Invaliding weak ptrs works well here because the only weak ptr we vend is | 74 // Invaliding weak ptrs works well here because the only weak ptr we vend is |
| 80 // to the sync side to give us information that should be used to populate the | 75 // to the sync side to give us information that should be used to populate the |
| 81 // javascript side. If javascript is disallowed, we don't care about updating | 76 // javascript side. If javascript is disallowed, we don't care about updating |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } | 120 } |
| 126 | 121 |
| 127 void SyncInternalsMessageHandler::HandleRegisterForEvents( | 122 void SyncInternalsMessageHandler::HandleRegisterForEvents( |
| 128 const ListValue* args) { | 123 const ListValue* args) { |
| 129 DCHECK(args->empty()); | 124 DCHECK(args->empty()); |
| 130 AllowJavascript(); | 125 AllowJavascript(); |
| 131 | 126 |
| 132 // is_registered_ flag protects us from double-registering. This could | 127 // is_registered_ flag protects us from double-registering. This could |
| 133 // happen on a page refresh, where the JavaScript gets re-run but the | 128 // happen on a page refresh, where the JavaScript gets re-run but the |
| 134 // message handler remains unchanged. | 129 // message handler remains unchanged. |
| 135 SyncService* service = sync_service_provider_.Run(); | 130 SyncService* service = GetSyncService(); |
| 136 if (service && !is_registered_) { | 131 if (service && !is_registered_) { |
| 137 service->AddObserver(this); | 132 service->AddObserver(this); |
| 138 service->AddProtocolEventObserver(this); | 133 service->AddProtocolEventObserver(this); |
| 139 js_controller_ = service->GetJsController(); | 134 js_controller_ = service->GetJsController(); |
| 140 js_controller_->AddJsEventHandler(this); | 135 js_controller_->AddJsEventHandler(this); |
| 141 is_registered_ = true; | 136 is_registered_ = true; |
| 142 } | 137 } |
| 143 } | 138 } |
| 144 | 139 |
| 145 void SyncInternalsMessageHandler::HandleRegisterForPerTypeCounters( | 140 void SyncInternalsMessageHandler::HandleRegisterForPerTypeCounters( |
| 146 const ListValue* args) { | 141 const ListValue* args) { |
| 147 DCHECK(args->empty()); | 142 DCHECK(args->empty()); |
| 148 AllowJavascript(); | 143 AllowJavascript(); |
| 149 | 144 |
| 150 SyncService* service = sync_service_provider_.Run(); | 145 SyncService* service = GetSyncService(); |
| 151 if (!service) | 146 if (!service) |
| 152 return; | 147 return; |
| 153 | 148 |
| 154 if (!is_registered_for_counters_) { | 149 if (!is_registered_for_counters_) { |
| 155 service->AddTypeDebugInfoObserver(this); | 150 service->AddTypeDebugInfoObserver(this); |
| 156 is_registered_for_counters_ = true; | 151 is_registered_for_counters_ = true; |
| 157 } else { | 152 } else { |
| 158 // Re-register to ensure counters get re-emitted. | 153 // Re-register to ensure counters get re-emitted. |
| 159 service->RemoveTypeDebugInfoObserver(this); | 154 service->RemoveTypeDebugInfoObserver(this); |
| 160 service->AddTypeDebugInfoObserver(this); | 155 service->AddTypeDebugInfoObserver(this); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 185 } | 180 } |
| 186 | 181 |
| 187 void SyncInternalsMessageHandler::HandleGetAllNodes(const ListValue* args) { | 182 void SyncInternalsMessageHandler::HandleGetAllNodes(const ListValue* args) { |
| 188 DCHECK_EQ(1U, args->GetSize()); | 183 DCHECK_EQ(1U, args->GetSize()); |
| 189 AllowJavascript(); | 184 AllowJavascript(); |
| 190 | 185 |
| 191 int request_id = 0; | 186 int request_id = 0; |
| 192 bool success = ExtractIntegerValue(args, &request_id); | 187 bool success = ExtractIntegerValue(args, &request_id); |
| 193 DCHECK(success); | 188 DCHECK(success); |
| 194 | 189 |
| 195 SyncService* service = sync_service_provider_.Run(); | 190 SyncService* service = GetSyncService(); |
| 196 if (service) { | 191 if (service) { |
| 197 // This opens up the possibility of non-javascript code calling us | 192 // This opens up the possibility of non-javascript code calling us |
| 198 // asynchronously, and potentially at times we're not allowed to call into | 193 // asynchronously, and potentially at times we're not allowed to call into |
| 199 // the javascript side. We guard against this by invalidating this weak ptr | 194 // the javascript side. We guard against this by invalidating this weak ptr |
| 200 // should javascript become disallowed. | 195 // should javascript become disallowed. |
| 201 service->GetAllNodes( | 196 service->GetAllNodes( |
| 202 base::Bind(&SyncInternalsMessageHandler::OnReceivedAllNodes, | 197 base::Bind(&SyncInternalsMessageHandler::OnReceivedAllNodes, |
| 203 weak_ptr_factory_.GetWeakPtr(), request_id)); | 198 weak_ptr_factory_.GetWeakPtr(), request_id)); |
| 204 } | 199 } |
| 205 } | 200 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 272 |
| 278 void SyncInternalsMessageHandler::HandleJsEvent( | 273 void SyncInternalsMessageHandler::HandleJsEvent( |
| 279 const std::string& name, | 274 const std::string& name, |
| 280 const JsEventDetails& details) { | 275 const JsEventDetails& details) { |
| 281 DVLOG(1) << "Handling event: " << name | 276 DVLOG(1) << "Handling event: " << name |
| 282 << " with details " << details.ToString(); | 277 << " with details " << details.ToString(); |
| 283 DispatchEvent(name, details.Get()); | 278 DispatchEvent(name, details.Get()); |
| 284 } | 279 } |
| 285 | 280 |
| 286 void SyncInternalsMessageHandler::SendAboutInfo() { | 281 void SyncInternalsMessageHandler::SendAboutInfo() { |
| 287 std::unique_ptr<DictionaryValue> value = about_sync_data_delegate_.Run( | 282 std::unique_ptr<DictionaryValue> value = |
| 288 sync_service_provider_.Run(), chrome::GetChannel()); | 283 about_sync_data_delegate_.Run(GetSyncService(), chrome::GetChannel()); |
| 289 DispatchEvent(syncer::sync_ui_util::kOnAboutInfoUpdated, *value); | 284 DispatchEvent(syncer::sync_ui_util::kOnAboutInfoUpdated, *value); |
| 290 } | 285 } |
| 291 | 286 |
| 292 SyncService* SyncInternalsMessageHandler::BindForSyncServiceProvider() { | 287 SyncService* SyncInternalsMessageHandler::GetSyncService() { |
| 293 return ProfileSyncServiceFactory::GetForProfile( | 288 return ProfileSyncServiceFactory::GetForProfile( |
| 294 Profile::FromWebUI(web_ui())->GetOriginalProfile()); | 289 Profile::FromWebUI(web_ui())->GetOriginalProfile()); |
| 295 } | 290 } |
| 296 | 291 |
| 297 void SyncInternalsMessageHandler::DispatchEvent(const std::string& name, | 292 void SyncInternalsMessageHandler::DispatchEvent(const std::string& name, |
| 298 const Value& details_value) { | 293 const Value& details_value) { |
| 299 CallJavascriptFunction(syncer::sync_ui_util::kDispatchEvent, Value(name), | 294 CallJavascriptFunction(syncer::sync_ui_util::kDispatchEvent, Value(name), |
| 300 details_value); | 295 details_value); |
| 301 } | 296 } |
| 302 | 297 |
| 303 void SyncInternalsMessageHandler::UnregisterModelNotifications() { | 298 void SyncInternalsMessageHandler::UnregisterModelNotifications() { |
| 304 SyncService* service = sync_service_provider_.Run(); | 299 SyncService* service = GetSyncService(); |
| 305 if (!service) | 300 if (!service) |
| 306 return; | 301 return; |
| 307 | 302 |
| 308 // Cannot use ScopedObserver to do all the tracking because most don't follow | 303 // Cannot use ScopedObserver to do all the tracking because most don't follow |
| 309 // AddObserver/RemoveObserver method naming style. | 304 // AddObserver/RemoveObserver method naming style. |
| 310 if (is_registered_) { | 305 if (is_registered_) { |
| 311 DCHECK(js_controller_); | 306 DCHECK(js_controller_); |
| 312 service->RemoveObserver(this); | 307 service->RemoveObserver(this); |
| 313 service->RemoveProtocolEventObserver(this); | 308 service->RemoveProtocolEventObserver(this); |
| 314 js_controller_->RemoveJsEventHandler(this); | 309 js_controller_->RemoveJsEventHandler(this); |
| 315 js_controller_ = nullptr; | 310 js_controller_ = nullptr; |
| 316 is_registered_ = false; | 311 is_registered_ = false; |
| 317 } | 312 } |
| 318 | 313 |
| 319 if (is_registered_for_counters_) { | 314 if (is_registered_for_counters_) { |
| 320 service->RemoveTypeDebugInfoObserver(this); | 315 service->RemoveTypeDebugInfoObserver(this); |
| 321 is_registered_for_counters_ = false; | 316 is_registered_for_counters_ = false; |
| 322 } | 317 } |
| 323 } | 318 } |
| OLD | NEW |