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" |
(...skipping 16 matching lines...) Expand all Loading... | |
27 : weak_ptr_factory_(this) {} | 27 : weak_ptr_factory_(this) {} |
28 | 28 |
29 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { | 29 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { |
30 if (js_controller_) | 30 if (js_controller_) |
31 js_controller_->RemoveJsEventHandler(this); | 31 js_controller_->RemoveJsEventHandler(this); |
32 } | 32 } |
33 | 33 |
34 void SyncInternalsMessageHandler::RegisterMessages() { | 34 void SyncInternalsMessageHandler::RegisterMessages() { |
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
36 | 36 |
37 // Register for ProfileSyncService events. | |
38 ProfileSyncService* service = GetProfileSyncService(); | |
39 if (service) | |
40 service->AddObserver(this); | |
41 | |
37 // Init our link to the JsController. | 42 // Init our link to the JsController. |
38 ProfileSyncService* service = GetProfileSyncService(); | |
39 if (service) | 43 if (service) |
40 js_controller_ = service->GetJsController(); | 44 js_controller_ = service->GetJsController(); |
41 if (js_controller_) | 45 if (js_controller_) |
42 js_controller_->AddJsEventHandler(this); | 46 js_controller_->AddJsEventHandler(this); |
43 | 47 |
44 web_ui()->RegisterMessageCallback( | 48 web_ui()->RegisterMessageCallback( |
45 "getAboutInfo", | 49 "requestUpdatedAboutInfo", |
46 base::Bind(&SyncInternalsMessageHandler::OnGetAboutInfo, | 50 base::Bind(&SyncInternalsMessageHandler::OnRequestUpdatedAboutInfo, |
47 base::Unretained(this))); | 51 base::Unretained(this))); |
48 | 52 |
49 web_ui()->RegisterMessageCallback( | 53 web_ui()->RegisterMessageCallback( |
50 "getListOfTypes", | 54 "requestListOfTypes", |
51 base::Bind(&SyncInternalsMessageHandler::OnGetListOfTypes, | 55 base::Bind(&SyncInternalsMessageHandler::OnRequestListOfTypes, |
52 base::Unretained(this))); | 56 base::Unretained(this))); |
53 | 57 |
54 RegisterJsControllerCallback("getNotificationState"); | 58 RegisterJsControllerCallback("getNotificationState"); |
55 RegisterJsControllerCallback("getNotificationInfo"); | 59 RegisterJsControllerCallback("getNotificationInfo"); |
56 RegisterJsControllerCallback("getAllNodes"); | 60 RegisterJsControllerCallback("getAllNodes"); |
57 RegisterJsControllerCallback("getClientServerTraffic"); | 61 RegisterJsControllerCallback("getClientServerTraffic"); |
58 } | 62 } |
59 | 63 |
60 void SyncInternalsMessageHandler::OnGetAboutInfo(const base::ListValue* args) { | 64 void SyncInternalsMessageHandler::OnRequestUpdatedAboutInfo( |
61 // TODO(rlarocque): We should DCHECK(!args) here. See crbug.com/334431. | 65 const base::ListValue* args) { |
62 scoped_ptr<base::DictionaryValue> value = | 66 DCHECK(args->empty()); |
Dan Beam
2014/02/13 22:32:45
nit: doesn't seem necessary ^
rlarocque
2014/02/13 23:34:30
Function calls without argument checking scare me.
| |
63 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); | 67 SendAboutInfo(); |
64 web_ui()->CallJavascriptFunction( | |
65 "chrome.sync.getAboutInfo.handleReply", | |
66 *value); | |
67 } | 68 } |
68 | 69 |
69 void SyncInternalsMessageHandler::OnGetListOfTypes( | 70 void SyncInternalsMessageHandler::OnRequestListOfTypes( |
70 const base::ListValue* args) { | 71 const base::ListValue* args) { |
71 // TODO(rlarocque): We should DCHECK(!args) here. See crbug.com/334431. | 72 DCHECK(args->empty()); |
Dan Beam
2014/02/13 22:32:45
^ same
| |
72 base::ListValue type_list; | 73 base::DictionaryValue event_details; |
74 scoped_ptr<base::ListValue> type_list(new base::ListValue()); | |
73 ModelTypeSet protocol_types = syncer::ProtocolTypes(); | 75 ModelTypeSet protocol_types = syncer::ProtocolTypes(); |
74 for (ModelTypeSet::Iterator it = protocol_types.First(); | 76 for (ModelTypeSet::Iterator it = protocol_types.First(); |
75 it.Good(); it.Inc()) { | 77 it.Good(); it.Inc()) { |
76 type_list.Append(new base::StringValue(ModelTypeToString(it.Get()))); | 78 type_list->Append(new base::StringValue(ModelTypeToString(it.Get()))); |
77 } | 79 } |
80 event_details.Set("types", type_list.release()); | |
78 web_ui()->CallJavascriptFunction( | 81 web_ui()->CallJavascriptFunction( |
79 "chrome.sync.getListOfTypes.handleReply", | 82 "chrome.sync.dispatchEvent", |
80 type_list); | 83 base::StringValue("onReceivedListOfTypes"), |
84 event_details); | |
81 } | 85 } |
82 | 86 |
83 void SyncInternalsMessageHandler::HandleJsReply( | 87 void SyncInternalsMessageHandler::HandleJsReply( |
84 const std::string& name, const JsArgList& args) { | 88 const std::string& name, const JsArgList& args) { |
85 DVLOG(1) << "Handling reply for " << name << " message" | 89 DVLOG(1) << "Handling reply for " << name << " message" |
86 << " with args " << args.ToString(); | 90 << " with args " << args.ToString(); |
87 const std::string& reply_handler = "chrome.sync." + name + ".handleReply"; | 91 const std::string& reply_handler = "chrome.sync." + name + ".handleReply"; |
88 std::vector<const base::Value*> arg_list(args.Get().begin(), | 92 std::vector<const base::Value*> arg_list(args.Get().begin(), |
89 args.Get().end()); | 93 args.Get().end()); |
90 web_ui()->CallJavascriptFunction(reply_handler, arg_list); | 94 web_ui()->CallJavascriptFunction(reply_handler, arg_list); |
91 } | 95 } |
92 | 96 |
97 void SyncInternalsMessageHandler::OnStateChanged() { | |
98 SendAboutInfo(); | |
99 } | |
100 | |
93 void SyncInternalsMessageHandler::HandleJsEvent( | 101 void SyncInternalsMessageHandler::HandleJsEvent( |
94 const std::string& name, | 102 const std::string& name, |
95 const JsEventDetails& details) { | 103 const JsEventDetails& details) { |
96 DVLOG(1) << "Handling event: " << name | 104 DVLOG(1) << "Handling event: " << name |
97 << " with details " << details.ToString(); | 105 << " with details " << details.ToString(); |
98 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", | 106 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", |
99 base::StringValue(name), | 107 base::StringValue(name), |
100 details.Get()); | 108 details.Get()); |
101 } | 109 } |
102 | 110 |
103 void SyncInternalsMessageHandler::RegisterJsControllerCallback( | 111 void SyncInternalsMessageHandler::RegisterJsControllerCallback( |
104 const std::string& name) { | 112 const std::string& name) { |
105 web_ui()->RegisterMessageCallback( | 113 web_ui()->RegisterMessageCallback( |
106 name, | 114 name, |
107 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController, | 115 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController, |
108 base::Unretained(this), | 116 base::Unretained(this), |
109 name)); | 117 name)); |
110 } | 118 } |
111 | 119 |
120 void SyncInternalsMessageHandler::SendAboutInfo() { | |
121 scoped_ptr<base::DictionaryValue> value = | |
122 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); | |
123 web_ui()->CallJavascriptFunction( | |
124 "chrome.sync.dispatchEvent", | |
125 base::StringValue("onAboutInfoUpdated"), | |
126 *value); | |
127 } | |
128 | |
112 void SyncInternalsMessageHandler::ForwardToJsController( | 129 void SyncInternalsMessageHandler::ForwardToJsController( |
113 const std::string& name, | 130 const std::string& name, |
114 const base::ListValue* args) { | 131 const base::ListValue* args) { |
115 if (js_controller_) { | 132 if (js_controller_) { |
116 scoped_ptr<base::ListValue> args_copy(args->DeepCopy()); | 133 scoped_ptr<base::ListValue> args_copy(args->DeepCopy()); |
117 JsArgList js_arg_list(args_copy.get()); | 134 JsArgList js_arg_list(args_copy.get()); |
118 js_controller_->ProcessJsMessage( | 135 js_controller_->ProcessJsMessage( |
119 name, js_arg_list, | 136 name, js_arg_list, |
120 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr())); | 137 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr())); |
121 } else { | 138 } else { |
122 DLOG(WARNING) << "No sync service; dropping message " << name; | 139 DLOG(WARNING) << "No sync service; dropping message " << name; |
123 } | 140 } |
124 } | 141 } |
125 | 142 |
126 // Gets the ProfileSyncService of the underlying original profile. | 143 // Gets the ProfileSyncService of the underlying original profile. |
127 // May return NULL (e.g., if sync is disabled on the command line). | 144 // May return NULL (e.g., if sync is disabled on the command line). |
128 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { | 145 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { |
129 Profile* profile = Profile::FromWebUI(web_ui()); | 146 Profile* profile = Profile::FromWebUI(web_ui()); |
130 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); | 147 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); |
131 return factory->GetForProfile(profile->GetOriginalProfile()); | 148 return factory->GetForProfile(profile->GetOriginalProfile()); |
132 } | 149 } |
133 | 150 |
OLD | NEW |