Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: chrome/browser/android/data_usage/data_use_ui_tab_model.h

Issue 1443683002: Notify DataUseTabModel of navigations and tab closures (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h>
9 9
10 #include <string> 10 #include <string>
11 #include <unordered_map>
12 11
12 #include "base/containers/hash_tables.h"
13 #include "base/gtest_prod_util.h" 13 #include "base/gtest_prod_util.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/thread_checker.h" 17 #include "base/threading/thread_checker.h"
18 #include "chrome/browser/android/data_usage/data_use_tab_model.h"
18 #include "components/keyed_service/core/keyed_service.h" 19 #include "components/keyed_service/core/keyed_service.h"
19 #include "ui/base/page_transition_types.h" 20 #include "ui/base/page_transition_types.h"
20 21
21 class GURL; 22 class GURL;
23 class IOThread;
24
25 namespace base {
26 class SingleThreadTaskRunner;
27 } // namespace base
22 28
23 namespace chrome { 29 namespace chrome {
24 30
25 namespace android { 31 namespace android {
26 32
27 // DataUseUITabModel tracks data use tracking start and end transitions on the 33 // DataUseUITabModel tracks data use tracking start and end transitions on the
28 // browser's tabs. It serves as a bridge between the DataUseTabModel, which 34 // 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, 35 // 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 36 // 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 37 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation
32 // and tab closure events to the DataUseTabModel, and receives tab tracking 38 // and tab closure events to the DataUseTabModel, and receives tab tracking
33 // transitions (start/end) from the DataUseTabModel, which it conveys to UI 39 // transitions (start/end) from the DataUseTabModel, which it conveys to UI
34 // notification logic. DataUseUITabModel is not thread-safe, and should be 40 // notification logic. DataUseUITabModel is not thread-safe, and should be
35 // accessed only on the UI thread. 41 // accessed only on the UI thread. DataUseUITabModel registers as an observer to
42 // DataUseTabModel::TabDataUseObserver, and gets notified when tracking has
43 // started and ended on a tab. DataUseUITabModel is not thread safe, and should
44 // only be accessed only on UI thread.
36 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab 45 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab
37 // is removed from the list of tabs. 46 // is removed from the list of tabs.
38 class DataUseUITabModel : public KeyedService { 47 class DataUseUITabModel : public KeyedService,
48 public DataUseTabModel::TabDataUseObserver {
39 public: 49 public:
40 explicit DataUseUITabModel( 50 explicit DataUseUITabModel(
41 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); 51 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
42 ~DataUseUITabModel() override; 52 ~DataUseUITabModel() override;
43 53
44 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes 54 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes
45 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that 55 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that
46 // are restored when Chromium restarts are not reported. 56 // are restored when Chromium restarts are not reported.
47 void ReportBrowserNavigation(const GURL& gurl, 57 void ReportBrowserNavigation(const GURL& gurl,
48 ui::PageTransition page_transition, 58 ui::PageTransition page_transition,
(...skipping 11 matching lines...) Expand all
60 const std::string& package_name); 70 const std::string& package_name);
61 71
62 // Returns true if data use tracking has been started for the tab with id 72 // 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. 73 // |tab_id|. Calling this function resets the state of the tab.
64 bool HasDataUseTrackingStarted(int32_t tab_id); 74 bool HasDataUseTrackingStarted(int32_t tab_id);
65 75
66 // Returns true if data use tracking has ended for the tab with id |tab_id|. 76 // 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. 77 // Calling this function resets the state of the tab.
68 bool HasDataUseTrackingEnded(int32_t tab_id); 78 bool HasDataUseTrackingEnded(int32_t tab_id);
69 79
80 // Sets the weak pointer to DataUseTabModel that can be used on IO thread.
81 // Should be called at most once.
82 void SetIOThread(IOThread* io_thread);
83
70 private: 84 private:
85 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType);
71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); 86 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState);
87 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest);
72 88
73 // DataUseTrackingEvent indicates the state of a tab. 89 // DataUseTrackingEvent indicates the state of a tab.
74 enum DataUseTrackingEvent { 90 enum DataUseTrackingEvent {
75 // Indicates that data use tracking has started. 91 // Indicates that data use tracking has started.
76 DATA_USE_TRACKING_STARTED, 92 DATA_USE_TRACKING_STARTED,
77 93
78 // Indicates that data use tracking has ended. 94 // Indicates that data use tracking has ended.
79 DATA_USE_TRACKING_ENDED, 95 DATA_USE_TRACKING_ENDED,
80 }; 96 };
81 97
82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; 98 typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents;
83 99
84 // DataUseTabModel::Observer implementation: 100 // Sets the weak pointer to DataUseTabModel. Also, registers |this| as an
85 // TODO(tbansal): Add override once DataUseTabModel is checked in. 101 // observer to the DataUseTabModel::TabDataUseObserver.
86 void OnTrackingStarted(int32_t tab_id); 102 void SetDataUseTabModel(base::WeakPtr<DataUseTabModel> data_use_tab_model);
87 void OnTrackingEnded(int32_t tab_id); 103
104 // DataUseTabModel::TabDataUseObserver implementation:
105 void NotifyTrackingStarting(int32_t tab_id) override;
106 void NotifyTrackingEnding(int32_t tab_id) override;
88 107
89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no 108 // 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 109 // existing entry for |tab_id|, and returns true. Otherwise, returns false
91 // without modifying the entry. 110 // without modifying the entry.
92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); 111 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event);
93 112
94 // Removes event entry for |tab_id|, if the entry is equal to |event|, and 113 // Removes event entry for |tab_id|, if the entry is equal to |event|, and
95 // returns true. Otherwise, returns false without modifying the entry. 114 // returns true. Otherwise, returns false without modifying the entry.
96 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); 115 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event);
97 116
117 // Converts |page_transition| to DataUseTabModel::TransitionType enum.
118 // Returns true if conversion was successful, and updates |transition_type|.
119 // Otherwise, returns false, and |transition_type| is not changed.
120 // |transition_type| must not be null.
121 bool ConvertTransitionType(
122 ui::PageTransition page_transition,
123 DataUseTabModel::TransitionType* transition_type) const;
124
98 // |tab_events_| stores tracking events of multiple tabs. 125 // |tab_events_| stores tracking events of multiple tabs.
99 TabEvents tab_events_; 126 TabEvents tab_events_;
100 127
101 // |io_task_runner_| accesses DataUseTabModel members on IO thread. 128 // |data_use_tab_model_| is notified by |this| about browser navigations
129 // and tab closures on IO thread. |data_use_tab_model_| should only be
130 // used on IO thread.
131 base::WeakPtr<DataUseTabModel> data_use_tab_model_;
132
102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 133 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
103 134
104 base::ThreadChecker thread_checker_; 135 base::ThreadChecker thread_checker_;
105 136
137 base::WeakPtrFactory<DataUseUITabModel> weak_factory_;
138
106 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); 139 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel);
107 }; 140 };
108 141
109 } // namespace android 142 } // namespace android
110 143
111 } // namespace chrome 144 } // namespace chrome
112 145
113 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ 146 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698