session_model_associator.h revision bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293
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 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ 6#define CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ 7#pragma once 8 9#include <string> 10#include <vector> 11 12#include "base/basictypes.h" 13#include "base/gtest_prod_util.h" 14#include "base/observer_list.h" 15#include "chrome/browser/sessions/session_service.h" 16#include "chrome/browser/sessions/session_types.h" 17#include "chrome/browser/sync/engine/syncapi.h" 18#include "chrome/browser/sync/glue/model_associator.h" 19#include "chrome/browser/sync/protocol/session_specifics.pb.h" 20#include "chrome/browser/sync/syncable/model_type.h" 21#include "chrome/common/notification_registrar.h" 22 23class Profile; 24class ProfileSyncService; 25 26namespace sync_api { 27class ReadNode; 28class WriteNode; 29class WriteTransaction; 30} // namespace sync_api 31 32namespace sync_pb { 33class SessionSpecifics; 34} // namespace sync_pb 35 36namespace browser_sync { 37 38static const char kSessionsTag[] = "google_chrome_sessions"; 39// Contains all logic for associating the Chrome sessions model and 40// the sync sessions model. 41class SessionModelAssociator : public PerDataTypeAssociatorInterface< 42 sync_pb::SessionSpecifics, std::string>, public NonThreadSafe { 43 public: 44 45 // Does not take ownership of sync_service. 46 explicit SessionModelAssociator(ProfileSyncService* sync_service); 47 virtual ~SessionModelAssociator(); 48 49 50 // AssociatorInterface and PerDataTypeAssociator Interface implementation. 51 virtual void AbortAssociation() { 52 return; 53 // No implementation needed, this associator runs on the main 54 // thread. 55 } 56 57 // Used to re-associate a foreign session. 58 virtual void Associate(const sync_pb::SessionSpecifics* specifics, 59 int64 sync_id); 60 61 // Updates the sync model with the local client data. 62 virtual bool AssociateModels(); 63 64 // The has_nodes out parameter is set to true if the chrome model 65 // has user-created nodes. The method may return false if an error 66 // occurred. 67 virtual bool ChromeModelHasUserCreatedNodes(bool* has_nodes); 68 69 // Will update the new tab page with current foreign sessions excluding the 70 // one being disassociated. 71 virtual void Disassociate(int64 sync_id); 72 73 // TODO(jerrica): Add functionality to stop rendering foreign sessions to the 74 // new tab page. 75 virtual bool DisassociateModels() { 76 // There is no local model stored with which to disassociate. 77 return true; 78 } 79 80 // Returns the chrome session specifics for the given sync id. 81 // Returns NULL if no specifics are found for the given sync id. 82 virtual const sync_pb::SessionSpecifics* GetChromeNodeFromSyncId( 83 int64 sync_id); 84 85 // Returns whether a node with the given permanent tag was found and update 86 // |sync_id| with that node's id. 87 virtual bool GetSyncIdForTaggedNode(const std::string* tag, int64* sync_id); 88 89 // Returns sync id for the given chrome model id. 90 // Returns sync_api::kInvalidId if the sync node is not found for the given 91 // chrome id. 92 virtual int64 GetSyncIdFromChromeId(std::string id); 93 94 95 // Initializes the given sync node from the given chrome node id. 96 // Returns false if no sync node was found for the given chrome node id or 97 // if the initialization of sync node fails. 98 virtual bool InitSyncNodeFromChromeId(std::string id, 99 sync_api::BaseNode* sync_node) { 100 return false; 101 } 102 103 // The has_nodes out parameter is set to true if the sync model has 104 // nodes other than the permanent tagged nodes. The method may 105 // return false if an error occurred. 106 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); 107 108 // Returns the tag used to uniquely identify this machine's session in the 109 // sync model. 110 std::string GetCurrentMachineTag(); 111 112 // Pulls the current sync model from the server, and returns true upon update 113 // of the client model. 114 bool GetSessionDataFromSyncModel(std::vector<ForeignSession*>* windows); 115 116 117 // Helper method for converting session specifics to windows. 118 void AppendForeignSessionFromSpecifics( 119 const sync_pb::SessionSpecifics* specifics, 120 std::vector<ForeignSession*>* session); 121 122 // Fills the given empty vector with foreign session windows to restore. 123 // If the vector is returned empty, then the session data could not be 124 // converted back into windows. 125 void AppendForeignSessionWithID(int64 id, 126 std::vector<ForeignSession*>* session, 127 sync_api::BaseTransaction* trans); 128 129 // Returns the syncable model type. 130 static syncable::ModelType model_type() { return syncable::SESSIONS; } 131 132 // Updates the server data based upon the current client session. If no node 133 // corresponding to this machine exists in the sync model, one is created. 134 void UpdateSyncModelDataFromClient(); 135 136 private: 137 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, WriteSessionToNode); 138 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceSessionTest, 139 WriteForeignSessionToNode); 140 141 // Returns the session service from |sync_service_|. 142 SessionService* GetSessionService(); 143 144 // Initializes the tag corresponding to this machine. 145 void InitializeCurrentMachineTag(); 146 147 // Populates the navigation portion of the session specifics. 148 void PopulateSessionSpecificsNavigation(const TabNavigation* navigation, 149 sync_pb::TabNavigation* tab_navigation); 150 151 // Populates the tab portion of the session specifics. 152 void PopulateSessionSpecificsTab(const SessionTab* tab, 153 sync_pb::SessionTab* session_tab); 154 155 // Populates the window portion of the session specifics. 156 void PopulateSessionSpecificsWindow(const SessionWindow* window, 157 sync_pb::SessionWindow* session_window); 158 159 // Specifies whether the window has tabs to sync. The new tab page does not 160 // count. If no tabs to sync, it returns true, otherwise false; 161 bool WindowHasNoTabsToSync(const SessionWindow* window); 162 163 // Internal method used in the callback to obtain the current session. 164 // We don't own |windows|. 165 void OnGotSession(int handle, std::vector<SessionWindow*>* windows); 166 167 // Used to populate a session tab from the session specifics tab provided. 168 void AppendSessionTabNavigation(std::vector<TabNavigation>* navigations, 169 const sync_pb::TabNavigation* navigation); 170 171 // Used to populate a session tab from the session specifics tab provided. 172 void PopulateSessionTabFromSpecifics(SessionTab* session_tab, 173 const sync_pb::SessionTab* tab, SessionID id); 174 175 // Used to populate a session window from the session specifics window 176 // provided. 177 void PopulateSessionWindowFromSpecifics(SessionWindow* session_window, 178 const sync_pb::SessionWindow* window); 179 180 // Updates the current session on the server. Creates a node for this machine 181 // if there is not one already. 182 bool UpdateSyncModel(sync_pb::SessionSpecifics* session_data, 183 sync_api::WriteTransaction* trans, 184 const sync_api::ReadNode* root); 185 186 // Stores the machine tag. 187 std::string current_machine_tag_; 188 189 // Weak pointer. 190 ProfileSyncService* sync_service_; 191 192 // Consumer used to obtain the current session. 193 CancelableRequestConsumer consumer_; 194 195 DISALLOW_COPY_AND_ASSIGN(SessionModelAssociator); 196}; 197 198} // namespace browser_sync 199 200#endif // CHROME_BROWSER_SYNC_GLUE_SESSION_MODEL_ASSOCIATOR_H_ 201 202