OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/sync_sessions/task_tracker.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/numerics/safe_conversions.h" |
| 10 |
| 11 namespace sync_sessions { |
| 12 |
| 13 namespace { |
| 14 // The maximum number of tasks we track in a tab. |
| 15 int kMaxNumTasksPerTab = 100; |
| 16 } |
| 17 |
| 18 TabTasks::TabTasks() {} |
| 19 |
| 20 TabTasks::~TabTasks() {} |
| 21 |
| 22 std::vector<int64_t> TabTasks::GetTaskIdsForNavigation( |
| 23 int navigation_index) const { |
| 24 CHECK_LE(0, navigation_index); |
| 25 CHECK_LT(navigation_index, GetNavigationsCount()); |
| 26 |
| 27 std::vector<int64_t> root_to_self_task_ids; |
| 28 // Position of the navigation in task_ids_ vector. |
| 29 int navigation_position = navigation_index - excluded_navigation_num_; |
| 30 |
| 31 // If navigation_index is an excluded ancestor task, returns empty. |
| 32 if (navigation_position < 0) |
| 33 return root_to_self_task_ids; |
| 34 |
| 35 TaskIdAndRoot task_id_and_root = task_ids_[navigation_position]; |
| 36 |
| 37 // If navigation_index is an invalid task, returns empty. |
| 38 if (task_id_and_root.root_navigation_index < 0) |
| 39 return root_to_self_task_ids; |
| 40 |
| 41 // The root task can be excluded. If so, consider the oldest ancestor |
| 42 // available as root. |
| 43 int root_navigation_index = |
| 44 task_id_and_root.root_navigation_index > excluded_navigation_num_ |
| 45 ? task_id_and_root.root_navigation_index - excluded_navigation_num_ |
| 46 : 0; |
| 47 for (int i = root_navigation_index; i <= navigation_position; i++) { |
| 48 // Fills the vector with valid tasks. |
| 49 if (task_ids_[i].root_navigation_index >= 0) |
| 50 root_to_self_task_ids.push_back(task_ids_[i].task_id); |
| 51 } |
| 52 return root_to_self_task_ids; |
| 53 } |
| 54 |
| 55 int TabTasks::GetNavigationsCount() const { |
| 56 return excluded_navigation_num_ + task_ids_.size(); |
| 57 } |
| 58 |
| 59 void TabTasks::UpdateWithNavigation(int navigation_index, |
| 60 ui::PageTransition transition, |
| 61 int64_t navigation_id) { |
| 62 // Triggered by some notifications on the current page, do nothing. |
| 63 if (navigation_index == current_navigation_index_) { |
| 64 DVLOG(1) << "Doing nothing for navigation_index: " << navigation_index |
| 65 << " of transition: " << transition; |
| 66 return; |
| 67 } |
| 68 |
| 69 // Going back/forward to some previous navigation. |
| 70 if (navigation_index < current_navigation_index_ || |
| 71 (navigation_index > current_navigation_index_ && |
| 72 transition & ui::PAGE_TRANSITION_FORWARD_BACK && |
| 73 base::checked_cast<size_t>(navigation_index) < task_ids_.size())) { |
| 74 DVLOG(1) << "Just updating task position with navigation_index: " |
| 75 << navigation_index << " of transition: " << transition; |
| 76 current_navigation_index_ = navigation_index; |
| 77 return; |
| 78 } |
| 79 |
| 80 // A new task for the new navigation. |
| 81 int root_navigation_index = navigation_index; |
| 82 if (current_navigation_index_ != -1 && |
| 83 (ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK) || |
| 84 ui::PageTransitionCoreTypeIs(transition, |
| 85 ui::PAGE_TRANSITION_AUTO_SUBFRAME) || |
| 86 ui::PageTransitionCoreTypeIs(transition, |
| 87 ui::PAGE_TRANSITION_MANUAL_SUBFRAME) || |
| 88 ui::PageTransitionCoreTypeIs(transition, |
| 89 ui::PAGE_TRANSITION_FORM_SUBMIT) || |
| 90 transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK)) { |
| 91 // Creating a sub-task with navigation at current_navigation_index as |
| 92 // parent. |
| 93 DVLOG(1) << "Creating a sub-task with navigation_index: " |
| 94 << navigation_index << " of transition: " << transition |
| 95 << " under navigation_index: " << current_navigation_index_; |
| 96 // Position in task_id_. |
| 97 int current_navigation_position = |
| 98 current_navigation_index_ - excluded_navigation_num_; |
| 99 // If current/parent task is excluded, consider the new task as a root task. |
| 100 if (current_navigation_position >= 0) { |
| 101 CHECK_LT(current_navigation_position, |
| 102 base::checked_cast<int>(task_ids_.size())); |
| 103 root_navigation_index = |
| 104 task_ids_[current_navigation_position].root_navigation_index; |
| 105 } else { |
| 106 DVLOG(1) << "Becaue parent task is excluded, consider the sub-task as a " |
| 107 "root task."; |
| 108 } |
| 109 } else { |
| 110 // Creating a root task. |
| 111 // For now, we don't consider tasks cross tabs, so first navigation of the |
| 112 // tab always creates a root task. |
| 113 DVLOG(1) << "Creating a root task with navigation_index: " |
| 114 << navigation_index << " of transition: " << transition; |
| 115 } |
| 116 |
| 117 // In most cases navigation_index == excluded_navigation_num_ + |
| 118 // task_ids_.size() if the previous navigation is end of chain, or |
| 119 // navigation_index < excluded_navigation_num_ + task_ids_.size() otherwise. |
| 120 // In few case navigation_index > excluded_navigation_num_ + task_ids_.size(), |
| 121 // we fill task_ids_ with invalid contents. A known case is the first |
| 122 // navigation after newtab. |
| 123 for (int i = task_ids_.size() + excluded_navigation_num_; |
| 124 i < navigation_index; i++) { |
| 125 task_ids_.push_back({-1, -1}); |
| 126 } |
| 127 |
| 128 // Erase all task ids associated with an outdated forward navigation stack. |
| 129 if (navigation_index > excluded_navigation_num_) { |
| 130 int new_task_id_position = navigation_index - excluded_navigation_num_; |
| 131 task_ids_.erase(task_ids_.begin() + new_task_id_position, task_ids_.end()); |
| 132 } else { |
| 133 excluded_navigation_num_ = navigation_index; |
| 134 // new task id position is 0 |
| 135 task_ids_.clear(); |
| 136 } |
| 137 |
| 138 // Exclude oldest ancestors if task number reaches the limit. |
| 139 int more_tasks_number = task_ids_.size() + 1 - kMaxNumTasksPerTab; |
| 140 if (more_tasks_number > 0) { |
| 141 task_ids_.erase(task_ids_.begin(), task_ids_.begin() + more_tasks_number); |
| 142 DVLOG(1) << "Excluding " << more_tasks_number |
| 143 << " oldest ancestor(s) from navigation index " |
| 144 << excluded_navigation_num_; |
| 145 excluded_navigation_num_ += more_tasks_number; |
| 146 } |
| 147 |
| 148 TaskIdAndRoot new_task = {root_navigation_index, navigation_id}; |
| 149 // Add the current task at navigation_index. |
| 150 task_ids_.push_back(new_task); |
| 151 current_navigation_index_ = navigation_index; |
| 152 return; |
| 153 } |
| 154 |
| 155 TaskTracker::TaskTracker() {} |
| 156 |
| 157 TaskTracker::~TaskTracker() {} |
| 158 |
| 159 TabTasks* TaskTracker::GetTabTasks(SessionID::id_type tab_id) { |
| 160 if (local_tab_tasks_map_.find(tab_id) == local_tab_tasks_map_.end()) { |
| 161 local_tab_tasks_map_[tab_id] = base::MakeUnique<TabTasks>(); |
| 162 } |
| 163 return local_tab_tasks_map_[tab_id].get(); |
| 164 } |
| 165 |
| 166 void TaskTracker::CleanTabTasks(SessionID::id_type tab_id) { |
| 167 auto iter = local_tab_tasks_map_.find(tab_id); |
| 168 if (iter != local_tab_tasks_map_.end()) { |
| 169 local_tab_tasks_map_.erase(iter); |
| 170 } |
| 171 } |
| 172 |
| 173 } // namespace sync_sessions |
OLD | NEW |