OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <string> |
9 | 9 |
10 #include <string> | 10 #include "base/containers/hash_tables.h" |
11 #include <unordered_map> | |
12 | |
13 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
14 #include "base/macros.h" | 12 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
17 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "chrome/browser/android/data_usage/data_use_tab_model.h" |
18 #include "components/keyed_service/core/keyed_service.h" | 17 #include "components/keyed_service/core/keyed_service.h" |
| 18 #include "components/sessions/core/session_id.h" |
19 #include "ui/base/page_transition_types.h" | 19 #include "ui/base/page_transition_types.h" |
20 | 20 |
21 class GURL; | 21 class GURL; |
22 | 22 |
| 23 namespace base { |
| 24 class SingleThreadTaskRunner; |
| 25 } |
| 26 |
23 namespace chrome { | 27 namespace chrome { |
24 | 28 |
25 namespace android { | 29 namespace android { |
26 | 30 |
27 // DataUseUITabModel tracks data use tracking start and end transitions on the | 31 // DataUseUITabModel tracks data use tracking start and end transitions on the |
28 // browser's tabs. It serves as a bridge between the DataUseTabModel, which | 32 // browser's tabs. It serves as a bridge between the DataUseTabModel, which |
29 // lives on the IO thread, browser navigation events and tab closure events, | 33 // lives on the IO thread, browser navigation events and tab closure events, |
30 // which are generated on the UI thread, and UI elements that appear when data | 34 // which are generated on the UI thread, and UI elements that appear when data |
31 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation | 35 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation |
32 // and tab closure events to the DataUseTabModel, and receives tab tracking | 36 // and tab closure events to the DataUseTabModel, and receives tab tracking |
33 // transitions (start/end) from the DataUseTabModel, which it conveys to UI | 37 // transitions (start/end) from the DataUseTabModel, which it conveys to UI |
34 // notification logic. DataUseUITabModel is not thread-safe, and should be | 38 // notification logic. DataUseUITabModel is not thread-safe, and should be |
35 // accessed only on the UI thread. | 39 // accessed only on the UI thread. DataUseUITabModel is registered as a |
| 40 // DataUseTabModel::TabDataUseObserver, and gets notified when tracking has |
| 41 // started and ended on a tab. DataUseUITabModel is not thread safe, and should |
| 42 // only be accessed only on UI thread. |
36 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab | 43 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab |
37 // is removed from the list of tabs. | 44 // is removed from the list of tabs. |
38 class DataUseUITabModel : public KeyedService { | 45 class DataUseUITabModel : public KeyedService, |
| 46 public DataUseTabModel::TabDataUseObserver { |
39 public: | 47 public: |
40 explicit DataUseUITabModel( | 48 explicit DataUseUITabModel( |
41 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); | 49 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); |
42 ~DataUseUITabModel() override; | 50 ~DataUseUITabModel() override; |
43 | 51 |
44 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes | 52 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes |
45 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that | 53 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that |
46 // are restored when Chromium restarts are not reported. | 54 // are restored when Chromium restarts are not reported. |
47 void ReportBrowserNavigation(const GURL& gurl, | 55 void ReportBrowserNavigation(const GURL& gurl, |
48 ui::PageTransition page_transition, | 56 ui::PageTransition page_transition, |
49 int32_t tab_id) const; | 57 SessionID::id_type tab_id) const; |
50 | 58 |
51 // Reports a tab closure for the tab with |tab_id| to the DataUseTabModel on | 59 // Reports a tab closure for the tab with |tab_id| to the DataUseTabModel on |
52 // IO thread. The tab could either have been closed or evicted from the memory | 60 // IO thread. The tab could either have been closed or evicted from the memory |
53 // by Android. | 61 // by Android. |
54 void ReportTabClosure(int32_t tab_id); | 62 void ReportTabClosure(SessionID::id_type tab_id); |
55 | 63 |
56 // Reports a custom tab navigation to the DataUseTabModel on the IO thread. | 64 // Reports a custom tab navigation to the DataUseTabModel on the IO thread. |
57 // Includes the |tab_id|, |url|, and |package_name| for the navigation. | 65 // Includes the |tab_id|, |url|, and |package_name| for the navigation. |
58 void ReportCustomTabInitialNavigation(int32_t tab_id, | 66 void ReportCustomTabInitialNavigation(SessionID::id_type tab_id, |
59 const std::string& url, | 67 const std::string& url, |
60 const std::string& package_name); | 68 const std::string& package_name); |
61 | 69 |
62 // Returns true if data use tracking has been started for the tab with id | 70 // Returns true if data use tracking has been started for the tab with id |
63 // |tab_id|. Calling this function resets the state of the tab. | 71 // |tab_id|. Calling this function resets the state of the tab. |
64 bool HasDataUseTrackingStarted(int32_t tab_id); | 72 bool HasDataUseTrackingStarted(SessionID::id_type tab_id); |
65 | 73 |
66 // Returns true if data use tracking has ended for the tab with id |tab_id|. | 74 // Returns true if data use tracking has ended for the tab with id |tab_id|. |
67 // Calling this function resets the state of the tab. | 75 // Calling this function resets the state of the tab. |
68 bool HasDataUseTrackingEnded(int32_t tab_id); | 76 bool HasDataUseTrackingEnded(SessionID::id_type tab_id); |
| 77 |
| 78 // Sets the weak pointer to DataUseTabModel. |
| 79 void SetDataUseTabModel(base::WeakPtr<DataUseTabModel> data_use_tab_model); |
| 80 |
| 81 base::WeakPtr<DataUseUITabModel> GetWeakPtr(); |
69 | 82 |
70 private: | 83 private: |
| 84 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType); |
71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); | 85 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); |
| 86 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest); |
72 | 87 |
73 // DataUseTrackingEvent indicates the state of a tab. | 88 // DataUseTrackingEvent indicates the state of a tab. |
74 enum DataUseTrackingEvent { | 89 enum DataUseTrackingEvent { |
75 // Indicates that data use tracking has started. | 90 // Indicates that data use tracking has started. |
76 DATA_USE_TRACKING_STARTED, | 91 DATA_USE_TRACKING_STARTED, |
77 | 92 |
78 // Indicates that data use tracking has ended. | 93 // Indicates that data use tracking has ended. |
79 DATA_USE_TRACKING_ENDED, | 94 DATA_USE_TRACKING_ENDED, |
80 }; | 95 }; |
81 | 96 |
82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; | 97 typedef base::hash_map<SessionID::id_type, DataUseTrackingEvent> TabEvents; |
83 | 98 |
84 // DataUseTabModel::Observer implementation: | 99 // DataUseTabModel::TabDataUseObserver implementation: |
85 // TODO(tbansal): Add override once DataUseTabModel is checked in. | 100 void NotifyTrackingStarting(SessionID::id_type tab_id) override; |
86 void OnTrackingStarted(int32_t tab_id); | 101 void NotifyTrackingEnding(SessionID::id_type tab_id) override; |
87 void OnTrackingEnded(int32_t tab_id); | |
88 | 102 |
89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no | 103 // Creates |event| for tab with id |tab_id| and value |event|, if there is no |
90 // existing entry for |tab_id|, and returns true. Otherwise, returns false | 104 // existing entry for |tab_id|, and returns true. Otherwise, returns false |
91 // without modifying the entry. | 105 // without modifying the entry. |
92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 106 bool MaybeCreateTabEvent(SessionID::id_type tab_id, |
| 107 DataUseTrackingEvent event); |
93 | 108 |
94 // Removes event entry for |tab_id|, if the entry is equal to |event|, and | 109 // Removes event entry for |tab_id|, if the entry is equal to |event|, and |
95 // returns true. Otherwise, returns false without modifying the entry. | 110 // returns true. Otherwise, returns false without modifying the entry. |
96 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 111 bool RemoveTabEvent(SessionID::id_type tab_id, DataUseTrackingEvent event); |
| 112 |
| 113 // Converts |page_transition| to DataUseTabModel::TransitionType enum. |
| 114 // Returns true if conversion was successful, and updates |transition_type|. |
| 115 // Otherwise, returns false, and |transition_type| is not changed. |
| 116 // |transition_type| must not be null. |
| 117 bool ConvertTransitionType( |
| 118 ui::PageTransition page_transition, |
| 119 DataUseTabModel::TransitionType* transition_type) const; |
97 | 120 |
98 // |tab_events_| stores tracking events of multiple tabs. | 121 // |tab_events_| stores tracking events of multiple tabs. |
99 TabEvents tab_events_; | 122 TabEvents tab_events_; |
100 | 123 |
101 // |io_task_runner_| accesses DataUseTabModel members on IO thread. | 124 // |data_use_tab_model_| is notified by |this| about browser navigations |
| 125 // and tab closures on IO thread. |data_use_tab_model_| should only be |
| 126 // used on IO thread. |
| 127 base::WeakPtr<DataUseTabModel> data_use_tab_model_; |
| 128 |
102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 129 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
103 | 130 |
104 base::ThreadChecker thread_checker_; | 131 base::ThreadChecker thread_checker_; |
105 | 132 |
| 133 base::WeakPtrFactory<DataUseUITabModel> weak_factory_; |
| 134 |
106 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); | 135 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); |
107 }; | 136 }; |
108 | 137 |
109 } // namespace android | 138 } // namespace android |
110 | 139 |
111 } // namespace chrome | 140 } // namespace chrome |
112 | 141 |
113 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 142 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
OLD | NEW |