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..dcd536b46d57915032774eb545892cb9930fc3f1 |
--- /dev/null |
+++ b/components/sync_sessions/task_tracker.cc |
@@ -0,0 +1,108 @@ |
+// 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 { |
+ |
+TabTasks::TabTasks(base::Clock* clock) : clock_((clock)) {} |
+ |
+TabTasks::~TabTasks() {} |
+ |
+std::vector<int64_t> TabTasks::RootToSelfTaskIdsOfNavigationIndex( |
+ 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) { |
+ // Triggered by some notifications on the current page, do nothing. |
+ 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
|
+ 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)) { |
Nicolas Zea
2017/04/10 17:09:00
What about other non-link transitions that should
shenchao
2017/04/11 18:53:44
Done.
|
+ // 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, |
+ 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.
|
+ |
+ // 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. |
+ 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(std::unique_ptr<base::Clock> clock) |
+ : clock_(std::move(clock)) {} |
+ |
+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>(clock_.get()); |
+ } |
+ return local_tab_tasks_map_[tab_id].get(); |
+} |
+ |
+} // namespace sync_sessions |