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
139
140
141
142
143
144
|
// Copyright (c) 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_INSTANT_INSTANT_LOADER_H_
#define CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_
#include <string>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "chrome/browser/instant/instant_commit_type.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
struct InstantAutocompleteResult;
class InstantLoaderDelegate;
class TabContents;
namespace content {
class NotificationDetails;
class NotificationSource;
class WebContents;
}
namespace gfx {
class Rect;
}
namespace history {
class HistoryAddPageArgs;
}
// InstantLoader is created with an "Instant URL". It loads the URL and tells
// its delegate (usually InstantController) of all interesting events. For
// example, it determines if the page actually supports the Instant API
// (http://dev.chromium.org/searchbox) and forwards messages (such as queries
// and autocomplete suggestions) between the page and the delegate.
class InstantLoader : public content::NotificationObserver {
public:
// Creates a new empty WebContents. Use Init() to actually load |instant_url|.
// |tab_contents| is the page the preview will be shown on top of and
// potentially replace. |instant_url| is typically the instant_url field of
// the default search engine's TemplateURL, with the "{searchTerms}" parameter
// replaced with an empty string.
InstantLoader(InstantLoaderDelegate* delegate,
const std::string& instant_url,
const TabContents* tab_contents);
virtual ~InstantLoader();
// Initializes |preview_contents_| and loads |instant_url_|.
void Init();
// Tells the preview page that the user typed |user_text| into the omnibox.
// If |verbatim| is false, the page predicts the query the user means to type
// and fetches results for the prediction. If |verbatim| is true, |user_text|
// is taken as the exact query (no prediction is made).
void Update(const string16& user_text, bool verbatim);
// Tells the preview page of the bounds of the omnibox dropdown (in screen
// coordinates). This is used by the page to offset the results to avoid them
// being covered by the omnibox dropdown.
void SetOmniboxBounds(const gfx::Rect& bounds);
// Tells the preview page about the available autocomplete results.
void SendAutocompleteResults(
const std::vector<InstantAutocompleteResult>& results);
// Tells the preview page that the user pressed the up or down key. |count|
// is a repeat count, negative for moving up, positive for moving down.
void OnUpOrDownKeyPressed(int count);
// Releases the preview TabContents passing ownership to the caller. This
// should be called when the preview is committed. Notifies the page but not
// the delegate. |text| is the final omnibox text being committed. NOTE: The
// caller should destroy this loader object right after this method, since
// none of the other methods will work once the preview has been released.
TabContents* ReleasePreviewContents(InstantCommitType type,
const string16& text) WARN_UNUSED_RESULT;
// The preview TabContents. The loader retains ownership. This will be
// non-NULL until ReleasePreviewContents() is called.
TabContents* preview_contents() const { return preview_contents_.get(); }
// Returns true if the preview page is known to support the Instant API. This
// starts out false, and becomes true whenever we get any message from the
// page. Once true, it never becomes false (the page isn't expected to drop
// Instant API support suddenly).
bool supports_instant() const { return supports_instant_; }
// Returns the URL that we're loading.
const std::string& instant_url() const { return instant_url_; }
// Returns info about the last navigation by the Instant page. If the page
// hasn't navigated since the last Update(), this contains NULL.
scoped_refptr<history::HistoryAddPageArgs> last_navigation() const {
return last_navigation_;
}
// Returns true if the mouse or a touch pointer is down due to activating the
// preview content.
bool IsPointerDownFromActivate() const;
// content::NotificationObserver:
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
private:
class WebContentsDelegateImpl;
void SetupPreviewContents();
void CleanupPreviewContents();
void ReplacePreviewContents(content::WebContents* old_contents,
content::WebContents* new_contents);
InstantLoaderDelegate* const loader_delegate_;
// See comments on the getter above.
scoped_ptr<TabContents> preview_contents_;
// Delegate of the preview WebContents. Used to detect when the user does some
// gesture on the WebContents and the preview needs to be activated.
scoped_ptr<WebContentsDelegateImpl> preview_delegate_;
// See comments on the getter above.
bool supports_instant_;
// See comments on the getter above.
const std::string instant_url_;
// Used to get notifications about renderers coming and going.
content::NotificationRegistrar registrar_;
// See comments on the getter above.
scoped_refptr<history::HistoryAddPageArgs> last_navigation_;
DISALLOW_COPY_AND_ASSIGN(InstantLoader);
};
#endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_
|