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.
5 #ifndef CHROME_BROWSER_UI_SEARCH_INSTANT_LOADER_H_
6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_LOADER_H_
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/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"
26 // InstantLoader is used to create and maintain a WebContents where we can
27 // preload a page into. It is used by InstantNTP to preload an Instant page.
28 class InstantLoader : public content::NotificationObserver,
29 public content::WebContentsDelegate,
30 public CoreTabHelperDelegate {
32 // InstantLoader calls these methods on its delegate in response to certain
33 // changes in the underlying contents.
36 // Called after someone has swapped in a different WebContents for ours.
37 virtual void OnSwappedContents() = 0;
39 // Called to open a URL using the underlying contents (see
40 // WebContentsDelegate::OpenURLFromTab). The Delegate should return the
41 // WebContents the URL is opened in, or NULL if the URL wasn't opened
43 virtual content::WebContents* OpenURLFromTab(
44 content::WebContents* source,
45 const content::OpenURLParams& params) = 0;
47 // Called when a main frame load is complete.
48 virtual void LoadCompletedMainFrame() = 0;
54 explicit InstantLoader(Delegate* delegate);
55 virtual ~InstantLoader();
57 // Creates a new WebContents in the context of |profile| that will be used to
58 // load |instant_url|. The page is not actually loaded until Load() is
59 // called. |on_stale_callback| will be called after kStalePageTimeoutMS
60 // has elapsed after Load() being called.
61 void Init(const GURL& instant_url,
63 const base::Closure& on_stale_callback);
65 // Loads |instant_url_| in |contents_|.
68 // Returns the contents currently held. May be NULL.
69 content::WebContents* contents() const { return contents_.get(); }
71 // Replaces the contents held with |contents|. Any existing contents is
72 // deleted. The expiration timer is not restarted.
73 void SetContents(scoped_ptr<content::WebContents> contents);
75 // Releases the contents currently held. Must only be called if contents() is
77 scoped_ptr<content::WebContents> ReleaseContents();
80 // Overridden from content::NotificationObserver:
81 virtual void Observe(int type,
82 const content::NotificationSource& source,
83 const content::NotificationDetails& details) OVERRIDE;
85 // Overridden from CoreTabHelperDelegate:
86 virtual void SwapTabContents(content::WebContents* old_contents,
87 content::WebContents* new_contents) OVERRIDE;
89 // Overridden from content::WebContentsDelegate:
90 virtual bool ShouldSuppressDialogs() OVERRIDE;
91 virtual bool ShouldFocusPageAfterCrash() OVERRIDE;
92 virtual void CanDownload(content::RenderViewHost* render_view_host,
94 const std::string& request_method,
95 const base::Callback<void(bool)>& callback) OVERRIDE;
96 virtual bool OnGoToEntryOffset(int offset) OVERRIDE;
97 virtual content::WebContents* OpenURLFromTab(
98 content::WebContents* source,
99 const content::OpenURLParams& params) OVERRIDE;
101 Delegate* const delegate_;
102 scoped_ptr<content::WebContents> contents_;
104 // The URL we will be loading.
107 // Called when |stale_page_timer_| fires.
108 base::Closure on_stale_callback_;
110 // Used to mark when the page is stale.
111 base::Timer stale_page_timer_;
113 // Used to get notifications about renderers.
114 content::NotificationRegistrar registrar_;
116 DISALLOW_COPY_AND_ASSIGN(InstantLoader);
119 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_LOADER_H_