| 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 } // namespace base |
| 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 registers as an observer to |
| 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. Also, registers |this| as an |
| 79 // observer to the DataUseTabModel::TabDataUseObserver. |
| 80 void SetDataUseTabModel(base::WeakPtr<DataUseTabModel> data_use_tab_model); |
| 69 | 81 |
| 70 private: | 82 private: |
| 83 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType); |
| 71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); | 84 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); |
| 85 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest); |
| 72 | 86 |
| 73 // DataUseTrackingEvent indicates the state of a tab. | 87 // DataUseTrackingEvent indicates the state of a tab. |
| 74 enum DataUseTrackingEvent { | 88 enum DataUseTrackingEvent { |
| 75 // Indicates that data use tracking has started. | 89 // Indicates that data use tracking has started. |
| 76 DATA_USE_TRACKING_STARTED, | 90 DATA_USE_TRACKING_STARTED, |
| 77 | 91 |
| 78 // Indicates that data use tracking has ended. | 92 // Indicates that data use tracking has ended. |
| 79 DATA_USE_TRACKING_ENDED, | 93 DATA_USE_TRACKING_ENDED, |
| 80 }; | 94 }; |
| 81 | 95 |
| 82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; | 96 typedef base::hash_map<SessionID::id_type, DataUseTrackingEvent> TabEvents; |
| 83 | 97 |
| 84 // DataUseTabModel::Observer implementation: | 98 // DataUseTabModel::TabDataUseObserver implementation: |
| 85 // TODO(tbansal): Add override once DataUseTabModel is checked in. | 99 void NotifyTrackingStarting(SessionID::id_type tab_id) override; |
| 86 void OnTrackingStarted(int32_t tab_id); | 100 void NotifyTrackingEnding(SessionID::id_type tab_id) override; |
| 87 void OnTrackingEnded(int32_t tab_id); | |
| 88 | 101 |
| 89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no | 102 // 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 | 103 // existing entry for |tab_id|, and returns true. Otherwise, returns false |
| 91 // without modifying the entry. | 104 // without modifying the entry. |
| 92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 105 bool MaybeCreateTabEvent(SessionID::id_type tab_id, |
| 106 DataUseTrackingEvent event); |
| 93 | 107 |
| 94 // Removes event entry for |tab_id|, if the entry is equal to |event|, and | 108 // Removes event entry for |tab_id|, if the entry is equal to |event|, and |
| 95 // returns true. Otherwise, returns false without modifying the entry. | 109 // returns true. Otherwise, returns false without modifying the entry. |
| 96 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); | 110 bool RemoveTabEvent(SessionID::id_type tab_id, DataUseTrackingEvent event); |
| 111 |
| 112 // Converts |page_transition| to DataUseTabModel::TransitionType enum. |
| 113 // Returns true if conversion was successful, and updates |transition_type|. |
| 114 // Otherwise, returns false, and |transition_type| is not changed. |
| 115 // |transition_type| must not be null. |
| 116 bool ConvertTransitionType( |
| 117 ui::PageTransition page_transition, |
| 118 DataUseTabModel::TransitionType* transition_type) const; |
| 97 | 119 |
| 98 // |tab_events_| stores tracking events of multiple tabs. | 120 // |tab_events_| stores tracking events of multiple tabs. |
| 99 TabEvents tab_events_; | 121 TabEvents tab_events_; |
| 100 | 122 |
| 101 // |io_task_runner_| accesses DataUseTabModel members on IO thread. | 123 // |data_use_tab_model_| is notified by |this| about browser navigations |
| 124 // and tab closures on IO thread. |data_use_tab_model_| should only be |
| 125 // used on IO thread. |
| 126 base::WeakPtr<DataUseTabModel> data_use_tab_model_; |
| 127 |
| 102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 128 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 103 | 129 |
| 104 base::ThreadChecker thread_checker_; | 130 base::ThreadChecker thread_checker_; |
| 105 | 131 |
| 132 base::WeakPtrFactory<DataUseUITabModel> weak_factory_; |
| 133 |
| 106 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); | 134 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); |
| 107 }; | 135 }; |
| 108 | 136 |
| 109 } // namespace android | 137 } // namespace android |
| 110 | 138 |
| 111 } // namespace chrome | 139 } // namespace chrome |
| 112 | 140 |
| 113 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ | 141 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ |
| OLD | NEW |