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

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

Issue 2776633003: Add taskid for navigation, created in session sync (Closed)
Patch Set: initialize task id by global id Created 3 years, 8 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
(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(base::checked_cast<size_t>(navigation_index), task_ids_.size());
26
27 std::vector<int64_t> root_to_self_task_ids;
28 TaskIdAndRoot task_id_and_root = task_ids_[navigation_index];
29
30 // If navigation_index is an invalid task, returns empty.
31 if (task_id_and_root.root_navigation_index < 0)
32 return root_to_self_task_ids;
33
34 int root_navigation_index = task_id_and_root.root_navigation_index;
35 for (int i = root_navigation_index; i <= navigation_index; i++) {
36 // Fills the vector with valid tasks.
37 if (task_ids_[i].root_navigation_index >= 0)
38 root_to_self_task_ids.push_back(task_ids_[i].task_id);
39 }
40 return root_to_self_task_ids;
41 }
42
43 void TabTasks::UpdateWithNavigation(int64_t navigation_index,
44 ui::PageTransition transition,
45 int64_t navigation_id) {
46 // Limit number of tasks per tab in memory less than kMaxNumTasksPerTab.
47 if (navigation_index >= kMaxNumTasksPerTab)
48 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.
49
50 // Triggered by some notifications on the current page, do nothing.
51 if (navigation_index == current_navigation_index_) {
52 DVLOG(1) << "Doing nothing for navigation_index: " << navigation_index
53 << " of transition: " << transition;
54 return;
55 }
56
57 // Going back/forward to some previous navigation.
58 if (navigation_index < current_navigation_index_ ||
59 (navigation_index > current_navigation_index_ &&
60 transition & ui::PAGE_TRANSITION_FORWARD_BACK &&
61 base::checked_cast<size_t>(navigation_index) < task_ids_.size())) {
62 DVLOG(1) << "Just updating task position with navigation_index: "
63 << navigation_index << " of transition: " << transition;
64 current_navigation_index_ = navigation_index;
65 return;
66 }
67
68 // A new task for the new navigation.
69 int root_navigation_index = navigation_index;
70 if (current_navigation_index_ != -1 &&
71 (ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK) ||
72 ui::PageTransitionCoreTypeIs(transition,
73 ui::PAGE_TRANSITION_AUTO_SUBFRAME) ||
74 ui::PageTransitionCoreTypeIs(transition,
75 ui::PAGE_TRANSITION_MANUAL_SUBFRAME) ||
76 ui::PageTransitionCoreTypeIs(transition,
77 ui::PAGE_TRANSITION_FORM_SUBMIT) ||
78 transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK)) {
79 // Creating a sub-task with navigation at current_navigation_index as
80 // parent.
81 DVLOG(1) << "Creating a sub-task with navigation_index: "
82 << navigation_index << " of transition: " << transition
83 << " under navigation_index: " << current_navigation_index_;
84 root_navigation_index =
85 task_ids_[current_navigation_index_].root_navigation_index;
86 } else {
87 // Creating a root task.
88 // For now, we don't consider tasks cross tabs, so first navigation of the
89 // tab always creates a root task.
90 DVLOG(1) << "Creating a root task with navigation_index: "
91 << navigation_index << " of transition: " << transition;
92 }
93 TaskIdAndRoot new_task = {root_navigation_index, navigation_id};
94
95 // In most cases navigation_index == task_ids_.size() if the previous
96 // navigation is end of chain, or navigation_index < task_ids_.size()
97 // otherwise. In few case navigation_index > task_ids_.size(), we fill
98 // task_ids_ with invalid contents. A known case is the first navigation after
99 // newtab.
100 for (int i = task_ids_.size(); i < navigation_index; i++)
101 task_ids_.push_back({-1, -1});
102
103 // 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.
104 task_ids_.erase(task_ids_.begin() + navigation_index, task_ids_.end());
105
106 // Add the current task at navigation_index.
107 task_ids_.push_back(new_task);
108 current_navigation_index_ = navigation_index;
109 return;
110 }
111
112 TaskTracker::TaskTracker() {}
113
114 TaskTracker::~TaskTracker() {}
115
116 TabTasks* TaskTracker::GetTabTasks(SessionID::id_type tab_id) {
117 if (local_tab_tasks_map_.find(tab_id) == local_tab_tasks_map_.end()) {
118 local_tab_tasks_map_[tab_id] = base::MakeUnique<TabTasks>();
119 }
120 return local_tab_tasks_map_[tab_id].get();
121 }
122
123 void TaskTracker::CleanTabTasks(SessionID::id_type tab_id) {
124 auto iter = local_tab_tasks_map_.find(tab_id);
125 if (iter != local_tab_tasks_map_.end()) {
126 local_tab_tasks_map_.erase(iter);
127 }
128 }
129
130 } // namespace sync_sessions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698