OLD | NEW |
| (Empty) |
1 // Copyright 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_INSTANT_INSTANT_LOADER_H_ | |
6 #define CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/callback.h" | |
10 #include "base/compiler_specific.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/timer.h" | |
13 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" | |
14 #include "content/public/browser/notification_observer.h" | |
15 #include "content/public/browser/notification_registrar.h" | |
16 #include "content/public/browser/web_contents_delegate.h" | |
17 | |
18 class GURL; | |
19 class Profile; | |
20 | |
21 namespace content { | |
22 struct OpenURLParams; | |
23 class WebContents; | |
24 } | |
25 | |
26 // InstantLoader is used to create and maintain a WebContents where we can | |
27 // preload a page into. It is used by InstantOverlay and InstantNTP to | |
28 // preload an Instant page. | |
29 class InstantLoader : public content::NotificationObserver, | |
30 public content::WebContentsDelegate, | |
31 public CoreTabHelperDelegate { | |
32 public: | |
33 // InstantLoader calls these methods on its delegate in response to certain | |
34 // changes in the underlying contents. | |
35 class Delegate { | |
36 public: | |
37 // Called after someone has swapped in a different WebContents for ours. | |
38 virtual void OnSwappedContents() = 0; | |
39 | |
40 // Called when the underlying contents receive focus. | |
41 virtual void OnFocus() = 0; | |
42 | |
43 // Called when the mouse pointer is down. | |
44 virtual void OnMouseDown() = 0; | |
45 | |
46 // Called when the mouse pointer is released (or a drag event ends). | |
47 virtual void OnMouseUp() = 0; | |
48 | |
49 // Called to open a URL using the underlying contents (see | |
50 // WebContentsDelegate::OpenURLFromTab). The Delegate should return the | |
51 // WebContents the URL is opened in, or NULL if the URL wasn't opened | |
52 // immediately. | |
53 virtual content::WebContents* OpenURLFromTab( | |
54 content::WebContents* source, | |
55 const content::OpenURLParams& params) = 0; | |
56 | |
57 protected: | |
58 ~Delegate(); | |
59 }; | |
60 | |
61 explicit InstantLoader(Delegate* delegate); | |
62 virtual ~InstantLoader(); | |
63 | |
64 // Creates a new WebContents in the context of |profile| that will be used to | |
65 // load |instant_url|. The page is not actually loaded until Load() is | |
66 // called. Uses |active_contents|, if non-NULL, to initialize the size of the | |
67 // new contents. |on_stale_callback| will be called after kStalePageTimeoutMS | |
68 // has elapsed after Load() being called. | |
69 void Init(const GURL& instant_url, | |
70 Profile* profile, | |
71 const content::WebContents* active_contents, | |
72 const base::Closure& on_stale_callback); | |
73 | |
74 // Loads |instant_url_| in |contents_|. | |
75 void Load(); | |
76 | |
77 // Returns the contents currently held. May be NULL. | |
78 content::WebContents* contents() const { return contents_.get(); } | |
79 | |
80 // Replaces the contents held with |contents|. Any existing contents is | |
81 // deleted. The expiration timer is not restarted. | |
82 void SetContents(scoped_ptr<content::WebContents> contents); | |
83 | |
84 // Releases the contents currently held. Must only be called if contents() is | |
85 // not NULL. | |
86 scoped_ptr<content::WebContents> ReleaseContents() WARN_UNUSED_RESULT; | |
87 | |
88 private: | |
89 // Overridden from content::NotificationObserver: | |
90 virtual void Observe(int type, | |
91 const content::NotificationSource& source, | |
92 const content::NotificationDetails& details) OVERRIDE; | |
93 | |
94 // Overridden from CoreTabHelperDelegate: | |
95 virtual void SwapTabContents(content::WebContents* old_contents, | |
96 content::WebContents* new_contents) OVERRIDE; | |
97 | |
98 // Overridden from content::WebContentsDelegate: | |
99 virtual bool ShouldSuppressDialogs() OVERRIDE; | |
100 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; | |
101 virtual void LostCapture() OVERRIDE; | |
102 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE; | |
103 virtual bool CanDownload(content::RenderViewHost* render_view_host, | |
104 int request_id, | |
105 const std::string& request_method) OVERRIDE; | |
106 virtual void HandleMouseDown() OVERRIDE; | |
107 virtual void HandleMouseUp() OVERRIDE; | |
108 virtual void HandlePointerActivate() OVERRIDE; | |
109 virtual void HandleGestureEnd() OVERRIDE; | |
110 virtual void DragEnded() OVERRIDE; | |
111 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; | |
112 virtual content::WebContents* OpenURLFromTab( | |
113 content::WebContents* source, | |
114 const content::OpenURLParams& params) OVERRIDE; | |
115 | |
116 Delegate* const delegate_; | |
117 scoped_ptr<content::WebContents> contents_; | |
118 | |
119 // The URL we will be loading. | |
120 GURL instant_url_; | |
121 | |
122 // Called when |stale_page_timer_| fires. | |
123 base::Closure on_stale_callback_; | |
124 | |
125 // Used to mark when the page is stale. | |
126 base::Timer stale_page_timer_; | |
127 | |
128 // Used to get notifications about renderers. | |
129 content::NotificationRegistrar registrar_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(InstantLoader); | |
132 }; | |
133 | |
134 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ | |
OLD | NEW |