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", | |
maniscalco
2014/05/23 00:55:00
I wonder if we could tie into the existing "regist
rlarocque
2014/05/27 20:13:26
It gets complicated.
The problem is that this reg
| |
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 scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue()); | |
maniscalco
2014/05/23 00:55:00
Too bad syncer::FooCounters don't have a common ba
rlarocque
2014/05/27 20:13:26
I'll admit that seems like a valid use of template
maniscalco
2014/05/27 20:31:07
Yep, you'd still have three listener functions, ea
| |
173 details->SetString("modelType", ModelTypeToString(type)); | |
174 details->SetString("counterType", "commit"); | |
175 details->Set("counters", counters.ToValue().release()); | |
176 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", | |
177 base::StringValue("onCountersUpdated"), | |
178 *details); | |
179 } | |
180 | |
181 void SyncInternalsMessageHandler::OnUpdateCountersUpdated( | |
182 syncer::ModelType type, | |
183 const syncer::UpdateCounters& counters) { | |
184 scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue()); | |
185 details->SetString("modelType", ModelTypeToString(type)); | |
186 details->SetString("counterType", "update"); | |
187 details->Set("counters", counters.ToValue().release()); | |
188 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", | |
189 base::StringValue("onCountersUpdated"), | |
190 *details); | |
191 } | |
192 | |
193 void SyncInternalsMessageHandler::OnStatusCountersUpdated( | |
194 syncer::ModelType type, | |
195 const syncer::StatusCounters& counters) { | |
196 scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue()); | |
197 details->SetString("modelType", ModelTypeToString(type)); | |
198 details->SetString("counterType", "status"); | |
199 details->Set("counters", counters.ToValue().release()); | |
200 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", | |
201 base::StringValue("onCountersUpdated"), | |
202 *details); | |
203 } | |
204 | |
140 void SyncInternalsMessageHandler::HandleJsEvent( | 205 void SyncInternalsMessageHandler::HandleJsEvent( |
141 const std::string& name, | 206 const std::string& name, |
142 const JsEventDetails& details) { | 207 const JsEventDetails& details) { |
143 DVLOG(1) << "Handling event: " << name | 208 DVLOG(1) << "Handling event: " << name |
144 << " with details " << details.ToString(); | 209 << " with details " << details.ToString(); |
145 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", | 210 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", |
146 base::StringValue(name), | 211 base::StringValue(name), |
147 details.Get()); | 212 details.Get()); |
148 } | 213 } |
149 | 214 |
150 void SyncInternalsMessageHandler::SendAboutInfo() { | 215 void SyncInternalsMessageHandler::SendAboutInfo() { |
151 scoped_ptr<base::DictionaryValue> value = | 216 scoped_ptr<base::DictionaryValue> value = |
152 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); | 217 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); |
153 web_ui()->CallJavascriptFunction( | 218 web_ui()->CallJavascriptFunction( |
154 "chrome.sync.dispatchEvent", | 219 "chrome.sync.dispatchEvent", |
155 base::StringValue("onAboutInfoUpdated"), | 220 base::StringValue("onAboutInfoUpdated"), |
156 *value); | 221 *value); |
157 } | 222 } |
158 | 223 |
159 // Gets the ProfileSyncService of the underlying original profile. | 224 // Gets the ProfileSyncService of the underlying original profile. |
160 // May return NULL (e.g., if sync is disabled on the command line). | 225 // May return NULL (e.g., if sync is disabled on the command line). |
161 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { | 226 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { |
162 Profile* profile = Profile::FromWebUI(web_ui()); | 227 Profile* profile = Profile::FromWebUI(web_ui()); |
163 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); | 228 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); |
164 return factory->GetForProfile(profile->GetOriginalProfile()); | 229 return factory->GetForProfile(profile->GetOriginalProfile()); |
165 } | 230 } |
166 | |
OLD | NEW |