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 TabTasks::TabTasks(base::Clock* clock) : clock_((clock)) {} | |
14 | |
15 TabTasks::~TabTasks() {} | |
16 | |
17 std::vector<int64_t> TabTasks::RootToSelfTaskIdsOfNavigationIndex( | |
18 int navigation_index) const { | |
19 CHECK_LE(0, navigation_index); | |
20 CHECK_LT(base::checked_cast<size_t>(navigation_index), task_ids_.size()); | |
21 | |
22 std::vector<int64_t> root_to_self_task_ids; | |
23 TaskIdAndRoot task_id_and_root = task_ids_[navigation_index]; | |
24 | |
25 // If navigation_index is an invalid task, returns empty. | |
26 if (task_id_and_root.root_navigation_index < 0) | |
27 return root_to_self_task_ids; | |
28 | |
29 int root_navigation_index = task_id_and_root.root_navigation_index; | |
30 for (int i = root_navigation_index; i <= navigation_index; i++) { | |
31 // Fills the vector with valid tasks. | |
32 if (task_ids_[i].root_navigation_index >= 0) | |
33 root_to_self_task_ids.push_back(task_ids_[i].task_id); | |
34 } | |
35 return root_to_self_task_ids; | |
36 } | |
37 | |
38 void TabTasks::UpdateWithNavigation(int64_t navigation_index, | |
39 ui::PageTransition transition) { | |
40 // Triggered by some notifications on the current page, do nothing. | |
41 if (navigation_index == current_navigation_index_) { | |
Nicolas Zea
2017/04/10 17:09:00
This will also capture a reload right?
shenchao
2017/04/11 18:53:44
Yes
| |
42 DVLOG(1) << "Doing nothing for navigation_index: " << navigation_index | |
43 << " of transition: " << transition; | |
44 return; | |
45 } | |
46 | |
47 // Going back/forward to some previous navigation. | |
48 if (navigation_index < current_navigation_index_ || | |
49 (navigation_index > current_navigation_index_ && | |
50 transition & ui::PAGE_TRANSITION_FORWARD_BACK && | |
51 base::checked_cast<size_t>(navigation_index) < task_ids_.size())) { | |
52 DVLOG(1) << "Just updating task position with navigation_index: " | |
53 << navigation_index << " of transition: " << transition; | |
54 current_navigation_index_ = navigation_index; | |
55 return; | |
56 } | |
57 | |
58 // A new task for the new navigation. | |
59 int root_navigation_index = navigation_index; | |
60 if (current_navigation_index_ != -1 && | |
61 ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)) { | |
Nicolas Zea
2017/04/10 17:09:00
What about other non-link transitions that should
shenchao
2017/04/11 18:53:44
Done.
| |
62 // Creating a sub-task with navigation at current_navigation_index as | |
63 // parent. | |
64 DVLOG(1) << "Creating a sub-task with navigation_index: " | |
65 << navigation_index << " of transition: " << transition | |
66 << " under navigation_index: " << current_navigation_index_; | |
67 root_navigation_index = | |
68 task_ids_[current_navigation_index_].root_navigation_index; | |
69 } else { | |
70 // Creating a root task. | |
71 // For now, we don't consider tasks cross tabs, so first navigation of the | |
72 // tab always creates a root task. | |
73 DVLOG(1) << "Creating a root task with navigation_index: " | |
74 << navigation_index << " of transition: " << transition; | |
75 } | |
76 TaskIdAndRoot new_task = {root_navigation_index, | |
77 clock_->Now().ToInternalValue()}; | |
Nicolas Zea
2017/04/10 17:09:00
I still think we shouldn't be introducing a new ti
shenchao
2017/04/17 21:47:33
Done.
| |
78 | |
79 // In most cases navigation_index == task_ids_.size() if the previous | |
80 // navigation is end of chain, or navigation_index < task_ids_.size() | |
81 // otherwise. In few case navigation_index > task_ids_.size(), we fill | |
82 // task_ids_ with invalid contents. A known case is the first navigation after | |
83 // newtab. | |
84 for (int i = task_ids_.size(); i < navigation_index; i++) | |
85 task_ids_.push_back({-1, -1}); | |
86 | |
87 // Erase those for forward for the previous navigations. | |
88 task_ids_.erase(task_ids_.begin() + navigation_index, task_ids_.end()); | |
89 | |
90 // Add the current task at navigation_index. | |
91 task_ids_.push_back(new_task); | |
92 current_navigation_index_ = navigation_index; | |
93 return; | |
94 } | |
95 | |
96 TaskTracker::TaskTracker(std::unique_ptr<base::Clock> clock) | |
97 : clock_(std::move(clock)) {} | |
98 | |
99 TaskTracker::~TaskTracker() {} | |
100 | |
101 TabTasks* TaskTracker::GetTabTasks(SessionID::id_type tab_id) { | |
102 if (local_tab_tasks_map_.find(tab_id) == local_tab_tasks_map_.end()) { | |
103 local_tab_tasks_map_[tab_id] = base::MakeUnique<TabTasks>(clock_.get()); | |
104 } | |
105 return local_tab_tasks_map_[tab_id].get(); | |
106 } | |
107 | |
108 } // namespace sync_sessions | |
OLD | NEW |