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 #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> |
| 10 #include <set> |
9 #include <string> | 11 #include <string> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
13 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
14 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
15 #include "base/scoped_vector.h" | 17 #include "base/scoped_vector.h" |
| 18 #include "base/string_util.h" |
| 19 #include "base/utf_string_conversions.h" |
| 20 #include "chrome/browser/browser_window.h" |
| 21 #include "chrome/browser/sessions/session_id.h" |
16 #include "chrome/browser/sessions/session_service.h" | 22 #include "chrome/browser/sessions/session_service.h" |
17 #include "chrome/browser/sessions/session_types.h" | 23 #include "chrome/browser/sessions/session_types.h" |
18 #include "chrome/browser/sync/engine/syncapi.h" | 24 #include "chrome/browser/sync/engine/syncapi.h" |
19 #include "chrome/browser/sync/glue/model_associator.h" | 25 #include "chrome/browser/sync/glue/model_associator.h" |
20 #include "chrome/browser/sync/protocol/session_specifics.pb.h" | 26 #include "chrome/browser/sync/protocol/session_specifics.pb.h" |
21 #include "chrome/browser/sync/syncable/model_type.h" | 27 #include "chrome/browser/sync/syncable/model_type.h" |
| 28 #include "chrome/browser/tab_contents/tab_contents.h" |
22 #include "chrome/common/notification_registrar.h" | 29 #include "chrome/common/notification_registrar.h" |
23 | 30 |
24 class Profile; | 31 class Profile; |
25 class ProfileSyncService; | 32 class ProfileSyncService; |
26 | 33 |
27 namespace sync_api { | 34 namespace sync_api { |
28 class ReadNode; | 35 class ReadNode; |
29 class WriteNode; | 36 class WriteNode; |
30 class WriteTransaction; | 37 class WriteTransaction; |
31 } // namespace sync_api | 38 } // namespace sync_api |
32 | 39 |
33 namespace sync_pb { | 40 namespace sync_pb { |
34 class SessionSpecifics; | 41 class SessionSpecifics; |
35 } // namespace sync_pb | 42 } // namespace sync_pb |
36 | 43 |
37 namespace browser_sync { | 44 namespace browser_sync { |
38 | 45 |
39 static const char kSessionsTag[] = "google_chrome_sessions"; | 46 static const char kSessionsTag[] = "google_chrome_sessions"; |
40 | 47 |
41 // Contains all logic for associating the Chrome sessions model and | 48 // Contains all logic for associating the Chrome sessions model and |
42 // the sync sessions model. | 49 // the sync sessions model. |
43 // In the case of sessions, our local model is nothing but a buffer (specifics_) | 50 class SessionModelAssociator |
44 // that gets overwritten everytime there is an update. From it, we build a new | 51 : public PerDataTypeAssociatorInterface<TabContents, size_t>, |
45 // foreign session windows list each time |GetSessionData| is called by the | 52 public NonThreadSafe { |
46 // ForeignSessionHandler. | |
47 class SessionModelAssociator : public PerDataTypeAssociatorInterface< | |
48 sync_pb::SessionSpecifics, std::string>, public NonThreadSafe { | |
49 public: | 53 public: |
50 | |
51 // Does not take ownership of sync_service. | 54 // Does not take ownership of sync_service. |
52 explicit SessionModelAssociator(ProfileSyncService* sync_service); | 55 explicit SessionModelAssociator(ProfileSyncService* sync_service); |
53 virtual ~SessionModelAssociator(); | 56 virtual ~SessionModelAssociator(); |
54 | 57 |
55 | |
56 // AssociatorInterface and PerDataTypeAssociator Interface implementation. | |
57 virtual void AbortAssociation() { | |
58 return; | |
59 // No implementation needed, this associator runs on the main | |
60 // thread. | |
61 } | |
62 | |
63 // Dummy method, we do everything all-at-once in UpdateFromSyncModel. | |
64 virtual void Associate(const sync_pb::SessionSpecifics* specifics, | |
65 int64 sync_id) { | |
66 } | |
67 | |
68 // Updates the sync model with the local client data. (calls | |
69 // UpdateFromSyncModel) | |
70 virtual bool AssociateModels(); | |
71 | |
72 // The has_nodes out parameter is set to true if the chrome model | |
73 // has user-created nodes. The method may return false if an error | |
74 // occurred. | |
75 virtual bool ChromeModelHasUserCreatedNodes(bool* has_nodes); | |
76 | |
77 // Dummy method, we do everything all-at-once in UpdateFromSyncModel. | |
78 virtual void Disassociate(int64 sync_id) { | |
79 } | |
80 | |
81 // Clear specifics_ buffer and notify foreign session handlers. | |
82 virtual bool DisassociateModels(); | |
83 | |
84 // Returns the chrome session specifics for the given sync id. | |
85 // Returns NULL if no specifics are found for the given sync id. | |
86 virtual const sync_pb::SessionSpecifics* GetChromeNodeFromSyncId( | |
87 int64 sync_id); | |
88 | |
89 // Returns whether a node with the given permanent tag was found and update | |
90 // |sync_id| with that node's id. | |
91 virtual bool GetSyncIdForTaggedNode(const std::string* tag, int64* sync_id); | |
92 | |
93 // Returns sync id for the given chrome model id. | |
94 // Returns sync_api::kInvalidId if the sync node is not found for the given | |
95 // chrome id. | |
96 virtual int64 GetSyncIdFromChromeId(const std::string& id); | |
97 | |
98 | |
99 // Initializes the given sync node from the given chrome node id. | |
100 // Returns false if no sync node was found for the given chrome node id or | |
101 // if the initialization of sync node fails. | |
102 virtual bool InitSyncNodeFromChromeId(const std::string& id, | |
103 sync_api::BaseNode* sync_node) { | |
104 return false; | |
105 } | |
106 | |
107 // The has_nodes out parameter is set to true if the sync model has | 58 // The has_nodes out parameter is set to true if the sync model has |
108 // nodes other than the permanent tagged nodes. The method may | 59 // nodes other than the permanent tagged nodes. The method may |
109 // return false if an error occurred. | 60 // return false if an error occurred. |
110 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); | 61 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); |
111 | 62 |
| 63 // AssociatorInterface and PerDataTypeAssociator Interface implementation. |
| 64 virtual void AbortAssociation() { |
| 65 return; |
| 66 // No implementation needed, this associator runs on the main thread. |
| 67 } |
| 68 |
| 69 // Returns sync id for the given chrome model id. |
| 70 // Returns sync_api::kInvalidId if the sync node is not found for the given |
| 71 // chrome id. |
| 72 virtual int64 GetSyncIdFromChromeId(const size_t& id); |
| 73 |
| 74 // Returns sync id for the given session tag. |
| 75 // Returns sync_api::kInvalidId if the sync node is not found for the given |
| 76 // tag |
| 77 virtual int64 GetSyncIdFromSessionTag(const std::string& tag); |
| 78 |
| 79 // Not used. |
| 80 virtual const TabContents* GetChromeNodeFromSyncId(int64 sync_id) { |
| 81 NOTREACHED(); |
| 82 return NULL; |
| 83 } |
| 84 |
| 85 // Not used. |
| 86 bool InitSyncNodeFromChromeId(const size_t& id, |
| 87 sync_api::BaseNode* sync_node) { |
| 88 NOTREACHED(); |
| 89 return false; |
| 90 } |
| 91 |
| 92 // Resync local window information. Updates the local sessions header node |
| 93 // with the status of open windows and the order of tabs they contain. Should |
| 94 // only be called for changes that affect a window, not a change within a |
| 95 // single tab. |
| 96 // |
| 97 // If |reload_tabs| is true, will also resync all tabs (same as calling |
| 98 // ReassociateTabs with a vector of all tabs). |
| 99 void ReassociateWindows(bool reload_tabs); |
| 100 |
| 101 // Loads and reassociates the local tabs referenced in |tabs|. |
| 102 void ReassociateTabs(const std::vector<TabContents*>& tabs); |
| 103 |
| 104 // Reassociates a single tab with the sync model. Will check if the tab |
| 105 // already is associated with a sync node and allocate one if necessary. |
| 106 void ReassociateTab(const TabContents& tab); |
| 107 |
| 108 // Associate a local tab and it's sync node. Will overwrite the contents of |
| 109 // the sync node with new specifics built from the tab. |
| 110 virtual void Associate(const TabContents* tab, int64 sync_id); |
| 111 |
| 112 // Looks up the specified sync node, and marks that tab as closed, then marks |
| 113 // the node as free and deletes association. |
| 114 virtual void Disassociate(int64 sync_id); |
| 115 |
| 116 // Load any ƒoreign session info stored in sync db and update the sync db |
| 117 // with local client data. Processes/reuses any sync nodes owned by this |
| 118 // client and creates any further sync nodes needed to store local header and |
| 119 // tab info. |
| 120 virtual bool AssociateModels(); |
| 121 |
| 122 // Clear local sync data buffers. Does not delete sync nodes to avoid |
| 123 // tombstones. TODO(zea): way to eventually delete orphaned nodes. |
| 124 virtual bool DisassociateModels(); |
| 125 |
112 // Returns the tag used to uniquely identify this machine's session in the | 126 // Returns the tag used to uniquely identify this machine's session in the |
113 // sync model. | 127 // sync model. |
114 std::string GetCurrentMachineTag(); | 128 inline const std::string& GetCurrentMachineTag() { |
| 129 DCHECK(!current_machine_tag_.empty()); |
| 130 return current_machine_tag_; |
| 131 } |
| 132 |
| 133 // Load and associate window and tab data for a foreign session |
| 134 bool AssociateForeignSpecifics(const sync_pb::SessionSpecifics& specifics, |
| 135 int64 modification_time); |
| 136 |
| 137 // Removes a foreign session from our internal bookkeeping. |
| 138 void DisassociateForeignSession(const std::string& foreign_session_tag); |
| 139 |
| 140 // Builds a list of all foreign sessions. |
| 141 // Caller does NOT own ForeignSession objects. |
| 142 bool GetAllForeignSessions(std::vector<const ForeignSession*>* sessions); |
| 143 |
| 144 // Loads all windows for foreign session with session tag |tag|. |
| 145 // Caller does NOT own ForeignSession objects. |
| 146 bool GetForeignSession(const std::string& tag, |
| 147 std::vector<SessionWindow*>* windows); |
| 148 |
| 149 // Looks up the foreign tab identified by |tab_id| and belonging to foreign |
| 150 // session |tag|. |
| 151 // Caller does NOT own the SessionTab object. |
| 152 bool GetForeignTab(const std::string& tag, |
| 153 const SessionID::id_type tab_id, |
| 154 const SessionTab** tab); |
| 155 |
| 156 // Returns the syncable model type. |
| 157 static syncable::ModelType model_type() { return syncable::SESSIONS; } |
| 158 |
| 159 private: |
| 160 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, WriteSessionToNode); |
| 161 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, |
| 162 WriteFilledSessionToNode); |
| 163 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, |
| 164 WriteForeignSessionToNode); |
| 165 |
| 166 // Keep all the links to local tab data in one place. |
| 167 class TabLinks { |
| 168 public: |
| 169 // To support usage as second value in maps we need default and copy |
| 170 // constructors. |
| 171 TabLinks() |
| 172 : sync_id_(0), |
| 173 session_tab_(NULL), |
| 174 tab_(NULL) {} |
| 175 |
| 176 // We only ever have either a SessionTab (for foreign tabs), or a |
| 177 // TabContents (for local tabs). |
| 178 TabLinks(int64 sync_id, const TabContents* tab) |
| 179 : sync_id_(sync_id), |
| 180 session_tab_(NULL) { |
| 181 tab_ = const_cast<TabContents*>(tab); |
| 182 } |
| 183 TabLinks(int64 sync_id, const SessionTab* session_tab) |
| 184 : sync_id_(sync_id), |
| 185 tab_(NULL) { |
| 186 session_tab_ = const_cast<SessionTab*>(session_tab); |
| 187 } |
| 188 |
| 189 // Getters. |
| 190 inline const int64 sync_id() const { return sync_id_; } |
| 191 inline const SessionTab* session_tab() const { return session_tab_; } |
| 192 inline const TabContents* tab() const { return tab_; } |
| 193 private: |
| 194 int64 sync_id_; |
| 195 SessionTab* session_tab_; |
| 196 TabContents* tab_; |
| 197 }; |
| 198 |
| 199 // A pool for managing free/used tab sync nodes. Performs lazy creation |
| 200 // of sync nodes when necessary. |
| 201 class TabNodePool { |
| 202 public: |
| 203 explicit TabNodePool(ProfileSyncService* sync_service); |
| 204 TabNodePool(ProfileSyncService* sync_service, |
| 205 const std::string& current_machine_tag); |
| 206 |
| 207 // Add a previously allocated tab sync node to our pool. Increases the size |
| 208 // of tab_syncid_pool_ by one and marks the new tab node as free. |
| 209 // Note: this should only be called when we discover tab sync nodes from |
| 210 // previous sessions, not for freeing tab nodes we created through |
| 211 // GetFreeTabNode (use FreeTabNode below for that). |
| 212 void AddTabNode(int64 sync_id); |
| 213 |
| 214 // Returns the sync_id for the next free tab node. If none are available, |
| 215 // creates a new tab node. |
| 216 // Note: We make use of the following "id's" |
| 217 // - a sync_id: an int64 used in |sync_api::InitByIdLookup| |
| 218 // - a tab_id: created by session service, unique to this client |
| 219 // - a tab_node_id: the id for a particular sync tab node. This is used |
| 220 // to generate the sync tab node tag through: |
| 221 // tab_tag = StringPrintf("%s_%ui", local_session_tag, tab_node_id); |
| 222 // tab_node_id and sync_id are both unique to a particular sync node. The |
| 223 // difference is that tab_node_id is controlled by the model associator and |
| 224 // is used when creating a new sync node, which returns the sync_id, created |
| 225 // by the sync db. |
| 226 int64 GetFreeTabNode(); |
| 227 |
| 228 // Return a tab node to our free pool. |
| 229 // Note: the difference between FreeTabNode and AddTabNode is that |
| 230 // FreeTabNode does not modify the size of |tab_syncid_pool_|, while |
| 231 // AddTabNode increases it by one. In the case of FreeTabNode, the size of |
| 232 // the |tab_syncid_pool_| should always be equal to the amount of tab nodes |
| 233 // associated with this machine. |
| 234 void FreeTabNode(int64 sync_id); |
| 235 |
| 236 // Clear tab pool. |
| 237 inline void clear() { tab_syncid_pool_.clear(); } |
| 238 |
| 239 // Return the number of tab nodes this client currently has allocated |
| 240 // (including both free and used nodes) |
| 241 inline size_t capacity() const { return tab_syncid_pool_.size(); } |
| 242 |
| 243 // Return empty status (all tab nodes are in use). |
| 244 inline bool empty() const { return tab_pool_fp_ == -1; } |
| 245 |
| 246 // Return full status (no tab nodes are in use). |
| 247 inline bool full() { |
| 248 return tab_pool_fp_ == static_cast<int64>(tab_syncid_pool_.size())-1; |
| 249 } |
| 250 |
| 251 inline void set_machine_tag(const std::string& machine_tag) { |
| 252 machine_tag_ = machine_tag; |
| 253 } |
| 254 private: |
| 255 // Pool of all available syncid's for tab's we have created. |
| 256 std::vector<int64> tab_syncid_pool_; |
| 257 |
| 258 // Free pointer for tab pool. Only those node id's, up to and including the |
| 259 // one indexed by the free pointer, are valid and free. The rest of the |
| 260 // |tab_syncid_pool_| is invalid because the nodes are in use. |
| 261 // To get the next free node, use tab_syncid_pool_[tab_pool_fp_--]. |
| 262 int64 tab_pool_fp_; |
| 263 |
| 264 // The machiine tag associated with this tab pool. Used in the title of new |
| 265 // sync nodes. |
| 266 std::string machine_tag_; |
| 267 |
| 268 // Our sync service profile (for making changes to the sync db) |
| 269 ProfileSyncService* sync_service_; |
| 270 |
| 271 DISALLOW_COPY_AND_ASSIGN(TabNodePool); |
| 272 }; |
| 273 |
| 274 // Functor for comparing SessionID's. |
| 275 class SessionIDLessThan { |
| 276 public: |
| 277 bool operator()(const SessionID& lhs, const SessionID& rhs) const { |
| 278 return lhs.id() < rhs.id(); |
| 279 } |
| 280 }; |
| 281 |
| 282 // Datatypes for accessing foreign tab data. |
| 283 typedef std::map<SessionID, SessionTab*, SessionIDLessThan> IDToSessionTabMap; |
| 284 typedef std::map<std::string, IDToSessionTabMap*> ForeignTabMap; |
| 285 typedef std::map<std::string, ForeignSession*> ForeignSessionMap; |
| 286 |
| 287 // Datatypes for accessing local tab data. |
| 288 typedef std::map<SessionID, TabLinks, SessionIDLessThan> TabLinksMap; |
| 289 |
| 290 // Delete all foreign session/window/tab objects allocated dynamically. |
| 291 // This is comprised of ForeignSession*, IDToSessionTabMap*, and any orphaned |
| 292 // SessionTab*'s. |
| 293 void DeleteForeignSessions(); |
| 294 |
| 295 // Determine if a window is of a type we're interested in syncing. |
| 296 static bool ShouldSyncWindowType(const Browser::Type& type); |
| 297 |
| 298 // Build a sync tag from tab_node_id. |
| 299 static inline std::string TabIdToTag( |
| 300 const std::string machine_tag, |
| 301 size_t tab_node_id) { |
| 302 return StringPrintf("%s %zu", |
| 303 machine_tag.c_str(), tab_node_id); |
| 304 } |
| 305 |
| 306 // Initializes the tag corresponding to this machine. |
| 307 void InitializeCurrentMachineTag(sync_api::WriteTransaction* trans); |
115 | 308 |
116 // Updates the server data based upon the current client session. If no node | 309 // Updates the server data based upon the current client session. If no node |
117 // corresponding to this machine exists in the sync model, one is created. | 310 // corresponding to this machine exists in the sync model, one is created. |
118 void UpdateSyncModelDataFromClient(); | 311 void UpdateSyncModelDataFromClient(); |
119 | 312 |
120 // Pulls the current sync model from the sync database and returns true upon | 313 // Pulls the current sync model from the sync database and returns true upon |
121 // update of the client model. Called by SessionChangeProcessor. | 314 // update of the client model. Will associate any foreign sessions as well as |
122 // Note that the specifics_ vector is reset and built from scratch each time. | 315 // keep track of any local tab nodes, adding them to our free tab node pool. |
123 bool UpdateFromSyncModel(const sync_api::BaseTransaction* trans); | 316 bool UpdateAssociationsFromSyncModel(const sync_api::ReadNode& root, |
124 | 317 const sync_api::BaseTransaction* trans); |
125 // Helper for UpdateFromSyncModel. Appends sync data to a vector of specifics. | 318 |
126 bool QuerySyncModel(const sync_api::BaseTransaction* trans, | 319 // Used to populate a session window from the session specifics window |
127 std::vector<const sync_pb::SessionSpecifics*>& specifics); | 320 // provided. |
128 | 321 void PopulateSessionWindowFromSpecifics(std::string foreign_session_tag, |
129 // Builds sessions from buffered specifics data | 322 const sync_pb::SessionWindow& window, |
130 bool GetSessionData(std::vector<ForeignSession*>* sessions); | 323 const int64 mtime, |
131 | 324 SessionWindow* session_window); |
132 // Helper method to generate session specifics from session windows. | 325 |
133 void FillSpecificsFromSessions(std::vector<SessionWindow*>* windows, | 326 // Used to populate a session tab from the session specifics tab provided. |
134 sync_pb::SessionSpecifics* session); | 327 void PopulateSessionTabFromSpecifics(const sync_pb::SessionTab& tab, |
135 | 328 const int64 mtime, |
136 // Helper method for converting session specifics to windows. | 329 SessionTab* session_tab); |
137 void AppendForeignSessionFromSpecifics( | 330 |
138 const sync_pb::SessionSpecifics* specifics, | 331 // Used to populate a session tab from the session specifics tab provided. |
139 std::vector<ForeignSession*>* session); | 332 void AppendSessionTabNavigation(const sync_pb::TabNavigation& navigation, |
140 | 333 std::vector<TabNavigation>* navigations); |
141 // Fills the given empty vector with foreign session windows to restore. | 334 |
142 // If the vector is returned empty, then the session data could not be | 335 // Fills a tab sync node with data from a TabContents object. |
143 // converted back into windows. | 336 // (from a local navigation event) |
144 void AppendForeignSessionWithID(int64 id, | 337 bool WriteTabContentsToSyncModel(const TabContents& tab, |
145 std::vector<ForeignSession*>* session, | 338 const int64 sync_id, |
146 sync_api::BaseTransaction* trans); | 339 sync_api::WriteTransaction* trans); |
147 | 340 |
148 // Returns the syncable model type. | 341 // Populates the navigation portion of the session specifics. |
149 static syncable::ModelType model_type() { return syncable::SESSIONS; } | 342 void PopulateSessionSpecificsNavigation( |
150 | 343 const TabNavigation* navigation, |
151 private: | 344 sync_pb::TabNavigation* tab_navigation); |
152 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, WriteSessionToNode); | 345 |
153 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, | 346 // Specifies whether the window has tabs to sync. The new tab page does not |
154 WriteForeignSessionToNode); | 347 // count. If no tabs to sync, it returns true, otherwise false; |
| 348 bool WindowHasNoTabsToSync(const SessionWindow& window); |
| 349 |
| 350 // Control which local tabs we're interested in syncing. |
| 351 // Ensures the profile matches sync's profile and that the tab has at least |
| 352 // one navigation entry and is not an empty tab. |
| 353 bool IsValidTab(const TabContents& tab); |
| 354 |
| 355 // Control which foreign tabs we're interested in displaying. |
| 356 // Checks that the tab has navigations and is not a new tab. |
| 357 // Note: a new tab page with back/forward history is valid. |
| 358 bool IsValidSessionTab(const SessionTab& tab); |
155 | 359 |
156 // Returns the session service from |sync_service_|. | 360 // Returns the session service from |sync_service_|. |
157 SessionService* GetSessionService(); | 361 SessionService* GetSessionService(); |
158 | 362 |
159 // Initializes the tag corresponding to this machine. | |
160 // Note: creates a syncable::BaseTransaction. | |
161 void InitializeCurrentMachineTag(); | |
162 | |
163 // Populates the navigation portion of the session specifics. | |
164 void PopulateSessionSpecificsNavigation(const TabNavigation* navigation, | |
165 sync_pb::TabNavigation* tab_navigation); | |
166 | |
167 // Populates the tab portion of the session specifics. | |
168 void PopulateSessionSpecificsTab(const SessionTab* tab, | |
169 sync_pb::SessionTab* session_tab); | |
170 | |
171 // Populates the window portion of the session specifics. | |
172 void PopulateSessionSpecificsWindow(const SessionWindow* window, | |
173 sync_pb::SessionWindow* session_window); | |
174 | |
175 // Specifies whether the window has tabs to sync. The new tab page does not | |
176 // count. If no tabs to sync, it returns true, otherwise false; | |
177 bool WindowHasNoTabsToSync(const SessionWindow* window); | |
178 | |
179 // Internal method used in the callback to obtain the current session. | 363 // Internal method used in the callback to obtain the current session. |
180 // We don't own |windows|. | 364 // We don't own |windows|. |
181 void OnGotSession(int handle, std::vector<SessionWindow*>* windows); | 365 void OnGotSession(int handle, std::vector<SessionWindow*>* windows); |
182 | 366 |
183 // Used to populate a session tab from the session specifics tab provided. | 367 // Populate a session specifics header from a list of SessionWindows |
184 void AppendSessionTabNavigation(std::vector<TabNavigation>* navigations, | 368 void PopulateSessionSpecificsHeader( |
185 const sync_pb::TabNavigation* navigation); | 369 const std::vector<SessionWindow*>& windows, |
186 | 370 sync_pb::SessionHeader* header_s); |
187 // Used to populate a session tab from the session specifics tab provided. | 371 |
188 void PopulateSessionTabFromSpecifics(SessionTab* session_tab, | 372 // Populates the window portion of the session specifics. |
189 const sync_pb::SessionTab* tab, SessionID id); | 373 void PopulateSessionSpecificsWindow(const SessionWindow& window, |
190 | 374 sync_pb::SessionWindow* session_window); |
191 // Used to populate a session window from the session specifics window | 375 |
192 // provided. | 376 // Syncs all the tabs in |window| with the local sync db. Will allocate tab |
193 void PopulateSessionWindowFromSpecifics(SessionWindow* session_window, | 377 // nodes if needed. |
194 const sync_pb::SessionWindow* window); | 378 bool SyncLocalWindowToSyncModel(const SessionWindow& window); |
195 | 379 |
196 // Updates the current session on the server. Creates a node for this machine | 380 // Fills a tab sync node with data from a SessionTab object. |
197 // if there is not one already. | 381 // (from ReadCurrentSessions) |
198 bool UpdateSyncModel(sync_pb::SessionSpecifics* session_data, | 382 bool WriteSessionTabToSyncModel(const SessionTab& tab, |
199 sync_api::WriteTransaction* trans, | 383 const int64 sync_id, |
200 const sync_api::ReadNode* root); | 384 sync_api::WriteTransaction* trans); |
201 // Stores the machine tag. | 385 |
| 386 // Populates the tab portion of the session specifics. |
| 387 void PopulateSessionSpecificsTab(const SessionTab& tab, |
| 388 sync_pb::SessionTab* session_tab); |
| 389 |
| 390 // Local client name. |
202 std::string current_machine_tag_; | 391 std::string current_machine_tag_; |
203 | 392 |
204 // Stores the current set of foreign session specifics. | 393 // Pool of all used/available sync nodes associated with tabs. |
205 // Used by ForeignSessionHandler through |GetSessionData|. | 394 TabNodePool tab_pool_; |
206 // Built by |QuerySyncModel| via |UpdateFromSyncModel|. | 395 |
207 std::vector<const sync_pb::SessionSpecifics*> specifics_; | 396 // SyncID for the sync node containing all the window information for this |
| 397 // client. |
| 398 int64 local_session_syncid_; |
| 399 |
| 400 // Mapping of current open (local) tabs to their sync identifiers. |
| 401 TabLinksMap tab_map_; |
| 402 |
| 403 // Per foreign client mapping of their tab id's to their SessionTab objects. |
| 404 ForeignTabMap foreign_tab_map_; |
| 405 |
| 406 // Map of foreign sessions, accessed by the foreign client id. |
| 407 ForeignSessionMap foreign_session_map_; |
| 408 |
| 409 // The set of foreign tabs that we have seen, and created SessionTab objects |
| 410 // for, but have not yet mapped to ForeignSessions. These are temporarily |
| 411 // orphaned tabs, and won't be deleted if we delete foreign_session_map_. |
| 412 std::set<SessionTab*> unmapped_tabs_; |
208 | 413 |
209 // Weak pointer. | 414 // Weak pointer. |
210 ProfileSyncService* sync_service_; | 415 ProfileSyncService* sync_service_; |
211 | 416 |
212 // Consumer used to obtain the current session. | 417 // Consumer used to obtain the current session. |
213 CancelableRequestConsumer consumer_; | 418 CancelableRequestConsumer consumer_; |
214 | 419 |
215 DISALLOW_COPY_AND_ASSIGN(SessionModelAssociator); | 420 DISALLOW_COPY_AND_ASSIGN(SessionModelAssociator); |
216 }; | 421 }; |
217 | 422 |
218 } // namespace browser_sync | 423 } // namespace browser_sync |
219 | 424 |
220 #endif // CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ | 425 #endif // CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ |
OLD | NEW |