OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/instant/instant_overlay.h" | 5 #include "chrome/browser/instant/instant_overlay.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/supports_user_data.h" | 8 #include "chrome/browser/instant/instant_controller.h" |
9 #include "chrome/browser/ui/search/search.h" | |
10 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
11 | 10 |
12 namespace { | |
13 | |
14 int kUserDataKey; | |
15 | |
16 class InstantOverlayUserData : public base::SupportsUserData::Data { | |
17 public: | |
18 explicit InstantOverlayUserData(InstantOverlay* overlay) | |
19 : overlay_(overlay) {} | |
20 | |
21 virtual InstantOverlay* overlay() const { return overlay_; } | |
22 | |
23 private: | |
24 virtual ~InstantOverlayUserData() {} | |
25 | |
26 InstantOverlay* const overlay_; | |
27 | |
28 DISALLOW_COPY_AND_ASSIGN(InstantOverlayUserData); | |
29 }; | |
30 | |
31 } // namespace | |
32 | |
33 // static | |
34 InstantOverlay* InstantOverlay::FromWebContents( | |
35 const content::WebContents* web_contents) { | |
36 InstantOverlayUserData* data = static_cast<InstantOverlayUserData*>( | |
37 web_contents->GetUserData(&kUserDataKey)); | |
38 return data ? data->overlay() : NULL; | |
39 } | |
40 | |
41 InstantOverlay::InstantOverlay(InstantController* controller, | 11 InstantOverlay::InstantOverlay(InstantController* controller, |
42 const std::string& instant_url) | 12 InstantService* service, |
43 : InstantPage(controller), | 13 scoped_ptr<content::WebContents> contents) |
44 loader_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 14 : InstantWebContentsContainer(controller, service), |
45 instant_url_(instant_url), | 15 controller_(controller), |
46 is_stale_(false), | |
47 is_pointer_down_from_activate_(false) { | 16 is_pointer_down_from_activate_(false) { |
| 17 contents_ = contents.Pass(); |
| 18 SetUpContents(); |
| 19 page_.set_supports_instant(true); |
48 } | 20 } |
49 | 21 |
50 InstantOverlay::~InstantOverlay() { | 22 InstantOverlay::~InstantOverlay() { |
51 } | 23 } |
52 | 24 |
53 void InstantOverlay::InitContents(Profile* profile, | 25 scoped_ptr<content::WebContents> InstantOverlay::ReleaseContents() { |
54 const content::WebContents* active_tab) { | 26 TearDownContents(); |
55 loader_.Init(GURL(instant_url_), profile, active_tab, | 27 return contents_.Pass(); |
56 base::Bind(&InstantOverlay::HandleStalePage, | |
57 base::Unretained(this))); | |
58 SetContents(loader_.contents()); | |
59 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); | |
60 loader_.Load(); | |
61 } | 28 } |
62 | 29 |
63 scoped_ptr<content::WebContents> InstantOverlay::ReleaseContents() { | 30 void InstantOverlay::SwapTabContents(content::WebContents* old_contents, |
64 contents()->RemoveUserData(&kUserDataKey); | 31 content::WebContents* new_contents) { |
65 SetContents(NULL); | 32 InstantWebContentsContainer::SwapTabContents(old_contents, new_contents); |
66 return loader_.ReleaseContents(); | 33 controller_->SwappedOverlayContents(); |
67 } | |
68 | |
69 void InstantOverlay::DidNavigate( | |
70 const history::HistoryAddPageArgs& add_page_args) { | |
71 last_navigation_ = add_page_args; | |
72 } | |
73 | |
74 bool InstantOverlay::IsUsingLocalPreview() const { | |
75 return instant_url_ == chrome::search::kLocalOmniboxPopupURL; | |
76 } | |
77 | |
78 void InstantOverlay::Update(const string16& text, | |
79 size_t selection_start, | |
80 size_t selection_end, | |
81 bool verbatim) { | |
82 last_navigation_ = history::HistoryAddPageArgs(); | |
83 InstantPage::Update(text, selection_start, selection_end, verbatim); | |
84 } | |
85 | |
86 bool InstantOverlay::ShouldProcessRenderViewCreated() { | |
87 return true; | |
88 } | |
89 | |
90 bool InstantOverlay::ShouldProcessRenderViewGone() { | |
91 return true; | |
92 } | |
93 | |
94 bool InstantOverlay::ShouldProcessAboutToNavigateMainFrame() { | |
95 return true; | |
96 } | |
97 | |
98 bool InstantOverlay::ShouldProcessSetSuggestions() { | |
99 return true; | |
100 } | |
101 | |
102 bool InstantOverlay::ShouldProcessShowInstantOverlay() { | |
103 return true; | |
104 } | |
105 | |
106 bool InstantOverlay::ShouldProcessNavigateToURL() { | |
107 return true; | |
108 } | |
109 | |
110 void InstantOverlay::OnSwappedContents() { | |
111 contents()->RemoveUserData(&kUserDataKey); | |
112 SetContents(loader_.contents()); | |
113 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); | |
114 instant_controller()->SwappedOverlayContents(); | |
115 } | |
116 | |
117 void InstantOverlay::OnFocus() { | |
118 // The preview is getting focus. Equivalent to it being clicked. | |
119 base::AutoReset<bool> reset(&is_pointer_down_from_activate_, true); | |
120 instant_controller()->FocusedOverlayContents(); | |
121 } | |
122 | |
123 void InstantOverlay::OnMouseDown() { | |
124 is_pointer_down_from_activate_ = true; | |
125 } | |
126 | |
127 void InstantOverlay::OnMouseUp() { | |
128 if (is_pointer_down_from_activate_) { | |
129 is_pointer_down_from_activate_ = false; | |
130 instant_controller()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); | |
131 } | |
132 } | 34 } |
133 | 35 |
134 content::WebContents* InstantOverlay::OpenURLFromTab( | 36 content::WebContents* InstantOverlay::OpenURLFromTab( |
135 content::WebContents* source, | 37 content::WebContents* source, |
136 const content::OpenURLParams& params) { | 38 const content::OpenURLParams& params) { |
137 if (!supports_instant()) { | 39 if (!page_.supports_instant()) |
138 // If the page doesn't yet support Instant, it hasn't fully loaded. | 40 return InstantWebContentsContainer::OpenURLFromTab(source, params); |
139 // This is a redirect that we should allow. http://crbug.com/177948 | |
140 content::NavigationController::LoadURLParams load_params(params.url); | |
141 load_params.transition_type = params.transition; | |
142 load_params.referrer = params.referrer; | |
143 load_params.extra_headers = params.extra_headers; | |
144 load_params.is_renderer_initiated = params.is_renderer_initiated; | |
145 load_params.transferred_global_request_id = | |
146 params.transferred_global_request_id; | |
147 load_params.is_cross_site_redirect = params.is_cross_site_redirect; | |
148 | 41 |
149 contents()->GetController().LoadURLWithParams(load_params); | 42 // Allow the navigation to continue if we are able to commit the overlay. |
150 return contents(); | 43 // |
| 44 // Cache the overlay contents since committing it will cause the contents |
| 45 // to be released (and be set to NULL). |
| 46 content::WebContents* overlay = contents(); |
| 47 if (controller_->CommitIfPossible(INSTANT_COMMIT_NAVIGATED)) { |
| 48 // If the commit was successful, the overlay's delegate should be the tab |
| 49 // strip, which will be able to handle the navigation. |
| 50 DCHECK_NE(this, overlay->GetDelegate()); |
| 51 return overlay->GetDelegate()->OpenURLFromTab(source, params); |
151 } | 52 } |
152 | 53 |
153 // We will allow the navigate to continue if we are able to commit the | |
154 // overlay. | |
155 // | |
156 // First, cache the overlay contents since committing it will cause the | |
157 // contents to be released (and be set to NULL). | |
158 content::WebContents* overlay = contents(); | |
159 if (instant_controller()->CommitIfPossible(INSTANT_COMMIT_NAVIGATED)) { | |
160 // If the commit was successful, the overlay's delegate should be the tab | |
161 // strip, which will be able to handle the navigation. | |
162 DCHECK_NE(&loader_, overlay->GetDelegate()); | |
163 return overlay->GetDelegate()->OpenURLFromTab(source, params); | |
164 } | |
165 return NULL; | 54 return NULL; |
166 } | 55 } |
167 | 56 |
168 void InstantOverlay::HandleStalePage() { | 57 void InstantOverlay::CloseContents(content::WebContents* /* source */) { |
169 is_stale_ = true; | 58 controller_->RenderViewGone(contents()); |
170 instant_controller()->ReloadOverlayIfStale(); | |
171 } | 59 } |
| 60 |
| 61 void InstantOverlay::LostCapture() { |
| 62 HandleMouseUp(); |
| 63 } |
| 64 |
| 65 void InstantOverlay::WebContentsFocused(content::WebContents* /* contents */) { |
| 66 // The overlay is getting focus. Equivalent to it being clicked. |
| 67 base::AutoReset<bool> reset(&is_pointer_down_from_activate_, true); |
| 68 controller_->FocusedOverlayContents(); |
| 69 } |
| 70 |
| 71 void InstantOverlay::HandleMouseDown() { |
| 72 is_pointer_down_from_activate_ = true; |
| 73 } |
| 74 |
| 75 void InstantOverlay::HandleMouseUp() { |
| 76 if (is_pointer_down_from_activate_) { |
| 77 is_pointer_down_from_activate_ = false; |
| 78 controller_->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); |
| 79 } |
| 80 } |
| 81 |
| 82 void InstantOverlay::HandlePointerActivate() { |
| 83 HandleMouseDown(); |
| 84 } |
| 85 |
| 86 void InstantOverlay::HandleGestureEnd() { |
| 87 HandleMouseUp(); |
| 88 } |
| 89 |
| 90 void InstantOverlay::DragEnded() { |
| 91 HandleMouseUp(); |
| 92 } |
OLD | NEW |