OLD | NEW |
| (Empty) |
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 #include "core/loader/PrerenderHandle.h" | |
32 | |
33 #include "core/dom/Document.h" | |
34 #include "core/frame/LocalFrame.h" | |
35 #include "core/loader/FrameLoader.h" | |
36 #include "core/loader/PrerendererClient.h" | |
37 #include "platform/Prerender.h" | |
38 #include "platform/weborigin/ReferrerPolicy.h" | |
39 #include "platform/weborigin/SecurityPolicy.h" | |
40 | |
41 namespace blink { | |
42 | |
43 // static | |
44 PrerenderHandle* PrerenderHandle::create(Document& document, | |
45 PrerenderClient* client, | |
46 const KURL& url, | |
47 const unsigned prerenderRelTypes) { | |
48 // Prerenders are unlike requests in most ways (for instance, they pass down | |
49 // fragments, and they don't return data), but they do have referrers. | |
50 if (!document.frame()) | |
51 return nullptr; | |
52 | |
53 Prerender* prerender = Prerender::create( | |
54 client, url, prerenderRelTypes, | |
55 SecurityPolicy::generateReferrer(document.getReferrerPolicy(), url, | |
56 document.outgoingReferrer())); | |
57 | |
58 PrerendererClient* prerendererClient = | |
59 PrerendererClient::from(document.page()); | |
60 if (prerendererClient) | |
61 prerendererClient->willAddPrerender(prerender); | |
62 prerender->add(); | |
63 | |
64 return new PrerenderHandle(document, prerender); | |
65 } | |
66 | |
67 PrerenderHandle::PrerenderHandle(Document& document, Prerender* prerender) | |
68 : ContextLifecycleObserver(&document), m_prerender(prerender) {} | |
69 | |
70 PrerenderHandle::~PrerenderHandle() { | |
71 if (m_prerender) { | |
72 m_prerender->abandon(); | |
73 detach(); | |
74 } | |
75 } | |
76 | |
77 void PrerenderHandle::cancel() { | |
78 // Avoid both abandoning and canceling the same prerender. In the abandon | |
79 // case, the LinkLoader cancels the PrerenderHandle as the Document is | |
80 // destroyed, even through the ContextLifecycleObserver has already abandoned | |
81 // it. | |
82 if (!m_prerender) | |
83 return; | |
84 m_prerender->cancel(); | |
85 detach(); | |
86 } | |
87 | |
88 const KURL& PrerenderHandle::url() const { | |
89 return m_prerender->url(); | |
90 } | |
91 | |
92 void PrerenderHandle::contextDestroyed(ExecutionContext*) { | |
93 // A PrerenderHandle is not removed from LifecycleNotifier::m_observers until | |
94 // the next GC runs. Thus contextDestroyed() can be called for a | |
95 // PrerenderHandle that is already cancelled (and thus detached). In that | |
96 // case, we should not detach the PrerenderHandle again. | |
97 if (!m_prerender) | |
98 return; | |
99 m_prerender->abandon(); | |
100 detach(); | |
101 } | |
102 | |
103 void PrerenderHandle::detach() { | |
104 m_prerender->dispose(); | |
105 m_prerender.clear(); | |
106 } | |
107 | |
108 DEFINE_TRACE(PrerenderHandle) { | |
109 visitor->trace(m_prerender); | |
110 ContextLifecycleObserver::trace(visitor); | |
111 } | |
112 | |
113 } // namespace blink | |
OLD | NEW |