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

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 comments, Using two weak ptrs now. 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 <unordered_map> 10 #include <unordered_map>
11 11
12 #include "base/gtest_prod_util.h" 12 #include "base/gtest_prod_util.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
17 #include "chrome/browser/android/data_usage/data_use_tab_model.h"
17 #include "components/keyed_service/core/keyed_service.h" 18 #include "components/keyed_service/core/keyed_service.h"
18 #include "ui/base/page_transition_types.h" 19 #include "ui/base/page_transition_types.h"
19 20
20 class GURL; 21 class GURL;
21 22
22 namespace chrome { 23 namespace chrome {
23 24
24 namespace android { 25 namespace android {
25 26
26 // DataUseUITabModel tracks data use tracking start and end transitions on the 27 // DataUseUITabModel tracks data use tracking start and end transitions on the
27 // browser's tabs. It serves as a bridge between the DataUseTabModel, which 28 // browser's tabs. It serves as a bridge between the DataUseTabModel, which
28 // lives on the IO thread, browser navigation events and tab closure events, 29 // lives on the IO thread, browser navigation events and tab closure events,
29 // which are generated on the UI thread, and UI elements that appear when data 30 // which are generated on the UI thread, and UI elements that appear when data
30 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation 31 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation
31 // and tab closure events to the DataUseTabModel, and receives tab tracking 32 // and tab closure events to the DataUseTabModel, and receives tab tracking
32 // transitions (start/end) from the DataUseTabModel, which it conveys to UI 33 // transitions (start/end) from the DataUseTabModel, which it conveys to UI
33 // notification logic. DataUseUITabModel is not thread-safe, and should be 34 // notification logic. DataUseUITabModel is not thread-safe, and should be
34 // accessed only on the UI thread. 35 // accessed only on the UI thread.
35 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab 36 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab
36 // is removed from the list of tabs. 37 // is removed from the list of tabs.
37 class DataUseUITabModel : public KeyedService { 38 class DataUseUITabModel : public KeyedService,
39 public DataUseTabModel::TabDataUseObserver {
38 public: 40 public:
39 explicit DataUseUITabModel( 41 explicit DataUseUITabModel(
40 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); 42 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
41 ~DataUseUITabModel() override; 43 ~DataUseUITabModel() override;
42 44
43 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes 45 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes
44 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that 46 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that
45 // are restored when Chromium restarts are not reported. 47 // are restored when Chromium restarts are not reported.
46 void ReportBrowserNavigation(const GURL& gurl, 48 void ReportBrowserNavigation(const GURL& gurl,
47 ui::PageTransition page_transition, 49 ui::PageTransition page_transition,
48 int32_t tab_id) const; 50 int32_t tab_id) const;
49 51
50 // Reports a tab closure for the tab with |tab_id| to the DataUseTabModel on 52 // Reports a tab closure for the tab with |tab_id| to the DataUseTabModel on
51 // IO thread. The tab could either have been closed or evicted from the memory 53 // IO thread. The tab could either have been closed or evicted from the memory
52 // by Android. 54 // by Android.
53 void ReportTabClosure(int32_t tab_id); 55 void ReportTabClosure(int32_t tab_id);
54 56
55 // Returns true if data use tracking has been started for the tab with id 57 // Returns true if data use tracking has been started for the tab with id
56 // |tab_id|. Calling this function resets the state of the tab. 58 // |tab_id|. Calling this function resets the state of the tab.
57 bool HasDataUseTrackingStarted(int32_t tab_id); 59 bool HasDataUseTrackingStarted(int32_t tab_id);
58 60
59 // Returns true if data use tracking has ended for the tab with id |tab_id|. 61 // Returns true if data use tracking has ended for the tab with id |tab_id|.
60 // Calling this function resets the state of the tab. 62 // Calling this function resets the state of the tab.
61 bool HasDataUseTrackingEnded(int32_t tab_id); 63 bool HasDataUseTrackingEnded(int32_t tab_id);
62 64
65 // Sets the weak pointer to DataUseTabModel that can be used on IO thread.
66 void SetIODataUseTabModel(
67 base::WeakPtr<DataUseTabModel> io_data_use_tab_model);
68
69 // Sets the weak pointer to DataUseTabModel that can be used on UI thread,
70 // and registers as an observer to DataUseTabModel::TabDataUseObserver.
71 void SetUIDataUseTabModel(
72 base::WeakPtr<DataUseTabModel> ui_data_use_tab_model);
sclittle 2015/11/16 23:13:42 I don't understand why the DataUseTabModel needs t
tbansal1 2015/11/17 21:12:33 Done.
73
63 private: 74 private:
64 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); 75 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState);
76 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType);
77 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest);
65 78
66 // DataUseTrackingEvent indicates the state of a tab. 79 // DataUseTrackingEvent indicates the state of a tab.
67 enum DataUseTrackingEvent { 80 enum DataUseTrackingEvent {
68 // Indicates that data use tracking has started. 81 // Indicates that data use tracking has started.
69 DATA_USE_TRACKING_STARTED, 82 DATA_USE_TRACKING_STARTED,
70 83
71 // Indicates that data use tracking has ended. 84 // Indicates that data use tracking has ended.
72 DATA_USE_TRACKING_ENDED, 85 DATA_USE_TRACKING_ENDED,
73 }; 86 };
74 87
75 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; 88 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents;
76 89
77 // DataUseTabModel::Observer implementation: 90 // DataUseTabModel::Observer implementation:
78 // TODO(tbansal): Add override once DataUseTabModel is checked in. 91 void NotifyTrackingStarting(int32_t tab_id) override;
79 void OnTrackingStarted(int32_t tab_id); 92 void NotifyTrackingEnding(int32_t tab_id) override;
80 void OnTrackingEnded(int32_t tab_id);
81 93
82 // Creates |event| for tab with id |tab_id| and value |event|, if there is no 94 // Creates |event| for tab with id |tab_id| and value |event|, if there is no
83 // existing entry for |tab_id|, and returns true. Otherwise, returns false 95 // existing entry for |tab_id|, and returns true. Otherwise, returns false
84 // without modifying the entry. 96 // without modifying the entry.
85 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); 97 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event);
86 98
87 // Removes event entry for |tab_id|, if the entry is equal to |event|, and 99 // Removes event entry for |tab_id|, if the entry is equal to |event|, and
88 // returns true. Otherwise, returns false without modifying the entry. 100 // returns true. Otherwise, returns false without modifying the entry.
89 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event); 101 bool RemoveTabEvent(int32_t tab_id, DataUseTrackingEvent event);
90 102
103 // Converts |page_transition| to DataUseTabModel::TransitionType enum.
104 // Returns true if conversion was successful, and updates |transition_type|.
105 // Otherwise, returns false, and |transition_type| is not changed.
106 // |transition_type| must not be null.
Raj 2015/11/16 22:00:09 nits: rename transition_type to output_transition
tbansal1 2015/11/17 21:12:33 I think |transition_type| is okay, since it is a
107 bool ConvertTransitionType(
108 ui::PageTransition page_transition,
109 DataUseTabModel::TransitionType* transition_type) const;
110
111 // |io_data_use_tab_model_| is used to call DataUseTabModel methods.
112 // |ui_data_use_tab_model_| is used to register as an observer to
113 // DataUseTabModel::TabDataUseObserver.
114 base::WeakPtr<DataUseTabModel> io_data_use_tab_model_;
115 base::WeakPtr<DataUseTabModel> ui_data_use_tab_model_;
sclittle 2015/11/16 23:13:42 It doesn't make sense to have a WeakPtr tied to a
tbansal1 2015/11/17 21:12:33 Done.
116
91 // |tab_events_| stores tracking events of multiple tabs. 117 // |tab_events_| stores tracking events of multiple tabs.
92 TabEvents tab_events_; 118 TabEvents tab_events_;
93 119
120 // True if |this| is registered as an observer to
121 // DataUseTabModel::TabDataUseObserver.
122 bool registered_as_observer_;
123
94 // |io_task_runner_| accesses DataUseTabModel members on IO thread. 124 // |io_task_runner_| accesses DataUseTabModel members on IO thread.
95 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 125 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
96 126
97 base::ThreadChecker thread_checker_; 127 base::ThreadChecker thread_checker_;
98 128
99 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel); 129 DISALLOW_COPY_AND_ASSIGN(DataUseUITabModel);
100 }; 130 };
101 131
102 } // namespace android 132 } // namespace android
103 133
104 } // namespace chrome 134 } // namespace chrome
105 135
106 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_ 136 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_DATA_USE_UI_TAB_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698