| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/memory/weak_ptr.h" |
| 17 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 18 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 19 #include "base/threading/non_thread_safe.h" | 20 #include "base/threading/non_thread_safe.h" |
| 20 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 21 #include "chrome/browser/sessions/session_id.h" | 22 #include "chrome/browser/sessions/session_id.h" |
| 22 #include "chrome/browser/sessions/session_service.h" | 23 #include "chrome/browser/sessions/session_service.h" |
| 23 #include "chrome/browser/sessions/session_types.h" | 24 #include "chrome/browser/sessions/session_types.h" |
| 24 #include "chrome/browser/sync/glue/model_associator.h" | 25 #include "chrome/browser/sync/glue/model_associator.h" |
| 25 #include "chrome/browser/sync/glue/synced_session_tracker.h" | 26 #include "chrome/browser/sync/glue/synced_session_tracker.h" |
| 26 #include "chrome/browser/sync/glue/synced_tab_delegate.h" | 27 #include "chrome/browser/sync/glue/synced_tab_delegate.h" |
| 27 #include "chrome/browser/sync/glue/synced_window_delegate.h" | 28 #include "chrome/browser/sync/glue/synced_window_delegate.h" |
| 28 #include "chrome/browser/sync/protocol/session_specifics.pb.h" | 29 #include "chrome/browser/sync/protocol/session_specifics.pb.h" |
| 29 #include "chrome/browser/sync/syncable/model_type.h" | 30 #include "chrome/browser/sync/syncable/model_type.h" |
| 31 #include "chrome/browser/sync/weak_handle.h" |
| 30 | 32 |
| 31 class Profile; | 33 class Profile; |
| 32 class ProfileSyncService; | 34 class ProfileSyncService; |
| 33 | 35 |
| 34 namespace sync_api { | 36 namespace sync_api { |
| 35 class BaseTransaction; | 37 class BaseTransaction; |
| 36 class ReadNode; | 38 class ReadNode; |
| 37 class WriteNode; | 39 class WriteNode; |
| 38 class WriteTransaction; | 40 class WriteTransaction; |
| 39 } // namespace sync_api | 41 } // namespace sync_api |
| 40 | 42 |
| 41 namespace sync_pb { | 43 namespace sync_pb { |
| 42 class SessionSpecifics; | 44 class SessionSpecifics; |
| 43 } // namespace sync_pb | 45 } // namespace sync_pb |
| 44 | 46 |
| 45 namespace browser_sync { | 47 namespace browser_sync { |
| 46 | 48 |
| 47 static const char kSessionsTag[] = "google_chrome_sessions"; | 49 static const char kSessionsTag[] = "google_chrome_sessions"; |
| 48 | 50 |
| 49 // Contains all logic for associating the Chrome sessions model and | 51 // Contains all logic for associating the Chrome sessions model and |
| 50 // the sync sessions model. | 52 // the sync sessions model. |
| 51 class SessionModelAssociator | 53 class SessionModelAssociator |
| 52 : public PerDataTypeAssociatorInterface<SyncedTabDelegate, size_t>, | 54 : public PerDataTypeAssociatorInterface<SyncedTabDelegate, size_t>, |
| 55 public base::SupportsWeakPtr<SessionModelAssociator>, |
| 53 public base::NonThreadSafe { | 56 public base::NonThreadSafe { |
| 54 public: | 57 public: |
| 55 // Does not take ownership of sync_service. | 58 // Does not take ownership of sync_service. |
| 56 explicit SessionModelAssociator(ProfileSyncService* sync_service); | 59 explicit SessionModelAssociator(ProfileSyncService* sync_service); |
| 57 SessionModelAssociator(ProfileSyncService* sync_service, | 60 SessionModelAssociator(ProfileSyncService* sync_service, |
| 58 bool setup_for_test); | 61 bool setup_for_test); |
| 59 virtual ~SessionModelAssociator(); | 62 virtual ~SessionModelAssociator(); |
| 60 | 63 |
| 61 // The has_nodes out parameter is set to true if the sync model has | 64 // The has_nodes out parameter is set to true if the sync model has |
| 62 // nodes other than the permanent tagged nodes. The method may | 65 // nodes other than the permanent tagged nodes. The method may |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // Note: a new tab page with back/forward history is valid. | 179 // Note: a new tab page with back/forward history is valid. |
| 177 static bool IsValidSessionTab(const SessionTab& tab); | 180 static bool IsValidSessionTab(const SessionTab& tab); |
| 178 | 181 |
| 179 // Returns the syncable model type. | 182 // Returns the syncable model type. |
| 180 static syncable::ModelType model_type() { return syncable::SESSIONS; } | 183 static syncable::ModelType model_type() { return syncable::SESSIONS; } |
| 181 | 184 |
| 182 // Testing only. Will cause the associator to call MessageLoop::Quit() | 185 // Testing only. Will cause the associator to call MessageLoop::Quit() |
| 183 // when a local change is made, or when timeout_milli occurs, whichever is | 186 // when a local change is made, or when timeout_milli occurs, whichever is |
| 184 // first. | 187 // first. |
| 185 void BlockUntilLocalChangeForTest(int64 timeout_milli); | 188 void BlockUntilLocalChangeForTest(int64 timeout_milli); |
| 189 |
| 190 // Callback for when the session name has been computed. |
| 191 void OnSessionNameInitialized(const std::string name); |
| 192 |
| 193 #if defined(OS_WIN) |
| 194 // Returns the computer name or the empty string an error occurred. |
| 195 static std::string GetComputerName(); |
| 196 #endif |
| 197 |
| 198 #if defined(OS_MACOSX) |
| 199 // Returns the Hardware model name, without trailing numbers, if possible. |
| 200 // See http://www.cocoadev.com/index.pl?MacintoshModels for an example list of |
| 201 // models. If an error occurs trying to read the model, this simply returns |
| 202 // "Unknown". |
| 203 static std::string GetHardwareModelName(); |
| 204 #endif |
| 205 |
| 186 private: | 206 private: |
| 187 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, WriteSessionToNode); | 207 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, WriteSessionToNode); |
| 188 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, | 208 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, |
| 189 WriteFilledSessionToNode); | 209 WriteFilledSessionToNode); |
| 190 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, | 210 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, |
| 191 WriteForeignSessionToNode); | 211 WriteForeignSessionToNode); |
| 192 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, TabNodePoolEmpty); | 212 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, TabNodePoolEmpty); |
| 193 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, TabNodePoolNonEmpty); | 213 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, TabNodePoolNonEmpty); |
| 214 FRIEND_TEST_ALL_PREFIXES(SessionModelAssociatorTest, PopulateSessionHeader); |
| 194 FRIEND_TEST_ALL_PREFIXES(SessionModelAssociatorTest, PopulateSessionWindow); | 215 FRIEND_TEST_ALL_PREFIXES(SessionModelAssociatorTest, PopulateSessionWindow); |
| 195 FRIEND_TEST_ALL_PREFIXES(SessionModelAssociatorTest, PopulateSessionTab); | 216 FRIEND_TEST_ALL_PREFIXES(SessionModelAssociatorTest, PopulateSessionTab); |
| 217 FRIEND_TEST_ALL_PREFIXES(SessionModelAssociatorTest, |
| 218 InitializeCurrentSessionName); |
| 196 | 219 |
| 197 // Keep all the links to local tab data in one place. | 220 // Keep all the links to local tab data in one place. |
| 198 class TabLinks { | 221 class TabLinks { |
| 199 public: | 222 public: |
| 200 // To support usage as second value in maps we need default and copy | 223 // To support usage as second value in maps we need default and copy |
| 201 // constructors. | 224 // constructors. |
| 202 TabLinks() | 225 TabLinks() |
| 203 : sync_id_(0), | 226 : sync_id_(0), |
| 204 session_tab_(NULL), | 227 session_tab_(NULL), |
| 205 tab_(NULL) {} | 228 tab_(NULL) {} |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 // Build a sync tag from tab_node_id. | 337 // Build a sync tag from tab_node_id. |
| 315 static inline std::string TabIdToTag( | 338 static inline std::string TabIdToTag( |
| 316 const std::string machine_tag, | 339 const std::string machine_tag, |
| 317 size_t tab_node_id) { | 340 size_t tab_node_id) { |
| 318 return base::StringPrintf("%s %"PRIuS"", machine_tag.c_str(), tab_node_id); | 341 return base::StringPrintf("%s %"PRIuS"", machine_tag.c_str(), tab_node_id); |
| 319 } | 342 } |
| 320 | 343 |
| 321 // Initializes the tag corresponding to this machine. | 344 // Initializes the tag corresponding to this machine. |
| 322 void InitializeCurrentMachineTag(sync_api::WriteTransaction* trans); | 345 void InitializeCurrentMachineTag(sync_api::WriteTransaction* trans); |
| 323 | 346 |
| 347 // Initializes the user visible name for this session |
| 348 void InitializeCurrentSessionName(); |
| 349 |
| 324 // Updates the server data based upon the current client session. If no node | 350 // Updates the server data based upon the current client session. If no node |
| 325 // corresponding to this machine exists in the sync model, one is created. | 351 // corresponding to this machine exists in the sync model, one is created. |
| 326 void UpdateSyncModelDataFromClient(); | 352 void UpdateSyncModelDataFromClient(); |
| 327 | 353 |
| 328 // Pulls the current sync model from the sync database and returns true upon | 354 // Pulls the current sync model from the sync database and returns true upon |
| 329 // update of the client model. Will associate any foreign sessions as well as | 355 // update of the client model. Will associate any foreign sessions as well as |
| 330 // keep track of any local tab nodes, adding them to our free tab node pool. | 356 // keep track of any local tab nodes, adding them to our free tab node pool. |
| 331 bool UpdateAssociationsFromSyncModel(const sync_api::ReadNode& root, | 357 bool UpdateAssociationsFromSyncModel(const sync_api::ReadNode& root, |
| 332 const sync_api::BaseTransaction* trans); | 358 const sync_api::BaseTransaction* trans); |
| 333 | 359 |
| 334 // Fills a tab sync node with data from a TabContents object. | 360 // Fills a tab sync node with data from a TabContents object. |
| 335 // (from a local navigation event) | 361 // (from a local navigation event) |
| 336 bool WriteTabContentsToSyncModel(const SyncedWindowDelegate& window, | 362 bool WriteTabContentsToSyncModel(const SyncedWindowDelegate& window, |
| 337 const SyncedTabDelegate& tab, | 363 const SyncedTabDelegate& tab, |
| 338 const int64 sync_id, | 364 const int64 sync_id, |
| 339 sync_api::WriteTransaction* trans); | 365 sync_api::WriteTransaction* trans); |
| 340 | 366 |
| 367 // Used to populate a session header from the session specifics header |
| 368 // provided. |
| 369 static void PopulateSessionHeaderFromSpecifics( |
| 370 const sync_pb::SessionHeader& header_specifics, |
| 371 SyncedSession* session_header); |
| 372 |
| 341 // Used to populate a session window from the session specifics window | 373 // Used to populate a session window from the session specifics window |
| 342 // provided. Tracks any foreign session data created through |tracker|. | 374 // provided. Tracks any foreign session data created through |tracker|. |
| 343 static void PopulateSessionWindowFromSpecifics( | 375 static void PopulateSessionWindowFromSpecifics( |
| 344 const std::string& foreign_session_tag, | 376 const std::string& foreign_session_tag, |
| 345 const sync_pb::SessionWindow& window, | 377 const sync_pb::SessionWindow& window, |
| 346 const int64 mtime, | 378 const int64 mtime, |
| 347 SessionWindow* session_window, | 379 SessionWindow* session_window, |
| 348 SyncedSessionTracker* tracker); | 380 SyncedSessionTracker* tracker); |
| 349 | 381 |
| 350 // Used to populate a session tab from the session specifics tab provided. | 382 // Used to populate a session tab from the session specifics tab provided. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 const int64 sync_id, | 420 const int64 sync_id, |
| 389 sync_api::WriteTransaction* trans); | 421 sync_api::WriteTransaction* trans); |
| 390 | 422 |
| 391 // Populates the tab portion of the session specifics. | 423 // Populates the tab portion of the session specifics. |
| 392 void PopulateSessionSpecificsTab(const SessionTab& tab, | 424 void PopulateSessionSpecificsTab(const SessionTab& tab, |
| 393 sync_pb::SessionTab* session_tab); | 425 sync_pb::SessionTab* session_tab); |
| 394 | 426 |
| 395 // For testing only. | 427 // For testing only. |
| 396 void QuitLoopForTest(); | 428 void QuitLoopForTest(); |
| 397 | 429 |
| 398 // Local client name. | 430 // Unique client tag. |
| 399 std::string current_machine_tag_; | 431 std::string current_machine_tag_; |
| 400 | 432 |
| 433 // User-visible machine name. |
| 434 std::string current_session_name_; |
| 435 |
| 401 // Pool of all used/available sync nodes associated with tabs. | 436 // Pool of all used/available sync nodes associated with tabs. |
| 402 TabNodePool tab_pool_; | 437 TabNodePool tab_pool_; |
| 403 | 438 |
| 404 // SyncID for the sync node containing all the window information for this | 439 // SyncID for the sync node containing all the window information for this |
| 405 // client. | 440 // client. |
| 406 int64 local_session_syncid_; | 441 int64 local_session_syncid_; |
| 407 | 442 |
| 408 // Mapping of current open (local) tabs to their sync identifiers. | 443 // Mapping of current open (local) tabs to their sync identifiers. |
| 409 TabLinksMap tab_map_; | 444 TabLinksMap tab_map_; |
| 410 | 445 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 423 // is made. | 458 // is made. |
| 424 bool waiting_for_change_; | 459 bool waiting_for_change_; |
| 425 ScopedRunnableMethodFactory<SessionModelAssociator> test_method_factory_; | 460 ScopedRunnableMethodFactory<SessionModelAssociator> test_method_factory_; |
| 426 | 461 |
| 427 DISALLOW_COPY_AND_ASSIGN(SessionModelAssociator); | 462 DISALLOW_COPY_AND_ASSIGN(SessionModelAssociator); |
| 428 }; | 463 }; |
| 429 | 464 |
| 430 } // namespace browser_sync | 465 } // namespace browser_sync |
| 431 | 466 |
| 432 #endif // CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ | 467 #endif // CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ |
| OLD | NEW |