Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: chrome/browser/sync/glue/tab_node_pool.cc

Issue 16421003: [Sync] Add logic to reassociate tab nodes after restart. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync/glue/tab_node_pool.h" 5 #include "chrome/browser/sync/glue/tab_node_pool.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/sync/profile_sync_service.h" 11 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "sync/internal_api/public/base/model_type.h" 12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/internal_api/public/read_node.h" 13 #include "sync/internal_api/public/read_node.h"
14 #include "sync/internal_api/public/write_node.h" 14 #include "sync/internal_api/public/write_node.h"
15 #include "sync/internal_api/public/write_transaction.h" 15 #include "sync/internal_api/public/write_transaction.h"
16 16
17 namespace browser_sync { 17 namespace browser_sync {
18 18
19 static const char kNoSessionsFolderError[] = 19 static const char kNoSessionsFolderError[] =
20 "Server did not create the top-level sessions node. We " 20 "Server did not create the top-level sessions node. We "
21 "might be running against an out-of-date server."; 21 "might be running against an out-of-date server.";
22 22
23 TabNodePool::TabNodePool( 23 TabNodePool::TabNodePool(
24 ProfileSyncService* sync_service) 24 ProfileSyncService* sync_service)
25 : tab_pool_fp_(-1), 25 : tab_pool_fp_(-1),
26 sync_service_(sync_service) { 26 max_used_tab_node_id_(0),
27 } 27 sync_service_(sync_service) {}
28 28
29 TabNodePool::~TabNodePool() {} 29 TabNodePool::~TabNodePool() {}
30 30
31 // Static 31 // Static
32 std::string TabNodePool::TabIdToTag( 32 std::string TabNodePool::TabIdToTag(
33 const std::string machine_tag, 33 const std::string machine_tag,
34 size_t tab_node_id) { 34 size_t tab_node_id) {
35 return base::StringPrintf("%s %" PRIuS "", machine_tag.c_str(), tab_node_id); 35 return base::StringPrintf("%s %" PRIuS "", machine_tag.c_str(), tab_node_id);
36 } 36 }
37 37
38 void TabNodePool::AddTabNode(int64 sync_id) { 38 void TabNodePool::AddOldTabNode(int64 sync_id, size_t tab_node_id) {
39 DCHECK_GT(sync_id, 0);
40 DCHECK(old_node_syncids_.find(sync_id) == old_node_syncids_.end());
41 if (max_used_tab_node_id_ < tab_node_id)
42 max_used_tab_node_id_ = tab_node_id;
43 // Grow the tab node pool by 1, to ensure tab pool is of correct size.
39 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1); 44 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1);
40 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; 45 old_node_syncids_.insert(sync_id);
Nicolas Zea 2013/06/06 21:27:39 I'm not sure we really need to have the notion of
shashi 2013/06/07 01:44:08 In case of Android we have two requirements: 1. Ol
Nicolas Zea 2013/06/07 14:42:28 Right, and I agree with that approach. My point is
shashi 2013/06/07 19:08:35 Got it, I am going to cleanup the API as per the s
41 } 46 }
42 47
43 int64 TabNodePool::GetFreeTabNode() { 48 int64 TabNodePool::GetFreeTabNode() {
44 DCHECK_GT(machine_tag_.length(), 0U); 49 DCHECK_GT(machine_tag_.length(), 0U);
45 if (tab_pool_fp_ == -1) { 50 if (tab_pool_fp_ == -1) {
46 // Tab pool has no free nodes, allocate new one. 51 // Tab pool has no free nodes, allocate new one.
47 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); 52 syncer::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare());
48 syncer::ReadNode root(&trans); 53 syncer::ReadNode root(&trans);
49 if (root.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::SESSIONS)) != 54 if (root.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::SESSIONS)) !=
50 syncer::BaseNode::INIT_OK) { 55 syncer::BaseNode::INIT_OK) {
51 LOG(ERROR) << kNoSessionsFolderError; 56 LOG(ERROR) << kNoSessionsFolderError;
52 return syncer::kInvalidId; 57 return syncer::kInvalidId;
53 } 58 }
54 size_t tab_node_id = tab_syncid_pool_.size(); 59 size_t tab_node_id = ++max_used_tab_node_id_;
55 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id); 60 std::string tab_node_tag = TabIdToTag(machine_tag_, tab_node_id);
56 syncer::WriteNode tab_node(&trans); 61 syncer::WriteNode tab_node(&trans);
57 syncer::WriteNode::InitUniqueByCreationResult result = 62 syncer::WriteNode::InitUniqueByCreationResult result =
58 tab_node.InitUniqueByCreation(syncer::SESSIONS, root, tab_node_tag); 63 tab_node.InitUniqueByCreation(syncer::SESSIONS, root, tab_node_tag);
59 if (result != syncer::WriteNode::INIT_SUCCESS) { 64 if (result != syncer::WriteNode::INIT_SUCCESS) {
60 LOG(ERROR) << "Could not create new node with tag " 65 LOG(ERROR) << "Could not create new node with tag "
61 << tab_node_tag << "!"; 66 << tab_node_tag << "!";
62 return syncer::kInvalidId; 67 return syncer::kInvalidId;
63 } 68 }
64 // We fill the new node with just enough data so that in case of a crash/bug 69 // We fill the new node with just enough data so that in case of a crash/bug
65 // we can identify the node as our own on re-association and reuse it. 70 // we can identify the node as our own on re-association and reuse it.
66 tab_node.SetTitle(UTF8ToWide(tab_node_tag)); 71 tab_node.SetTitle(UTF8ToWide(tab_node_tag));
67 sync_pb::SessionSpecifics specifics; 72 sync_pb::SessionSpecifics specifics;
68 specifics.set_session_tag(machine_tag_); 73 specifics.set_session_tag(machine_tag_);
69 specifics.set_tab_node_id(tab_node_id); 74 specifics.set_tab_node_id(tab_node_id);
70 tab_node.SetSessionSpecifics(specifics); 75 tab_node.SetSessionSpecifics(specifics);
71 76
72 // Grow the pool by 1 since we created a new node. We don't actually need 77 // Grow the pool by 1 since we created a new node. We don't actually need
73 // to put the node's id in the pool now, since the pool is still empty. 78 // to put the node's id in the pool now, since the pool is still empty.
74 // The id will be added when that tab is closed and the node is freed. 79 // The id will be added when that tab is closed and the node is freed.
75 tab_syncid_pool_.resize(tab_node_id + 1); 80 tab_syncid_pool_.resize(tab_syncid_pool_.size() + 1);
76 DVLOG(1) << "Adding sync node " 81 DVLOG(1) << "Adding sync node " << tab_node.GetId()
77 << tab_node.GetId() << " to tab syncid pool"; 82 << " to tab syncid pool";
78 return tab_node.GetId(); 83 return tab_node.GetId();
79 } else { 84 } else {
80 // There are nodes available, grab next free and decrement free pointer. 85 // There are nodes available, grab next free and decrement free pointer.
81 return tab_syncid_pool_[static_cast<size_t>(tab_pool_fp_--)]; 86 return tab_syncid_pool_[static_cast<size_t>(tab_pool_fp_--)];
82 } 87 }
83 } 88 }
84 89
85 void TabNodePool::FreeTabNode(int64 sync_id) { 90 void TabNodePool::FreeTabNode(int64 sync_id) {
86 // Pool size should always match # of free tab nodes. 91 // Pool size should always match # of free tab nodes.
87 DCHECK_LT(tab_pool_fp_, static_cast<int64>(tab_syncid_pool_.size())); 92 DCHECK_LT(tab_pool_fp_, static_cast<int64>(tab_syncid_pool_.size()));
88 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id; 93 tab_syncid_pool_[static_cast<size_t>(++tab_pool_fp_)] = sync_id;
89 } 94 }
90 95
96 bool TabNodePool::RemoveIfOldSyncNodeExists(int64 sync_id) {
97 std::set<int64>::iterator position = old_node_syncids_.find(sync_id);
98 if (position != old_node_syncids_.end()) {
99 // If sync_id exists remove it and return.
100 old_node_syncids_.erase(position);
101 return true;
102 }
103 return false;
104 }
105
106 void TabNodePool::FreeUnusedOldSyncNodes(const std::set<int64>& used_sync_ids) {
107 for (std::set<int64>::iterator it = old_node_syncids_.begin();
108 it != old_node_syncids_.end();) {
109 if (used_sync_ids.find(*it) == used_sync_ids.end()) {
110 FreeTabNode(*it);
111 old_node_syncids_.erase(it++);
112 } else {
113 ++it;
114 }
115 }
116 }
117
91 } // namespace browser_sync 118 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698