OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_DATA_USAGE_TAB_ID_PROVIDER_H_ |
| 6 #define CHROME_BROWSER_DATA_USAGE_TAB_ID_PROVIDER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/callback_forward.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/supports_user_data.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 |
| 16 namespace base { |
| 17 class TaskRunner; |
| 18 } |
| 19 |
| 20 namespace tracked_objects { |
| 21 class Location; |
| 22 } |
| 23 |
| 24 namespace chrome_browser_data_usage { |
| 25 |
| 26 // Object that can be attached to a URLRequest to provide the tab ID of that |
| 27 // URLRequest to callbacks that want them. Callbacks are either run immediately |
| 28 // if the tab ID is already available, or queued to be run later once the tab ID |
| 29 // becomes available. |
| 30 class TabIdProvider : public base::SupportsUserData::Data { |
| 31 public: |
| 32 // Constructs a tab ID provider, posting the |tab_id_getter| task onto |
| 33 // |task_runner|. |
| 34 TabIdProvider(base::TaskRunner* task_runner, |
| 35 const tracked_objects::Location& from_here, |
| 36 const base::Callback<int32_t(void)>& tab_id_getter); |
| 37 |
| 38 ~TabIdProvider() override; |
| 39 |
| 40 // Calls |callback| with the tab ID, either immediately if it's already |
| 41 // available, or later once it becomes available. |
| 42 void ProvideTabId(const base::Callback<void(int32_t)>& callback); |
| 43 |
| 44 base::WeakPtr<TabIdProvider> GetWeakPtr(); |
| 45 |
| 46 static const void* kUserDataKey; |
| 47 |
| 48 private: |
| 49 class CallbackRunner; |
| 50 |
| 51 // Called when the |tab_id| is ready. |
| 52 void OnTabIdReady(int32_t tab_id); |
| 53 |
| 54 base::ThreadChecker thread_checker_; |
| 55 bool is_tab_id_ready_; |
| 56 int32_t tab_id_; |
| 57 base::WeakPtr<CallbackRunner> weak_callback_runner_; |
| 58 base::WeakPtrFactory<TabIdProvider> weak_ptr_factory_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(TabIdProvider); |
| 61 }; |
| 62 |
| 63 } // namespace chrome_browser_data_usage |
| 64 |
| 65 #endif // CHROME_BROWSER_DATA_USAGE_TAB_ID_PROVIDER_H_ |
OLD | NEW |