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/util/weak_handle.h" | 17 #include "sync/internal_api/public/util/weak_handle.h" |
18 #include "sync/js/js_arg_list.h" | |
19 #include "sync/js/js_event_details.h" | 18 #include "sync/js/js_event_details.h" |
20 | 19 |
21 using syncer::JsArgList; | |
22 using syncer::JsEventDetails; | 20 using syncer::JsEventDetails; |
23 using syncer::JsReplyHandler; | |
24 using syncer::ModelTypeSet; | 21 using syncer::ModelTypeSet; |
25 using syncer::WeakHandle; | 22 using syncer::WeakHandle; |
26 | 23 |
27 SyncInternalsMessageHandler::SyncInternalsMessageHandler() | 24 SyncInternalsMessageHandler::SyncInternalsMessageHandler() |
28 : weak_ptr_factory_(this) {} | 25 : weak_ptr_factory_(this) {} |
29 | 26 |
30 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { | 27 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { |
31 if (js_controller_) | 28 if (js_controller_) |
32 js_controller_->RemoveJsEventHandler(this); | 29 js_controller_->RemoveJsEventHandler(this); |
33 | 30 |
(...skipping 15 matching lines...) Expand all Loading... |
49 web_ui()->RegisterMessageCallback( | 46 web_ui()->RegisterMessageCallback( |
50 "requestUpdatedAboutInfo", | 47 "requestUpdatedAboutInfo", |
51 base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo, | 48 base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo, |
52 base::Unretained(this))); | 49 base::Unretained(this))); |
53 | 50 |
54 web_ui()->RegisterMessageCallback( | 51 web_ui()->RegisterMessageCallback( |
55 "requestListOfTypes", | 52 "requestListOfTypes", |
56 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes, | 53 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes, |
57 base::Unretained(this))); | 54 base::Unretained(this))); |
58 | 55 |
59 RegisterJsControllerCallback("getAllNodes"); | 56 web_ui()->RegisterMessageCallback( |
| 57 "getAllNodes", |
| 58 base::Bind(&SyncInternalsMessageHandler::HandleGetAllNodes, |
| 59 base::Unretained(this))); |
60 } | 60 } |
61 | 61 |
62 void SyncInternalsMessageHandler::HandleRegisterForEvents( | 62 void SyncInternalsMessageHandler::HandleRegisterForEvents( |
63 const base::ListValue* args) { | 63 const base::ListValue* args) { |
64 DCHECK(args->empty()); | 64 DCHECK(args->empty()); |
65 | 65 |
66 ProfileSyncService* service = GetProfileSyncService(); | 66 ProfileSyncService* service = GetProfileSyncService(); |
67 if (service) { | 67 if (service) { |
68 service->AddObserver(this); | 68 service->AddObserver(this); |
69 service->AddProtocolEventObserver(this); | 69 service->AddProtocolEventObserver(this); |
(...skipping 18 matching lines...) Expand all Loading... |
88 it.Good(); it.Inc()) { | 88 it.Good(); it.Inc()) { |
89 type_list->Append(new base::StringValue(ModelTypeToString(it.Get()))); | 89 type_list->Append(new base::StringValue(ModelTypeToString(it.Get()))); |
90 } | 90 } |
91 event_details.Set("types", type_list.release()); | 91 event_details.Set("types", type_list.release()); |
92 web_ui()->CallJavascriptFunction( | 92 web_ui()->CallJavascriptFunction( |
93 "chrome.sync.dispatchEvent", | 93 "chrome.sync.dispatchEvent", |
94 base::StringValue("onReceivedListOfTypes"), | 94 base::StringValue("onReceivedListOfTypes"), |
95 event_details); | 95 event_details); |
96 } | 96 } |
97 | 97 |
98 void SyncInternalsMessageHandler::HandleJsReply( | 98 void SyncInternalsMessageHandler::HandleGetAllNodes( |
99 const std::string& name, const JsArgList& args) { | 99 const base::ListValue* args) { |
100 DVLOG(1) << "Handling reply for " << name << " message" | 100 DCHECK_EQ(1U, args->GetSize()); |
101 << " with args " << args.ToString(); | 101 int request_id = 0; |
102 const std::string& reply_handler = "chrome.sync." + name + ".handleReply"; | 102 bool success = args->GetInteger(0, &request_id); |
103 std::vector<const base::Value*> arg_list(args.Get().begin(), | 103 DCHECK(success); |
104 args.Get().end()); | 104 |
105 web_ui()->CallJavascriptFunction(reply_handler, arg_list); | 105 ProfileSyncService* service = GetProfileSyncService(); |
| 106 if (service) { |
| 107 service->GetAllNodes( |
| 108 base::Bind(&SyncInternalsMessageHandler::OnReceivedAllNodes, |
| 109 weak_ptr_factory_.GetWeakPtr(), request_id)); |
| 110 } |
| 111 } |
| 112 |
| 113 void SyncInternalsMessageHandler::OnReceivedAllNodes( |
| 114 int request_id, |
| 115 scoped_ptr<base::ListValue> nodes) { |
| 116 base::ListValue response_args; |
| 117 response_args.Append(new base::FundamentalValue(request_id)); |
| 118 response_args.Append(nodes.release()); |
| 119 |
| 120 web_ui()->CallJavascriptFunction("chrome.sync.getAllNodesCallback", |
| 121 response_args); |
106 } | 122 } |
107 | 123 |
108 void SyncInternalsMessageHandler::OnStateChanged() { | 124 void SyncInternalsMessageHandler::OnStateChanged() { |
109 SendAboutInfo(); | 125 SendAboutInfo(); |
110 } | 126 } |
111 | 127 |
112 void SyncInternalsMessageHandler::OnProtocolEvent( | 128 void SyncInternalsMessageHandler::OnProtocolEvent( |
113 const syncer::ProtocolEvent& event) { | 129 const syncer::ProtocolEvent& event) { |
114 scoped_ptr<base::DictionaryValue> value( | 130 scoped_ptr<base::DictionaryValue> value( |
115 syncer::ProtocolEvent::ToValue(event)); | 131 syncer::ProtocolEvent::ToValue(event)); |
116 web_ui()->CallJavascriptFunction( | 132 web_ui()->CallJavascriptFunction( |
117 "chrome.sync.dispatchEvent", | 133 "chrome.sync.dispatchEvent", |
118 base::StringValue("onProtocolEvent"), | 134 base::StringValue("onProtocolEvent"), |
119 *value); | 135 *value); |
120 } | 136 } |
121 | 137 |
122 void SyncInternalsMessageHandler::HandleJsEvent( | 138 void SyncInternalsMessageHandler::HandleJsEvent( |
123 const std::string& name, | 139 const std::string& name, |
124 const JsEventDetails& details) { | 140 const JsEventDetails& details) { |
125 DVLOG(1) << "Handling event: " << name | 141 DVLOG(1) << "Handling event: " << name |
126 << " with details " << details.ToString(); | 142 << " with details " << details.ToString(); |
127 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", | 143 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", |
128 base::StringValue(name), | 144 base::StringValue(name), |
129 details.Get()); | 145 details.Get()); |
130 } | 146 } |
131 | 147 |
132 void SyncInternalsMessageHandler::RegisterJsControllerCallback( | |
133 const std::string& name) { | |
134 web_ui()->RegisterMessageCallback( | |
135 name, | |
136 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController, | |
137 base::Unretained(this), | |
138 name)); | |
139 } | |
140 | |
141 void SyncInternalsMessageHandler::SendAboutInfo() { | 148 void SyncInternalsMessageHandler::SendAboutInfo() { |
142 scoped_ptr<base::DictionaryValue> value = | 149 scoped_ptr<base::DictionaryValue> value = |
143 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); | 150 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); |
144 web_ui()->CallJavascriptFunction( | 151 web_ui()->CallJavascriptFunction( |
145 "chrome.sync.dispatchEvent", | 152 "chrome.sync.dispatchEvent", |
146 base::StringValue("onAboutInfoUpdated"), | 153 base::StringValue("onAboutInfoUpdated"), |
147 *value); | 154 *value); |
148 } | 155 } |
149 | 156 |
150 void SyncInternalsMessageHandler::ForwardToJsController( | |
151 const std::string& name, | |
152 const base::ListValue* args) { | |
153 if (js_controller_) { | |
154 scoped_ptr<base::ListValue> args_copy(args->DeepCopy()); | |
155 JsArgList js_arg_list(args_copy.get()); | |
156 js_controller_->ProcessJsMessage( | |
157 name, js_arg_list, | |
158 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr())); | |
159 } else { | |
160 DLOG(WARNING) << "No sync service; dropping message " << name; | |
161 } | |
162 } | |
163 | |
164 // Gets the ProfileSyncService of the underlying original profile. | 157 // Gets the ProfileSyncService of the underlying original profile. |
165 // May return NULL (e.g., if sync is disabled on the command line). | 158 // May return NULL (e.g., if sync is disabled on the command line). |
166 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { | 159 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { |
167 Profile* profile = Profile::FromWebUI(web_ui()); | 160 Profile* profile = Profile::FromWebUI(web_ui()); |
168 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); | 161 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); |
169 return factory->GetForProfile(profile->GetOriginalProfile()); | 162 return factory->GetForProfile(profile->GetOriginalProfile()); |
170 } | 163 } |
171 | 164 |
OLD | NEW |