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

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, addressed 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
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 // Should be called at most once.
74 void SetIODataUseTabModel(base::WeakPtr<DataUseTabModel> data_use_tab_model);
sclittle 2015/11/18 21:42:04 nit: pass in a const base::WeakPtr<DataUseTabModel
tbansal1 2015/11/19 00:47:07 Done. Made the CreateTabObserverOnIOThread a stati
75
76 // Notifies |this| of starting and ending of data tracking for tab with id
77 // |tab_id|.
78 void NotifyTrackingStarting(int32_t tab_id);
79 void NotifyTrackingEnding(int32_t tab_id);
80
81 class TabObserverOnIOThread;
sclittle 2015/11/18 21:42:04 nit: Can this be moved to private?
tbansal1 2015/11/19 00:47:07 No, CreateTabObserverOnIOThread in .cc file needs
82
70 private: 83 private:
84 // Friends for testing.
85 friend class DataUseUITabModelTest;
71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); 86 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState);
87 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType);
88 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest);
72 89
73 // DataUseTrackingEvent indicates the state of a tab. 90 // DataUseTrackingEvent indicates the state of a tab.
74 enum DataUseTrackingEvent { 91 enum DataUseTrackingEvent {
75 // Indicates that data use tracking has started. 92 // Indicates that data use tracking has started.
76 DATA_USE_TRACKING_STARTED, 93 DATA_USE_TRACKING_STARTED,
77 94
78 // Indicates that data use tracking has ended. 95 // Indicates that data use tracking has ended.
79 DATA_USE_TRACKING_ENDED, 96 DATA_USE_TRACKING_ENDED,
80 }; 97 };
81 98
82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; 99 typedef base::hash_map<int32_t, DataUseTrackingEvent> TabEvents;
83 100
84 // DataUseTabModel::Observer implementation: 101 void SetTabDataUseObserver(
85 // TODO(tbansal): Add override once DataUseTabModel is checked in. 102 scoped_refptr<TabObserverOnIOThread> tab_data_use_observer);
86 void OnTrackingStarted(int32_t tab_id);
87 void OnTrackingEnded(int32_t tab_id);
88 103
89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no 104 // 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 105 // existing entry for |tab_id|, and returns true. Otherwise, returns false
91 // without modifying the entry. 106 // without modifying the entry.
92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); 107 bool MaybeCreateTabEvent(int32_t tab_id, 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(int32_t tab_id, DataUseTrackingEvent event);
97 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;
120
121 // |tab_data_use_observer_| notifies |this| of starting and ending of data
122 // tracking.
sclittle 2015/11/18 21:42:04 nit: Could you also mention here that it should on
tbansal1 2015/11/19 00:47:07 Done.
123 scoped_refptr<TabObserverOnIOThread> tab_data_use_observer_;
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 // |io_data_use_tab_model_| is notified by |this| about browser navigations
129 // and tab closures on IO thread. |io_data_use_tab_model_| should only be
130 // used on IO thread.
131 base::WeakPtr<DataUseTabModel> io_data_use_tab_model_;
132
102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 133 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
134 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
103 135
104 base::ThreadChecker thread_checker_; 136 base::ThreadChecker thread_checker_;
105 137
138 base::WeakPtrFactory<DataUseUITabModel> weak_factory_;
139
106 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); 140 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel);
107 }; 141 };
108 142
109 } // namespace android 143 } // namespace android
110 144
111 } // namespace chrome 145 } // namespace chrome
112 146
113 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ 147 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698