Chromium Code Reviews| Index: chrome/browser/ui/webui/ntp/foreign_session_handler.cc |
| diff --git a/chrome/browser/ui/webui/ntp/foreign_session_handler.cc b/chrome/browser/ui/webui/ntp/foreign_session_handler.cc |
| index 4a6bbaed1601a893c36c1a9f65c796fe3fc52ed2..f36638e7a6ff9398eec1b22da95728be5873de32 100644 |
| --- a/chrome/browser/ui/webui/ntp/foreign_session_handler.cc |
| +++ b/chrome/browser/ui/webui/ntp/foreign_session_handler.cc |
| @@ -21,13 +21,14 @@ |
| #include "chrome/browser/ui/webui/web_ui_util.h" |
| #include "chrome/common/chrome_notification_types.h" |
| #include "chrome/common/url_constants.h" |
| +#include "content/public/browser/notification_service.h" |
| #include "content/public/browser/notification_source.h" |
| #include "content/public/browser/web_ui.h" |
| namespace browser_sync { |
| // Maximum number of session we're going to display on the NTP |
| -static const int kMaxSessionsToShow = 10; |
| +static const size_t kMaxSessionsToShow = 10; |
| // Invalid value, used to note that we don't have a tab or window number. |
| static const int kInvalidId = -1; |
| @@ -63,12 +64,8 @@ void ForeignSessionHandler::Observe( |
| switch (type) { |
| case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE: |
| case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED: |
| - HandleGetForeignSessions(&list_value); |
| - break; |
| case chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED: |
| - // Calling foreignSessions with empty list will automatically hide |
| - // foreign session section. |
| - web_ui()->CallJavascriptFunction("ntp.foreignSessions", list_value); |
| + HandleGetForeignSessions(&list_value); |
| break; |
| default: |
| NOTREACHED(); |
| @@ -90,44 +87,40 @@ SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() { |
| return model_associator; |
| } |
| +bool ForeignSessionHandler::IsTabSyncEnabled() { |
| + ProfileSyncService* service(ProfileSyncServiceFactory:: |
| + GetInstance()->GetForProfile(Profile::FromWebUI(web_ui()))); |
| + return service && service->GetSessionModelAssociator() != NULL; |
|
Dan Beam
2012/03/26 23:18:54
nit: remove "!= NULL" if possible, IMO
Patrick Dubroy
2012/03/27 00:36:51
Done.
|
| +} |
| + |
| void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { |
| SessionModelAssociator* associator = GetModelAssociator(); |
| std::vector<const SyncedSession*> sessions; |
| - if (associator == NULL) { |
| - // Called before associator created, exit. |
| - return; |
| - } |
| - |
| - // Note: we don't own the SyncedSessions themselves. |
| - if (!associator->GetAllForeignSessions(&sessions)) { |
| - LOG(ERROR) << "ForeignSessionHandler failed to get session data from" |
| - "SessionModelAssociator."; |
| - return; |
| - } |
| - int added_count = 0; |
| ListValue session_list; |
| - for (std::vector<const SyncedSession*>::const_iterator i = |
| - sessions.begin(); i != sessions.end() && |
| - added_count < kMaxSessionsToShow; ++i) { |
| - const SyncedSession* session = *i; |
| - scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); |
| - session_data->SetString("tag", session->session_tag); |
| - session_data->SetString("name", session->session_name); |
| - scoped_ptr<ListValue> window_list(new ListValue()); |
| - for (SyncedSession::SyncedWindowMap::const_iterator it = |
| - session->windows.begin(); it != session->windows.end(); ++it) { |
| - SessionWindow* window = it->second; |
| - scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); |
| - if (SessionWindowToValue(*window, window_data.get())) { |
| - window_list->Append(window_data.release()); |
| + if (associator && associator->GetAllForeignSessions(&sessions)) { |
| + // Note: we don't own the SyncedSessions themselves. |
| + for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { |
| + const SyncedSession* session = sessions[i]; |
| + scoped_ptr<DictionaryValue> session_data(new DictionaryValue()); |
| + session_data->SetString("tag", session->session_tag); |
| + session_data->SetString("name", session->session_name); |
| + scoped_ptr<ListValue> window_list(new ListValue()); |
| + for (SyncedSession::SyncedWindowMap::const_iterator it = |
| + session->windows.begin(); it != session->windows.end(); ++it) { |
|
Dan Beam
2012/03/26 23:18:54
nit: +1 \s
Patrick Dubroy
2012/03/27 00:36:51
Done.
|
| + SessionWindow* window = it->second; |
| + scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); |
| + if (SessionWindowToValue(*window, window_data.get())) |
| + window_list->Append(window_data.release()); |
| } |
| + session_data->Set("windows", window_list.release()); |
| + session_list.Append(session_data.release()); |
| } |
| - session_data->Set("windows", window_list.release()); |
| - session_list.Append(session_data.release()); |
| - added_count++; |
| } |
| - web_ui()->CallJavascriptFunction("ntp.foreignSessions", session_list); |
| + base::FundamentalValue tab_sync_enabled(IsTabSyncEnabled()); |
| + web_ui()->CallJavascriptFunction("ntp.foreignSessions", |
| + session_list, |
| + tab_sync_enabled); |
| } |
| void ForeignSessionHandler::HandleOpenForeignSession( |