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