OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/foreign_session_handler.h" | 5 #include "chrome/browser/ui/webui/foreign_session_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/i18n/time_formatting.h" | 13 #include "base/i18n/time_formatting.h" |
14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
16 #include "base/prefs/scoped_user_pref_update.h" | 16 #include "base/prefs/scoped_user_pref_update.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/browser/chrome_notification_types.h" | |
22 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/sessions/session_restore.h" | 22 #include "chrome/browser/sessions/session_restore.h" |
24 #include "chrome/browser/sync/profile_sync_service.h" | 23 #include "chrome/browser/sync/profile_sync_service.h" |
25 #include "chrome/browser/sync/profile_sync_service_factory.h" | 24 #include "chrome/browser/sync/profile_sync_service_factory.h" |
26 #include "chrome/browser/ui/host_desktop.h" | 25 #include "chrome/browser/ui/host_desktop.h" |
27 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 26 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
28 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
29 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
30 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
31 #include "components/pref_registry/pref_registry_syncable.h" | 30 #include "components/pref_registry/pref_registry_syncable.h" |
32 #include "content/public/browser/notification_service.h" | |
33 #include "content/public/browser/notification_source.h" | |
34 #include "content/public/browser/url_data_source.h" | 31 #include "content/public/browser/url_data_source.h" |
35 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
36 #include "content/public/browser/web_ui.h" | 33 #include "content/public/browser/web_ui.h" |
37 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
38 #include "ui/base/l10n/time_format.h" | 35 #include "ui/base/l10n/time_format.h" |
39 #include "ui/base/webui/web_ui_util.h" | 36 #include "ui/base/webui/web_ui_util.h" |
40 | 37 |
41 namespace browser_sync { | 38 namespace browser_sync { |
42 | 39 |
43 namespace { | 40 namespace { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 void ForeignSessionHandler::RegisterMessages() { | 205 void ForeignSessionHandler::RegisterMessages() { |
209 Profile* profile = Profile::FromWebUI(web_ui()); | 206 Profile* profile = Profile::FromWebUI(web_ui()); |
210 | 207 |
211 ProfileSyncService* service = | 208 ProfileSyncService* service = |
212 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | 209 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
213 | 210 |
214 // NOTE: The ProfileSyncService can be null in tests. | 211 // NOTE: The ProfileSyncService can be null in tests. |
215 if (service) | 212 if (service) |
216 scoped_observer_.Add(service); | 213 scoped_observer_.Add(service); |
217 | 214 |
218 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, | |
219 content::Source<Profile>(profile)); | |
220 | |
221 web_ui()->RegisterMessageCallback("deleteForeignSession", | 215 web_ui()->RegisterMessageCallback("deleteForeignSession", |
222 base::Bind(&ForeignSessionHandler::HandleDeleteForeignSession, | 216 base::Bind(&ForeignSessionHandler::HandleDeleteForeignSession, |
223 base::Unretained(this))); | 217 base::Unretained(this))); |
224 web_ui()->RegisterMessageCallback("getForeignSessions", | 218 web_ui()->RegisterMessageCallback("getForeignSessions", |
225 base::Bind(&ForeignSessionHandler::HandleGetForeignSessions, | 219 base::Bind(&ForeignSessionHandler::HandleGetForeignSessions, |
226 base::Unretained(this))); | 220 base::Unretained(this))); |
227 web_ui()->RegisterMessageCallback("openForeignSession", | 221 web_ui()->RegisterMessageCallback("openForeignSession", |
228 base::Bind(&ForeignSessionHandler::HandleOpenForeignSession, | 222 base::Bind(&ForeignSessionHandler::HandleOpenForeignSession, |
229 base::Unretained(this))); | 223 base::Unretained(this))); |
230 web_ui()->RegisterMessageCallback("setForeignSessionCollapsed", | 224 web_ui()->RegisterMessageCallback("setForeignSessionCollapsed", |
231 base::Bind(&ForeignSessionHandler::HandleSetForeignSessionCollapsed, | 225 base::Bind(&ForeignSessionHandler::HandleSetForeignSessionCollapsed, |
232 base::Unretained(this))); | 226 base::Unretained(this))); |
233 } | 227 } |
234 | 228 |
235 void ForeignSessionHandler::Observe( | |
236 int type, | |
237 const content::NotificationSource& source, | |
238 const content::NotificationDetails& details) { | |
239 switch (type) { | |
240 case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED: | |
241 HandleGetForeignSessions(nullptr); | |
242 break; | |
243 default: | |
244 NOTREACHED(); | |
245 } | |
246 } | |
247 | |
248 void ForeignSessionHandler::OnSyncConfigurationCompleted() { | 229 void ForeignSessionHandler::OnSyncConfigurationCompleted() { |
249 HandleGetForeignSessions(nullptr); | 230 HandleGetForeignSessions(nullptr); |
250 } | 231 } |
251 | 232 |
| 233 void ForeignSessionHandler::OnForeignSessionUpdated() { |
| 234 HandleGetForeignSessions(nullptr); |
| 235 } |
| 236 |
252 bool ForeignSessionHandler::IsTabSyncEnabled() { | 237 bool ForeignSessionHandler::IsTabSyncEnabled() { |
253 Profile* profile = Profile::FromWebUI(web_ui()); | 238 Profile* profile = Profile::FromWebUI(web_ui()); |
254 ProfileSyncService* service = | 239 ProfileSyncService* service = |
255 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | 240 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
256 return service && service->GetActiveDataTypes().Has(syncer::PROXY_TABS); | 241 return service && service->GetActiveDataTypes().Has(syncer::PROXY_TABS); |
257 } | 242 } |
258 | 243 |
259 base::string16 ForeignSessionHandler::FormatSessionTime( | 244 base::string16 ForeignSessionHandler::FormatSessionTime( |
260 const base::Time& time) { | 245 const base::Time& time) { |
261 // Return a time like "1 hour ago", "2 days ago", etc. | 246 // Return a time like "1 hour ago", "2 days ago", etc. |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 // collapsed state persists. | 428 // collapsed state persists. |
444 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | 429 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
445 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); | 430 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); |
446 if (is_collapsed) | 431 if (is_collapsed) |
447 update.Get()->SetBoolean(session_tag, true); | 432 update.Get()->SetBoolean(session_tag, true); |
448 else | 433 else |
449 update.Get()->Remove(session_tag, NULL); | 434 update.Get()->Remove(session_tag, NULL); |
450 } | 435 } |
451 | 436 |
452 } // namespace browser_sync | 437 } // namespace browser_sync |
OLD | NEW |