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

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 <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 class IOThread;
23
24 namespace base {
25 class SingleThreadTaskRunner;
26 } // namespace base
22 27
23 namespace chrome { 28 namespace chrome {
24 29
25 namespace android { 30 namespace android {
26 31
27 // DataUseUITabModel tracks data use tracking start and end transitions on the 32 // DataUseUITabModel tracks data use tracking start and end transitions on the
28 // browser's tabs. It serves as a bridge between the DataUseTabModel, which 33 // 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, 34 // 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 35 // 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 36 // use tracking starts and ends in a Tab. DataUseUITabModel forwards navigation
32 // and tab closure events to the DataUseTabModel, and receives tab tracking 37 // and tab closure events to the DataUseTabModel, and receives tab tracking
33 // transitions (start/end) from the DataUseTabModel, which it conveys to UI 38 // transitions (start/end) from the DataUseTabModel, which it conveys to UI
34 // notification logic. DataUseUITabModel is not thread-safe, and should be 39 // notification logic. DataUseUITabModel is not thread-safe, and should be
35 // accessed only on the UI thread. 40 // accessed only on the UI thread. DataUseUITabModel registers as an observer to
41 // DataUseTabModel::TabDataUseObserver, and gets notified when tracking has
42 // started and ended on a tab. DataUseUITabModel is not thread safe, and should
43 // only be accessed only on UI thread.
36 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab 44 // TODO(tbansal): DataUseTabModel should notify DataUseUITabModel when a tab
37 // is removed from the list of tabs. 45 // is removed from the list of tabs.
38 class DataUseUITabModel : public KeyedService { 46 class DataUseUITabModel : public KeyedService,
47 public DataUseTabModel::TabDataUseObserver {
39 public: 48 public:
40 explicit DataUseUITabModel( 49 explicit DataUseUITabModel(
41 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); 50 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
42 ~DataUseUITabModel() override; 51 ~DataUseUITabModel() override;
43 52
44 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes 53 // Reports a browser navigation to the DataUseTabModel on IO thread. Includes
45 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that 54 // the |page_transition|, |tab_id|, and |gurl| for the navigation. Tabs that
46 // are restored when Chromium restarts are not reported. 55 // are restored when Chromium restarts are not reported.
47 void ReportBrowserNavigation(const GURL& gurl, 56 void ReportBrowserNavigation(const GURL& gurl,
48 ui::PageTransition page_transition, 57 ui::PageTransition page_transition,
49 int32_t tab_id) const; 58 SessionID::id_type tab_id) const;
50 59
51 // Reports a tab closure for the tab with |tab_id| to the DataUseTabModel on 60 // 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 61 // IO thread. The tab could either have been closed or evicted from the memory
53 // by Android. 62 // by Android.
54 void ReportTabClosure(int32_t tab_id); 63 void ReportTabClosure(SessionID::id_type tab_id);
55 64
56 // Reports a custom tab navigation to the DataUseTabModel on the IO thread. 65 // Reports a custom tab navigation to the DataUseTabModel on the IO thread.
57 // Includes the |tab_id|, |url|, and |package_name| for the navigation. 66 // Includes the |tab_id|, |url|, and |package_name| for the navigation.
58 void ReportCustomTabInitialNavigation(int32_t tab_id, 67 void ReportCustomTabInitialNavigation(SessionID::id_type tab_id,
59 const std::string& url, 68 const std::string& url,
60 const std::string& package_name); 69 const std::string& package_name);
61 70
62 // Returns true if data use tracking has been started for the tab with id 71 // 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. 72 // |tab_id|. Calling this function resets the state of the tab.
64 bool HasDataUseTrackingStarted(int32_t tab_id); 73 bool HasDataUseTrackingStarted(SessionID::id_type tab_id);
65 74
66 // Returns true if data use tracking has ended for the tab with id |tab_id|. 75 // 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. 76 // Calling this function resets the state of the tab.
68 bool HasDataUseTrackingEnded(int32_t tab_id); 77 bool HasDataUseTrackingEnded(SessionID::id_type tab_id);
78
79 // Sets the weak pointer to DataUseTabModel that can be used on IO thread.
80 // Should be called at most once.
81 void SetIOThread(IOThread* io_thread);
69 82
70 private: 83 private:
84 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ConvertTransitionType);
71 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState); 85 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, EntranceExitState);
86 FRIEND_TEST_ALL_PREFIXES(DataUseUITabModelTest, ReportTabEventsTest);
72 87
73 // DataUseTrackingEvent indicates the state of a tab. 88 // DataUseTrackingEvent indicates the state of a tab.
74 enum DataUseTrackingEvent { 89 enum DataUseTrackingEvent {
75 // Indicates that data use tracking has started. 90 // Indicates that data use tracking has started.
76 DATA_USE_TRACKING_STARTED, 91 DATA_USE_TRACKING_STARTED,
77 92
78 // Indicates that data use tracking has ended. 93 // Indicates that data use tracking has ended.
79 DATA_USE_TRACKING_ENDED, 94 DATA_USE_TRACKING_ENDED,
80 }; 95 };
81 96
82 typedef std::unordered_map<int32_t, DataUseTrackingEvent> TabEvents; 97 typedef base::hash_map<SessionID::id_type, DataUseTrackingEvent> TabEvents;
83 98
84 // DataUseTabModel::Observer implementation: 99 // Sets the weak pointer to DataUseTabModel. Also, registers |this| as an
85 // TODO(tbansal): Add override once DataUseTabModel is checked in. 100 // observer to the DataUseTabModel::TabDataUseObserver.
86 void OnTrackingStarted(int32_t tab_id); 101 void SetDataUseTabModel(base::WeakPtr<DataUseTabModel> data_use_tab_model);
87 void OnTrackingEnded(int32_t tab_id); 102
103 // DataUseTabModel::TabDataUseObserver implementation:
104 void NotifyTrackingStarting(SessionID::id_type tab_id) override;
105 void NotifyTrackingEnding(SessionID::id_type tab_id) override;
88 106
89 // Creates |event| for tab with id |tab_id| and value |event|, if there is no 107 // 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 108 // existing entry for |tab_id|, and returns true. Otherwise, returns false
91 // without modifying the entry. 109 // without modifying the entry.
92 bool MaybeCreateTabEvent(int32_t tab_id, DataUseTrackingEvent event); 110 bool MaybeCreateTabEvent(SessionID::id_type tab_id,
111 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(SessionID::id_type tab_id, DataUseTrackingEvent event);
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;
97 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