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