1 // Copyright (c) 2011 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.
5 #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_HOST_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_HOST_H_
8 #include "base/callback.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/observer_list.h"
12 #include "content/browser/appcache/appcache_group.h"
13 #include "content/browser/appcache/appcache_service_impl.h"
14 #include "content/browser/appcache/appcache_storage.h"
15 #include "content/common/appcache_interfaces.h"
16 #include "content/common/content_export.h"
17 #include "content/public/common/resource_type.h"
25 FORWARD_DECLARE_TEST(AppCacheGroupTest
, CleanupUnusedGroup
);
26 FORWARD_DECLARE_TEST(AppCacheGroupTest
, QueueUpdate
);
27 FORWARD_DECLARE_TEST(AppCacheHostTest
, Basic
);
28 FORWARD_DECLARE_TEST(AppCacheHostTest
, SelectNoCache
);
29 FORWARD_DECLARE_TEST(AppCacheHostTest
, ForeignEntry
);
30 FORWARD_DECLARE_TEST(AppCacheHostTest
, FailedCacheLoad
);
31 FORWARD_DECLARE_TEST(AppCacheHostTest
, FailedGroupLoad
);
32 FORWARD_DECLARE_TEST(AppCacheHostTest
, SetSwappableCache
);
33 FORWARD_DECLARE_TEST(AppCacheHostTest
, ForDedicatedWorker
);
34 FORWARD_DECLARE_TEST(AppCacheHostTest
, SelectCacheAllowed
);
35 FORWARD_DECLARE_TEST(AppCacheHostTest
, SelectCacheBlocked
);
36 FORWARD_DECLARE_TEST(AppCacheTest
, CleanupUnusedCache
);
38 class AppCacheFrontend
;
39 class AppCacheGroupTest
;
40 class AppCacheHostTest
;
41 class AppCacheRequestHandler
;
42 class AppCacheRequestHandlerTest
;
43 class AppCacheStorageImplTest
;
45 class AppCacheUpdateJobTest
;
47 typedef base::Callback
<void(AppCacheStatus
, void*)> GetStatusCallback
;
48 typedef base::Callback
<void(bool, void*)> StartUpdateCallback
;
49 typedef base::Callback
<void(bool, void*)> SwapCacheCallback
;
51 // Server-side representation of an application cache host.
52 class CONTENT_EXPORT AppCacheHost
53 : public AppCacheStorage::Delegate
,
54 public AppCacheGroup::UpdateObserver
,
55 public AppCacheServiceImpl::Observer
{
58 class CONTENT_EXPORT Observer
{
60 // Called just after the cache selection algorithm completes.
61 virtual void OnCacheSelectionComplete(AppCacheHost
* host
) = 0;
63 // Called just prior to the instance being deleted.
64 virtual void OnDestructionImminent(AppCacheHost
* host
) = 0;
66 virtual ~Observer() {}
69 AppCacheHost(int host_id
, AppCacheFrontend
* frontend
,
70 AppCacheServiceImpl
* service
);
71 ~AppCacheHost() override
;
73 // Adds/removes an observer, the AppCacheHost does not take
74 // ownership of the observer.
75 void AddObserver(Observer
* observer
);
76 void RemoveObserver(Observer
* observer
);
78 // Support for cache selection and scriptable method calls.
79 void SelectCache(const GURL
& document_url
,
80 const int64 cache_document_was_loaded_from
,
81 const GURL
& manifest_url
);
82 void SelectCacheForWorker(int parent_process_id
,
84 void SelectCacheForSharedWorker(int64 appcache_id
);
85 void MarkAsForeignEntry(const GURL
& document_url
,
86 int64 cache_document_was_loaded_from
);
87 void GetStatusWithCallback(const GetStatusCallback
& callback
,
88 void* callback_param
);
89 void StartUpdateWithCallback(const StartUpdateCallback
& callback
,
90 void* callback_param
);
91 void SwapCacheWithCallback(const SwapCacheCallback
& callback
,
92 void* callback_param
);
94 // Called prior to the main resource load. When the system contains multiple
95 // candidates for a main resource load, the appcache preferred by the host
96 // that created this host is used to break ties.
97 void SetSpawningHostId(int spawning_process_id
, int spawning_host_id
);
99 // May return NULL if the spawning host context has been closed, or if a
100 // spawning host context was never identified.
101 const AppCacheHost
* GetSpawningHost() const;
103 const GURL
& preferred_manifest_url() const {
104 return preferred_manifest_url_
;
106 void set_preferred_manifest_url(const GURL
& url
) {
107 preferred_manifest_url_
= url
;
110 // Support for loading resources out of the appcache.
111 // May return NULL if the request isn't subject to retrieval from an appache.
112 AppCacheRequestHandler
* CreateRequestHandler(
113 net::URLRequest
* request
,
114 ResourceType resource_type
,
115 bool should_reset_appcache
);
117 // Support for devtools inspecting appcache resources.
118 void GetResourceList(std::vector
<AppCacheResourceInfo
>* resource_infos
);
120 // Breaks any existing association between this host and a cache.
121 // 'manifest_url' is sent to DevTools as the manifest url that could have
122 // been associated before or could be associated later with this host.
123 // Associations are broken either thru the cache selection algorithm
124 // implemented in this class, or by the update algorithm (see
125 // AppCacheUpdateJob).
126 void AssociateNoCache(const GURL
& manifest_url
);
128 // Establishes an association between this host and an incomplete cache.
129 // 'manifest_url' is manifest url of the cache group being updated.
130 // Associations with incomplete caches are established by the update algorithm
131 // (see AppCacheUpdateJob).
132 void AssociateIncompleteCache(AppCache
* cache
, const GURL
& manifest_url
);
134 // Establishes an association between this host and a complete cache.
135 // Associations with complete caches are established either thru the cache
136 // selection algorithm implemented (in this class), or by the update algorithm
137 // (see AppCacheUpdateJob).
138 void AssociateCompleteCache(AppCache
* cache
);
140 // Adds a reference to the newest complete cache in a group, unless it's the
141 // same as the cache that is currently associated with the host.
142 void SetSwappableCache(AppCacheGroup
* group
);
144 // Used to ensure that a loaded appcache survives a frame navigation.
145 void LoadMainResourceCache(int64 cache_id
);
147 // Used to notify the host that a namespace resource is being delivered as
148 // the main resource of the page and to provide its url.
149 void NotifyMainResourceIsNamespaceEntry(const GURL
& namespace_entry_url
);
151 // Used to notify the host that the main resource was blocked by a policy. To
152 // work properly, this method needs to by invoked prior to cache selection.
153 void NotifyMainResourceBlocked(const GURL
& manifest_url
);
155 // Used by the update job to keep track of which hosts are associated
156 // with which pending master entries.
157 const GURL
& pending_master_entry_url() const {
158 return new_master_entry_url_
;
161 int host_id() const { return host_id_
; }
162 AppCacheServiceImpl
* service() const { return service_
; }
163 AppCacheStorage
* storage() const { return storage_
; }
164 AppCacheFrontend
* frontend() const { return frontend_
; }
165 AppCache
* associated_cache() const { return associated_cache_
.get(); }
166 bool was_select_cache_called() const { return was_select_cache_called_
; }
168 void enable_cache_selection(bool enable
) {
169 is_cache_selection_enabled_
= enable
;
172 bool is_selection_pending() const {
173 return pending_selected_cache_id_
!= kAppCacheNoCacheId
||
174 !pending_selected_manifest_url_
.is_empty();
177 const GURL
& first_party_url() const { return first_party_url_
; }
179 // Methods to support cross site navigations.
180 void PrepareForTransfer();
181 void CompleteTransfer(int host_id
, AppCacheFrontend
* frontend
);
184 friend class content::AppCacheHostTest
;
185 friend class content::AppCacheStorageImplTest
;
186 friend class content::AppCacheRequestHandlerTest
;
187 friend class content::AppCacheUpdateJobTest
;
189 AppCacheStatus
GetStatus();
190 void LoadSelectedCache(int64 cache_id
);
191 void LoadOrCreateGroup(const GURL
& manifest_url
);
193 // See public Associate*Host() methods above.
194 void AssociateCacheHelper(AppCache
* cache
, const GURL
& manifest_url
);
196 // AppCacheStorage::Delegate impl
197 void OnCacheLoaded(AppCache
* cache
, int64 cache_id
) override
;
198 void OnGroupLoaded(AppCacheGroup
* group
, const GURL
& manifest_url
) override
;
199 // AppCacheServiceImpl::Observer impl
200 void OnServiceReinitialized(
201 AppCacheStorageReference
* old_storage_ref
) override
;
203 void FinishCacheSelection(AppCache
* cache
, AppCacheGroup
* group
);
204 void DoPendingGetStatus();
205 void DoPendingStartUpdate();
206 void DoPendingSwapCache();
208 void ObserveGroupBeingUpdated(AppCacheGroup
* group
);
210 // AppCacheGroup::UpdateObserver methods.
211 void OnUpdateComplete(AppCacheGroup
* group
) override
;
213 // Returns true if this host is for a dedicated worker context.
214 bool is_for_dedicated_worker() const {
215 return parent_host_id_
!= kAppCacheNoHostId
;
218 // Returns the parent context's host instance. This is only valid
219 // to call when this instance is_for_dedicated_worker.
220 AppCacheHost
* GetParentAppCacheHost() const;
222 // Identifies the corresponding appcache host in the child process.
225 // Information about the host that created this one; the manifest
226 // preferred by our creator influences which cache our main resource
227 // should be loaded from.
228 int spawning_host_id_
;
229 int spawning_process_id_
;
230 GURL preferred_manifest_url_
;
232 // Hosts for dedicated workers are special cased to shunt
233 // request handling off to the dedicated worker's parent.
234 // The scriptable api is not accessible in dedicated workers
235 // so the other aspects of this class are not relevant for
236 // these special case instances.
238 int parent_process_id_
;
240 // Defined prior to refs to AppCaches and Groups because destruction
241 // order matters, the disabled_storage_reference_ must outlive those
242 // objects. See additional comments for the storage_ member.
243 scoped_refptr
<AppCacheStorageReference
> disabled_storage_reference_
;
245 // The cache associated with this host, if any.
246 scoped_refptr
<AppCache
> associated_cache_
;
248 // Hold a reference to the newest complete cache (if associated cache is
249 // not the newest) to keep the newest cache in existence while the app cache
250 // group is in use. The newest complete cache may have no associated hosts
251 // holding any references to it and would otherwise be deleted prematurely.
252 scoped_refptr
<AppCache
> swappable_cache_
;
254 // Keep a reference to the group being updated until the update completes.
255 scoped_refptr
<AppCacheGroup
> group_being_updated_
;
257 // Similarly, keep a reference to the newest cache of the group until the
258 // update completes. When adding a new master entry to a cache that is not
259 // in use in any other host, this reference keeps the cache in memory.
260 scoped_refptr
<AppCache
> newest_cache_of_group_being_updated_
;
262 // Keep a reference to the cache of the main resource so it survives frame
264 scoped_refptr
<AppCache
> main_resource_cache_
;
265 int64 pending_main_resource_cache_id_
;
267 // Cache loading is async, if we're loading a specific cache or group
268 // for the purposes of cache selection, one or the other of these will
269 // indicate which cache or group is being loaded.
270 int64 pending_selected_cache_id_
;
271 GURL pending_selected_manifest_url_
;
273 // Used to defend against bad IPC messages.
274 bool was_select_cache_called_
;
276 // Used to avoid stepping on pages controlled by ServiceWorkers.
277 bool is_cache_selection_enabled_
;
279 // A new master entry to be added to the cache, may be empty.
280 GURL new_master_entry_url_
;
282 // The frontend proxy to deliver notifications to the child process.
283 AppCacheFrontend
* frontend_
;
285 // Our central service object.
286 AppCacheServiceImpl
* service_
;
288 // And the equally central storage object, with a twist. In some error
289 // conditions the storage object gets recreated and reinitialized. The
290 // disabled_storage_reference_ (defined earlier) allows for cleanup of an
291 // instance that got disabled after we had latched onto it. In normal
292 // circumstances, disabled_storage_reference_ is expected to be NULL.
293 // When non-NULL both storage_ and disabled_storage_reference_ refer to the
295 AppCacheStorage
* storage_
;
297 // Since these are synchronous scriptable API calls in the client, there can
298 // only be one type of callback pending. Also, we have to wait until we have a
299 // cache selection prior to responding to these calls, as cache selection
300 // involves async loading of a cache or a group from storage.
301 GetStatusCallback pending_get_status_callback_
;
302 StartUpdateCallback pending_start_update_callback_
;
303 SwapCacheCallback pending_swap_cache_callback_
;
304 void* pending_callback_param_
;
306 // True if an intercept or fallback namespace resource was
307 // delivered as the main resource.
308 bool main_resource_was_namespace_entry_
;
309 GURL namespace_entry_url_
;
311 // True if requests for this host were blocked by a policy.
312 bool main_resource_blocked_
;
313 GURL blocked_manifest_url_
;
315 // Tells if info about associated cache is pending. Info is pending
316 // when update job has not returned success yet.
317 bool associated_cache_info_pending_
;
319 // List of objects observing us.
320 base::ObserverList
<Observer
> observers_
;
322 // Used to inform the QuotaManager of what origins are currently in use.
325 // First party url to be used in policy checks.
326 GURL first_party_url_
;
328 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, CleanupUnusedGroup
);
329 FRIEND_TEST_ALL_PREFIXES(content::AppCacheGroupTest
, QueueUpdate
);
330 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, Basic
);
331 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, SelectNoCache
);
332 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, ForeignEntry
);
333 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, FailedCacheLoad
);
334 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, FailedGroupLoad
);
335 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, SetSwappableCache
);
336 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, ForDedicatedWorker
);
337 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, SelectCacheAllowed
);
338 FRIEND_TEST_ALL_PREFIXES(content::AppCacheHostTest
, SelectCacheBlocked
);
339 FRIEND_TEST_ALL_PREFIXES(content::AppCacheTest
, CleanupUnusedCache
);
341 DISALLOW_COPY_AND_ASSIGN(AppCacheHost
);
344 } // namespace content
346 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_HOST_H_