OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_ |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/threading/non_thread_safe.h" | 10 #include "base/threading/non_thread_safe.h" |
11 #include "chrome/browser/prerender/prerender_manager.h" | 11 #include "chrome/browser/prerender/prerender_manager.h" |
12 | 12 |
13 class GURL; | 13 class GURL; |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 class SessionStorageNamespace; | 16 class SessionStorageNamespace; |
17 } | 17 } |
18 | 18 |
19 namespace prerender { | 19 namespace prerender { |
20 | 20 |
21 class PrerenderContents; | 21 class PrerenderContents; |
22 | 22 |
23 // A class representing a running prerender to a client of the PrerenderManager. | 23 // A class representing a running prerender to a client of the PrerenderManager. |
24 // Methods on PrerenderManager which start prerenders return a caller-owned | 24 // Methods on PrerenderManager which start prerenders return a caller-owned |
25 // PrerenderHandle* to the client (or NULL if they are unable to start a | 25 // PrerenderHandle* to the client (or NULL if they are unable to start a |
26 // prerender). Because the PrerenderManager can stop running prerenders at any | 26 // prerender). Calls on the handle of a prerender that is not running at no-ops. |
27 // time, callers may wish to check PrerenderHandle::IsValid() before operating | 27 // Destroying a handle before a prerender starts will prevent it from ever |
28 // on their prerenders. | 28 // starting. Destroying a handle while a prerendering is running will stop the |
29 class PrerenderHandle : public base::NonThreadSafe { | 29 // prerender, without making any calls to the observer. |
| 30 class PrerenderHandle : public base::NonThreadSafe, |
| 31 public PrerenderContents::Observer { |
30 public: | 32 public: |
| 33 class Observer { |
| 34 public: |
| 35 // Signals that the prerender has started running. |
| 36 virtual void OnPrerenderStart(PrerenderHandle* handle) = 0; |
| 37 |
| 38 // Signals that the prerender has stopped running. |
| 39 virtual void OnPrerenderStop(PrerenderHandle* handle) = 0; |
| 40 |
| 41 // Signals the discovery, through redirects, of a new alias for this |
| 42 // prerender. |
| 43 virtual void OnPrerenderAddAlias(PrerenderHandle* handle, |
| 44 const GURL& alias_url) = 0; |
| 45 |
| 46 protected: |
| 47 Observer(); |
| 48 virtual ~Observer(); |
| 49 }; |
| 50 |
31 // Before calling the destructor, the caller must invalidate the handle by | 51 // Before calling the destructor, the caller must invalidate the handle by |
32 // calling either OnNavigateAway or OnCancel. | 52 // calling either OnNavigateAway or OnCancel. |
33 ~PrerenderHandle(); | 53 virtual ~PrerenderHandle(); |
| 54 |
| 55 void SetObserver(Observer* observer); |
34 | 56 |
35 // The launcher is navigating away from the context that launched this | 57 // The launcher is navigating away from the context that launched this |
36 // prerender. The prerender will likely stay alive briefly though, in case we | 58 // prerender. The prerender will likely stay alive briefly though, in case we |
37 // are going through a redirect chain that will target it. This call | 59 // are going through a redirect chain that will target it. |
38 // invalidates the handle. If the prerender handle is already invalid, this | |
39 // call does nothing. | |
40 void OnNavigateAway(); | 60 void OnNavigateAway(); |
41 | 61 |
42 // The launcher has taken explicit action to remove this prerender (for | 62 // The launcher has taken explicit action to remove this prerender (for |
43 // instance, removing a link element from a document). This call invalidates | 63 // instance, removing a link element from a document). This call invalidates |
44 // the handle. If the prerender handle is already invalid, this call does | 64 // the handle. If the prerender handle is already invalid, this call does |
45 // nothing. | 65 // nothing. |
46 void OnCancel(); | 66 void OnCancel(); |
47 | 67 |
48 // True if the prerender handle is still connected to a (pending or running) | |
49 // prerender. Handles can become invalid through explicit requests by the | |
50 // client, such as calling OnCancel() or OnNavigateAway(), and handles | |
51 // also become invalid when the PrerenderManager cancels prerenders. | |
52 bool IsValid() const; | |
53 | |
54 // True if this prerender was launched by a page that was itself being | |
55 // prerendered, and so has not yet been started. | |
56 bool IsPending() const; | |
57 | |
58 // True if this prerender is currently active. | 68 // True if this prerender is currently active. |
59 bool IsPrerendering() const; | 69 bool IsPrerendering() const; |
60 | 70 |
61 // True if we started a prerender, and it has finished loading. | 71 // True if we started a prerender, and it has finished loading. |
62 bool IsFinishedLoading() const; | 72 bool IsFinishedLoading() const; |
63 | 73 |
64 private: | 74 private: |
65 friend class PrerenderManager; | 75 friend class PrerenderManager; |
66 | 76 |
67 explicit PrerenderHandle(PrerenderManager::PrerenderData* prerender_data); | 77 explicit PrerenderHandle(PrerenderManager::PrerenderData* prerender_data); |
68 | 78 |
69 void SwapPrerenderDataWith(PrerenderHandle* other_prerender_handle); | 79 void AdoptPrerenderDataFrom(PrerenderHandle* other_handle); |
| 80 |
| 81 // From PrerenderContents::Observer: |
| 82 virtual void OnPrerenderStart(PrerenderContents* prerender_contents) OVERRIDE; |
| 83 virtual void OnPrerenderStop(PrerenderContents* prerender_contents) OVERRIDE; |
| 84 virtual void OnPrerenderAddAlias(PrerenderContents* prerender_contents, |
| 85 const GURL& alias_url) OVERRIDE; |
| 86 virtual void OnPrerenderCreatedMatchCompleteReplacement( |
| 87 PrerenderContents* contents, PrerenderContents* replacement) OVERRIDE; |
| 88 |
| 89 Observer* observer_; |
70 | 90 |
71 base::WeakPtr<PrerenderManager::PrerenderData> prerender_data_; | 91 base::WeakPtr<PrerenderManager::PrerenderData> prerender_data_; |
72 base::WeakPtrFactory<PrerenderHandle> weak_ptr_factory_; | 92 base::WeakPtrFactory<PrerenderHandle> weak_ptr_factory_; |
73 | 93 |
74 DISALLOW_COPY_AND_ASSIGN(PrerenderHandle); | 94 DISALLOW_COPY_AND_ASSIGN(PrerenderHandle); |
75 }; | 95 }; |
76 | 96 |
77 } // namespace prerender | 97 } // namespace prerender |
78 | 98 |
79 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_ | 99 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_ |
OLD | NEW |