OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_DISPATCHER_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_DISPATCHER_H_ |
6 #define COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_DISPATCHER_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_DISPATCHER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "components/offline_pages/core/offline_page_item.h" | 12 #include "components/offline_pages/core/offline_page_item.h" |
13 #include "components/offline_pages/core/prefetch/prefetch_dispatcher.h" | 13 #include "components/offline_pages/core/prefetch/prefetch_dispatcher.h" |
14 | 14 #include "components/offline_pages/core/prefetch/prefetch_types.h" |
15 class GURL; | |
16 | 15 |
17 namespace offline_pages { | 16 namespace offline_pages { |
18 | 17 |
19 // Serves as the entry point for external signals into the prefetching system. | 18 // Serves as the entry point for external signals into the prefetching system. |
20 // It listens to these events, converts them to the appropriate internal tasks | 19 // It listens to these events, converts them to the appropriate internal tasks |
21 // and manage their execution and inter-dependencies. | 20 // and manage their execution and inter-dependencies. |
22 class PrefetchDispatcher { | 21 class PrefetchDispatcher { |
23 public: | 22 public: |
24 struct PrefetchURL { | |
25 // Used to differentiate URLs from different sources. |name_space| should | |
26 // be unique per source. |id| can be anything useful to identify the page, | |
27 // but will not be used for deduplication. | |
28 ClientId client_id; | |
29 | |
30 // This URL will be prefetched by the system. | |
31 GURL url; | |
32 }; | |
33 | |
34 // A |ScopedBackgroundTask| is created when we are running in a background | 23 // A |ScopedBackgroundTask| is created when we are running in a background |
35 // task. Destroying this object should notify the system that we are done | 24 // task. Destroying this object should notify the system that we are done |
36 // processing the background task. | 25 // processing the background task. |
37 class ScopedBackgroundTask { | 26 class ScopedBackgroundTask { |
38 public: | 27 public: |
39 ScopedBackgroundTask() = default; | 28 ScopedBackgroundTask() = default; |
40 virtual ~ScopedBackgroundTask() = default; | 29 virtual ~ScopedBackgroundTask() = default; |
41 | 30 |
42 // Used on destruction to inform the system about whether rescheduling is | 31 // Used on destruction to inform the system about whether rescheduling is |
43 // required. | 32 // required. |
44 virtual void SetNeedsReschedule(bool reschedule) = 0; | 33 virtual void SetNeedsReschedule(bool reschedule) = 0; |
45 }; | 34 }; |
46 | 35 |
47 virtual ~PrefetchDispatcher() = default; | 36 virtual ~PrefetchDispatcher() = default; |
48 | 37 |
49 // Called when a consumer has candidate URLs for the system to prefetch. | 38 // Called when a client has candidate URLs for the system to prefetch, along |
50 // Duplicates are accepted by the PrefetchDispatcher but ignored. | 39 // with the client's unique namespace. URLs that are currently in the system |
| 40 // for this client are acceptable but ignored. |
51 virtual void AddCandidatePrefetchURLs( | 41 virtual void AddCandidatePrefetchURLs( |
| 42 const std::string& name_space, |
52 const std::vector<PrefetchURL>& prefetch_urls) = 0; | 43 const std::vector<PrefetchURL>& prefetch_urls) = 0; |
53 | 44 |
54 // Called when all existing suggestions are no longer considered valid for a | 45 // Called when all existing suggestions are no longer considered valid for a |
55 // given namespace. The prefetch system should remove any URLs that | 46 // given namespace. The prefetch system should remove any URLs that |
56 // have not yet started downloading within that namespace. | 47 // have not yet started downloading within that namespace. |
57 virtual void RemoveAllUnprocessedPrefetchURLs( | 48 virtual void RemoveAllUnprocessedPrefetchURLs( |
58 const std::string& name_space) = 0; | 49 const std::string& name_space) = 0; |
59 | 50 |
60 // Called to invalidate a single PrefetchURL entry identified by |client_id|. | 51 // Called to invalidate a single PrefetchURL entry identified by |client_id|. |
61 // If multiple have the same |client_id|, they will all be removed. | 52 // If multiple have the same |client_id|, they will all be removed. |
62 virtual void RemovePrefetchURLsByClientId(const ClientId& client_id) = 0; | 53 virtual void RemovePrefetchURLsByClientId(const std::string& name_space, |
| 54 const std::string& client_id) = 0; |
63 | 55 |
64 // Called when Android OS has scheduled us for background work. When | 56 // Called when Android OS has scheduled us for background work. When |
65 // destroyed, |task| will call back and inform the OS that we are done work | 57 // destroyed, |task| will call back and inform the OS that we are done work |
66 // (if required). |task| also manages rescheduling behavior. | 58 // (if required). |task| also manages rescheduling behavior. |
67 virtual void BeginBackgroundTask( | 59 virtual void BeginBackgroundTask( |
68 std::unique_ptr<ScopedBackgroundTask> task) = 0; | 60 std::unique_ptr<ScopedBackgroundTask> task) = 0; |
69 | 61 |
70 // Called when a task must stop immediately due to system constraints. After | 62 // Called when a task must stop immediately due to system constraints. After |
71 // this call completes, the system will reschedule the task based on whether | 63 // this call completes, the system will reschedule the task based on whether |
72 // SetNeedsReschedule has been called. | 64 // SetNeedsReschedule has been called. |
73 virtual void StopBackgroundTask(ScopedBackgroundTask* task) = 0; | 65 virtual void StopBackgroundTask(ScopedBackgroundTask* task) = 0; |
74 }; | 66 }; |
75 | 67 |
76 } // namespace offline_pages | 68 } // namespace offline_pages |
77 | 69 |
78 #endif // COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_DISPATCHER_H_ | 70 #endif // COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_DISPATCHER_H_ |
OLD | NEW |