blob: b0f4bf09ccdf9a1d3dbfa35afc85c53716534431 [file] [log] [blame]
Mei Liangda912622018-07-19 20:17:121// Copyright 2018 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
Mei Liang2efb9d132019-01-16 05:39:285#ifndef CHROME_BROWSER_COMPLEX_TASKS_TASK_TAB_HELPER_H_
6#define CHROME_BROWSER_COMPLEX_TASKS_TASK_TAB_HELPER_H_
Mei Liangda912622018-07-19 20:17:127
8#include <map>
Lei Zhangdd52b5f2021-04-27 23:08:419#include <unordered_map>
Mei Liangda912622018-07-19 20:17:1210
Jan Wilken Dörrieb5a41c32020-12-09 18:55:4711#include "base/containers/contains.h"
Mei Liangda912622018-07-19 20:17:1212#include "base/macros.h"
David Maunder445c72b2019-05-02 16:14:2013#include "build/build_config.h"
David Maunder93160462019-07-12 17:02:0914#include "components/sessions/content/navigation_task_id.h"
Mei Liangda912622018-07-19 20:17:1215#include "content/public/browser/navigation_details.h"
16#include "content/public/browser/web_contents_observer.h"
17#include "content/public/browser/web_contents_user_data.h"
18
David Maunder445c72b2019-05-02 16:14:2019namespace sessions {
David Maunder93160462019-07-12 17:02:0920class NavigationTaskId;
David Maunder445c72b2019-05-02 16:14:2021}
22
Mei Liangda912622018-07-19 20:17:1223namespace tasks {
24
Mei Liang2efb9d132019-01-16 05:39:2825// This is a tab helper that collects navigation state information of a
26// complex task.
Mei Liangda912622018-07-19 20:17:1227class TaskTabHelper : public content::WebContentsObserver,
28 public content::WebContentsUserData<TaskTabHelper> {
29 public:
30 ~TaskTabHelper() override;
31
32 // WebContentsObserver
33 void NavigationEntryCommitted(
34 const content::LoadCommittedDetails& load_details) override;
35 void NavigationListPruned(
36 const content::PrunedDetails& pruned_details) override;
David Maunder93160462019-07-12 17:02:0937 static sessions::NavigationTaskId* GetCurrentTaskId(
David Maunder445c72b2019-05-02 16:14:2038 content::WebContents* web_contents);
David Maunder93160462019-07-12 17:02:0939 const sessions::NavigationTaskId* get_task_id_for_navigation(
David Maunder9a2945a2019-05-08 00:04:3540 int nav_id) const {
David Maunder93160462019-07-12 17:02:0941 if (!base::Contains(local_navigation_task_id_map_, nav_id))
David Maunder9a2945a2019-05-08 00:04:3542 return nullptr;
David Maunder93160462019-07-12 17:02:0943 return &local_navigation_task_id_map_.find(nav_id)->second;
David Maunder9a2945a2019-05-08 00:04:3544 }
Mei Liangda912622018-07-19 20:17:1245
46 protected:
47 explicit TaskTabHelper(content::WebContents* web_contents);
48
49 enum class HubType { DEFAULT_SEARCH_ENGINE, FORM_SUBMIT, OTHER };
50
51 virtual HubType GetSpokeEntryHubType() const;
52
53 // For testing
54 int GetSpokesForTesting(int id) {
55 return entry_index_to_spoke_count_map_[id];
56 }
57
58 private:
59 friend class content::WebContentsUserData<TaskTabHelper>;
David Maunder9a2945a2019-05-08 00:04:3560 void UpdateAndRecordTaskIds(
61 const content::LoadCommittedDetails& load_details);
Mei Liangda912622018-07-19 20:17:1262
63 void RecordHubAndSpokeNavigationUsage(int sample);
64
David Maunder445c72b2019-05-02 16:14:2065 int64_t GetParentTaskId();
66 int64_t GetParentRootTaskId();
David Maunder445c72b2019-05-02 16:14:2067
Mei Liangda912622018-07-19 20:17:1268 int last_pruned_navigation_entry_index_;
69 std::map<int, int> entry_index_to_spoke_count_map_;
David Maunder93160462019-07-12 17:02:0970 std::unordered_map<int, sessions::NavigationTaskId>
71 local_navigation_task_id_map_;
Mei Liangda912622018-07-19 20:17:1272
François Doray4f51d5d2018-12-03 22:26:2473 WEB_CONTENTS_USER_DATA_KEY_DECL();
74
Mei Liangda912622018-07-19 20:17:1275 DISALLOW_COPY_AND_ASSIGN(TaskTabHelper);
76};
77
78} // namespace tasks
79
Mei Liang2efb9d132019-01-16 05:39:2880#endif // CHROME_BROWSER_COMPLEX_TASKS_TASK_TAB_HELPER_H_