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

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: Addressed sclittle comments Created 5 years, 1 month 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/single_thread_task_runner.h"
17 #include "base/threading/thread_checker.h" 18 #include "base/threading/thread_checker.h"
19 #include "chrome/browser/android/data_usage/data_use_tab_model.h"
18 #include "components/keyed_service/core/keyed_service.h" 20 #include "components/keyed_service/core/keyed_service.h"
19 #include "ui/base/page_transition_types.h" 21 #include "ui/base/page_transition_types.h"
20 22
21 class GURL; 23 class GURL;
22 24
23 namespace chrome { 25 namespace chrome {
24 26
25 namespace android { 27 namespace android {
26 28
27 // DataUseUITabModel tracks data use tracking start and end transitions on the 29 // DataUseUITabModel tracks data use tracking start and end transitions on the
28 // browser's tabs. It serves as a bridge between the DataUseTabModel, which 30 // 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, 31 // 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 32 // 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 33 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation
32 // and tab closure events to the DataUseTabModel, and receives tab tracking 34 // and tab closure events to the DataUseTabModel, and receives tab tracking
33 // transitions (start/end) from the DataUseTabModel, which it conveys to UI 35 // transitions (start/end) from the DataUseTabModel, which it conveys to UI
34 // notification logic. DataUseUITabModel is not thread-safe, and should be 36 // notification logic. DataUseUITabModel is not thread-safe, and should be
35 // accessed only on the UI thread. 37 // accessed only on the UI thread.
36 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab 38 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab
37 // is removed from the list of tabs. 39 // is removed from the list of tabs.
38 class DataUseUITabModel : public KeyedService { 40 class DataUseUITabModel : public KeyedService {
39 public: 41 public:
40 explicit DataUseUITabModel( 42 DataUseUITabModel(scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
41 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); 43 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
42 ~DataUseUITabModel() override; 44 ~DataUseUITabModel() override;
43 45
44 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes 46 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes
45 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that 47 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that
46 // are restored when Chromium restarts are not reported. 48 // are restored when Chromium restarts are not reported.
47 void ReportBrowserNavigation(const GURL& gurl, 49 void ReportBrowserNavigation(const GURL& gurl,
48 ui::PageTransition page_transition, 50 ui::PageTransition page_transition,
49 int32_t tab_id) const; 51 int32_t tab_id) const;
50 52
51 // Reports a tab closure for the tab with |tab_id| to the DataUseTabModel on 53 // 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 54 // IO thread. The tab could either have been closed or evicted from the memory
53 // by Android. 55 // by Android.
54 void ReportTabClosure(int32_t tab_id); 56 void ReportTabClosure(int32_t tab_id);
55 57
56 // Reports a custom tab navigation to the DataUseTabModel on the IO thread. 58 // Reports a custom tab navigation to the DataUseTabModel on the IO thread.
57 // Includes the |tab_id|, |url|, and |package_name| for the navigation. 59 // Includes the |tab_id|, |url|, and |package_name| for the navigation.
58 void ReportCustomTabInitialNavigation(int32_t tab_id, 60 void ReportCustomTabInitialNavigation(int32_t tab_id,
59 const std::string& url, 61 const std::string& url,
60 const std::string& package_name); 62 const std::string& package_name);
61 63
62 // Returns true if data use tracking has been started for the tab with id 64 // 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. 65 // |tab_id|. Calling this function resets the state of the tab.
64 bool HasDataUseTrackingStarted(int32_t tab_id); 66 bool HasDataUseTrackingStarted(int32_t tab_id);
65 67
66 // Returns true if data use tracking has ended for the tab with id |tab_id|. 68 // 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. 69 // Calling this function resets the state of the tab.
68 bool HasDataUseTrackingEnded(int32_t tab_id); 70 bool HasDataUseTrackingEnded(int32_t tab_id);
69 71
72 // Sets the weak pointer to DataUseTabModel that can be used on IO thread.
73 void SetIODataUseTabModel(base::WeakPtr<DataUseTabModel> data_use_tab_model);
sclittle 2015/11/17 22:50:11 This should be private.
tbansal1 2015/11/18 01:32:23 Done.
tbansal1 2015/11/18 01:40:01 This is called by profile_impl*, so can't be priva
74
75 // Notifies |this| of starting and ending of data tracking for tab with id
76 // |tab_id|.
77 void NotifyTrackingStarting(int32_t tab_id);
78 void NotifyTrackingEnding(int32_t tab_id);
79
80 // TabObserverOnIOThread registers as an observer to
81 // DataUseTabModel::TabDataUseObserver, and gets notified when tracking has
82 // started and ended on a tab. It passes these notifications to
83 // DataUseUITabModel on UI thread. TabObserverOnIOThread is not thread safe,
84 // and should only be accessed only on IO thread.
85 class TabObserverOnIOThread
mmenke 2015/11/17 22:48:40 This doesn't need to be public, does it? In fact,
sclittle 2015/11/17 22:50:12 Does this inner class definition need to be in the
tbansal1 2015/11/18 01:32:23 Done.
tbansal1 2015/11/18 01:32:23 Done.
86 : public base::RefCountedThreadSafe<TabObserverOnIOThread>,
87 public DataUseTabModel::TabDataUseObserver {
88 public:
89 TabObserverOnIOThread(
90 base::WeakPtr<DataUseUITabModel> data_use_ui_tab_model,
91 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
92
93 // Sets the |data_use_tab_model_|, and registers as an observer to
94 // |data_use_tab_model_|.
95 void SetIODataUseTabModel(
96 base::WeakPtr<DataUseTabModel> data_use_tab_model);
97
98 private:
99 friend class DataUseUITabModelTest;
100
101 // Ref counted classes have private destructors to avoid any code deleting
102 // the object accidentally while there are still references to it.
103 friend class base::RefCountedThreadSafe<TabObserverOnIOThread>;
104
105 ~TabObserverOnIOThread();
106
107 // DataUseTabModel::Observer implementation:
108 void NotifyTrackingStarting(int32_t tab_id) override;
109 void NotifyTrackingEnding(int32_t tab_id) override;
110
111 // |data_use_ui_tab_model_| is notified of starting and ending of tracking.
112 // |data_use_ui_tab_model_| should only be used on UI thread.
113 base::WeakPtr<DataUseUITabModel> data_use_ui_tab_model_;
114
115 // |this| registers as an observer to |data_use_tab_model_|.
116 base::WeakPtr<DataUseTabModel> data_use_tab_model_;
117
118 // Used to call methods on |data_use_ui_tab_model_|.
119 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
120
121 // True if |this| is registered as an observer to |data_use_tab_model_|.
122 bool registered_as_observer_;
123
124 base::ThreadChecker thread_checker_;
125
126 DISALLOW_COPY_AND_ASSIGN(TabObserverOnIOThread);
127 };
128
129 void SetTabDataUseObserver(
130 scoped_refptr<TabObserverOnIOThread> tab_data_use_observer);
131
70 private: 132 private:
133 // Friends for testing.
134 friend class DataUseUITabModelTest;
71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); 135 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState);
136 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType);
137 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest);
72 138
73 // DataUseTrackingEvent indicates the state of a tab. 139 // DataUseTrackingEvent indicates the state of a tab.
74 enum DataUseTrackingEvent { 140 enum DataUseTrackingEvent {
75 // Indicates that data use tracking has started. 141 // Indicates that data use tracking has started.
76 DATA_USE_TRACKING_STARTED, 142 DATA_USE_TRACKING_STARTED,
77 143
78 // Indicates that data use tracking has ended. 144 // Indicates that data use tracking has ended.
79 DATA_USE_TRACKING_ENDED, 145 DATA_USE_TRACKING_ENDED,
80 }; 146 };
81 147
82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; 148 typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents;
83
84 // DataUseTabModel::Observer implementation:
85 // TODO(tbansal): Add override once DataUseTabModel is checked in.
86 void OnTrackingStarted(int32_t tab_id);
87 void OnTrackingEnded(int32_t tab_id);
88 149
89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no 150 // 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 151 // existing entry for |tab_id|, and returns true. Otherwise, returns false
91 // without modifying the entry. 152 // without modifying the entry.
92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); 153 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event);
93 154
94 // Removes event entry for |tab_id|, if the entry is equal to |event|, and 155 // Removes event entry for |tab_id|, if the entry is equal to |event|, and
95 // returns true. Otherwise, returns false without modifying the entry. 156 // returns true. Otherwise, returns false without modifying the entry.
96 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); 157 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event);
97 158
159 // Converts |page_transition| to DataUseTabModel::TransitionType enum.
160 // Returns true if conversion was successful, and updates |transition_type|.
161 // Otherwise, returns false, and |transition_type| is not changed.
162 // |transition_type| must not be null.
163 bool ConvertTransitionType(
164 ui::PageTransition page_transition,
165 DataUseTabModel::TransitionType* transition_type) const;
166
167 // |tab_data_use_observer_| notifies |this| of starting and ending of data
168 // tracking.
169 scoped_refptr<TabObserverOnIOThread> tab_data_use_observer_;
170
98 // |tab_events_| stores tracking events of multiple tabs. 171 // |tab_events_| stores tracking events of multiple tabs.
99 TabEvents tab_events_; 172 TabEvents tab_events_;
100 173
101 // |io_task_runner_| accesses DataUseTabModel members on IO thread. 174 // |io_data_use_tab_model_| is notified by |this| about browser navigations
175 // and tab closures on IO thread. |io_data_use_tab_model_| should only be
176 // used on IO thread.
177 base::WeakPtr<DataUseTabModel> io_data_use_tab_model_;
178
179 // Used to call methods on |tab_data_use_observer_|.
102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 180 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
103 181
104 base::ThreadChecker thread_checker_; 182 base::ThreadChecker thread_checker_;
105 183
184 base::WeakPtrFactory<DataUseUITabModel> weak_factory_;
185
106 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); 186 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel);
107 }; 187 };
108 188
109 } // namespace android 189 } // namespace android
110 190
111 } // namespace chrome 191 } // namespace chrome
112 192
113 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ 193 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698