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