Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 1 | // 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 Liang | 2efb9d13 | 2019-01-16 05:39:28 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_COMPLEX_TASKS_TASK_TAB_HELPER_H_ |
| 6 | #define CHROME_BROWSER_COMPLEX_TASKS_TASK_TAB_HELPER_H_ |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 7 | |
| 8 | #include <map> |
Lei Zhang | dd52b5f | 2021-04-27 23:08:41 | [diff] [blame] | 9 | #include <unordered_map> |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 10 | |
Jan Wilken Dörrie | b5a41c3 | 2020-12-09 18:55:47 | [diff] [blame] | 11 | #include "base/containers/contains.h" |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 12 | #include "base/macros.h" |
David Maunder | 445c72b | 2019-05-02 16:14:20 | [diff] [blame] | 13 | #include "build/build_config.h" |
David Maunder | 9316046 | 2019-07-12 17:02:09 | [diff] [blame] | 14 | #include "components/sessions/content/navigation_task_id.h" |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 15 | #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 Maunder | 445c72b | 2019-05-02 16:14:20 | [diff] [blame] | 19 | namespace sessions { |
David Maunder | 9316046 | 2019-07-12 17:02:09 | [diff] [blame] | 20 | class NavigationTaskId; |
David Maunder | 445c72b | 2019-05-02 16:14:20 | [diff] [blame] | 21 | } |
| 22 | |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 23 | namespace tasks { |
| 24 | |
Mei Liang | 2efb9d13 | 2019-01-16 05:39:28 | [diff] [blame] | 25 | // This is a tab helper that collects navigation state information of a |
| 26 | // complex task. |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 27 | class 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 Maunder | 9316046 | 2019-07-12 17:02:09 | [diff] [blame] | 37 | static sessions::NavigationTaskId* GetCurrentTaskId( |
David Maunder | 445c72b | 2019-05-02 16:14:20 | [diff] [blame] | 38 | content::WebContents* web_contents); |
David Maunder | 9316046 | 2019-07-12 17:02:09 | [diff] [blame] | 39 | const sessions::NavigationTaskId* get_task_id_for_navigation( |
David Maunder | 9a2945a | 2019-05-08 00:04:35 | [diff] [blame] | 40 | int nav_id) const { |
David Maunder | 9316046 | 2019-07-12 17:02:09 | [diff] [blame] | 41 | if (!base::Contains(local_navigation_task_id_map_, nav_id)) |
David Maunder | 9a2945a | 2019-05-08 00:04:35 | [diff] [blame] | 42 | return nullptr; |
David Maunder | 9316046 | 2019-07-12 17:02:09 | [diff] [blame] | 43 | return &local_navigation_task_id_map_.find(nav_id)->second; |
David Maunder | 9a2945a | 2019-05-08 00:04:35 | [diff] [blame] | 44 | } |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 45 | |
| 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 Maunder | 9a2945a | 2019-05-08 00:04:35 | [diff] [blame] | 60 | void UpdateAndRecordTaskIds( |
| 61 | const content::LoadCommittedDetails& load_details); |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 62 | |
| 63 | void RecordHubAndSpokeNavigationUsage(int sample); |
| 64 | |
David Maunder | 445c72b | 2019-05-02 16:14:20 | [diff] [blame] | 65 | int64_t GetParentTaskId(); |
| 66 | int64_t GetParentRootTaskId(); |
David Maunder | 445c72b | 2019-05-02 16:14:20 | [diff] [blame] | 67 | |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 68 | int last_pruned_navigation_entry_index_; |
| 69 | std::map<int, int> entry_index_to_spoke_count_map_; |
David Maunder | 9316046 | 2019-07-12 17:02:09 | [diff] [blame] | 70 | std::unordered_map<int, sessions::NavigationTaskId> |
| 71 | local_navigation_task_id_map_; |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 72 | |
François Doray | 4f51d5d | 2018-12-03 22:26:24 | [diff] [blame] | 73 | WEB_CONTENTS_USER_DATA_KEY_DECL(); |
| 74 | |
Mei Liang | da91262 | 2018-07-19 20:17:12 | [diff] [blame] | 75 | DISALLOW_COPY_AND_ASSIGN(TaskTabHelper); |
| 76 | }; |
| 77 | |
| 78 | } // namespace tasks |
| 79 | |
Mei Liang | 2efb9d13 | 2019-01-16 05:39:28 | [diff] [blame] | 80 | #endif // CHROME_BROWSER_COMPLEX_TASKS_TASK_TAB_HELPER_H_ |