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

Side by Side Diff: components/sync_sessions/task_tracker.cc

Issue 2868043003: Track task ids for navigations cross multiple tabs. (Closed)
Patch Set: Created 3 years, 7 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "components/sync_sessions/task_tracker.h" 5 #include "components/sync_sessions/task_tracker.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/numerics/safe_conversions.h" 9 #include "base/numerics/safe_conversions.h"
10 10
11 namespace sync_sessions { 11 namespace sync_sessions {
12 12
13 namespace { 13 namespace {
14 // The maximum number of tasks we track in a tab. 14 // The maximum number of tasks we track in a tab.
15 int kMaxNumTasksPerTab = 100; 15 int kMaxNumTasksPerTab = 100;
16 } 16 }
17 17
18 TabTasks::TabTasks() {} 18 TabTasks::TabTasks() {}
19 19
20 TabTasks::TabTasks(const TabTasks* source_tab) {
21 if (source_tab->current_navigation_index_ >= 0) {
22 source_tab_task_ids_ = source_tab->GetTaskIdsForNavigation(
23 source_tab->current_navigation_index_);
24 }
25 }
26
20 TabTasks::~TabTasks() {} 27 TabTasks::~TabTasks() {}
21 28
22 std::vector<int64_t> TabTasks::GetTaskIdsForNavigation( 29 std::vector<int64_t> TabTasks::GetTaskIdsForNavigation(
23 int navigation_index) const { 30 int navigation_index) const {
24 CHECK_LE(0, navigation_index); 31 CHECK_LE(0, navigation_index);
25 CHECK_LT(navigation_index, GetNavigationsCount()); 32 CHECK_LT(navigation_index, GetNavigationsCount());
26 33
27 std::vector<int64_t> root_to_self_task_ids; 34 std::vector<int64_t> root_to_self_task_ids;
28 // Position of the navigation in task_ids_ vector. 35 // Position of the navigation in task_ids_ vector.
29 int navigation_position = navigation_index - excluded_navigation_num_; 36 int navigation_position = navigation_index - excluded_navigation_num_;
30 37
31 // If navigation_index is an excluded ancestor task, returns empty. 38 // If navigation_index is an excluded ancestor task, returns empty.
32 if (navigation_position < 0) 39 if (navigation_position < 0)
33 return root_to_self_task_ids; 40 return root_to_self_task_ids;
34 41
35 TaskIdAndRoot task_id_and_root = task_ids_[navigation_position]; 42 TaskIdAndRoot task_id_and_root = task_ids_[navigation_position];
36 43
37 // If navigation_index is an invalid task, returns empty. 44 // If navigation_index is an invalid task, returns empty.
38 if (task_id_and_root.root_navigation_index < 0) 45 if (task_id_and_root.root_navigation_index < 0)
39 return root_to_self_task_ids; 46 return root_to_self_task_ids;
40 47
41 // The root task can be excluded. If so, consider the oldest ancestor 48 // The root task can be excluded. If so, consider the oldest ancestor
42 // available as root. 49 // available as root.
43 int root_navigation_index = 50 int root_navigation_index =
44 task_id_and_root.root_navigation_index > excluded_navigation_num_ 51 task_id_and_root.root_navigation_index > excluded_navigation_num_
45 ? task_id_and_root.root_navigation_index - excluded_navigation_num_ 52 ? task_id_and_root.root_navigation_index - excluded_navigation_num_
46 : 0; 53 : 0;
54 // Only consider tasks from source tab when no tasks in current tab is
55 // excluded because max number per tab limit.
56 if (excluded_navigation_num_ == 0) {
57 int first_task_of_tab_position = 0;
58 // skipping invalid heading tasks
59 for (; first_task_of_tab_position < navigation_position &&
60 task_ids_[first_task_of_tab_position].root_navigation_index < 0;
61 first_task_of_tab_position++) {
62 }
63 // If task of current navigation can track back to the first task of the
64 // tab, then consider the source tab.
65 if (task_id_and_root.root_navigation_index == first_task_of_tab_position)
66 root_to_self_task_ids = source_tab_task_ids_;
67 }
47 for (int i = root_navigation_index; i <= navigation_position; i++) { 68 for (int i = root_navigation_index; i <= navigation_position; i++) {
48 // Fills the vector with valid tasks. 69 // Fills the vector with valid tasks.
49 if (task_ids_[i].root_navigation_index >= 0) 70 if (task_ids_[i].root_navigation_index >= 0)
50 root_to_self_task_ids.push_back(task_ids_[i].task_id); 71 root_to_self_task_ids.push_back(task_ids_[i].task_id);
51 } 72 }
52 return root_to_self_task_ids; 73 return root_to_self_task_ids;
53 } 74 }
54 75
55 int TabTasks::GetNavigationsCount() const { 76 int TabTasks::GetNavigationsCount() const {
56 return excluded_navigation_num_ + task_ids_.size(); 77 return excluded_navigation_num_ + task_ids_.size();
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 177
157 TaskTracker::~TaskTracker() {} 178 TaskTracker::~TaskTracker() {}
158 179
159 TabTasks* TaskTracker::GetTabTasks(SessionID::id_type tab_id) { 180 TabTasks* TaskTracker::GetTabTasks(SessionID::id_type tab_id) {
160 if (local_tab_tasks_map_.find(tab_id) == local_tab_tasks_map_.end()) { 181 if (local_tab_tasks_map_.find(tab_id) == local_tab_tasks_map_.end()) {
161 local_tab_tasks_map_[tab_id] = base::MakeUnique<TabTasks>(); 182 local_tab_tasks_map_[tab_id] = base::MakeUnique<TabTasks>();
162 } 183 }
163 return local_tab_tasks_map_[tab_id].get(); 184 return local_tab_tasks_map_[tab_id].get();
164 } 185 }
165 186
187 TabTasks* TaskTracker::GetTabTasks(SessionID::id_type tab_id,
188 SessionID::id_type source_tab_id) {
189 if (local_tab_tasks_map_.find(tab_id) == local_tab_tasks_map_.end()) {
190 auto iter = local_tab_tasks_map_.find(source_tab_id);
191 if (iter != local_tab_tasks_map_.end()) {
192 local_tab_tasks_map_[tab_id] =
193 base::MakeUnique<TabTasks>(iter->second.get());
194 } else {
195 local_tab_tasks_map_[tab_id] = base::MakeUnique<TabTasks>();
196 }
197 }
198 return local_tab_tasks_map_[tab_id].get();
199 }
200
166 void TaskTracker::CleanTabTasks(SessionID::id_type tab_id) { 201 void TaskTracker::CleanTabTasks(SessionID::id_type tab_id) {
167 auto iter = local_tab_tasks_map_.find(tab_id); 202 auto iter = local_tab_tasks_map_.find(tab_id);
168 if (iter != local_tab_tasks_map_.end()) { 203 if (iter != local_tab_tasks_map_.end()) {
169 local_tab_tasks_map_.erase(iter); 204 local_tab_tasks_map_.erase(iter);
170 } 205 }
171 } 206 }
172 207
173 } // namespace sync_sessions 208 } // namespace sync_sessions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698