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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/sync/about_sync_util.h" | 11 #include "chrome/browser/sync/about_sync_util.h" |
12 #include "chrome/browser/sync/profile_sync_service.h" | 12 #include "chrome/browser/sync/profile_sync_service.h" |
13 #include "chrome/browser/sync/profile_sync_service_factory.h" | 13 #include "chrome/browser/sync/profile_sync_service_factory.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/web_ui.h" | 15 #include "content/public/browser/web_ui.h" |
16 #include "sync/internal_api/public/events/protocol_event.h" | 16 #include "sync/internal_api/public/events/protocol_event.h" |
| 17 #include "sync/internal_api/public/sessions/commit_counters.h" |
| 18 #include "sync/internal_api/public/sessions/status_counters.h" |
| 19 #include "sync/internal_api/public/sessions/update_counters.h" |
17 #include "sync/internal_api/public/util/weak_handle.h" | 20 #include "sync/internal_api/public/util/weak_handle.h" |
18 #include "sync/js/js_event_details.h" | 21 #include "sync/js/js_event_details.h" |
19 | 22 |
20 using syncer::JsEventDetails; | 23 using syncer::JsEventDetails; |
21 using syncer::ModelTypeSet; | 24 using syncer::ModelTypeSet; |
22 using syncer::WeakHandle; | 25 using syncer::WeakHandle; |
23 | 26 |
24 SyncInternalsMessageHandler::SyncInternalsMessageHandler() | 27 SyncInternalsMessageHandler::SyncInternalsMessageHandler() |
25 : is_registered_(false), | 28 : is_registered_(false), |
26 weak_ptr_factory_(this) {} | 29 is_registered_for_counters_(false), |
| 30 weak_ptr_factory_(this) { |
| 31 } |
27 | 32 |
28 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { | 33 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { |
29 if (js_controller_) | 34 if (js_controller_) |
30 js_controller_->RemoveJsEventHandler(this); | 35 js_controller_->RemoveJsEventHandler(this); |
31 | 36 |
32 ProfileSyncService* service = GetProfileSyncService(); | 37 ProfileSyncService* service = GetProfileSyncService(); |
33 if (service && service->HasObserver(this)) { | 38 if (service && service->HasObserver(this)) { |
34 service->RemoveObserver(this); | 39 service->RemoveObserver(this); |
35 service->RemoveProtocolEventObserver(this); | 40 service->RemoveProtocolEventObserver(this); |
36 } | 41 } |
| 42 |
| 43 if (service && is_registered_for_counters_) { |
| 44 service->RemoveTypeDebugInfoObserver(this); |
| 45 } |
37 } | 46 } |
38 | 47 |
39 void SyncInternalsMessageHandler::RegisterMessages() { | 48 void SyncInternalsMessageHandler::RegisterMessages() { |
40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 49 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
41 | 50 |
42 web_ui()->RegisterMessageCallback( | 51 web_ui()->RegisterMessageCallback( |
43 "registerForEvents", | 52 "registerForEvents", |
44 base::Bind(&SyncInternalsMessageHandler::HandleRegisterForEvents, | 53 base::Bind(&SyncInternalsMessageHandler::HandleRegisterForEvents, |
45 base::Unretained(this))); | 54 base::Unretained(this))); |
46 | 55 |
47 web_ui()->RegisterMessageCallback( | 56 web_ui()->RegisterMessageCallback( |
| 57 "registerForPerTypeCounters", |
| 58 base::Bind(&SyncInternalsMessageHandler::HandleRegisterForPerTypeCounters, |
| 59 base::Unretained(this))); |
| 60 |
| 61 web_ui()->RegisterMessageCallback( |
48 "requestUpdatedAboutInfo", | 62 "requestUpdatedAboutInfo", |
49 base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo, | 63 base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo, |
50 base::Unretained(this))); | 64 base::Unretained(this))); |
51 | 65 |
52 web_ui()->RegisterMessageCallback( | 66 web_ui()->RegisterMessageCallback( |
53 "requestListOfTypes", | 67 "requestListOfTypes", |
54 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes, | 68 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes, |
55 base::Unretained(this))); | 69 base::Unretained(this))); |
56 | 70 |
57 web_ui()->RegisterMessageCallback( | 71 web_ui()->RegisterMessageCallback( |
(...skipping 12 matching lines...) Expand all Loading... |
70 ProfileSyncService* service = GetProfileSyncService(); | 84 ProfileSyncService* service = GetProfileSyncService(); |
71 if (service && !is_registered_) { | 85 if (service && !is_registered_) { |
72 service->AddObserver(this); | 86 service->AddObserver(this); |
73 service->AddProtocolEventObserver(this); | 87 service->AddProtocolEventObserver(this); |
74 js_controller_ = service->GetJsController(); | 88 js_controller_ = service->GetJsController(); |
75 js_controller_->AddJsEventHandler(this); | 89 js_controller_->AddJsEventHandler(this); |
76 is_registered_ = true; | 90 is_registered_ = true; |
77 } | 91 } |
78 } | 92 } |
79 | 93 |
| 94 void SyncInternalsMessageHandler::HandleRegisterForPerTypeCounters( |
| 95 const base::ListValue* args) { |
| 96 DCHECK(args->empty()); |
| 97 |
| 98 ProfileSyncService* service = GetProfileSyncService(); |
| 99 if (service && !is_registered_for_counters_) { |
| 100 service->AddTypeDebugInfoObserver(this); |
| 101 is_registered_for_counters_ = true; |
| 102 } else { |
| 103 // Re-register to ensure counters get re-emitted. |
| 104 service->RemoveTypeDebugInfoObserver(this); |
| 105 service->AddTypeDebugInfoObserver(this); |
| 106 } |
| 107 } |
| 108 |
80 void SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo( | 109 void SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo( |
81 const base::ListValue* args) { | 110 const base::ListValue* args) { |
82 DCHECK(args->empty()); | 111 DCHECK(args->empty()); |
83 SendAboutInfo(); | 112 SendAboutInfo(); |
84 } | 113 } |
85 | 114 |
86 void SyncInternalsMessageHandler::HandleRequestListOfTypes( | 115 void SyncInternalsMessageHandler::HandleRequestListOfTypes( |
87 const base::ListValue* args) { | 116 const base::ListValue* args) { |
88 DCHECK(args->empty()); | 117 DCHECK(args->empty()); |
89 base::DictionaryValue event_details; | 118 base::DictionaryValue event_details; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 void SyncInternalsMessageHandler::OnProtocolEvent( | 159 void SyncInternalsMessageHandler::OnProtocolEvent( |
131 const syncer::ProtocolEvent& event) { | 160 const syncer::ProtocolEvent& event) { |
132 scoped_ptr<base::DictionaryValue> value( | 161 scoped_ptr<base::DictionaryValue> value( |
133 syncer::ProtocolEvent::ToValue(event)); | 162 syncer::ProtocolEvent::ToValue(event)); |
134 web_ui()->CallJavascriptFunction( | 163 web_ui()->CallJavascriptFunction( |
135 "chrome.sync.dispatchEvent", | 164 "chrome.sync.dispatchEvent", |
136 base::StringValue("onProtocolEvent"), | 165 base::StringValue("onProtocolEvent"), |
137 *value); | 166 *value); |
138 } | 167 } |
139 | 168 |
| 169 void SyncInternalsMessageHandler::OnCommitCountersUpdated( |
| 170 syncer::ModelType type, |
| 171 const syncer::CommitCounters& counters) { |
| 172 EmitCounterUpdate(type, "commit", counters.ToValue()); |
| 173 } |
| 174 |
| 175 void SyncInternalsMessageHandler::OnUpdateCountersUpdated( |
| 176 syncer::ModelType type, |
| 177 const syncer::UpdateCounters& counters) { |
| 178 EmitCounterUpdate(type, "update", counters.ToValue()); |
| 179 } |
| 180 |
| 181 void SyncInternalsMessageHandler::OnStatusCountersUpdated( |
| 182 syncer::ModelType type, |
| 183 const syncer::StatusCounters& counters) { |
| 184 EmitCounterUpdate(type, "status", counters.ToValue()); |
| 185 } |
| 186 |
| 187 void SyncInternalsMessageHandler::EmitCounterUpdate( |
| 188 syncer::ModelType type, |
| 189 const std::string& counter_type, |
| 190 scoped_ptr<base::DictionaryValue> value) { |
| 191 scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue()); |
| 192 details->SetString("modelType", ModelTypeToString(type)); |
| 193 details->SetString("counterType", counter_type); |
| 194 details->Set("counters", value.release()); |
| 195 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", |
| 196 base::StringValue("onCountersUpdated"), |
| 197 *details); |
| 198 } |
| 199 |
140 void SyncInternalsMessageHandler::HandleJsEvent( | 200 void SyncInternalsMessageHandler::HandleJsEvent( |
141 const std::string& name, | 201 const std::string& name, |
142 const JsEventDetails& details) { | 202 const JsEventDetails& details) { |
143 DVLOG(1) << "Handling event: " << name | 203 DVLOG(1) << "Handling event: " << name |
144 << " with details " << details.ToString(); | 204 << " with details " << details.ToString(); |
145 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", | 205 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", |
146 base::StringValue(name), | 206 base::StringValue(name), |
147 details.Get()); | 207 details.Get()); |
148 } | 208 } |
149 | 209 |
150 void SyncInternalsMessageHandler::SendAboutInfo() { | 210 void SyncInternalsMessageHandler::SendAboutInfo() { |
151 scoped_ptr<base::DictionaryValue> value = | 211 scoped_ptr<base::DictionaryValue> value = |
152 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); | 212 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); |
153 web_ui()->CallJavascriptFunction( | 213 web_ui()->CallJavascriptFunction( |
154 "chrome.sync.dispatchEvent", | 214 "chrome.sync.dispatchEvent", |
155 base::StringValue("onAboutInfoUpdated"), | 215 base::StringValue("onAboutInfoUpdated"), |
156 *value); | 216 *value); |
157 } | 217 } |
158 | 218 |
159 // Gets the ProfileSyncService of the underlying original profile. | 219 // Gets the ProfileSyncService of the underlying original profile. |
160 // May return NULL (e.g., if sync is disabled on the command line). | 220 // May return NULL (e.g., if sync is disabled on the command line). |
161 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { | 221 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { |
162 Profile* profile = Profile::FromWebUI(web_ui()); | 222 Profile* profile = Profile::FromWebUI(web_ui()); |
163 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); | 223 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); |
164 return factory->GetForProfile(profile->GetOriginalProfile()); | 224 return factory->GetForProfile(profile->GetOriginalProfile()); |
165 } | 225 } |
166 | |
OLD | NEW |