Home | History | Annotate | Download | only in search
      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_UI_SEARCH_INSTANT_LOADER_H_
      6 #define CHROME_BROWSER_UI_SEARCH_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/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 InstantNTP to preload an Instant page.
     28 class InstantLoader : public content::NotificationObserver,
     29                       public content::WebContentsDelegate,
     30                       public CoreTabHelperDelegate {
     31  public:
     32   // InstantLoader calls these methods on its delegate in response to certain
     33   // changes in the underlying contents.
     34   class Delegate {
     35    public:
     36     // Called after someone has swapped in a different WebContents for ours.
     37     virtual void OnSwappedContents() = 0;
     38 
     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
     42     // immediately.
     43     virtual content::WebContents* OpenURLFromTab(
     44         content::WebContents* source,
     45         const content::OpenURLParams& params) = 0;
     46 
     47     // Called when a main frame load is complete.
     48     virtual void LoadCompletedMainFrame() = 0;
     49 
     50    protected:
     51     ~Delegate();
     52   };
     53 
     54   explicit InstantLoader(Delegate* delegate);
     55   virtual ~InstantLoader();
     56 
     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,
     62             Profile* profile,
     63             const base::Closure& on_stale_callback);
     64 
     65   // Loads |instant_url_| in |contents_|.
     66   void Load();
     67 
     68   // Returns the contents currently held. May be NULL.
     69   content::WebContents* contents() const { return contents_.get(); }
     70 
     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);
     74 
     75   // Releases the contents currently held. Must only be called if contents() is
     76   // not NULL.
     77   scoped_ptr<content::WebContents> ReleaseContents();
     78 
     79  private:
     80   // Overridden from content::NotificationObserver:
     81   virtual void Observe(int type,
     82                        const content::NotificationSource& source,
     83                        const content::NotificationDetails& details) OVERRIDE;
     84 
     85   // Overridden from CoreTabHelperDelegate:
     86   virtual void SwapTabContents(content::WebContents* old_contents,
     87                                content::WebContents* new_contents) OVERRIDE;
     88 
     89   // Overridden from content::WebContentsDelegate:
     90   virtual bool ShouldSuppressDialogs() OVERRIDE;
     91   virtual bool ShouldFocusPageAfterCrash() OVERRIDE;
     92   virtual void CanDownload(content::RenderViewHost* render_view_host,
     93                            int request_id,
     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;
    100 
    101   Delegate* const delegate_;
    102   scoped_ptr<content::WebContents> contents_;
    103 
    104   // The URL we will be loading.
    105   GURL instant_url_;
    106 
    107   // Called when |stale_page_timer_| fires.
    108   base::Closure on_stale_callback_;
    109 
    110   // Used to mark when the page is stale.
    111   base::Timer stale_page_timer_;
    112 
    113   // Used to get notifications about renderers.
    114   content::NotificationRegistrar registrar_;
    115 
    116   DISALLOW_COPY_AND_ASSIGN(InstantLoader);
    117 };
    118 
    119 #endif  // CHROME_BROWSER_UI_SEARCH_INSTANT_LOADER_H_
    120