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/sync/glue/session_model_associator.h" | 5 #include "chrome/browser/sync/glue/session_model_associator.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 return true; | 185 return true; |
186 } | 186 } |
187 | 187 |
188 bool SessionModelAssociator::GetSessionData( | 188 bool SessionModelAssociator::GetSessionData( |
189 std::vector<ForeignSession*>* sessions) { | 189 std::vector<ForeignSession*>* sessions) { |
190 DCHECK(CalledOnValidThread()); | 190 DCHECK(CalledOnValidThread()); |
191 | 191 |
192 // Build vector of sessions from specifics data | 192 // Build vector of sessions from specifics data |
193 for (std::vector<const sync_pb::SessionSpecifics*>::const_iterator i = | 193 for (std::vector<const sync_pb::SessionSpecifics*>::const_iterator i = |
194 specifics_.begin(); i != specifics_.end(); ++i) { | 194 specifics_.begin(); i != specifics_.end(); ++i) { |
195 AppendForeignSessionFromSpecifics(*i, sessions); | 195 // Only include sessions with open windows. |
| 196 if ((*i)->session_window_size() > 0) |
| 197 AppendForeignSessionFromSpecifics(*i, sessions); |
196 } | 198 } |
197 | 199 |
198 return true; | 200 return true; |
199 } | 201 } |
200 | 202 |
201 void SessionModelAssociator::AppendForeignSessionFromSpecifics( | 203 void SessionModelAssociator::AppendForeignSessionFromSpecifics( |
202 const sync_pb::SessionSpecifics* specifics, | 204 const sync_pb::SessionSpecifics* specifics, |
203 std::vector<ForeignSession*>* session) { | 205 std::vector<ForeignSession*>* session) { |
204 ForeignSession* foreign_session = new ForeignSession(); | 206 ForeignSession* foreign_session = new ForeignSession(); |
205 foreign_session->foreign_tession_tag = specifics->session_tag(); | 207 foreign_session->foreign_session_tag = specifics->session_tag(); |
206 session->insert(session->end(), foreign_session); | 208 session->insert(session->end(), foreign_session); |
207 for (int i = 0; i < specifics->session_window_size(); i++) { | 209 for (int i = 0; i < specifics->session_window_size(); i++) { |
208 const sync_pb::SessionWindow* window = &specifics->session_window(i); | 210 const sync_pb::SessionWindow* window = &specifics->session_window(i); |
209 SessionWindow* session_window = new SessionWindow(); | 211 SessionWindow* session_window = new SessionWindow(); |
210 PopulateSessionWindowFromSpecifics(session_window, window); | 212 PopulateSessionWindowFromSpecifics(session_window, window); |
211 foreign_session->windows.insert( | 213 foreign_session->windows.insert( |
212 foreign_session->windows.end(), session_window); | 214 foreign_session->windows.end(), session_window); |
213 } | 215 } |
214 } | 216 } |
215 | 217 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 } | 381 } |
380 num_populated++; | 382 num_populated++; |
381 } | 383 } |
382 if (num_populated == 0) | 384 if (num_populated == 0) |
383 return true; | 385 return true; |
384 return false; | 386 return false; |
385 } | 387 } |
386 | 388 |
387 void SessionModelAssociator::OnGotSession(int handle, | 389 void SessionModelAssociator::OnGotSession(int handle, |
388 std::vector<SessionWindow*>* windows) { | 390 std::vector<SessionWindow*>* windows) { |
389 sync_pb::SessionSpecifics session; | 391 sync_pb::SessionSpecifics specifics; |
390 // Set the tag, then iterate through the vector of windows, extracting the | 392 // Set the tag, then iterate through the vector of windows, extracting the |
391 // window data, along with the tabs data and tab navigation data to populate | 393 // window data, along with the tabs data and tab navigation data to populate |
392 // the session specifics. | 394 // the session specifics. |
393 session.set_session_tag(GetCurrentMachineTag()); | 395 specifics.set_session_tag(GetCurrentMachineTag()); |
394 for (std::vector<SessionWindow*>::const_iterator i = windows->begin(); | 396 FillSpecificsFromSessions(windows, &specifics); |
395 i != windows->end(); ++i) { | |
396 const SessionWindow* window = *i; | |
397 if (WindowHasNoTabsToSync(window)) { | |
398 continue; | |
399 } | |
400 sync_pb::SessionWindow* session_window = session.add_session_window(); | |
401 PopulateSessionSpecificsWindow(window, session_window); | |
402 } | |
403 bool has_nodes = false; | 397 bool has_nodes = false; |
404 if (!SyncModelHasUserCreatedNodes(&has_nodes)) | 398 if (!SyncModelHasUserCreatedNodes(&has_nodes)) |
405 return; | 399 return; |
406 if (session.session_window_size() == 0 && has_nodes) | 400 if (specifics.session_window_size() == 0 && has_nodes) |
407 return; | 401 return; |
408 sync_api::WriteTransaction trans( | 402 sync_api::WriteTransaction trans( |
409 sync_service_->backend()->GetUserShareHandle()); | 403 sync_service_->backend()->GetUserShareHandle()); |
410 sync_api::ReadNode root(&trans); | 404 sync_api::ReadNode root(&trans); |
411 if (!root.InitByTagLookup(kSessionsTag)) { | 405 if (!root.InitByTagLookup(kSessionsTag)) { |
412 LOG(ERROR) << kNoSessionsFolderError; | 406 LOG(ERROR) << kNoSessionsFolderError; |
413 return; | 407 return; |
414 } | 408 } |
415 UpdateSyncModel(&session, &trans, &root); | 409 UpdateSyncModel(&specifics, &trans, &root); |
| 410 } |
| 411 |
| 412 void SessionModelAssociator::FillSpecificsFromSessions( |
| 413 std::vector<SessionWindow*>* windows, |
| 414 sync_pb::SessionSpecifics* session) { |
| 415 for (std::vector<SessionWindow*>::const_iterator i = windows->begin(); |
| 416 i != windows->end(); ++i) { |
| 417 const SessionWindow* window = *i; |
| 418 if (WindowHasNoTabsToSync(window)) { |
| 419 continue; |
| 420 } |
| 421 sync_pb::SessionWindow* session_window = session->add_session_window(); |
| 422 PopulateSessionSpecificsWindow(window, session_window); |
| 423 } |
416 } | 424 } |
417 | 425 |
418 void SessionModelAssociator::AppendSessionTabNavigation( | 426 void SessionModelAssociator::AppendSessionTabNavigation( |
419 std::vector<TabNavigation>* navigations, | 427 std::vector<TabNavigation>* navigations, |
420 const sync_pb::TabNavigation* navigation) { | 428 const sync_pb::TabNavigation* navigation) { |
421 int index = 0; | 429 int index = 0; |
422 GURL virtual_url; | 430 GURL virtual_url; |
423 GURL referrer; | 431 GURL referrer; |
424 string16 title; | 432 string16 title; |
425 std::string state; | 433 std::string state; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 return false; | 563 return false; |
556 } | 564 } |
557 create_node.SetSessionSpecifics(*session_data); | 565 create_node.SetSessionSpecifics(*session_data); |
558 return true; | 566 return true; |
559 } | 567 } |
560 write_node.SetSessionSpecifics(*session_data); | 568 write_node.SetSessionSpecifics(*session_data); |
561 return true; | 569 return true; |
562 } | 570 } |
563 | 571 |
564 } // namespace browser_sync | 572 } // namespace browser_sync |
OLD | NEW |