OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <set> | 10 #include <set> |
(...skipping 18 matching lines...) Expand all Loading... | |
29 #include "chrome/browser/sync/glue/model_associator.h" | 29 #include "chrome/browser/sync/glue/model_associator.h" |
30 #include "chrome/browser/sync/glue/synced_session_tracker.h" | 30 #include "chrome/browser/sync/glue/synced_session_tracker.h" |
31 #include "chrome/browser/sync/glue/synced_tab_delegate.h" | 31 #include "chrome/browser/sync/glue/synced_tab_delegate.h" |
32 #include "chrome/browser/sync/glue/synced_window_delegate.h" | 32 #include "chrome/browser/sync/glue/synced_window_delegate.h" |
33 #include "sync/syncable/model_type.h" | 33 #include "sync/syncable/model_type.h" |
34 | 34 |
35 class Prefservice; | 35 class Prefservice; |
36 class Profile; | 36 class Profile; |
37 class ProfileSyncService; | 37 class ProfileSyncService; |
38 | 38 |
39 namespace content { | |
40 class NavigationEntry; | |
41 } // namespace content | |
42 | |
39 namespace sync_api { | 43 namespace sync_api { |
40 class BaseTransaction; | 44 class BaseTransaction; |
41 class ReadNode; | 45 class ReadNode; |
42 class WriteTransaction; | 46 class WriteTransaction; |
43 } // namespace sync_api | 47 } // namespace sync_api |
44 | 48 |
45 namespace sync_pb { | 49 namespace sync_pb { |
46 class SessionHeader; | 50 class SessionHeader; |
47 class SessionSpecifics; | 51 class SessionSpecifics; |
48 class SessionTab; | 52 class SessionTab; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 static syncable::ModelType model_type() { return syncable::SESSIONS; } | 221 static syncable::ModelType model_type() { return syncable::SESSIONS; } |
218 | 222 |
219 // Testing only. Will cause the associator to call MessageLoop::Quit() | 223 // Testing only. Will cause the associator to call MessageLoop::Quit() |
220 // when a local change is made, or when timeout_milli occurs, whichever is | 224 // when a local change is made, or when timeout_milli occurs, whichever is |
221 // first. | 225 // first. |
222 void BlockUntilLocalChangeForTest(int64 timeout_milli); | 226 void BlockUntilLocalChangeForTest(int64 timeout_milli); |
223 | 227 |
224 // Callback for when the session name has been computed. | 228 // Callback for when the session name has been computed. |
225 void OnSessionNameInitialized(const std::string& name); | 229 void OnSessionNameInitialized(const std::string& name); |
226 | 230 |
231 // If a valid favicon for the page at |url| is found, fills |png_favicon| with | |
232 // the png-encoded image and returns true. Else, returns false. | |
233 bool GetSyncedFaviconForPageURL(const std::string& url, | |
234 std::string* png_favicon) const; | |
235 | |
227 private: | 236 private: |
237 friend class SyncSessionModelAssociatorTest; | |
228 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, WriteSessionToNode); | 238 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, WriteSessionToNode); |
229 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, | 239 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, |
230 WriteFilledSessionToNode); | 240 WriteFilledSessionToNode); |
231 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, | 241 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, |
232 WriteForeignSessionToNode); | 242 WriteForeignSessionToNode); |
233 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, TabNodePoolEmpty); | 243 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, TabNodePoolEmpty); |
234 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, TabNodePoolNonEmpty); | 244 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, TabNodePoolNonEmpty); |
235 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, ValidTabs); | 245 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, ValidTabs); |
236 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, ExistingTabs); | 246 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, ExistingTabs); |
237 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, MissingLocalTabNode); | 247 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, MissingLocalTabNode); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 | 289 |
280 // The currently visible url of the tab (used for syncing favicons). | 290 // The currently visible url of the tab (used for syncing favicons). |
281 GURL url_; | 291 GURL url_; |
282 | 292 |
283 // Handle for loading favicons. | 293 // Handle for loading favicons. |
284 FaviconService::Handle favicon_load_handle_; | 294 FaviconService::Handle favicon_load_handle_; |
285 }; | 295 }; |
286 | 296 |
287 // A pool for managing free/used tab sync nodes. Performs lazy creation | 297 // A pool for managing free/used tab sync nodes. Performs lazy creation |
288 // of sync nodes when necessary. | 298 // of sync nodes when necessary. |
299 // TODO(zea): pull this into it's own file. | |
Andrew T Wilson (Slow)
2012/04/23 23:43:11
nit: it's->its
Nicolas Zea
2012/04/24 22:09:49
Done.
| |
289 class TabNodePool { | 300 class TabNodePool { |
290 public: | 301 public: |
291 explicit TabNodePool(ProfileSyncService* sync_service); | 302 explicit TabNodePool(ProfileSyncService* sync_service); |
292 ~TabNodePool(); | 303 ~TabNodePool(); |
293 | 304 |
294 // Add a previously allocated tab sync node to our pool. Increases the size | 305 // Add a previously allocated tab sync node to our pool. Increases the size |
295 // of tab_syncid_pool_ by one and marks the new tab node as free. | 306 // of tab_syncid_pool_ by one and marks the new tab node as free. |
296 // Note: this should only be called when we discover tab sync nodes from | 307 // Note: this should only be called when we discover tab sync nodes from |
297 // previous sessions, not for freeing tab nodes we created through | 308 // previous sessions, not for freeing tab nodes we created through |
298 // GetFreeTabNode (use FreeTabNode below for that). | 309 // GetFreeTabNode (use FreeTabNode below for that). |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
393 sync_api::WriteTransaction* trans, | 404 sync_api::WriteTransaction* trans, |
394 SyncError* error); | 405 SyncError* error); |
395 | 406 |
396 // Fills a tab sync node with data from a TabContents object. Updates | 407 // Fills a tab sync node with data from a TabContents object. Updates |
397 // |tab_link| with the current url if it's valid and triggers a favicon | 408 // |tab_link| with the current url if it's valid and triggers a favicon |
398 // load if the url has changed. | 409 // load if the url has changed. |
399 // Returns true on success, false if we need to reassociate due to corruption. | 410 // Returns true on success, false if we need to reassociate due to corruption. |
400 bool WriteTabContentsToSyncModel(TabLink* tab_link, | 411 bool WriteTabContentsToSyncModel(TabLink* tab_link, |
401 SyncError* error); | 412 SyncError* error); |
402 | 413 |
414 // Decrements the favicon usage counters for the favicon used by |page_url|. | |
415 // Deletes the favicon and associated pages from the favicon usage maps | |
416 // if no page is found to be referring to the favicon anymore. | |
417 void DecrementAndCleanFaviconForURL(std::string page_url); | |
418 | |
419 // Helper method to build sync's tab specifics from a newly modified | |
420 // tab, window, and the locally stored previous tab data. After completing, | |
421 // |prev_tab| will be updated to reflect the current data, |sync_tab| will | |
422 // be filled with the tab data (preserving old timestamps as necessary), and | |
423 // |new_url| will be the tab's current url. | |
424 void AssociateTabContents(const SyncedWindowDelegate& window, | |
425 const SyncedTabDelegate& new_tab, | |
426 SyncedSessionTab* prev_tab, | |
427 sync_pb::SessionTab* sync_tab, | |
428 GURL* new_url); | |
429 | |
403 // Load the favicon for the tab specified by |tab_link|. Will cancel any | 430 // Load the favicon for the tab specified by |tab_link|. Will cancel any |
404 // outstanding request for this tab. OnFaviconDataAvailable(..) will be called | 431 // outstanding request for this tab. OnFaviconDataAvailable(..) will be called |
405 // when the load completes. | 432 // when the load completes. |
406 void LoadFaviconForTab(TabLink* tab_link); | 433 void LoadFaviconForTab(TabLink* tab_link); |
407 | 434 |
408 // Callback method to store a tab's favicon into its sync node once it becomes | 435 // Callback method to store a tab's favicon into its sync node once it becomes |
409 // available. Does nothing if no favicon data was available. | 436 // available. Does nothing if no favicon data was available. |
410 void OnFaviconDataAvailable(FaviconService::Handle handle, | 437 void OnFaviconDataAvailable(FaviconService::Handle handle, |
411 history::FaviconData favicon); | 438 history::FaviconData favicon); |
412 | 439 |
413 // Used to populate a session header from the session specifics header | 440 // Used to populate a session header from the session specifics header |
414 // provided. | 441 // provided. |
415 static void PopulateSessionHeaderFromSpecifics( | 442 static void PopulateSessionHeaderFromSpecifics( |
416 const sync_pb::SessionHeader& header_specifics, | 443 const sync_pb::SessionHeader& header_specifics, |
417 const base::Time& mtime, | 444 const base::Time& mtime, |
418 SyncedSession* session_header); | 445 SyncedSession* session_header); |
419 | 446 |
420 // Used to populate a session window from the session specifics window | 447 // Used to populate a session window from the session specifics window |
421 // provided. Tracks any foreign session data created through |tracker|. | 448 // provided. Tracks any foreign session data created through |tracker|. |
422 static void PopulateSessionWindowFromSpecifics( | 449 static void PopulateSessionWindowFromSpecifics( |
423 const std::string& foreign_session_tag, | 450 const std::string& foreign_session_tag, |
424 const sync_pb::SessionWindow& window, | 451 const sync_pb::SessionWindow& window, |
425 const base::Time& mtime, | 452 const base::Time& mtime, |
426 SessionWindow* session_window, | 453 SessionWindow* session_window, |
427 SyncedSessionTracker* tracker); | 454 SyncedSessionTracker* tracker); |
428 | 455 |
429 // Used to populate a session tab from the session specifics tab provided. | 456 // Used to populate a session tab from the session specifics tab provided. |
430 static void PopulateSessionTabFromSpecifics(const sync_pb::SessionTab& tab, | 457 static void PopulateSessionTabFromSpecifics(const sync_pb::SessionTab& tab, |
431 const base::Time& mtime, | 458 const base::Time& mtime, |
432 SessionTab* session_tab); | 459 SyncedSessionTab* session_tab); |
433 | 460 |
434 // Helper method to take the favicon data in a foreign tab and store it | 461 // Helper method to load the favicon data from the tab specifics. If the |
435 // into the history db. | 462 // favicon is valid, stores it in |synced_favicons_|, increments |
463 // |synced_favicon_usage_| and updates |synced_favicon_pages_| appropriately. | |
436 void LoadForeignTabFavicon(const sync_pb::SessionTab& tab); | 464 void LoadForeignTabFavicon(const sync_pb::SessionTab& tab); |
437 | 465 |
438 // Used to populate a session tab from the session specifics tab provided. | 466 // Append a new navigation from sync specifics onto |tab| navigation vectors. |
439 static void AppendSessionTabNavigation( | 467 static void AppendSessionTabNavigation( |
440 const sync_pb::TabNavigation& navigation, | 468 const sync_pb::TabNavigation& navigation, |
441 std::vector<TabNavigation>* navigations); | 469 SyncedSessionTab* tab); |
442 | 470 |
443 // Populates the navigation portion of the session specifics. | 471 // Populates the navigation portion of the session specifics. |
444 static void PopulateSessionSpecificsNavigation( | 472 static void PopulateSessionSpecificsNavigation( |
445 const TabNavigation* navigation, | 473 const content::NavigationEntry& navigation, |
446 sync_pb::TabNavigation* tab_navigation); | 474 sync_pb::TabNavigation* tab_navigation); |
447 | 475 |
448 // Returns true if this tab belongs to this profile and belongs to a window, | 476 // Returns true if this tab belongs to this profile and belongs to a window, |
449 // false otherwise. | 477 // false otherwise. |
450 bool IsValidTab(const SyncedTabDelegate& tab) const; | 478 bool IsValidTab(const SyncedTabDelegate& tab) const; |
451 | 479 |
452 // Having a valid entry is defined as the url being valid and and having a | 480 // Having a valid entry is defined as the url being valid and and having a |
453 // syncable scheme (non chrome:// and file:// url's). In other words, we don't | 481 // syncable scheme (non chrome:// and file:// url's). In other words, we don't |
454 // want to sync a tab that is nothing but chrome:// and file:// navigations or | 482 // want to sync a tab that is nothing but chrome:// and file:// navigations or |
455 // invalid url's. | 483 // invalid url's. |
456 bool TabHasValidEntry(const SyncedTabDelegate& tab) const; | 484 bool TabHasValidEntry(const SyncedTabDelegate& tab) const; |
457 | 485 |
458 // For testing only. | 486 // For testing only. |
487 size_t NumFaviconsForTesting() const; | |
488 | |
489 // For testing only. | |
459 void QuitLoopForSubtleTesting(); | 490 void QuitLoopForSubtleTesting(); |
460 | 491 |
461 // Unique client tag. | 492 // Unique client tag. |
462 std::string current_machine_tag_; | 493 std::string current_machine_tag_; |
463 | 494 |
464 // User-visible machine name. | 495 // User-visible machine name. |
465 std::string current_session_name_; | 496 std::string current_session_name_; |
466 | 497 |
467 // Pool of all used/available sync nodes associated with tabs. | 498 // Pool of all used/available sync nodes associated with tabs. |
468 TabNodePool tab_pool_; | 499 TabNodePool tab_pool_; |
(...skipping 27 matching lines...) Expand all Loading... | |
496 | 527 |
497 // Pref service. Used to persist the session sync guid. Weak pointer. | 528 // Pref service. Used to persist the session sync guid. Weak pointer. |
498 PrefService* const pref_service_; | 529 PrefService* const pref_service_; |
499 | 530 |
500 DataTypeErrorHandler* error_handler_; | 531 DataTypeErrorHandler* error_handler_; |
501 | 532 |
502 // Used for loading favicons. For each outstanding favicon load, stores the | 533 // Used for loading favicons. For each outstanding favicon load, stores the |
503 // SessionID for the tab whose favicon is being set. | 534 // SessionID for the tab whose favicon is being set. |
504 CancelableRequestConsumerTSimple<SessionID::id_type> load_consumer_; | 535 CancelableRequestConsumerTSimple<SessionID::id_type> load_consumer_; |
505 | 536 |
537 // Synced favicon storage and tracking. | |
538 // Map of favicon URL -> favicon data for favicons synced from other clients. | |
539 // Favicons are stored as png-encoded strings. | |
540 // TODO(zea): if this becomes expensive memory-wise, reconsider using the | |
541 // favicon service instead. For now, this is simpler due to the history | |
542 // backend not properly supporting expiration of synced favicons. | |
543 // See crbug.com/122890. | |
544 std::map<std::string, linked_ptr<std::string> > synced_favicons_; | |
545 // Map of favicon URL -> usage count. When the count reaches zero, we can | |
546 // safely delete the favicon as no tabs are referring to it anymore. | |
547 std::map<std::string, size_t> synced_favicon_usage_; | |
548 // Map of page URL -> favicon url. | |
549 std::map<std::string, std::string> synced_favicon_pages_; | |
550 | |
506 DISALLOW_COPY_AND_ASSIGN(SessionModelAssociator); | 551 DISALLOW_COPY_AND_ASSIGN(SessionModelAssociator); |
507 }; | 552 }; |
508 | 553 |
509 } // namespace browser_sync | 554 } // namespace browser_sync |
510 | 555 |
511 #endif // CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ | 556 #endif // CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ |
OLD | NEW |