| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dom_ui/foreign_session_handler.h" | 5 #include "chrome/browser/dom_ui/foreign_session_handler.h" |
| 6 | 6 |
| 7 #include "base/scoped_vector.h" | 7 #include "base/scoped_vector.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
| 10 #include "chrome/browser/dom_ui/value_helper.h" | 10 #include "chrome/browser/dom_ui/value_helper.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 dom_ui_->RegisterMessageCallback("getForeignSessions", | 28 dom_ui_->RegisterMessageCallback("getForeignSessions", |
| 29 NewCallback(this, | 29 NewCallback(this, |
| 30 &ForeignSessionHandler::HandleGetForeignSessions)); | 30 &ForeignSessionHandler::HandleGetForeignSessions)); |
| 31 dom_ui_->RegisterMessageCallback("reopenForeignSession", | 31 dom_ui_->RegisterMessageCallback("reopenForeignSession", |
| 32 NewCallback(this, | 32 NewCallback(this, |
| 33 &ForeignSessionHandler::HandleReopenForeignSession)); | 33 &ForeignSessionHandler::HandleReopenForeignSession)); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void ForeignSessionHandler::Init() { | 36 void ForeignSessionHandler::Init() { |
| 37 registrar_.Add(this, NotificationType::SYNC_CONFIGURE_DONE, | 37 registrar_.Add(this, NotificationType::SYNC_CONFIGURE_DONE, |
| 38 NotificationService::AllSources()); | 38 NotificationService::AllSources()); |
| 39 registrar_.Add(this, NotificationType::FOREIGN_SESSION_UPDATED, | 39 registrar_.Add(this, NotificationType::FOREIGN_SESSION_UPDATED, |
| 40 NotificationService::AllSources()); | 40 NotificationService::AllSources()); |
| 41 registrar_.Add(this, NotificationType::FOREIGN_SESSION_DELETED, | 41 registrar_.Add(this, NotificationType::FOREIGN_SESSION_DISABLED, |
| 42 NotificationService::AllSources()); | 42 NotificationService::AllSources()); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void ForeignSessionHandler::Observe(NotificationType type, | 45 void ForeignSessionHandler::Observe(NotificationType type, |
| 46 const NotificationSource& source, | 46 const NotificationSource& source, |
| 47 const NotificationDetails& details) { | 47 const NotificationDetails& details) { |
| 48 if (type != NotificationType::SYNC_CONFIGURE_DONE && | 48 ListValue list_value; |
| 49 type != NotificationType::FOREIGN_SESSION_UPDATED && | 49 switch (type.value) { |
| 50 type != NotificationType::FOREIGN_SESSION_DELETED) { | 50 case NotificationType::SYNC_CONFIGURE_DONE: |
| 51 NOTREACHED(); | 51 case NotificationType::FOREIGN_SESSION_UPDATED: |
| 52 return; | 52 HandleGetForeignSessions(&list_value); |
| 53 break; |
| 54 case NotificationType::FOREIGN_SESSION_DISABLED: |
| 55 // Calling foreignSessions with empty list will automatically hide |
| 56 // foreign session section. |
| 57 dom_ui_->CallJavascriptFunction(L"foreignSessions", list_value); |
| 58 break; |
| 59 default: |
| 60 NOTREACHED(); |
| 53 } | 61 } |
| 54 ListValue list_value; | |
| 55 HandleGetForeignSessions(&list_value); | |
| 56 } | 62 } |
| 57 | 63 |
| 58 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() { | 64 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator() { |
| 59 ProfileSyncService* service = dom_ui_->GetProfile()->GetProfileSyncService(); | 65 ProfileSyncService* service = dom_ui_->GetProfile()->GetProfileSyncService(); |
| 60 if (service == NULL) | 66 if (service == NULL) |
| 61 return NULL; | 67 return NULL; |
| 62 // We only want to set the model associator if there is one, and it is done | 68 // We only want to set the model associator if there is one, and it is done |
| 63 // syncing sessions. | 69 // syncing sessions. |
| 64 SessionModelAssociator* model_associator = service-> | 70 SessionModelAssociator* model_associator = service-> |
| 65 GetSessionModelAssociator(); | 71 GetSessionModelAssociator(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 ScopedVector<ForeignSession> session; | 105 ScopedVector<ForeignSession> session; |
| 100 associator->AppendForeignSessionWithID(id, &session.get(), &trans); | 106 associator->AppendForeignSessionWithID(id, &session.get(), &trans); |
| 101 | 107 |
| 102 DCHECK(session.size() == 1); | 108 DCHECK(session.size() == 1); |
| 103 std::vector<SessionWindow*> windows = (*session.begin())->windows; | 109 std::vector<SessionWindow*> windows = (*session.begin())->windows; |
| 104 SessionRestore::RestoreForeignSessionWindows(dom_ui_->GetProfile(), &windows); | 110 SessionRestore::RestoreForeignSessionWindows(dom_ui_->GetProfile(), &windows); |
| 105 } | 111 } |
| 106 | 112 |
| 107 void ForeignSessionHandler::GetForeignSessions( | 113 void ForeignSessionHandler::GetForeignSessions( |
| 108 SessionModelAssociator* associator) { | 114 SessionModelAssociator* associator) { |
| 109 ScopedVector<ForeignSession> windows; | 115 ScopedVector<ForeignSession> clients; |
| 110 associator->GetSessionDataFromSyncModel(&windows.get()); | 116 if (!associator->GetSessionData(&clients.get())) { |
| 117 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" |
| 118 "SessionModelAssociator."; |
| 119 return; |
| 120 } |
| 111 int added_count = 0; | 121 int added_count = 0; |
| 112 ListValue list_value; | 122 ListValue client_list; |
| 113 for (std::vector<ForeignSession*>::const_iterator i = | 123 for (std::vector<ForeignSession*>::const_iterator i = |
| 114 windows.begin(); i != windows.end() && | 124 clients->begin(); i != clients->end() && |
| 115 added_count < kMaxSessionsToShow; ++i) { | 125 added_count < kMaxSessionsToShow; ++i) { |
| 116 ForeignSession* foreign_session = *i; | 126 ForeignSession* foreign_session = *i; |
| 117 std::vector<TabRestoreService::Entry*> entries; | 127 std::vector<TabRestoreService::Entry*> entries; |
| 118 dom_ui_->GetProfile()->GetTabRestoreService()->CreateEntriesFromWindows( | 128 dom_ui_->GetProfile()->GetTabRestoreService()->CreateEntriesFromWindows( |
| 119 &foreign_session->windows, &entries); | 129 &foreign_session->windows, &entries); |
| 130 scoped_ptr<ListValue> window_list(new ListValue()); |
| 120 for (std::vector<TabRestoreService::Entry*>::const_iterator it = | 131 for (std::vector<TabRestoreService::Entry*>::const_iterator it = |
| 121 entries.begin(); it != entries.end(); ++it) { | 132 entries.begin(); it != entries.end(); ++it) { |
| 122 TabRestoreService::Entry* entry = *it; | 133 TabRestoreService::Entry* entry = *it; |
| 123 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 134 scoped_ptr<DictionaryValue> window_data(new DictionaryValue()); |
| 124 if (entry->type == TabRestoreService::WINDOW && | 135 if (entry->type == TabRestoreService::WINDOW && |
| 125 ValueHelper::WindowToValue( | 136 ValueHelper::WindowToValue( |
| 126 *static_cast<TabRestoreService::Window*>(entry), value.get())) { | 137 *static_cast<TabRestoreService::Window*>(entry), |
| 138 window_data.get())) { |
| 127 // The javascript checks if the session id is a valid session id, | 139 // The javascript checks if the session id is a valid session id, |
| 128 // when rendering session information to the new tab page, and it | 140 // when rendering session information to the new tab page, and it |
| 129 // sends the sessionTag back when we need to restore a session. | 141 // sends the sessionTag back when we need to restore a session. |
| 130 value->SetString("sessionTag", foreign_session->foreign_tession_tag); | 142 |
| 131 value->SetInteger("sessionId", entry->id); | 143 // TODO(zea): sessionTag is per client, it might be better per window. |
| 132 list_value.Append(value.release()); // Give ownership to |list_value|. | 144 window_data->SetString("sessionTag", |
| 145 foreign_session->foreign_tession_tag); |
| 146 window_data->SetInteger("sessionId", entry->id); |
| 147 |
| 148 // Give ownership to |list_value|. |
| 149 window_list->Append(window_data.release()); |
| 133 } | 150 } |
| 134 } | 151 } |
| 135 added_count++; | 152 added_count++; |
| 153 |
| 154 // Give ownership to |client_list| |
| 155 client_list.Append(window_list.release()); |
| 136 } | 156 } |
| 137 dom_ui_->CallJavascriptFunction(L"foreignSessions", list_value); | 157 dom_ui_->CallJavascriptFunction(L"foreignSessions", client_list); |
| 138 } | 158 } |
| 139 | 159 |
| 140 } // namespace browser_sync | 160 } // namespace browser_sync |
| 141 | |
| OLD | NEW |