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