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/observer_list.h" | |
10 #include "base/threading/non_thread_safe.h" | 11 #include "base/threading/non_thread_safe.h" |
11 #include "chrome/browser/prerender/prerender_manager.h" | 12 #include "chrome/browser/prerender/prerender_manager.h" |
12 | 13 |
13 class GURL; | 14 class GURL; |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 class SessionStorageNamespace; | 17 class SessionStorageNamespace; |
17 } | 18 } |
18 | 19 |
19 namespace prerender { | 20 namespace prerender { |
20 | 21 |
21 class PrerenderContents; | 22 class PrerenderContents; |
22 | 23 |
23 // A class representing a running prerender to a client of the PrerenderManager. | 24 // A class representing a running prerender to a client of the PrerenderManager. |
24 // Methods on PrerenderManager which start prerenders return a caller-owned | 25 // Methods on PrerenderManager which start prerenders return a caller-owned |
25 // PrerenderHandle* to the client (or NULL if they are unable to start a | 26 // PrerenderHandle* to the client (or NULL if they are unable to start a |
26 // prerender). Because the PrerenderManager can stop running prerenders at any | 27 // prerender). Because the PrerenderManager can stop running prerenders at any |
27 // time, callers may wish to check PrerenderHandle::IsValid() before operating | 28 // time, callers may wish to check PrerenderHandle::IsValid() before operating |
28 // on their prerenders. | 29 // on their prerenders. |
29 class PrerenderHandle : public base::NonThreadSafe { | 30 class PrerenderHandle : public base::NonThreadSafe, |
31 public PrerenderContents::Observer { | |
30 public: | 32 public: |
33 class Observer { | |
34 public: | |
35 virtual ~Observer(); | |
36 | |
37 // Signals that the prerender has started running. | |
38 virtual void OnPrerenderStart(PrerenderHandle* handle) = 0; | |
39 | |
40 // Signals that the prerender has stopped running. | |
41 virtual void OnPrerenderStop(PrerenderHandle* handle) = 0; | |
42 | |
43 // Signals the discovery, through redirects, of a new alias for this | |
44 // prerender. | |
45 virtual void OnPrerenderAddAlias(PrerenderHandle* handle, | |
46 const GURL& alias_url) = 0; | |
47 | |
48 protected: | |
49 Observer(); | |
50 }; | |
51 | |
31 // Before calling the destructor, the caller must invalidate the handle by | 52 // Before calling the destructor, the caller must invalidate the handle by |
32 // calling either OnNavigateAway or OnCancel. | 53 // calling either OnNavigateAway or OnCancel. |
33 ~PrerenderHandle(); | 54 virtual ~PrerenderHandle(); |
55 | |
56 void AddObserver(Observer* observer); | |
57 void RemoveObserver(Observer* observer); | |
34 | 58 |
35 // The launcher is navigating away from the context that launched this | 59 // The launcher is navigating away from the context that launched this |
36 // prerender. The prerender will likely stay alive briefly though, in case we | 60 // 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 | 61 // are going through a redirect chain that will target it. This call |
38 // invalidates the handle. If the prerender handle is already invalid, this | 62 // invalidates the handle. If the prerender handle is already invalid, this |
39 // call does nothing. | 63 // call does nothing. |
40 void OnNavigateAway(); | 64 void OnNavigateAway(); |
41 | 65 |
42 // The launcher has taken explicit action to remove this prerender (for | 66 // The launcher has taken explicit action to remove this prerender (for |
43 // instance, removing a link element from a document). This call invalidates | 67 // instance, removing a link element from a document). This call invalidates |
44 // the handle. If the prerender handle is already invalid, this call does | 68 // the handle. If the prerender handle is already invalid, this call does |
45 // nothing. | 69 // nothing. |
46 void OnCancel(); | 70 void OnCancel(); |
47 | 71 |
48 // True if the prerender handle is still connected to a (pending or running) | 72 // True if this prerender handle has been canceled. |
49 // prerender. Handles can become invalid through explicit requests by the | 73 bool has_been_canceled() const { |
50 // client, such as calling OnCancel() or OnNavigateAway(), and handles | 74 return has_been_canceled_; |
51 // also become invalid when the PrerenderManager cancels prerenders. | 75 } |
76 | |
77 // True if this prerender handle has associated prerender data. It is false | |
78 // for canceled prerenders, and also for pending prerenders. | |
52 bool IsValid() const; | 79 bool IsValid() const; |
mmenke
2012/12/06 22:22:15
I suggest getting rid of this, and just relying on
gavinp
2012/12/10 17:55:10
Done. I was halfway there already (remember seeing
| |
53 | 80 |
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. | 81 // True if this prerender is currently active. |
59 bool IsPrerendering() const; | 82 bool IsPrerendering() const; |
60 | 83 |
61 // True if we started a prerender, and it has finished loading. | 84 // True if we started a prerender, and it has finished loading. |
62 bool IsFinishedLoading() const; | 85 bool IsFinishedLoading() const; |
63 | 86 |
64 private: | 87 private: |
65 friend class PrerenderManager; | 88 friend class PrerenderManager; |
66 | 89 |
67 explicit PrerenderHandle(PrerenderManager::PrerenderData* prerender_data); | 90 explicit PrerenderHandle(PrerenderManager::PrerenderData* prerender_data); |
68 | 91 |
69 void SwapPrerenderDataWith(PrerenderHandle* other_prerender_handle); | 92 void AdoptPrerenderDataFrom(PrerenderHandle* other_handle); |
70 | 93 |
94 // From PrerenderContents::Observer: | |
95 virtual void OnPrerenderStart(PrerenderContents* prerender_contents) OVERRIDE; | |
96 virtual void OnPrerenderStop(PrerenderContents* prerender_contents) OVERRIDE; | |
97 virtual void OnPrerenderAddAlias(PrerenderContents* prerender_contents, | |
98 const GURL& alias_url) OVERRIDE; | |
99 | |
100 ObserverList<Observer> observer_list_; | |
101 | |
102 bool has_been_canceled_; | |
71 base::WeakPtr<PrerenderManager::PrerenderData> prerender_data_; | 103 base::WeakPtr<PrerenderManager::PrerenderData> prerender_data_; |
mmenke
2012/12/07 16:46:08
This can *almost* be made a normal pointer - the o
| |
72 base::WeakPtrFactory<PrerenderHandle> weak_ptr_factory_; | 104 base::WeakPtrFactory<PrerenderHandle> weak_ptr_factory_; |
73 | 105 |
74 DISALLOW_COPY_AND_ASSIGN(PrerenderHandle); | 106 DISALLOW_COPY_AND_ASSIGN(PrerenderHandle); |
75 }; | 107 }; |
76 | 108 |
77 } // namespace prerender | 109 } // namespace prerender |
78 | 110 |
79 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_ | 111 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_ |
OLD | NEW |