| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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_loader.h" | 5 #include "chrome/browser/instant/instant_loader.h" |
| 6 | 6 |
| 7 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 7 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 8 #include "chrome/browser/instant/instant_controller.h" | 8 #include "chrome/browser/instant/instant_controller.h" |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h" | 9 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h" |
| 11 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | 10 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
| 12 #include "chrome/browser/ui/constrained_window_tab_helper.h" | 11 #include "chrome/browser/ui/constrained_window_tab_helper.h" |
| 13 #include "chrome/browser/ui/constrained_window_tab_helper_delegate.h" | 12 #include "chrome/browser/ui/constrained_window_tab_helper_delegate.h" |
| 14 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | 13 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
| 15 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" | 14 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 15 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 17 #include "chrome/common/render_messages.h" | |
| 18 #include "content/public/browser/navigation_entry.h" | |
| 19 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
| 21 #include "content/public/browser/render_view_host.h" | |
| 22 #include "content/public/browser/render_widget_host_view.h" | 18 #include "content/public/browser/render_widget_host_view.h" |
| 23 #include "content/public/browser/web_contents_delegate.h" | 19 #include "content/public/browser/web_contents_delegate.h" |
| 20 #include "ipc/ipc_message.h" |
| 24 | 21 |
| 25 namespace { | 22 namespace { |
| 26 | 23 |
| 27 int kUserDataKey; | 24 int kUserDataKey; |
| 28 | 25 |
| 29 class InstantLoaderUserData : public base::SupportsUserData::Data { | 26 class InstantLoaderUserData : public base::SupportsUserData::Data { |
| 30 public: | 27 public: |
| 31 explicit InstantLoaderUserData(InstantLoader* loader) : loader_(loader) {} | 28 explicit InstantLoaderUserData(InstantLoader* loader) : loader_(loader) {} |
| 32 | 29 |
| 33 InstantLoader* loader() const { return loader_; } | 30 InstantLoader* loader() const { return loader_; } |
| 34 | 31 |
| 35 private: | 32 private: |
| 36 ~InstantLoaderUserData() {} | 33 ~InstantLoaderUserData() {} |
| 37 | 34 |
| 38 InstantLoader* const loader_; | 35 InstantLoader* const loader_; |
| 39 | 36 |
| 40 DISALLOW_COPY_AND_ASSIGN(InstantLoaderUserData); | 37 DISALLOW_COPY_AND_ASSIGN(InstantLoaderUserData); |
| 41 }; | 38 }; |
| 42 | 39 |
| 43 } | 40 } |
| 44 | 41 |
| 45 // WebContentsDelegateImpl ----------------------------------------------------- | 42 // WebContentsDelegateImpl ----------------------------------------------------- |
| 46 | 43 |
| 47 class InstantLoader::WebContentsDelegateImpl | 44 class InstantLoader::WebContentsDelegateImpl |
| 48 : public ConstrainedWindowTabHelperDelegate, | 45 : public ConstrainedWindowTabHelperDelegate, |
| 49 public CoreTabHelperDelegate, | 46 public CoreTabHelperDelegate, |
| 50 public content::WebContentsDelegate, | 47 public content::WebContentsDelegate { |
| 51 public content::WebContentsObserver { | |
| 52 public: | 48 public: |
| 53 explicit WebContentsDelegateImpl(InstantLoader* loader); | 49 explicit WebContentsDelegateImpl(InstantLoader* loader); |
| 54 | 50 |
| 55 bool is_pointer_down_from_activate() const { | |
| 56 return is_pointer_down_from_activate_; | |
| 57 } | |
| 58 | |
| 59 // Start observing |web_contents| instead of whatever is currently being | |
| 60 // observed. If |web_contents| is NULL, effectively stops observing. | |
| 61 void ObserveContents(content::WebContents* web_contents); | |
| 62 | |
| 63 private: | 51 private: |
| 64 // Overridden from ConstrainedWindowTabHelperDelegate: | 52 // Overridden from ConstrainedWindowTabHelperDelegate: |
| 65 virtual bool ShouldFocusConstrainedWindow() OVERRIDE; | 53 virtual bool ShouldFocusConstrainedWindow() OVERRIDE; |
| 66 | 54 |
| 67 // Overridden from CoreTabHelperDelegate: | 55 // Overridden from CoreTabHelperDelegate: |
| 68 virtual void SwapTabContents(content::WebContents* old_contents, | 56 virtual void SwapTabContents(content::WebContents* old_contents, |
| 69 content::WebContents* new_contents) OVERRIDE; | 57 content::WebContents* new_contents) OVERRIDE; |
| 70 | 58 |
| 71 // Overridden from content::WebContentsDelegate: | 59 // Overridden from content::WebContentsDelegate: |
| 72 virtual bool ShouldSuppressDialogs() OVERRIDE; | 60 virtual bool ShouldSuppressDialogs() OVERRIDE; |
| 73 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; | 61 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; |
| 74 virtual void LostCapture() OVERRIDE; | 62 virtual void LostCapture() OVERRIDE; |
| 75 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE; | 63 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE; |
| 76 virtual bool CanDownload(content::RenderViewHost* render_view_host, | 64 virtual bool CanDownload(content::RenderViewHost* render_view_host, |
| 77 int request_id, | 65 int request_id, |
| 78 const std::string& request_method) OVERRIDE; | 66 const std::string& request_method) OVERRIDE; |
| 79 virtual void HandleMouseDown() OVERRIDE; | 67 virtual void HandleMouseDown() OVERRIDE; |
| 80 virtual void HandleMouseUp() OVERRIDE; | 68 virtual void HandleMouseUp() OVERRIDE; |
| 81 virtual void HandlePointerActivate() OVERRIDE; | 69 virtual void HandlePointerActivate() OVERRIDE; |
| 82 virtual void HandleGestureEnd() OVERRIDE; | 70 virtual void HandleGestureEnd() OVERRIDE; |
| 83 virtual void DragEnded() OVERRIDE; | 71 virtual void DragEnded() OVERRIDE; |
| 84 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; | 72 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; |
| 85 | 73 |
| 86 // Overridden from content::WebContentsObserver: | 74 void MaybeCommitFromPointerRelease(); |
| 87 virtual void DidFinishLoad( | |
| 88 int64 frame_id, | |
| 89 const GURL& validated_url, | |
| 90 bool is_main_frame, | |
| 91 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 92 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 93 | |
| 94 // Message from renderer indicating the page has suggestions. | |
| 95 void OnSetSuggestions(int page_id, | |
| 96 const std::vector<InstantSuggestion>& suggestions); | |
| 97 | |
| 98 // Message from the renderer determining whether it supports the Instant API. | |
| 99 void OnInstantSupportDetermined(int page_id, bool result); | |
| 100 | |
| 101 // Message from the renderer requesting the preview be shown. | |
| 102 void OnShowInstantPreview(int page_id, | |
| 103 InstantShownReason reason, | |
| 104 int height, | |
| 105 InstantSizeUnits units); | |
| 106 | |
| 107 void CommitFromPointerReleaseIfNecessary(); | |
| 108 void MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant); | |
| 109 | 75 |
| 110 InstantLoader* const loader_; | 76 InstantLoader* const loader_; |
| 111 | 77 |
| 112 // True if the mouse or a touch pointer is down from an activate. | |
| 113 bool is_pointer_down_from_activate_; | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); | 78 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); |
| 116 }; | 79 }; |
| 117 | 80 |
| 118 InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl( | 81 InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl( |
| 119 InstantLoader* loader) | 82 InstantLoader* loader) |
| 120 : loader_(loader), | 83 : loader_(loader) { |
| 121 is_pointer_down_from_activate_(false) { | |
| 122 } | |
| 123 | |
| 124 void InstantLoader::WebContentsDelegateImpl::ObserveContents( | |
| 125 content::WebContents* web_contents) { | |
| 126 Observe(web_contents); | |
| 127 } | 84 } |
| 128 | 85 |
| 129 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() { | 86 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() { |
| 130 // Return false so that constrained windows are not initially focused. If we | 87 // Return false so that constrained windows are not initially focused. If we |
| 131 // did otherwise the preview would prematurely get committed when focus goes | 88 // did otherwise the preview would prematurely get committed when focus goes |
| 132 // to the constrained window. | 89 // to the constrained window. |
| 133 return false; | 90 return false; |
| 134 } | 91 } |
| 135 | 92 |
| 136 void InstantLoader::WebContentsDelegateImpl::SwapTabContents( | 93 void InstantLoader::WebContentsDelegateImpl::SwapTabContents( |
| 137 content::WebContents* old_contents, | 94 content::WebContents* old_contents, |
| 138 content::WebContents* new_contents) { | 95 content::WebContents* new_contents) { |
| 139 // If this is being called, something is swapping in to our | 96 // If this is being called, something is swapping in to loader's |contents_| |
| 140 // |preview_contents_| before we've added it to the tab strip. | 97 // before we've added it to the tab strip. |
| 141 loader_->ReplacePreviewContents(old_contents, new_contents); | 98 loader_->ReplacePreviewContents(old_contents, new_contents); |
| 142 } | 99 } |
| 143 | 100 |
| 144 bool InstantLoader::WebContentsDelegateImpl::ShouldSuppressDialogs() { | 101 bool InstantLoader::WebContentsDelegateImpl::ShouldSuppressDialogs() { |
| 145 // Any message shown during Instant cancels Instant, so we suppress them. | 102 // Any message shown during Instant cancels Instant, so we suppress them. |
| 146 return true; | 103 return true; |
| 147 } | 104 } |
| 148 | 105 |
| 149 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusPageAfterCrash() { | 106 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusPageAfterCrash() { |
| 150 return false; | 107 return false; |
| 151 } | 108 } |
| 152 | 109 |
| 153 void InstantLoader::WebContentsDelegateImpl::LostCapture() { | 110 void InstantLoader::WebContentsDelegateImpl::LostCapture() { |
| 154 CommitFromPointerReleaseIfNecessary(); | 111 MaybeCommitFromPointerRelease(); |
| 155 } | 112 } |
| 156 | 113 |
| 157 void InstantLoader::WebContentsDelegateImpl::WebContentsFocused( | 114 void InstantLoader::WebContentsDelegateImpl::WebContentsFocused( |
| 158 content::WebContents* contents) { | 115 content::WebContents* /* contents */) { |
| 159 loader_->controller_->InstantLoaderContentsFocused(loader_); | 116 loader_->controller_->InstantLoaderContentsFocused(); |
| 160 } | 117 } |
| 161 | 118 |
| 162 bool InstantLoader::WebContentsDelegateImpl::CanDownload( | 119 bool InstantLoader::WebContentsDelegateImpl::CanDownload( |
| 163 content::RenderViewHost* render_view_host, | 120 content::RenderViewHost* /* render_view_host */, |
| 164 int request_id, | 121 int /* request_id */, |
| 165 const std::string& request_method) { | 122 const std::string& /* request_method */) { |
| 166 // Downloads are disabled. | 123 // Downloads are disabled. |
| 167 return false; | 124 return false; |
| 168 } | 125 } |
| 169 | 126 |
| 170 void InstantLoader::WebContentsDelegateImpl::HandleMouseDown() { | 127 void InstantLoader::WebContentsDelegateImpl::HandleMouseDown() { |
| 171 is_pointer_down_from_activate_ = true; | 128 loader_->is_pointer_down_from_activate_ = true; |
| 172 } | 129 } |
| 173 | 130 |
| 174 void InstantLoader::WebContentsDelegateImpl::HandleMouseUp() { | 131 void InstantLoader::WebContentsDelegateImpl::HandleMouseUp() { |
| 175 CommitFromPointerReleaseIfNecessary(); | 132 MaybeCommitFromPointerRelease(); |
| 176 } | 133 } |
| 177 | 134 |
| 178 void InstantLoader::WebContentsDelegateImpl::HandlePointerActivate() { | 135 void InstantLoader::WebContentsDelegateImpl::HandlePointerActivate() { |
| 179 is_pointer_down_from_activate_ = true; | 136 loader_->is_pointer_down_from_activate_ = true; |
| 180 } | 137 } |
| 181 | 138 |
| 182 void InstantLoader::WebContentsDelegateImpl::HandleGestureEnd() { | 139 void InstantLoader::WebContentsDelegateImpl::HandleGestureEnd() { |
| 183 CommitFromPointerReleaseIfNecessary(); | 140 MaybeCommitFromPointerRelease(); |
| 184 } | 141 } |
| 185 | 142 |
| 186 void InstantLoader::WebContentsDelegateImpl::DragEnded() { | 143 void InstantLoader::WebContentsDelegateImpl::DragEnded() { |
| 187 // If the user drags, we won't get a mouse up (at least on Linux). Commit the | 144 // If the user drags, we won't get a mouse up (at least on Linux). Commit the |
| 188 // Instant result when the drag ends, so that during the drag the page won't | 145 // Instant result when the drag ends, so that during the drag the page won't |
| 189 // move around. | 146 // move around. |
| 190 CommitFromPointerReleaseIfNecessary(); | 147 MaybeCommitFromPointerRelease(); |
| 191 } | 148 } |
| 192 | 149 |
| 193 bool InstantLoader::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) { | 150 bool InstantLoader::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) { |
| 194 return false; | 151 return false; |
| 195 } | 152 } |
| 196 | 153 |
| 197 void InstantLoader::WebContentsDelegateImpl::DidFinishLoad( | 154 void InstantLoader::WebContentsDelegateImpl::MaybeCommitFromPointerRelease() { |
| 198 int64 frame_id, | 155 if (loader_->is_pointer_down_from_activate_) { |
| 199 const GURL& validated_url, | 156 loader_->is_pointer_down_from_activate_ = false; |
| 200 bool is_main_frame, | 157 loader_->controller_->CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST); |
| 201 content::RenderViewHost* render_view_host) { | |
| 202 if (is_main_frame && !loader_->supports_instant_) | |
| 203 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
| 204 } | |
| 205 | |
| 206 bool InstantLoader::WebContentsDelegateImpl::OnMessageReceived( | |
| 207 const IPC::Message& message) { | |
| 208 bool handled = true; | |
| 209 IPC_BEGIN_MESSAGE_MAP(WebContentsDelegateImpl, message) | |
| 210 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | |
| 211 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
| 212 OnInstantSupportDetermined) | |
| 213 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantPreview, | |
| 214 OnShowInstantPreview); | |
| 215 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 216 IPC_END_MESSAGE_MAP() | |
| 217 return handled; | |
| 218 } | |
| 219 | |
| 220 void InstantLoader::WebContentsDelegateImpl::OnSetSuggestions( | |
| 221 int page_id, | |
| 222 const std::vector<InstantSuggestion>& suggestions) { | |
| 223 DCHECK(loader_->preview_contents()); | |
| 224 DCHECK(loader_->preview_contents_->web_contents()); | |
| 225 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()-> | |
| 226 GetController().GetActiveEntry(); | |
| 227 if (entry && page_id == entry->GetPageID()) { | |
| 228 MaybeSetAndNotifyInstantSupportDetermined(true); | |
| 229 loader_->controller_->SetSuggestions(loader_, suggestions); | |
| 230 } | 158 } |
| 231 } | 159 } |
| 232 | 160 |
| 233 void InstantLoader::WebContentsDelegateImpl::OnInstantSupportDetermined( | |
| 234 int page_id, | |
| 235 bool result) { | |
| 236 DCHECK(loader_->preview_contents()); | |
| 237 DCHECK(loader_->preview_contents_->web_contents()); | |
| 238 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()-> | |
| 239 GetController().GetActiveEntry(); | |
| 240 if (entry && page_id == entry->GetPageID()) | |
| 241 MaybeSetAndNotifyInstantSupportDetermined(result); | |
| 242 } | |
| 243 | |
| 244 void InstantLoader::WebContentsDelegateImpl::OnShowInstantPreview( | |
| 245 int page_id, | |
| 246 InstantShownReason reason, | |
| 247 int height, | |
| 248 InstantSizeUnits units) { | |
| 249 DCHECK(loader_->preview_contents()); | |
| 250 DCHECK(loader_->preview_contents_->web_contents()); | |
| 251 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()-> | |
| 252 GetController().GetActiveEntry(); | |
| 253 if (entry && page_id == entry->GetPageID()) { | |
| 254 MaybeSetAndNotifyInstantSupportDetermined(true); | |
| 255 loader_->controller_->ShowInstantPreview(loader_, reason, height, units); | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 void InstantLoader::WebContentsDelegateImpl | |
| 260 ::CommitFromPointerReleaseIfNecessary() { | |
| 261 if (is_pointer_down_from_activate_) { | |
| 262 is_pointer_down_from_activate_ = false; | |
| 263 loader_->controller_->CommitInstantLoader(loader_); | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 void InstantLoader::WebContentsDelegateImpl | |
| 268 ::MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant) { | |
| 269 // If we already determined that the loader supports Instant, nothing to do. | |
| 270 if (!loader_->supports_instant_) { | |
| 271 loader_->supports_instant_ = supports_instant; | |
| 272 loader_->controller_->InstantSupportDetermined(loader_, supports_instant); | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 // InstantLoader --------------------------------------------------------------- | 161 // InstantLoader --------------------------------------------------------------- |
| 277 | 162 |
| 278 // static | 163 // static |
| 279 InstantLoader* InstantLoader::FromWebContents( | 164 InstantLoader* InstantLoader::FromWebContents( |
| 280 content::WebContents* web_contents) { | 165 const content::WebContents* web_contents) { |
| 281 InstantLoaderUserData* data = static_cast<InstantLoaderUserData*>( | 166 InstantLoaderUserData* data = static_cast<InstantLoaderUserData*>( |
| 282 web_contents->GetUserData(&kUserDataKey)); | 167 web_contents->GetUserData(&kUserDataKey)); |
| 283 return data ? data->loader() : NULL; | 168 return data ? data->loader() : NULL; |
| 284 } | 169 } |
| 285 | 170 |
| 286 InstantLoader::InstantLoader(InstantController* controller, | 171 InstantLoader::InstantLoader(InstantController* controller, |
| 287 const std::string& instant_url, | 172 const std::string& instant_url) |
| 288 const TabContents* tab_contents) | 173 : client_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 289 : controller_(controller), | 174 controller_(controller), |
| 290 preview_delegate_(new WebContentsDelegateImpl( | 175 delegate_(new WebContentsDelegateImpl( |
| 291 ALLOW_THIS_IN_INITIALIZER_LIST(this))), | 176 ALLOW_THIS_IN_INITIALIZER_LIST(this))), |
| 292 preview_contents_( | 177 instant_url_(instant_url), |
| 293 TabContents::Factory::CreateTabContents( | |
| 294 content::WebContents::CreateWithSessionStorage( | |
| 295 tab_contents->profile(), NULL, MSG_ROUTING_NONE, | |
| 296 tab_contents->web_contents(), | |
| 297 tab_contents->web_contents()->GetController(). | |
| 298 GetSessionStorageNamespaceMap()))), | |
| 299 supports_instant_(false), | 178 supports_instant_(false), |
| 300 instant_url_(instant_url) { | 179 is_pointer_down_from_activate_(false) { |
| 301 } | 180 } |
| 302 | 181 |
| 303 InstantLoader::~InstantLoader() { | 182 InstantLoader::~InstantLoader() { |
| 304 } | 183 } |
| 305 | 184 |
| 306 void InstantLoader::Init() { | 185 void InstantLoader::InitContents(const content::WebContents* active_tab) { |
| 186 contents_.reset(content::WebContents::CreateWithSessionStorage( |
| 187 active_tab->GetBrowserContext(), NULL, MSG_ROUTING_NONE, active_tab, |
| 188 active_tab->GetController().GetSessionStorageNamespaceMap())); |
| 189 // Not a leak. TabContents will delete itself when the WebContents is gone. |
| 190 TabContents::Factory::CreateTabContents(contents()); |
| 307 SetupPreviewContents(); | 191 SetupPreviewContents(); |
| 308 // This HTTP header and value are set on loads that originate from instant. | 192 |
| 193 // This HTTP header and value are set on loads that originate from Instant. |
| 309 const char kInstantHeader[] = "X-Purpose: Instant"; | 194 const char kInstantHeader[] = "X-Purpose: Instant"; |
| 310 DVLOG(1) << "LoadURL: " << instant_url_; | 195 DVLOG(1) << "LoadURL: " << instant_url_; |
| 311 preview_contents_->web_contents()->GetController().LoadURL(GURL(instant_url_), | 196 contents_->GetController().LoadURL(GURL(instant_url_), content::Referrer(), |
| 312 content::Referrer(), content::PAGE_TRANSITION_GENERATED, kInstantHeader); | 197 content::PAGE_TRANSITION_GENERATED, kInstantHeader); |
| 313 preview_contents_->web_contents()->WasHidden(); | 198 contents_->WasHidden(); |
| 314 } | 199 } |
| 315 | 200 |
| 316 void InstantLoader::Update(const string16& user_text, bool verbatim) { | 201 content::WebContents* InstantLoader::ReleaseContents() { |
| 317 // TODO: Support real cursor position. | 202 CleanupPreviewContents(); |
| 318 last_navigation_ = history::HistoryAddPageArgs(); | 203 return contents_.release(); |
| 319 content::RenderViewHost* rvh = | |
| 320 preview_contents_->web_contents()->GetRenderViewHost(); | |
| 321 rvh->Send(new ChromeViewMsg_SearchBoxChange(rvh->GetRoutingID(), user_text, | |
| 322 verbatim, user_text.size(), user_text.size())); | |
| 323 } | |
| 324 | |
| 325 void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) { | |
| 326 content::RenderViewHost* rvh = | |
| 327 preview_contents_->web_contents()->GetRenderViewHost(); | |
| 328 rvh->Send(new ChromeViewMsg_SearchBoxResize(rvh->GetRoutingID(), bounds)); | |
| 329 } | |
| 330 | |
| 331 void InstantLoader::SendAutocompleteResults( | |
| 332 const std::vector<InstantAutocompleteResult>& results) { | |
| 333 content::RenderViewHost* rvh = | |
| 334 preview_contents_->web_contents()->GetRenderViewHost(); | |
| 335 rvh->Send(new ChromeViewMsg_SearchBoxAutocompleteResults(rvh->GetRoutingID(), | |
| 336 results)); | |
| 337 } | |
| 338 | |
| 339 void InstantLoader::SendThemeBackgroundInfo( | |
| 340 const ThemeBackgroundInfo& theme_info) { | |
| 341 content::RenderViewHost* rvh = | |
| 342 preview_contents_->web_contents()->GetRenderViewHost(); | |
| 343 rvh->Send(new ChromeViewMsg_SearchBoxThemeChanged(rvh->GetRoutingID(), | |
| 344 theme_info)); | |
| 345 } | |
| 346 | |
| 347 void InstantLoader::SendThemeAreaHeight(int height) { | |
| 348 content::RenderViewHost* rvh = | |
| 349 preview_contents_->web_contents()->GetRenderViewHost(); | |
| 350 rvh->Send(new ChromeViewMsg_SearchBoxThemeAreaHeightChanged( | |
| 351 rvh->GetRoutingID(), height)); | |
| 352 } | |
| 353 | |
| 354 void InstantLoader::SetDisplayInstantResults(bool display_instant_results) { | |
| 355 content::RenderViewHost* rvh = | |
| 356 preview_contents_->web_contents()->GetRenderViewHost(); | |
| 357 rvh->Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults( | |
| 358 rvh->GetRoutingID(), display_instant_results)); | |
| 359 } | |
| 360 | |
| 361 void InstantLoader::OnUpOrDownKeyPressed(int count) { | |
| 362 content::RenderViewHost* rvh = | |
| 363 preview_contents_->web_contents()->GetRenderViewHost(); | |
| 364 rvh->Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(rvh->GetRoutingID(), | |
| 365 count)); | |
| 366 } | |
| 367 | |
| 368 void InstantLoader::SearchModeChanged(const chrome::search::Mode& mode) { | |
| 369 content::RenderViewHost* rvh = | |
| 370 preview_contents_->web_contents()->GetRenderViewHost(); | |
| 371 rvh->Send(new ChromeViewMsg_SearchBoxModeChanged(rvh->GetRoutingID(), mode)); | |
| 372 } | 204 } |
| 373 | 205 |
| 374 void InstantLoader::DidNavigate( | 206 void InstantLoader::DidNavigate( |
| 375 const history::HistoryAddPageArgs& add_page_args) { | 207 const history::HistoryAddPageArgs& add_page_args) { |
| 376 last_navigation_ = add_page_args; | 208 last_navigation_ = add_page_args; |
| 377 } | 209 } |
| 378 | 210 |
| 379 TabContents* InstantLoader::ReleasePreviewContents(InstantCommitType type, | 211 void InstantLoader::Update(const string16& text, |
| 380 const string16& text) { | 212 size_t selection_start, |
| 381 content::RenderViewHost* rvh = | 213 size_t selection_end, |
| 382 preview_contents_->web_contents()->GetRenderViewHost(); | 214 bool verbatim) { |
| 383 if (type == INSTANT_COMMIT_FOCUS_LOST) | 215 last_navigation_ = history::HistoryAddPageArgs(); |
| 384 rvh->Send(new ChromeViewMsg_SearchBoxCancel(rvh->GetRoutingID(), text)); | 216 client_.Update(text, selection_start, selection_end, verbatim); |
| 385 else | |
| 386 rvh->Send(new ChromeViewMsg_SearchBoxSubmit(rvh->GetRoutingID(), text)); | |
| 387 CleanupPreviewContents(); | |
| 388 return preview_contents_.release(); | |
| 389 } | 217 } |
| 390 | 218 |
| 391 void InstantLoader::CleanupPreviewContents() { | 219 void InstantLoader::Submit(const string16& text) { |
| 392 content::WebContents* old_contents = preview_contents_->web_contents(); | 220 client_.Submit(text); |
| 393 old_contents->RemoveUserData(&kUserDataKey); | |
| 394 old_contents->SetDelegate(NULL); | |
| 395 preview_delegate_->ObserveContents(NULL); | |
| 396 | |
| 397 BlockedContentTabHelper::FromWebContents(old_contents)-> | |
| 398 SetAllContentsBlocked(false); | |
| 399 ConstrainedWindowTabHelper::FromWebContents(old_contents)->set_delegate(NULL); | |
| 400 TabSpecificContentSettings::FromWebContents(old_contents)-> | |
| 401 SetPopupsBlocked(false); | |
| 402 CoreTabHelper::FromWebContents(old_contents)->set_delegate(NULL); | |
| 403 if (ThumbnailTabHelper* thumbnail_tab_helper = | |
| 404 ThumbnailTabHelper::FromWebContents(old_contents)) | |
| 405 thumbnail_tab_helper->set_enabled(true); | |
| 406 | |
| 407 #if defined(OS_MACOSX) | |
| 408 if (content::RenderWidgetHostView* rwhv = | |
| 409 old_contents->GetRenderWidgetHostView()) | |
| 410 rwhv->SetTakesFocusOnlyOnMouseDown(false); | |
| 411 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | |
| 412 content::Source<content::NavigationController>( | |
| 413 &old_contents->GetController())); | |
| 414 #endif | |
| 415 } | 221 } |
| 416 | 222 |
| 417 bool InstantLoader::IsPointerDownFromActivate() const { | 223 void InstantLoader::Cancel(const string16& text) { |
| 418 return preview_delegate_->is_pointer_down_from_activate(); | 224 client_.Cancel(text); |
| 225 } |
| 226 |
| 227 void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) { |
| 228 client_.SetOmniboxBounds(bounds); |
| 229 } |
| 230 |
| 231 void InstantLoader::SendAutocompleteResults( |
| 232 const std::vector<InstantAutocompleteResult>& results) { |
| 233 client_.SendAutocompleteResults(results); |
| 234 } |
| 235 |
| 236 void InstantLoader::UpOrDownKeyPressed(int count) { |
| 237 client_.UpOrDownKeyPressed(count); |
| 238 } |
| 239 |
| 240 void InstantLoader::SearchModeChanged(const chrome::search::Mode& mode) { |
| 241 client_.SearchModeChanged(mode); |
| 242 } |
| 243 |
| 244 void InstantLoader::SendThemeBackgroundInfo( |
| 245 const ThemeBackgroundInfo& theme_info) { |
| 246 client_.SendThemeBackgroundInfo(theme_info); |
| 247 } |
| 248 |
| 249 void InstantLoader::SendThemeAreaHeight(int height) { |
| 250 client_.SendThemeAreaHeight(height); |
| 251 } |
| 252 |
| 253 void InstantLoader::SetDisplayInstantResults(bool display_instant_results) { |
| 254 client_.SetDisplayInstantResults(display_instant_results); |
| 255 } |
| 256 |
| 257 void InstantLoader::SetSuggestions( |
| 258 const std::vector<InstantSuggestion>& suggestions) { |
| 259 InstantSupportDetermined(true); |
| 260 controller_->SetSuggestions(contents(), suggestions); |
| 261 } |
| 262 |
| 263 void InstantLoader::InstantSupportDetermined(bool supports_instant) { |
| 264 // If we had already determined that the page supports Instant, nothing to do. |
| 265 if (supports_instant_) |
| 266 return; |
| 267 |
| 268 supports_instant_ = supports_instant; |
| 269 controller_->InstantSupportDetermined(contents(), supports_instant); |
| 270 } |
| 271 |
| 272 void InstantLoader::ShowInstantPreview(InstantShownReason reason, |
| 273 int height, |
| 274 InstantSizeUnits units) { |
| 275 InstantSupportDetermined(true); |
| 276 controller_->ShowInstantPreview(reason, height, units); |
| 419 } | 277 } |
| 420 | 278 |
| 421 void InstantLoader::Observe(int type, | 279 void InstantLoader::Observe(int type, |
| 422 const content::NotificationSource& source, | 280 const content::NotificationSource& source, |
| 423 const content::NotificationDetails& details) { | 281 const content::NotificationDetails& details) { |
| 424 #if defined(OS_MACOSX) | 282 #if defined(OS_MACOSX) |
| 425 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) { | 283 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) { |
| 426 if (content::RenderWidgetHostView* rwhv = | 284 if (content::RenderWidgetHostView* rwhv = |
| 427 preview_contents_->web_contents()->GetRenderWidgetHostView()) | 285 contents_->GetRenderWidgetHostView()) |
| 428 rwhv->SetTakesFocusOnlyOnMouseDown(true); | 286 rwhv->SetTakesFocusOnlyOnMouseDown(true); |
| 429 return; | 287 return; |
| 430 } | 288 } |
| 431 NOTREACHED(); | 289 NOTREACHED(); |
| 432 #endif | 290 #endif |
| 433 } | 291 } |
| 434 | 292 |
| 435 void InstantLoader::SetupPreviewContents() { | 293 void InstantLoader::SetupPreviewContents() { |
| 436 content::WebContents* new_contents = preview_contents_->web_contents(); | 294 client_.SetContents(contents()); |
| 437 new_contents->SetUserData(&kUserDataKey, new InstantLoaderUserData(this)); | 295 contents_->SetUserData(&kUserDataKey, new InstantLoaderUserData(this)); |
| 438 WebContentsDelegateImpl* new_delegate = preview_delegate_.get(); | 296 contents_->SetDelegate(delegate_.get()); |
| 439 new_contents->SetDelegate(new_delegate); | |
| 440 new_delegate->ObserveContents(new_contents); | |
| 441 | 297 |
| 442 // Disable popups and such (mainly to avoid losing focus and reverting the | 298 // Disable popups and such (mainly to avoid losing focus and reverting the |
| 443 // preview prematurely). | 299 // preview prematurely). |
| 444 BlockedContentTabHelper::FromWebContents(new_contents)-> | 300 if (BlockedContentTabHelper* blocked_content_tab_helper = |
| 445 SetAllContentsBlocked(true); | 301 BlockedContentTabHelper::FromWebContents(contents())) |
| 446 ConstrainedWindowTabHelper::FromWebContents(new_contents)-> | 302 blocked_content_tab_helper->SetAllContentsBlocked(true); |
| 447 set_delegate(new_delegate); | 303 if (ConstrainedWindowTabHelper* constrained_window_tab_helper = |
| 448 TabSpecificContentSettings::FromWebContents(new_contents)-> | 304 ConstrainedWindowTabHelper::FromWebContents(contents())) |
| 449 SetPopupsBlocked(true); | 305 constrained_window_tab_helper->set_delegate(delegate_.get()); |
| 450 CoreTabHelper::FromWebContents(new_contents)->set_delegate(new_delegate); | 306 if (TabSpecificContentSettings* tab_specific_content_settings = |
| 307 TabSpecificContentSettings::FromWebContents(contents())) |
| 308 tab_specific_content_settings->SetPopupsBlocked(true); |
| 309 if (CoreTabHelper* core_tab_helper = |
| 310 CoreTabHelper::FromWebContents(contents())) |
| 311 core_tab_helper->set_delegate(delegate_.get()); |
| 451 if (ThumbnailTabHelper* thumbnail_tab_helper = | 312 if (ThumbnailTabHelper* thumbnail_tab_helper = |
| 452 ThumbnailTabHelper::FromWebContents(new_contents)) | 313 ThumbnailTabHelper::FromWebContents(contents())) |
| 453 thumbnail_tab_helper->set_enabled(false); | 314 thumbnail_tab_helper->set_enabled(false); |
| 454 | 315 |
| 455 #if defined(OS_MACOSX) | 316 #if defined(OS_MACOSX) |
| 456 // If |preview_contents_| does not currently have a RWHV, we will call | 317 // If |contents_| doesn't yet have a RWHV, SetTakesFocusOnlyOnMouseDown() will |
| 457 // SetTakesFocusOnlyOnMouseDown() as a result of the RENDER_VIEW_HOST_CHANGED | 318 // be called later, when NOTIFICATION_RENDER_VIEW_HOST_CHANGED is received. |
| 458 // notification. | |
| 459 if (content::RenderWidgetHostView* rwhv = | 319 if (content::RenderWidgetHostView* rwhv = |
| 460 new_contents->GetRenderWidgetHostView()) | 320 contents_->GetRenderWidgetHostView()) |
| 461 rwhv->SetTakesFocusOnlyOnMouseDown(true); | 321 rwhv->SetTakesFocusOnlyOnMouseDown(true); |
| 462 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | 322 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, |
| 463 content::Source<content::NavigationController>( | 323 content::Source<content::NavigationController>( |
| 464 &new_contents->GetController())); | 324 &contents_->GetController())); |
| 325 #endif |
| 326 } |
| 327 |
| 328 void InstantLoader::CleanupPreviewContents() { |
| 329 client_.SetContents(NULL); |
| 330 contents_->RemoveUserData(&kUserDataKey); |
| 331 contents_->SetDelegate(NULL); |
| 332 |
| 333 if (BlockedContentTabHelper* blocked_content_tab_helper = |
| 334 BlockedContentTabHelper::FromWebContents(contents())) |
| 335 blocked_content_tab_helper->SetAllContentsBlocked(false); |
| 336 if (ConstrainedWindowTabHelper* constrained_window_tab_helper = |
| 337 ConstrainedWindowTabHelper::FromWebContents(contents())) |
| 338 constrained_window_tab_helper->set_delegate(NULL); |
| 339 if (TabSpecificContentSettings* tab_specific_content_settings = |
| 340 TabSpecificContentSettings::FromWebContents(contents())) |
| 341 tab_specific_content_settings->SetPopupsBlocked(false); |
| 342 if (CoreTabHelper* core_tab_helper = |
| 343 CoreTabHelper::FromWebContents(contents())) |
| 344 core_tab_helper->set_delegate(NULL); |
| 345 if (ThumbnailTabHelper* thumbnail_tab_helper = |
| 346 ThumbnailTabHelper::FromWebContents(contents())) |
| 347 thumbnail_tab_helper->set_enabled(true); |
| 348 |
| 349 #if defined(OS_MACOSX) |
| 350 if (content::RenderWidgetHostView* rwhv = |
| 351 contents_->GetRenderWidgetHostView()) |
| 352 rwhv->SetTakesFocusOnlyOnMouseDown(false); |
| 353 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, |
| 354 content::Source<content::NavigationController>( |
| 355 &contents_->GetController())); |
| 465 #endif | 356 #endif |
| 466 } | 357 } |
| 467 | 358 |
| 468 void InstantLoader::ReplacePreviewContents(content::WebContents* old_contents, | 359 void InstantLoader::ReplacePreviewContents(content::WebContents* old_contents, |
| 469 content::WebContents* new_contents) { | 360 content::WebContents* new_contents) { |
| 470 DCHECK_EQ(old_contents, preview_contents_->web_contents()); | 361 DCHECK_EQ(old_contents, contents()); |
| 471 CleanupPreviewContents(); | 362 CleanupPreviewContents(); |
| 472 // We release here without deleting so that the caller still has the | 363 // We release here without deleting so that the caller still has the |
| 473 // responsibility for deleting the TabContents. | 364 // responsibility for deleting the WebContents. |
| 474 ignore_result(preview_contents_.release()); | 365 ignore_result(contents_.release()); |
| 475 preview_contents_.reset(TabContents::FromWebContents(new_contents)); | 366 contents_.reset(new_contents); |
| 476 SetupPreviewContents(); | 367 SetupPreviewContents(); |
| 477 controller_->SwappedTabContents(this); | 368 controller_->SwappedWebContents(); |
| 478 } | 369 } |
| OLD | NEW |