Chromium Code Reviews| Index: components/sync_sessions/task_tracker.cc |
| diff --git a/components/sync_sessions/task_tracker.cc b/components/sync_sessions/task_tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..312784e07795264e71d4d1211ee5f2dade947e87 |
| --- /dev/null |
| +++ b/components/sync_sessions/task_tracker.cc |
| @@ -0,0 +1,130 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/sync_sessions/task_tracker.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/numerics/safe_conversions.h" |
| + |
| +namespace sync_sessions { |
| + |
| +namespace { |
| +// The maximum number of tasks we track in a tab. |
| +int kMaxNumTasksPerTab = 100; |
| +} |
| + |
| +TabTasks::TabTasks() {} |
| + |
| +TabTasks::~TabTasks() {} |
| + |
| +std::vector<int64_t> TabTasks::GetTaskIdsForNavigation( |
| + int navigation_index) const { |
| + CHECK_LE(0, navigation_index); |
| + CHECK_LT(base::checked_cast<size_t>(navigation_index), task_ids_.size()); |
| + |
| + std::vector<int64_t> root_to_self_task_ids; |
| + TaskIdAndRoot task_id_and_root = task_ids_[navigation_index]; |
| + |
| + // If navigation_index is an invalid task, returns empty. |
| + if (task_id_and_root.root_navigation_index < 0) |
| + return root_to_self_task_ids; |
| + |
| + int root_navigation_index = task_id_and_root.root_navigation_index; |
| + for (int i = root_navigation_index; i <= navigation_index; i++) { |
| + // Fills the vector with valid tasks. |
| + if (task_ids_[i].root_navigation_index >= 0) |
| + root_to_self_task_ids.push_back(task_ids_[i].task_id); |
| + } |
| + return root_to_self_task_ids; |
| +} |
| + |
| +void TabTasks::UpdateWithNavigation(int64_t navigation_index, |
| + ui::PageTransition transition, |
| + int64_t navigation_id) { |
| + // Limit number of tasks per tab in memory less than kMaxNumTasksPerTab. |
| + if (navigation_index >= kMaxNumTasksPerTab) |
| + return; |
|
Nicolas Zea
2017/04/17 23:18:46
I think the limiting should be done at the oldest
shenchao
2017/04/18 16:59:36
We can do that. Here is the plan:
1. Add size_t ex
shenchao
2017/04/19 22:10:01
Done.
|
| + |
| + // Triggered by some notifications on the current page, do nothing. |
| + if (navigation_index == current_navigation_index_) { |
| + DVLOG(1) << "Doing nothing for navigation_index: " << navigation_index |
| + << " of transition: " << transition; |
| + return; |
| + } |
| + |
| + // Going back/forward to some previous navigation. |
| + if (navigation_index < current_navigation_index_ || |
| + (navigation_index > current_navigation_index_ && |
| + transition & ui::PAGE_TRANSITION_FORWARD_BACK && |
| + base::checked_cast<size_t>(navigation_index) < task_ids_.size())) { |
| + DVLOG(1) << "Just updating task position with navigation_index: " |
| + << navigation_index << " of transition: " << transition; |
| + current_navigation_index_ = navigation_index; |
| + return; |
| + } |
| + |
| + // A new task for the new navigation. |
| + int root_navigation_index = navigation_index; |
| + if (current_navigation_index_ != -1 && |
| + (ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK) || |
| + ui::PageTransitionCoreTypeIs(transition, |
| + ui::PAGE_TRANSITION_AUTO_SUBFRAME) || |
| + ui::PageTransitionCoreTypeIs(transition, |
| + ui::PAGE_TRANSITION_MANUAL_SUBFRAME) || |
| + ui::PageTransitionCoreTypeIs(transition, |
| + ui::PAGE_TRANSITION_FORM_SUBMIT) || |
| + transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK)) { |
| + // Creating a sub-task with navigation at current_navigation_index as |
| + // parent. |
| + DVLOG(1) << "Creating a sub-task with navigation_index: " |
| + << navigation_index << " of transition: " << transition |
| + << " under navigation_index: " << current_navigation_index_; |
| + root_navigation_index = |
| + task_ids_[current_navigation_index_].root_navigation_index; |
| + } else { |
| + // Creating a root task. |
| + // For now, we don't consider tasks cross tabs, so first navigation of the |
| + // tab always creates a root task. |
| + DVLOG(1) << "Creating a root task with navigation_index: " |
| + << navigation_index << " of transition: " << transition; |
| + } |
| + TaskIdAndRoot new_task = {root_navigation_index, navigation_id}; |
| + |
| + // In most cases navigation_index == task_ids_.size() if the previous |
| + // navigation is end of chain, or navigation_index < task_ids_.size() |
| + // otherwise. In few case navigation_index > task_ids_.size(), we fill |
| + // task_ids_ with invalid contents. A known case is the first navigation after |
| + // newtab. |
| + for (int i = task_ids_.size(); i < navigation_index; i++) |
| + task_ids_.push_back({-1, -1}); |
| + |
| + // Erase those for forward for the previous navigations. |
|
Nicolas Zea
2017/04/17 23:18:46
nit: This comment is unclear. How about "Erase all
shenchao
2017/04/19 22:10:01
Done.
|
| + task_ids_.erase(task_ids_.begin() + navigation_index, task_ids_.end()); |
| + |
| + // Add the current task at navigation_index. |
| + task_ids_.push_back(new_task); |
| + current_navigation_index_ = navigation_index; |
| + return; |
| +} |
| + |
| +TaskTracker::TaskTracker() {} |
| + |
| +TaskTracker::~TaskTracker() {} |
| + |
| +TabTasks* TaskTracker::GetTabTasks(SessionID::id_type tab_id) { |
| + if (local_tab_tasks_map_.find(tab_id) == local_tab_tasks_map_.end()) { |
| + local_tab_tasks_map_[tab_id] = base::MakeUnique<TabTasks>(); |
| + } |
| + return local_tab_tasks_map_[tab_id].get(); |
| +} |
| + |
| +void TaskTracker::CleanTabTasks(SessionID::id_type tab_id) { |
| + auto iter = local_tab_tasks_map_.find(tab_id); |
| + if (iter != local_tab_tasks_map_.end()) { |
| + local_tab_tasks_map_.erase(iter); |
| + } |
| +} |
| + |
| +} // namespace sync_sessions |