• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_
7 
8 #include "base/basictypes.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/threading/non_thread_safe.h"
11 #include "chrome/browser/prerender/prerender_manager.h"
12 
13 class GURL;
14 
15 namespace content {
16 class SessionStorageNamespace;
17 }
18 
19 namespace prerender {
20 
21 class PrerenderContents;
22 
23 // A class representing a running prerender to a client of the PrerenderManager.
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
26 // prerender). Calls on the handle of a prerender that is not running at no-ops.
27 // Destroying a handle before a prerender starts will prevent it from ever
28 // starting. Destroying a handle while a prerendering is running will stop the
29 // prerender, without making any calls to the observer.
30 class PrerenderHandle : public base::NonThreadSafe,
31                         public PrerenderContents::Observer {
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 had its load event.
39     virtual void OnPrerenderStopLoading(PrerenderHandle* handle) = 0;
40 
41     // Signals that the prerender has stopped running.
42     virtual void OnPrerenderStop(PrerenderHandle* handle) = 0;
43 
44     // Signals that this prerender has just become a MatchComplete replacement.
45     virtual void OnPrerenderCreatedMatchCompleteReplacement(
46         PrerenderHandle* handle) = 0;
47 
48    protected:
49     Observer();
50     virtual ~Observer();
51   };
52 
53   // Before calling the destructor, the caller must invalidate the handle by
54   // calling either OnNavigateAway or OnCancel.
55   virtual ~PrerenderHandle();
56 
57   void SetObserver(Observer* observer);
58 
59   // The launcher is navigating away from the context that launched this
60   // prerender. The prerender will likely stay alive briefly though, in case we
61   // are going through a redirect chain that will target it.
62   void OnNavigateAway();
63 
64   // The launcher has taken explicit action to remove this prerender (for
65   // instance, removing a link element from a document). This call invalidates
66   // the handle. If the prerender handle is already invalid, this call does
67   // nothing.
68   void OnCancel();
69 
70   // True if this prerender is currently active.
71   bool IsPrerendering() const;
72 
73   // True if we started a prerender, and it has finished loading.
74   bool IsFinishedLoading() const;
75 
76   PrerenderContents* contents() const;
77 
78   // Returns whether the prerender matches the URL provided.
79   bool Matches(
80       const GURL& url,
81       const content::SessionStorageNamespace* session_storage_namespace) const;
82 
83   // Returns whether this PrerenderHandle represents the same prerender as
84   // the other PrerenderHandle object specified.
85   bool RepresentingSamePrerenderAs(PrerenderHandle* other) const;
86 
87   // Retrieves the SessionStorageNamespace of the underlying prerender, if
88   // available.
89   content::SessionStorageNamespace* GetSessionStorageNamespace() const;
90 
91   // Returns the child id of the prerender.
92   int GetChildId() const;
93 
94  private:
95   friend class PrerenderManager;
96 
97   explicit PrerenderHandle(PrerenderManager::PrerenderData* prerender_data);
98 
99   void AdoptPrerenderDataFrom(PrerenderHandle* other_handle);
100 
101   // From PrerenderContents::Observer:
102   virtual void OnPrerenderStart(PrerenderContents* prerender_contents) OVERRIDE;
103   virtual void OnPrerenderStopLoading(PrerenderContents* prerender_contents)
104       OVERRIDE;
105   virtual void OnPrerenderStop(PrerenderContents* prerender_contents) OVERRIDE;
106   virtual void OnPrerenderCreatedMatchCompleteReplacement(
107       PrerenderContents* contents, PrerenderContents* replacement) OVERRIDE;
108 
109   Observer* observer_;
110 
111   base::WeakPtr<PrerenderManager::PrerenderData> prerender_data_;
112   base::WeakPtrFactory<PrerenderHandle> weak_ptr_factory_;
113 
114   DISALLOW_COPY_AND_ASSIGN(PrerenderHandle);
115 };
116 
117 }  // namespace prerender
118 
119 #endif  // CHROME_BROWSER_PRERENDER_PRERENDER_HANDLE_H_
120