1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PRIVATE_PRERENDER_HANDLE_H_
32 #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PRIVATE_PRERENDER_HANDLE_H_
33 
34 #include "base/macros.h"
35 #include "base/memory/scoped_refptr.h"
36 #include "base/util/type_safety/pass_key.h"
37 #include "third_party/blink/public/mojom/prerender/prerender.mojom-blink.h"
38 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
39 #include "third_party/blink/renderer/platform/heap/handle.h"
40 #include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
41 #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
42 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
43 
44 namespace blink {
45 
46 class ExecutionContext;
47 class Document;
48 class PrerenderClient;
49 
50 // This is the Blink-side liaison of mojom::PrerenderProcessor to request the
51 // browser process to start prerendering, and implements
52 // mojom::PrerenderProcessorClient to observe events about prerendering. This is
53 // instantiated per prerender request, for example, when a new <link
54 // rel=prerender> element is added, when the element's href is changed etc.
55 //
56 // When you no longer need the prerendering page (e.g., when the
57 // <link rel=prerender> element is removed), you can ask the browser process to
58 // cancel the running prerender by Cancel(). If mojo connections are reset
59 // without Cancel() call, the browser process considers this prerendering
60 // request to be abandoned and may still use the prerendered page if a
61 // navigation occurs to that URL shortly after.
62 //
63 // TODO(https://crbug.com/1126305): Rename this to PrerenderProcessorClient.
64 class PrerenderHandle final : public GarbageCollected<PrerenderHandle>,
65                               public mojom::blink::PrerenderProcessorClient {
66  public:
67   static PrerenderHandle* Create(
68       Document&,
69       PrerenderClient*,
70       const KURL&,
71       mojom::blink::PrerenderRelType prerender_rel_type);
72 
73   using PassKey = util::PassKey<PrerenderHandle>;
74   PrerenderHandle(
75       PassKey,
76       ExecutionContext*,
77       PrerenderClient*,
78       const KURL&,
79       HeapMojoRemote<mojom::blink::PrerenderProcessor>,
80       mojo::PendingReceiver<mojom::blink::PrerenderProcessorClient>);
81   ~PrerenderHandle() override;
82 
83   // Asks the browser process to cancel the running prerender.
84   void Cancel();
85 
86   const KURL& Url() const;
87 
88   // mojom::blink::PrerenderProcessorClient:
89   void OnPrerenderStart() override;
90   void OnPrerenderStopLoading() override;
91   void OnPrerenderDomContentLoaded() override;
92   void OnPrerenderStop() override;
93 
94   virtual void Trace(Visitor*) const;
95 
96  private:
97   const KURL url_;
98   const WeakMember<PrerenderClient> client_;
99   HeapMojoRemote<mojom::blink::PrerenderProcessor> remote_processor_;
100   HeapMojoReceiver<mojom::blink::PrerenderProcessorClient, PrerenderHandle>
101       receiver_;
102 
103   DISALLOW_COPY_AND_ASSIGN(PrerenderHandle);
104 };
105 
106 }  // namespace blink
107 
108 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PRIVATE_PRERENDER_HANDLE_H_
109