summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/search/instant_loader.h
blob: 251c43b4eaf3343e5701a0f8b596e2548ece54de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_SEARCH_INSTANT_LOADER_H_
#define CHROME_BROWSER_UI_SEARCH_INSTANT_LOADER_H_

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/timer.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_contents_delegate.h"

class GURL;
class Profile;

namespace content {
struct OpenURLParams;
class WebContents;
}

// InstantLoader is used to create and maintain a WebContents where we can
// preload a page into. It is used by InstantOverlay and InstantNTP to
// preload an Instant page.
class InstantLoader : public content::NotificationObserver,
                      public content::WebContentsDelegate,
                      public CoreTabHelperDelegate {
 public:
  // InstantLoader calls these methods on its delegate in response to certain
  // changes in the underlying contents.
  class Delegate {
   public:
    // Called after someone has swapped in a different WebContents for ours.
    virtual void OnSwappedContents() = 0;

    // Called when the underlying contents receive focus.
    virtual void OnFocus() = 0;

    // Called when the mouse pointer is down.
    virtual void OnMouseDown() = 0;

    // Called when the mouse pointer is released (or a drag event ends).
    virtual void OnMouseUp() = 0;

    // Called to open a URL using the underlying contents (see
    // WebContentsDelegate::OpenURLFromTab). The Delegate should return the
    // WebContents the URL is opened in, or NULL if the URL wasn't opened
    // immediately.
    virtual content::WebContents* OpenURLFromTab(
        content::WebContents* source,
        const content::OpenURLParams& params) = 0;

    // Called when a main frame load is complete.
    virtual void LoadCompletedMainFrame() = 0;

   protected:
    ~Delegate();
  };

  explicit InstantLoader(Delegate* delegate);
  virtual ~InstantLoader();

  // Creates a new WebContents in the context of |profile| that will be used to
  // load |instant_url|. The page is not actually loaded until Load() is
  // called. Uses |active_contents|, if non-NULL, to initialize the size of the
  // new contents. |on_stale_callback| will be called after kStalePageTimeoutMS
  // has elapsed after Load() being called.
  void Init(const GURL& instant_url,
            Profile* profile,
            const content::WebContents* active_contents,
            const base::Closure& on_stale_callback);

  // Loads |instant_url_| in |contents_|.
  void Load();

  // Returns the contents currently held. May be NULL.
  content::WebContents* contents() const { return contents_.get(); }

  // Replaces the contents held with |contents|. Any existing contents is
  // deleted. The expiration timer is not restarted.
  void SetContents(scoped_ptr<content::WebContents> contents);

  // Releases the contents currently held. Must only be called if contents() is
  // not NULL.
  scoped_ptr<content::WebContents> ReleaseContents() WARN_UNUSED_RESULT;

 private:
  // Overridden from content::NotificationObserver:
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  // Overridden from CoreTabHelperDelegate:
  virtual void SwapTabContents(content::WebContents* old_contents,
                               content::WebContents* new_contents) OVERRIDE;

  // Overridden from content::WebContentsDelegate:
  virtual bool ShouldSuppressDialogs() OVERRIDE;
  virtual bool ShouldFocusPageAfterCrash() OVERRIDE;
  virtual void LostCapture() OVERRIDE;
  virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE;
  virtual void CanDownload(content::RenderViewHost* render_view_host,
                           int request_id,
                           const std::string& request_method,
                           const base::Callback<void(bool)>& callback) OVERRIDE;
  virtual void HandleMouseDown() OVERRIDE;
  virtual void HandleMouseUp() OVERRIDE;
  virtual void HandlePointerActivate() OVERRIDE;
  virtual void HandleGestureEnd() OVERRIDE;
  virtual void DragEnded() OVERRIDE;
  virtual bool OnGoToEntryOffset(int offset) OVERRIDE;
  virtual content::WebContents* OpenURLFromTab(
      content::WebContents* source,
      const content::OpenURLParams& params) OVERRIDE;

  Delegate* const delegate_;
  scoped_ptr<content::WebContents> contents_;

  // The URL we will be loading.
  GURL instant_url_;

  // Called when |stale_page_timer_| fires.
  base::Closure on_stale_callback_;

  // Used to mark when the page is stale.
  base::Timer stale_page_timer_;

  // Used to get notifications about renderers.
  content::NotificationRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(InstantLoader);
};

#endif  // CHROME_BROWSER_UI_SEARCH_INSTANT_LOADER_H_