1 // Copyright 2016 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 CONTENT_BROWSER_APPCACHE_APPCACHE_NAVIGATION_HANDLE_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_NAVIGATION_HANDLE_H_
7 
8 #include <memory>
9 #include "base/macros.h"
10 #include "base/unguessable_token.h"
11 
12 namespace content {
13 
14 class AppCacheHost;
15 class ChromeAppCacheService;
16 
17 // This class is used to manage the lifetime of AppCacheHosts created during
18 // navigation. This is a UI thread class.
19 //
20 // The lifetime of the AppCacheNavigationHandle and the AppCacheHost are the
21 // following :
22 //   1) We create a AppCacheNavigationHandle and precreated AppCacheHost on the
23 //      UI thread with a app cache host id of -1.
24 //
25 //   2) When the navigation is ready to commit, the NavigationRequest will
26 //      update the CommitNavigationParams based on the id from the
27 //      AppCacheNavigationHandle.
28 //
29 //   3) The commit leads to AppCache registrations happening from the renderer.
30 //      This is via the AppCacheBackend.RegisterHost mojo call. The
31 //      AppCacheBackendImpl class which handles these calls will be informed
32 //      about these hosts when the navigation commits. It will ignore the
33 //      host registrations as they have already been registered. The
34 //      ownership of the precreated AppCacheHost is passed from the
35 //      AppCacheNavigationHandle to the AppCacheBackendImpl.
36 //
37 //   4) Meanwhile, RenderFrameHostImpl takes ownership of
38 //      AppCacheNavigationHandle once navigation commits, so that the precreated
39 //      AppCacheHost is not destroyed before IPC above reaches AppCacheBackend.
40 //
41 //   5) When the next navigation commits, previous AppCacheNavigationHandle is
42 //      destroyed.
43 
44 class AppCacheNavigationHandle {
45  public:
46   AppCacheNavigationHandle(ChromeAppCacheService* appcache_service,
47                            int process_id);
48   ~AppCacheNavigationHandle();
49 
appcache_host_id()50   const base::UnguessableToken& appcache_host_id() const {
51     return appcache_host_id_;
52   }
53 
54   // Returns the precreated AppCacheHost pointer. Ownership of the host is
55   // released here.
56   static std::unique_ptr<AppCacheHost> TakePrecreatedHost(
57       const base::UnguessableToken& host_id);
58 
59   // Returns the raw AppCacheHost pointer. Ownership remains with this class.
host()60   AppCacheHost* host() { return precreated_host_.get(); }
61 
62  private:
63   const base::UnguessableToken appcache_host_id_;
64   std::unique_ptr<AppCacheHost> precreated_host_;
65 
66   DISALLOW_COPY_AND_ASSIGN(AppCacheNavigationHandle);
67 };
68 
69 }  // namespace content
70 
71 #endif  // CONTENT_BROWSER_APPCACHE_APPCACHE_NAVIGATION_HANDLE_H_
72