| 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_controller.h" | 5 #include "chrome/browser/instant/instant_controller.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "chrome/browser/instant/instant_controller_extended_impl.h" |
| 8 #include "base/string_util.h" | 8 #include "chrome/browser/instant/instant_controller_impl.h" |
| 9 #include "base/stringprintf.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "chrome/browser/autocomplete/autocomplete_provider.h" | |
| 12 #include "chrome/browser/history/history_service.h" | |
| 13 #include "chrome/browser/history/history_service_factory.h" | |
| 14 #include "chrome/browser/history/history_tab_helper.h" | |
| 15 #include "chrome/browser/history/top_sites.h" | |
| 16 #include "chrome/browser/instant/instant_ntp.h" | |
| 17 #include "chrome/browser/instant/instant_overlay.h" | |
| 18 #include "chrome/browser/instant/instant_tab.h" | |
| 19 #include "chrome/browser/platform_util.h" | |
| 20 #include "chrome/browser/search_engines/search_terms_data.h" | |
| 21 #include "chrome/browser/search_engines/template_url_service.h" | |
| 22 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 23 #include "chrome/browser/ui/browser_instant_controller.h" | |
| 24 #include "chrome/browser/ui/search/search.h" | 9 #include "chrome/browser/ui/search/search.h" |
| 25 #include "chrome/browser/ui/search/search_tab_helper.h" | |
| 26 #include "chrome/common/chrome_notification_types.h" | |
| 27 #include "chrome/common/chrome_switches.h" | |
| 28 #include "chrome/common/url_constants.h" | |
| 29 #include "content/public/browser/navigation_entry.h" | |
| 30 #include "content/public/browser/notification_service.h" | |
| 31 #include "content/public/browser/render_widget_host_view.h" | |
| 32 #include "content/public/browser/web_contents.h" | |
| 33 #include "content/public/browser/web_contents_view.h" | |
| 34 #include "net/base/escape.h" | |
| 35 #include "third_party/icu/public/common/unicode/normalizer2.h" | |
| 36 | 10 |
| 37 #if defined(TOOLKIT_VIEWS) | 11 // static |
| 38 #include "ui/views/widget/widget.h" | 12 scoped_ptr<InstantController> InstantController::Create( |
| 39 #endif | 13 chrome::BrowserInstantController* browser, |
| 40 | 14 Profile* profile) { |
| 41 namespace { | 15 if (chrome::search::IsInstantExtendedAPIEnabled()) { |
| 42 | 16 return scoped_ptr<InstantController>( |
| 43 // An artificial delay (in milliseconds) we introduce before telling the Instant | 17 new InstantControllerExtendedImpl(browser, profile)); |
| 44 // page about the new omnibox bounds, in cases where the bounds shrink. This is | 18 } |
| 45 // to avoid the page jumping up/down very fast in response to bounds changes. | 19 return scoped_ptr<InstantController>( |
| 46 const int kUpdateBoundsDelayMS = 1000; | 20 new InstantControllerImpl(browser, profile)); |
| 47 | |
| 48 // The maximum number of times we'll load a non-Instant-supporting search engine | |
| 49 // before we give up and blacklist it for the rest of the browsing session. | |
| 50 const int kMaxInstantSupportFailures = 10; | |
| 51 | |
| 52 // For reporting events of interest. | |
| 53 enum InstantControllerEvent { | |
| 54 INSTANT_CONTROLLER_EVENT_URL_ADDED_TO_BLACKLIST = 0, | |
| 55 INSTANT_CONTROLLER_EVENT_URL_REMOVED_FROM_BLACKLIST = 1, | |
| 56 INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST = 2, | |
| 57 INSTANT_CONTROLLER_EVENT_MAX = 3, | |
| 58 }; | |
| 59 | |
| 60 void RecordEventHistogram(InstantControllerEvent event) { | |
| 61 UMA_HISTOGRAM_ENUMERATION("Instant.InstantControllerEvent", | |
| 62 event, | |
| 63 INSTANT_CONTROLLER_EVENT_MAX); | |
| 64 } | 21 } |
| 65 | 22 |
| 66 void AddSessionStorageHistogram(bool extended_enabled, | 23 InstantController::InstantController() { |
| 67 const content::WebContents* tab1, | |
| 68 const content::WebContents* tab2) { | |
| 69 base::HistogramBase* histogram = base::BooleanHistogram::FactoryGet( | |
| 70 std::string("Instant.SessionStorageNamespace") + | |
| 71 (extended_enabled ? "_Extended" : "_Instant"), | |
| 72 base::HistogramBase::kUmaTargetedHistogramFlag); | |
| 73 const content::SessionStorageNamespaceMap& session_storage_map1 = | |
| 74 tab1->GetController().GetSessionStorageNamespaceMap(); | |
| 75 const content::SessionStorageNamespaceMap& session_storage_map2 = | |
| 76 tab2->GetController().GetSessionStorageNamespaceMap(); | |
| 77 bool is_session_storage_the_same = | |
| 78 session_storage_map1.size() == session_storage_map2.size(); | |
| 79 if (is_session_storage_the_same) { | |
| 80 // The size is the same, so let's check that all entries match. | |
| 81 for (content::SessionStorageNamespaceMap::const_iterator | |
| 82 it1 = session_storage_map1.begin(), | |
| 83 it2 = session_storage_map2.begin(); | |
| 84 it1 != session_storage_map1.end() && it2 != session_storage_map2.end(); | |
| 85 ++it1, ++it2) { | |
| 86 if (it1->first != it2->first || it1->second != it2->second) { | |
| 87 is_session_storage_the_same = false; | |
| 88 break; | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 histogram->AddBoolean(is_session_storage_the_same); | |
| 93 } | |
| 94 | |
| 95 string16 Normalize(const string16& str) { | |
| 96 UErrorCode status = U_ZERO_ERROR; | |
| 97 const icu::Normalizer2* normalizer = | |
| 98 icu::Normalizer2::getInstance(NULL, "nfkc_cf", UNORM2_COMPOSE, status); | |
| 99 if (normalizer == NULL || U_FAILURE(status)) | |
| 100 return str; | |
| 101 icu::UnicodeString norm_str(normalizer->normalize( | |
| 102 icu::UnicodeString(FALSE, str.c_str(), str.size()), status)); | |
| 103 if (U_FAILURE(status)) | |
| 104 return str; | |
| 105 return string16(norm_str.getBuffer(), norm_str.length()); | |
| 106 } | |
| 107 | |
| 108 bool NormalizeAndStripPrefix(string16* text, const string16& prefix) { | |
| 109 string16 norm_prefix = Normalize(prefix); | |
| 110 string16 norm_text = Normalize(*text); | |
| 111 if (norm_prefix.size() <= norm_text.size() && | |
| 112 norm_text.compare(0, norm_prefix.size(), norm_prefix) == 0) { | |
| 113 *text = norm_text.erase(0, norm_prefix.size()); | |
| 114 return true; | |
| 115 } | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 // For TOOLKIT_VIEWS, the top level widget is always focused. If the focus | |
| 120 // change originated in views determine the child Widget from the view that is | |
| 121 // being focused. | |
| 122 gfx::NativeView GetViewGainingFocus(gfx::NativeView view_gaining_focus) { | |
| 123 #if defined(TOOLKIT_VIEWS) | |
| 124 views::Widget* widget = view_gaining_focus ? | |
| 125 views::Widget::GetWidgetForNativeView(view_gaining_focus) : NULL; | |
| 126 if (widget) { | |
| 127 views::FocusManager* focus_manager = widget->GetFocusManager(); | |
| 128 if (focus_manager && focus_manager->is_changing_focus() && | |
| 129 focus_manager->GetFocusedView() && | |
| 130 focus_manager->GetFocusedView()->GetWidget()) | |
| 131 return focus_manager->GetFocusedView()->GetWidget()->GetNativeView(); | |
| 132 } | |
| 133 #endif | |
| 134 return view_gaining_focus; | |
| 135 } | |
| 136 | |
| 137 // Returns true if |view| is the top-level contents view or a child view in the | |
| 138 // view hierarchy of |contents|. | |
| 139 bool IsViewInContents(gfx::NativeView view, content::WebContents* contents) { | |
| 140 content::RenderWidgetHostView* rwhv = contents->GetRenderWidgetHostView(); | |
| 141 if (!view || !rwhv) | |
| 142 return false; | |
| 143 | |
| 144 gfx::NativeView tab_view = contents->GetView()->GetNativeView(); | |
| 145 if (view == rwhv->GetNativeView() || view == tab_view) | |
| 146 return true; | |
| 147 | |
| 148 // Walk up the view hierarchy to determine if the view is a subview of the | |
| 149 // WebContents view (such as a windowed plugin or http auth dialog). | |
| 150 while (view) { | |
| 151 view = platform_util::GetParent(view); | |
| 152 if (view == tab_view) | |
| 153 return true; | |
| 154 } | |
| 155 | |
| 156 return false; | |
| 157 } | |
| 158 | |
| 159 bool IsFullHeight(const InstantOverlayModel& model) { | |
| 160 return model.height() == 100 && model.height_units() == INSTANT_SIZE_PERCENT; | |
| 161 } | |
| 162 | |
| 163 bool IsContentsFrom(const InstantPage* page, | |
| 164 const content::WebContents* contents) { | |
| 165 return page && (page->contents() == contents); | |
| 166 } | |
| 167 | |
| 168 // Adds a transient NavigationEntry to the supplied |contents|'s | |
| 169 // NavigationController if the page's URL has not already been updated with the | |
| 170 // supplied |search_terms|. Sets the |search_terms| on the transient entry for | |
| 171 // search terms extraction to work correctly. | |
| 172 void EnsureSearchTermsAreSet(content::WebContents* contents, | |
| 173 const string16& search_terms) { | |
| 174 content::NavigationController& controller = contents->GetController(); | |
| 175 | |
| 176 // If search terms are already correct or there is already a transient entry | |
| 177 // (there shouldn't be), bail out early. | |
| 178 if (chrome::search::GetSearchTerms(contents) == search_terms || | |
| 179 controller.GetTransientEntry()) | |
| 180 return; | |
| 181 | |
| 182 content::NavigationEntry* transient = content::NavigationEntry::Create( | |
| 183 *controller.GetActiveEntry()); | |
| 184 transient->SetExtraData(chrome::search::kInstantExtendedSearchTermsKey, | |
| 185 search_terms); | |
| 186 controller.SetTransientEntry(transient); | |
| 187 | |
| 188 chrome::search::SearchTabHelper::FromWebContents(contents)-> | |
| 189 NavigationEntryUpdated(); | |
| 190 } | |
| 191 | |
| 192 } // namespace | |
| 193 | |
| 194 InstantController::InstantController(chrome::BrowserInstantController* browser, | |
| 195 bool extended_enabled) | |
| 196 : browser_(browser), | |
| 197 extended_enabled_(extended_enabled), | |
| 198 instant_enabled_(false), | |
| 199 use_local_overlay_only_(true), | |
| 200 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 201 last_omnibox_text_has_inline_autocompletion_(false), | |
| 202 last_verbatim_(false), | |
| 203 last_transition_type_(content::PAGE_TRANSITION_LINK), | |
| 204 last_match_was_search_(false), | |
| 205 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), | |
| 206 omnibox_bounds_(-1, -1, 0, 0), | |
| 207 allow_overlay_to_show_search_suggestions_(false), | |
| 208 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
| 209 } | 24 } |
| 210 | 25 |
| 211 InstantController::~InstantController() { | 26 InstantController::~InstantController() { |
| 212 } | 27 } |
| 213 | |
| 214 bool InstantController::Update(const AutocompleteMatch& match, | |
| 215 const string16& user_text, | |
| 216 const string16& full_text, | |
| 217 size_t selection_start, | |
| 218 size_t selection_end, | |
| 219 bool verbatim, | |
| 220 bool user_input_in_progress, | |
| 221 bool omnibox_popup_is_open, | |
| 222 bool escape_pressed, | |
| 223 bool is_keyword_search) { | |
| 224 if (!extended_enabled_ && !instant_enabled_) | |
| 225 return false; | |
| 226 | |
| 227 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 228 "Update: %s user_text='%s' full_text='%s' selection_start=%d " | |
| 229 "selection_end=%d verbatim=%d typing=%d popup=%d escape_pressed=%d " | |
| 230 "is_keyword_search=%d", | |
| 231 AutocompleteMatch::TypeToString(match.type).c_str(), | |
| 232 UTF16ToUTF8(user_text).c_str(), UTF16ToUTF8(full_text).c_str(), | |
| 233 static_cast<int>(selection_start), static_cast<int>(selection_end), | |
| 234 verbatim, user_input_in_progress, omnibox_popup_is_open, escape_pressed, | |
| 235 is_keyword_search)); | |
| 236 | |
| 237 // TODO(dhollowa): Complete keyword match UI. For now just hide suggestions. | |
| 238 // http://crbug.com/153932. Note, this early escape is happens prior to the | |
| 239 // DCHECKs below because |user_text| and |full_text| have different semantics | |
| 240 // when keyword search is in effect. | |
| 241 if (is_keyword_search) { | |
| 242 if (instant_tab_) | |
| 243 instant_tab_->Update(string16(), 0, 0, true); | |
| 244 else | |
| 245 HideOverlay(); | |
| 246 last_match_was_search_ = false; | |
| 247 return false; | |
| 248 } | |
| 249 | |
| 250 // If the popup is open, the user has to be typing. | |
| 251 DCHECK(!omnibox_popup_is_open || user_input_in_progress); | |
| 252 | |
| 253 // If the popup is closed, there should be no inline autocompletion. | |
| 254 DCHECK(omnibox_popup_is_open || user_text.empty() || user_text == full_text) | |
| 255 << user_text << "|" << full_text; | |
| 256 | |
| 257 // If there's no text in the omnibox, the user can't have typed any. | |
| 258 DCHECK(!full_text.empty() || user_text.empty()) << user_text; | |
| 259 | |
| 260 // If the user isn't typing, and the popup is closed, there can't be any | |
| 261 // user-typed text. | |
| 262 DCHECK(user_input_in_progress || omnibox_popup_is_open || user_text.empty()) | |
| 263 << user_text; | |
| 264 | |
| 265 // The overlay is being clicked and will commit soon. Don't change anything. | |
| 266 // TODO(sreeram): Add a browser test for this. | |
| 267 if (overlay_ && overlay_->is_pointer_down_from_activate()) | |
| 268 return false; | |
| 269 | |
| 270 // In non-extended mode, SearchModeChanged() is never called, so fake it. The | |
| 271 // mode is set to "disallow suggestions" here, so that if one of the early | |
| 272 // "return false" conditions is hit, suggestions will be disallowed. If the | |
| 273 // query is sent to the overlay, the mode is set to "allow" further below. | |
| 274 if (!extended_enabled_) | |
| 275 search_mode_.mode = chrome::search::Mode::MODE_DEFAULT; | |
| 276 | |
| 277 last_match_was_search_ = AutocompleteMatch::IsSearchType(match.type) && | |
| 278 !user_text.empty(); | |
| 279 | |
| 280 // In non extended mode, Instant is disabled for URLs and keyword mode. | |
| 281 if (!extended_enabled_ && | |
| 282 (!last_match_was_search_ || | |
| 283 match.type == AutocompleteMatch::SEARCH_OTHER_ENGINE)) { | |
| 284 HideOverlay(); | |
| 285 return false; | |
| 286 } | |
| 287 | |
| 288 // If we have an |instant_tab_| use it, else ensure we have an overlay that is | |
| 289 // current or is using the local overlay. | |
| 290 if (!instant_tab_ && !(overlay_ && overlay_->IsUsingLocalOverlay()) && | |
| 291 !EnsureOverlayIsCurrent(false)) { | |
| 292 HideOverlay(); | |
| 293 return false; | |
| 294 } | |
| 295 | |
| 296 if (extended_enabled_) { | |
| 297 if (!omnibox_popup_is_open) { | |
| 298 if (!user_input_in_progress) { | |
| 299 // If the user isn't typing and the omnibox popup is closed, it means a | |
| 300 // regular navigation, tab-switch or the user hitting Escape. | |
| 301 if (instant_tab_) { | |
| 302 // The user is on a search results page. It may be showing results for | |
| 303 // a partial query the user typed before they hit Escape. Send the | |
| 304 // omnibox text to the page to restore the original results. | |
| 305 // | |
| 306 // In a tab switch, |instant_tab_| won't have updated yet, so it may | |
| 307 // be pointing to the previous tab (which was a search results page). | |
| 308 // Ensure we don't send the omnibox text to a random webpage (the new | |
| 309 // tab), by comparing the old and new WebContents. | |
| 310 if (escape_pressed && | |
| 311 instant_tab_->contents() == browser_->GetActiveWebContents()) { | |
| 312 instant_tab_->Submit(full_text); | |
| 313 } | |
| 314 } else if (!full_text.empty()) { | |
| 315 // If |full_text| is empty, the user is on the NTP. The overlay may | |
| 316 // be showing custom NTP content; hide only if that's not the case. | |
| 317 HideOverlay(); | |
| 318 } | |
| 319 } else if (full_text.empty()) { | |
| 320 // The user is typing, and backspaced away all omnibox text. Clear | |
| 321 // |last_omnibox_text_| so that we don't attempt to set suggestions. | |
| 322 last_omnibox_text_.clear(); | |
| 323 last_suggestion_ = InstantSuggestion(); | |
| 324 if (instant_tab_) { | |
| 325 // On a search results page, tell it to clear old results. | |
| 326 instant_tab_->Update(string16(), 0, 0, true); | |
| 327 } else if (search_mode_.is_origin_ntp()) { | |
| 328 // On the NTP, tell the overlay to clear old results. Don't hide the | |
| 329 // overlay so it can show a blank page or logo if it wants. | |
| 330 overlay_->Update(string16(), 0, 0, true); | |
| 331 } else { | |
| 332 HideOverlay(); | |
| 333 } | |
| 334 } else { | |
| 335 // The user switched to a tab with partial text already in the omnibox. | |
| 336 HideOverlay(); | |
| 337 | |
| 338 // The new tab may or may not be a search results page; we don't know | |
| 339 // since SearchModeChanged() hasn't been called yet. If it later turns | |
| 340 // out to be, we should store |full_text| now, so that if the user hits | |
| 341 // Enter, we'll send the correct query to instant_tab_->Submit(). If the | |
| 342 // partial text is not a query (|last_match_was_search_| is false), we | |
| 343 // won't Submit(), so no need to worry about that. | |
| 344 last_omnibox_text_ = full_text; | |
| 345 last_suggestion_ = InstantSuggestion(); | |
| 346 } | |
| 347 return false; | |
| 348 } else if (full_text.empty()) { | |
| 349 // The user typed a solitary "?". Same as the backspace case above. | |
| 350 last_omnibox_text_.clear(); | |
| 351 last_suggestion_ = InstantSuggestion(); | |
| 352 if (instant_tab_) | |
| 353 instant_tab_->Update(string16(), 0, 0, true); | |
| 354 else if (search_mode_.is_origin_ntp()) | |
| 355 overlay_->Update(string16(), 0, 0, true); | |
| 356 else | |
| 357 HideOverlay(); | |
| 358 return false; | |
| 359 } | |
| 360 } else if (!omnibox_popup_is_open || full_text.empty()) { | |
| 361 // In the non-extended case, hide the overlay as long as the user isn't | |
| 362 // actively typing a non-empty query. | |
| 363 HideOverlay(); | |
| 364 return false; | |
| 365 } | |
| 366 | |
| 367 last_omnibox_text_has_inline_autocompletion_ = user_text != full_text; | |
| 368 | |
| 369 // If the user continues typing the same query as the suggested text is | |
| 370 // showing, reuse the suggestion (but only for INSTANT_COMPLETE_NEVER). | |
| 371 bool reused_suggestion = false; | |
| 372 if (last_suggestion_.behavior == INSTANT_COMPLETE_NEVER && | |
| 373 !last_omnibox_text_has_inline_autocompletion_) { | |
| 374 if (StartsWith(last_omnibox_text_, full_text, false)) { | |
| 375 // The user is backspacing away characters. | |
| 376 last_suggestion_.text.insert(0, last_omnibox_text_, full_text.size(), | |
| 377 last_omnibox_text_.size() - full_text.size()); | |
| 378 reused_suggestion = true; | |
| 379 } else if (StartsWith(full_text, last_omnibox_text_, false)) { | |
| 380 // The user is typing forward. Normalize any added characters. | |
| 381 reused_suggestion = NormalizeAndStripPrefix(&last_suggestion_.text, | |
| 382 string16(full_text, last_omnibox_text_.size())); | |
| 383 } | |
| 384 } | |
| 385 if (!reused_suggestion) | |
| 386 last_suggestion_ = InstantSuggestion(); | |
| 387 | |
| 388 last_omnibox_text_ = full_text; | |
| 389 | |
| 390 if (!extended_enabled_) { | |
| 391 // In non-extended mode, the query is verbatim if there's any selection | |
| 392 // (including inline autocompletion) or if the cursor is not at the end. | |
| 393 verbatim = verbatim || selection_start != selection_end || | |
| 394 selection_start != full_text.size(); | |
| 395 } | |
| 396 last_verbatim_ = verbatim; | |
| 397 | |
| 398 last_transition_type_ = match.transition; | |
| 399 url_for_history_ = match.destination_url; | |
| 400 | |
| 401 // Allow search suggestions. In extended mode, SearchModeChanged() will set | |
| 402 // this, but it's not called in non-extended mode, so fake it. | |
| 403 if (!extended_enabled_) | |
| 404 search_mode_.mode = chrome::search::Mode::MODE_SEARCH_SUGGESTIONS; | |
| 405 | |
| 406 if (instant_tab_) { | |
| 407 instant_tab_->Update(user_text, selection_start, selection_end, verbatim); | |
| 408 } else { | |
| 409 if (first_interaction_time_.is_null()) | |
| 410 first_interaction_time_ = base::Time::Now(); | |
| 411 allow_overlay_to_show_search_suggestions_ = true; | |
| 412 | |
| 413 // For extended mode, if the loader is not ready at this point, switch over | |
| 414 // to a backup loader. | |
| 415 if (extended_enabled_ && !overlay_->supports_instant() && | |
| 416 !overlay_->IsUsingLocalOverlay() && browser_->GetActiveWebContents()) { | |
| 417 CreateOverlay(chrome::search::kLocalOmniboxPopupURL, | |
| 418 browser_->GetActiveWebContents()); | |
| 419 } | |
| 420 | |
| 421 overlay_->Update(extended_enabled_ ? user_text : full_text, | |
| 422 selection_start, selection_end, verbatim); | |
| 423 } | |
| 424 | |
| 425 content::NotificationService::current()->Notify( | |
| 426 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | |
| 427 content::Source<InstantController>(this), | |
| 428 content::NotificationService::NoDetails()); | |
| 429 | |
| 430 // We don't have new suggestions yet, but we can either reuse the existing | |
| 431 // suggestion or reset the existing "gray text". | |
| 432 browser_->SetInstantSuggestion(last_suggestion_); | |
| 433 | |
| 434 return true; | |
| 435 } | |
| 436 | |
| 437 scoped_ptr<content::WebContents> InstantController::ReleaseNTPContents() { | |
| 438 if (!extended_enabled_ || !ntp_) | |
| 439 return scoped_ptr<content::WebContents>(NULL); | |
| 440 | |
| 441 LOG_INSTANT_DEBUG_EVENT(this, "ReleaseNTPContents"); | |
| 442 | |
| 443 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); | |
| 444 ntp_.reset(); | |
| 445 ResetNTP(); | |
| 446 return ntp_contents.Pass(); | |
| 447 } | |
| 448 | |
| 449 // TODO(tonyg): This method only fires when the omnibox bounds change. It also | |
| 450 // needs to fire when the overlay bounds change (e.g.: open/close info bar). | |
| 451 void InstantController::SetPopupBounds(const gfx::Rect& bounds) { | |
| 452 if (!extended_enabled_ && !instant_enabled_) | |
| 453 return; | |
| 454 | |
| 455 if (popup_bounds_ == bounds) | |
| 456 return; | |
| 457 | |
| 458 popup_bounds_ = bounds; | |
| 459 if (popup_bounds_.height() > last_popup_bounds_.height()) { | |
| 460 update_bounds_timer_.Stop(); | |
| 461 SendPopupBoundsToPage(); | |
| 462 } else if (!update_bounds_timer_.IsRunning()) { | |
| 463 update_bounds_timer_.Start(FROM_HERE, | |
| 464 base::TimeDelta::FromMilliseconds(kUpdateBoundsDelayMS), this, | |
| 465 &InstantController::SendPopupBoundsToPage); | |
| 466 } | |
| 467 } | |
| 468 | |
| 469 void InstantController::SetOmniboxBounds(const gfx::Rect& bounds) { | |
| 470 if (!extended_enabled_ || omnibox_bounds_ == bounds) | |
| 471 return; | |
| 472 | |
| 473 omnibox_bounds_ = bounds; | |
| 474 if (overlay_) | |
| 475 overlay_->SetOmniboxBounds(omnibox_bounds_); | |
| 476 if (ntp_) | |
| 477 ntp_->SetOmniboxBounds(omnibox_bounds_); | |
| 478 if (instant_tab_) | |
| 479 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | |
| 480 } | |
| 481 | |
| 482 void InstantController::HandleAutocompleteResults( | |
| 483 const std::vector<AutocompleteProvider*>& providers) { | |
| 484 if (!extended_enabled_) | |
| 485 return; | |
| 486 | |
| 487 if (!instant_tab_ && !overlay_) | |
| 488 return; | |
| 489 | |
| 490 DVLOG(1) << "AutocompleteResults:"; | |
| 491 std::vector<InstantAutocompleteResult> results; | |
| 492 for (ACProviders::const_iterator provider = providers.begin(); | |
| 493 provider != providers.end(); ++provider) { | |
| 494 // Skip SearchProvider, since it only echoes suggestions. | |
| 495 if ((*provider)->type() == AutocompleteProvider::TYPE_SEARCH) | |
| 496 continue; | |
| 497 for (ACMatches::const_iterator match = (*provider)->matches().begin(); | |
| 498 match != (*provider)->matches().end(); ++match) { | |
| 499 InstantAutocompleteResult result; | |
| 500 result.provider = UTF8ToUTF16((*provider)->GetName()); | |
| 501 result.type = UTF8ToUTF16(AutocompleteMatch::TypeToString(match->type)); | |
| 502 result.description = match->description; | |
| 503 result.destination_url = UTF8ToUTF16(match->destination_url.spec()); | |
| 504 result.transition = match->transition; | |
| 505 result.relevance = match->relevance; | |
| 506 DVLOG(1) << " " << result.relevance << " " << result.type << " " | |
| 507 << result.provider << " " << result.destination_url << " '" | |
| 508 << result.description << "' " << result.transition; | |
| 509 results.push_back(result); | |
| 510 } | |
| 511 } | |
| 512 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 513 "HandleAutocompleteResults: total_results=%d", | |
| 514 static_cast<int>(results.size()))); | |
| 515 | |
| 516 if (instant_tab_) | |
| 517 instant_tab_->SendAutocompleteResults(results); | |
| 518 else | |
| 519 overlay_->SendAutocompleteResults(results); | |
| 520 } | |
| 521 | |
| 522 bool InstantController::OnUpOrDownKeyPressed(int count) { | |
| 523 if (!extended_enabled_) | |
| 524 return false; | |
| 525 | |
| 526 if (!instant_tab_ && !overlay_) | |
| 527 return false; | |
| 528 | |
| 529 if (instant_tab_) | |
| 530 instant_tab_->UpOrDownKeyPressed(count); | |
| 531 else | |
| 532 overlay_->UpOrDownKeyPressed(count); | |
| 533 | |
| 534 return true; | |
| 535 } | |
| 536 | |
| 537 void InstantController::OnCancel(const AutocompleteMatch& match, | |
| 538 const string16& full_text) { | |
| 539 if (!extended_enabled_) | |
| 540 return; | |
| 541 | |
| 542 if (!instant_tab_ && !overlay_) | |
| 543 return; | |
| 544 | |
| 545 // We manually reset the state here since the JS is not expected to do it. | |
| 546 // TODO(sreeram): Handle the case where user_text is now a URL | |
| 547 last_match_was_search_ = AutocompleteMatch::IsSearchType(match.type) && | |
| 548 !full_text.empty(); | |
| 549 last_omnibox_text_ = full_text; | |
| 550 last_suggestion_ = InstantSuggestion(); | |
| 551 | |
| 552 if (instant_tab_) | |
| 553 instant_tab_->CancelSelection(full_text); | |
| 554 else | |
| 555 overlay_->CancelSelection(full_text); | |
| 556 } | |
| 557 | |
| 558 content::WebContents* InstantController::GetOverlayContents() const { | |
| 559 return overlay_ ? overlay_->contents() : NULL; | |
| 560 } | |
| 561 | |
| 562 bool InstantController::IsOverlayingSearchResults() const { | |
| 563 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && | |
| 564 (last_match_was_search_ || | |
| 565 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER); | |
| 566 } | |
| 567 | |
| 568 bool InstantController::CommitIfPossible(InstantCommitType type) { | |
| 569 if (!extended_enabled_ && !instant_enabled_) | |
| 570 return false; | |
| 571 | |
| 572 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 573 "CommitIfPossible: type=%d last_omnibox_text_='%s' " | |
| 574 "last_match_was_search_=%d instant_tab_=%d", type, | |
| 575 UTF16ToUTF8(last_omnibox_text_).c_str(), last_match_was_search_, | |
| 576 instant_tab_ != NULL)); | |
| 577 | |
| 578 // If we are on an already committed search results page, send a submit event | |
| 579 // to the page, but otherwise, nothing else to do. | |
| 580 if (instant_tab_) { | |
| 581 if (type == INSTANT_COMMIT_PRESSED_ENTER && | |
| 582 (last_match_was_search_ || | |
| 583 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER)) { | |
| 584 EnsureSearchTermsAreSet(instant_tab_->contents(), last_omnibox_text_); | |
| 585 instant_tab_->Submit(last_omnibox_text_); | |
| 586 instant_tab_->contents()->GetView()->Focus(); | |
| 587 return true; | |
| 588 } | |
| 589 return false; | |
| 590 } | |
| 591 | |
| 592 if (!IsOverlayingSearchResults() && type != INSTANT_COMMIT_NAVIGATED) | |
| 593 return false; | |
| 594 | |
| 595 // There may re-entrance here, from the call to browser_->CommitInstant below, | |
| 596 // which can cause a TabDeactivated notification which gets back here. | |
| 597 // In this case, overlay_->ReleaseContents() was called already. | |
| 598 if (!GetOverlayContents()) | |
| 599 return false; | |
| 600 | |
| 601 // Never commit the local overlay. | |
| 602 if (overlay_->IsUsingLocalOverlay()) | |
| 603 return false; | |
| 604 | |
| 605 if (type == INSTANT_COMMIT_FOCUS_LOST) | |
| 606 overlay_->Cancel(last_omnibox_text_); | |
| 607 else if (type != INSTANT_COMMIT_NAVIGATED) | |
| 608 overlay_->Submit(last_omnibox_text_); | |
| 609 | |
| 610 scoped_ptr<content::WebContents> overlay = overlay_->ReleaseContents(); | |
| 611 | |
| 612 // If the overlay page has navigated since the last Update(), we need to add | |
| 613 // the navigation to history ourselves. Else, the page will navigate after | |
| 614 // commit, and it will be added to history in the usual manner. | |
| 615 const history::HistoryAddPageArgs& last_navigation = | |
| 616 overlay_->last_navigation(); | |
| 617 if (!last_navigation.url.is_empty()) { | |
| 618 content::NavigationEntry* entry = overlay->GetController().GetActiveEntry(); | |
| 619 | |
| 620 // The last navigation should be the same as the active entry if the overlay | |
| 621 // is in search mode. During navigation, the active entry could have | |
| 622 // changed since DidCommitProvisionalLoadForFrame is called after the entry | |
| 623 // is changed. | |
| 624 // TODO(shishir): Should we commit the last navigation for | |
| 625 // INSTANT_COMMIT_NAVIGATED. | |
| 626 DCHECK(type == INSTANT_COMMIT_NAVIGATED || | |
| 627 last_navigation.url == entry->GetURL()); | |
| 628 | |
| 629 // Add the page to history. | |
| 630 HistoryTabHelper* history_tab_helper = | |
| 631 HistoryTabHelper::FromWebContents(overlay.get()); | |
| 632 history_tab_helper->UpdateHistoryForNavigation(last_navigation); | |
| 633 | |
| 634 // Update the page title. | |
| 635 history_tab_helper->UpdateHistoryPageTitle(*entry); | |
| 636 } | |
| 637 | |
| 638 // Add a fake history entry with a non-Instant search URL, so that search | |
| 639 // terms extraction (for autocomplete history matches) works. | |
| 640 HistoryService* history = HistoryServiceFactory::GetForProfile( | |
| 641 Profile::FromBrowserContext(overlay->GetBrowserContext()), | |
| 642 Profile::EXPLICIT_ACCESS); | |
| 643 if (history) { | |
| 644 history->AddPage(url_for_history_, base::Time::Now(), NULL, 0, GURL(), | |
| 645 history::RedirectList(), last_transition_type_, | |
| 646 history::SOURCE_BROWSED, false); | |
| 647 } | |
| 648 | |
| 649 if (type == INSTANT_COMMIT_PRESSED_ALT_ENTER) { | |
| 650 overlay->GetController().PruneAllButActive(); | |
| 651 } else { | |
| 652 content::WebContents* active_tab = browser_->GetActiveWebContents(); | |
| 653 AddSessionStorageHistogram(extended_enabled_, active_tab, overlay.get()); | |
| 654 overlay->GetController().CopyStateFromAndPrune( | |
| 655 &active_tab->GetController()); | |
| 656 } | |
| 657 | |
| 658 if (extended_enabled_) { | |
| 659 // Adjust the search terms shown in the omnibox for this query. Hitting | |
| 660 // ENTER searches for what the user typed, so use last_omnibox_text_. | |
| 661 // Clicking on the overlay commits what is currently showing, so add in the | |
| 662 // gray text in that case. | |
| 663 if (type == INSTANT_COMMIT_FOCUS_LOST && | |
| 664 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER) { | |
| 665 // Update |last_omnibox_text_| so that the controller commits the proper | |
| 666 // query if the user focuses the omnibox and presses Enter. | |
| 667 last_omnibox_text_ += last_suggestion_.text; | |
| 668 } | |
| 669 | |
| 670 EnsureSearchTermsAreSet(overlay.get(), last_omnibox_text_); | |
| 671 } | |
| 672 | |
| 673 // Save notification source before we release the overlay. | |
| 674 content::Source<content::WebContents> notification_source(overlay.get()); | |
| 675 | |
| 676 browser_->CommitInstant(overlay.Pass(), | |
| 677 type == INSTANT_COMMIT_PRESSED_ALT_ENTER); | |
| 678 | |
| 679 content::NotificationService::current()->Notify( | |
| 680 chrome::NOTIFICATION_INSTANT_COMMITTED, | |
| 681 notification_source, | |
| 682 content::NotificationService::NoDetails()); | |
| 683 | |
| 684 // Hide explicitly. See comments in HideOverlay() for why. | |
| 685 model_.SetOverlayState(chrome::search::Mode(), 0, INSTANT_SIZE_PERCENT); | |
| 686 | |
| 687 // Delay deletion as we could've gotten here from an InstantOverlay method. | |
| 688 MessageLoop::current()->DeleteSoon(FROM_HERE, overlay_.release()); | |
| 689 | |
| 690 // Try to create another overlay immediately so that it is ready for the next | |
| 691 // user interaction. | |
| 692 EnsureOverlayIsCurrent(false); | |
| 693 | |
| 694 LOG_INSTANT_DEBUG_EVENT(this, "Committed"); | |
| 695 return true; | |
| 696 } | |
| 697 | |
| 698 void InstantController::OmniboxFocusChanged( | |
| 699 OmniboxFocusState state, | |
| 700 OmniboxFocusChangeReason reason, | |
| 701 gfx::NativeView view_gaining_focus) { | |
| 702 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 703 "OmniboxFocusChanged: %d to %d for reason %d", omnibox_focus_state_, | |
| 704 state, reason)); | |
| 705 | |
| 706 OmniboxFocusState old_focus_state = omnibox_focus_state_; | |
| 707 omnibox_focus_state_ = state; | |
| 708 if (!extended_enabled_ && !instant_enabled_) | |
| 709 return; | |
| 710 | |
| 711 // Tell the page if the key capture mode changed unless the focus state | |
| 712 // changed because of TYPING. This is because in that case, the browser hasn't | |
| 713 // really stopped capturing key strokes. | |
| 714 // | |
| 715 // (More practically, if we don't do this check, the page would receive | |
| 716 // onkeycapturechange before the corresponding onchange, and the page would | |
| 717 // have no way of telling whether the keycapturechange happened because of | |
| 718 // some actual user action or just because they started typing.) | |
| 719 if (extended_enabled_ && GetOverlayContents() && | |
| 720 reason != OMNIBOX_FOCUS_CHANGE_TYPING) { | |
| 721 const bool is_key_capture_enabled = | |
| 722 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE; | |
| 723 if (overlay_) | |
| 724 overlay_->KeyCaptureChanged(is_key_capture_enabled); | |
| 725 if (instant_tab_) | |
| 726 instant_tab_->KeyCaptureChanged(is_key_capture_enabled); | |
| 727 } | |
| 728 | |
| 729 // If focus went from outside the omnibox to the omnibox, preload the default | |
| 730 // search engine, in anticipation of the user typing a query. If the reverse | |
| 731 // happened, commit or discard the overlay. | |
| 732 if (state != OMNIBOX_FOCUS_NONE && old_focus_state == OMNIBOX_FOCUS_NONE) { | |
| 733 // On explicit user actions, ignore the Instant blacklist. | |
| 734 EnsureOverlayIsCurrent(reason == OMNIBOX_FOCUS_CHANGE_EXPLICIT); | |
| 735 } else if (state == OMNIBOX_FOCUS_NONE && | |
| 736 old_focus_state != OMNIBOX_FOCUS_NONE) { | |
| 737 OmniboxLostFocus(view_gaining_focus); | |
| 738 } | |
| 739 } | |
| 740 | |
| 741 void InstantController::SearchModeChanged( | |
| 742 const chrome::search::Mode& old_mode, | |
| 743 const chrome::search::Mode& new_mode) { | |
| 744 if (!extended_enabled_) | |
| 745 return; | |
| 746 | |
| 747 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 748 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin, | |
| 749 old_mode.mode, new_mode.origin, new_mode.mode)); | |
| 750 | |
| 751 search_mode_ = new_mode; | |
| 752 if (!new_mode.is_search_suggestions()) | |
| 753 HideOverlay(); | |
| 754 | |
| 755 ResetInstantTab(); | |
| 756 } | |
| 757 | |
| 758 void InstantController::ActiveTabChanged() { | |
| 759 if (!extended_enabled_ && !instant_enabled_) | |
| 760 return; | |
| 761 | |
| 762 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged"); | |
| 763 | |
| 764 // When switching tabs, always hide the overlay. | |
| 765 HideOverlay(); | |
| 766 | |
| 767 if (extended_enabled_) | |
| 768 ResetInstantTab(); | |
| 769 } | |
| 770 | |
| 771 void InstantController::TabDeactivated(content::WebContents* contents) { | |
| 772 LOG_INSTANT_DEBUG_EVENT(this, "TabDeactivated"); | |
| 773 if (extended_enabled_ && !contents->IsBeingDestroyed()) | |
| 774 CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); | |
| 775 } | |
| 776 | |
| 777 void InstantController::SetInstantEnabled(bool instant_enabled, | |
| 778 bool use_local_overlay_only) { | |
| 779 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 780 "SetInstantEnabled: instant_enabled=%d, use_local_overlay_only=%d", | |
| 781 instant_enabled, use_local_overlay_only)); | |
| 782 | |
| 783 // Non extended mode does not care about |use_local_overlay_only|. | |
| 784 if (instant_enabled == instant_enabled_ && | |
| 785 (!extended_enabled_ || | |
| 786 use_local_overlay_only == use_local_overlay_only_)) { | |
| 787 return; | |
| 788 } | |
| 789 | |
| 790 instant_enabled_ = instant_enabled; | |
| 791 use_local_overlay_only_ = use_local_overlay_only; | |
| 792 HideInternal(); | |
| 793 overlay_.reset(); | |
| 794 if (extended_enabled_ || instant_enabled_) | |
| 795 EnsureOverlayIsCurrent(false); | |
| 796 if (extended_enabled_) | |
| 797 ResetNTP(); | |
| 798 if (instant_tab_) | |
| 799 instant_tab_->SetDisplayInstantResults(instant_enabled_); | |
| 800 } | |
| 801 | |
| 802 void InstantController::ThemeChanged(const ThemeBackgroundInfo& theme_info) { | |
| 803 if (!extended_enabled_) | |
| 804 return; | |
| 805 | |
| 806 if (overlay_) | |
| 807 overlay_->SendThemeBackgroundInfo(theme_info); | |
| 808 if (ntp_) | |
| 809 ntp_->SendThemeBackgroundInfo(theme_info); | |
| 810 if (instant_tab_) | |
| 811 instant_tab_->SendThemeBackgroundInfo(theme_info); | |
| 812 } | |
| 813 | |
| 814 void InstantController::SwappedOverlayContents() { | |
| 815 model_.SetOverlayContents(GetOverlayContents()); | |
| 816 } | |
| 817 | |
| 818 void InstantController::FocusedOverlayContents() { | |
| 819 #if defined(USE_AURA) | |
| 820 // On aura the omnibox only receives a focus lost if we initiate the focus | |
| 821 // change. This does that. | |
| 822 if (!model_.mode().is_default()) | |
| 823 browser_->InstantOverlayFocused(); | |
| 824 #endif | |
| 825 } | |
| 826 | |
| 827 void InstantController::ReloadOverlayIfStale() { | |
| 828 // The local overlay is never stale. | |
| 829 if (overlay_ && overlay_->IsUsingLocalOverlay()) | |
| 830 return; | |
| 831 | |
| 832 // If the overlay is showing or the omnibox has focus, don't delete the | |
| 833 // overlay. It will get refreshed the next time the overlay is hidden or the | |
| 834 // omnibox loses focus. | |
| 835 if ((!overlay_ || overlay_->is_stale()) && | |
| 836 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && | |
| 837 model_.mode().is_default()) { | |
| 838 overlay_.reset(); | |
| 839 EnsureOverlayIsCurrent(false); | |
| 840 } | |
| 841 } | |
| 842 | |
| 843 void InstantController::LogDebugEvent(const std::string& info) const { | |
| 844 DVLOG(1) << info; | |
| 845 | |
| 846 debug_events_.push_front(std::make_pair( | |
| 847 base::Time::Now().ToInternalValue(), info)); | |
| 848 static const size_t kMaxDebugEventSize = 2000; | |
| 849 if (debug_events_.size() > kMaxDebugEventSize) | |
| 850 debug_events_.pop_back(); | |
| 851 } | |
| 852 | |
| 853 void InstantController::ClearDebugEvents() { | |
| 854 debug_events_.clear(); | |
| 855 } | |
| 856 | |
| 857 void InstantController::DeleteMostVisitedItem(const GURL& url) { | |
| 858 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | |
| 859 if (!top_sites) | |
| 860 return; | |
| 861 | |
| 862 top_sites->AddBlacklistedURL(url); | |
| 863 } | |
| 864 | |
| 865 void InstantController::UndoMostVisitedDeletion(const GURL& url) { | |
| 866 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | |
| 867 if (!top_sites) | |
| 868 return; | |
| 869 | |
| 870 top_sites->RemoveBlacklistedURL(url); | |
| 871 } | |
| 872 | |
| 873 void InstantController::UndoAllMostVisitedDeletions() { | |
| 874 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | |
| 875 if (!top_sites) | |
| 876 return; | |
| 877 | |
| 878 top_sites->ClearBlacklistedURLs(); | |
| 879 } | |
| 880 | |
| 881 void InstantController::Observe(int type, | |
| 882 const content::NotificationSource& source, | |
| 883 const content::NotificationDetails& details) { | |
| 884 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | |
| 885 RequestMostVisitedItems(); | |
| 886 } | |
| 887 | |
| 888 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the | |
| 889 // RenderViewHost being created which is not always true. Fix this. | |
| 890 void InstantController::InstantPageRenderViewCreated( | |
| 891 const content::WebContents* contents) { | |
| 892 if (!extended_enabled_) | |
| 893 return; | |
| 894 | |
| 895 // Update theme info so that the page picks it up. | |
| 896 browser_->UpdateThemeInfo(); | |
| 897 | |
| 898 // Ensure the searchbox API has the correct initial state. | |
| 899 if (IsContentsFrom(overlay(), contents)) { | |
| 900 overlay_->SetDisplayInstantResults(instant_enabled_); | |
| 901 overlay_->KeyCaptureChanged( | |
| 902 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | |
| 903 overlay_->SetOmniboxBounds(omnibox_bounds_); | |
| 904 overlay_->InitializeFonts(); | |
| 905 overlay_->GrantChromeSearchAccessFromOrigin(GURL(overlay_->instant_url())); | |
| 906 } else if (IsContentsFrom(ntp(), contents)) { | |
| 907 ntp_->SetDisplayInstantResults(instant_enabled_); | |
| 908 ntp_->SetOmniboxBounds(omnibox_bounds_); | |
| 909 ntp_->InitializeFonts(); | |
| 910 ntp_->GrantChromeSearchAccessFromOrigin(GURL(ntp_->instant_url())); | |
| 911 } else { | |
| 912 NOTREACHED(); | |
| 913 } | |
| 914 StartListeningToMostVisitedChanges(); | |
| 915 } | |
| 916 | |
| 917 void InstantController::InstantSupportDetermined( | |
| 918 const content::WebContents* contents, | |
| 919 bool supports_instant) { | |
| 920 if (IsContentsFrom(instant_tab(), contents)) { | |
| 921 if (!supports_instant) | |
| 922 MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release()); | |
| 923 } else if (IsContentsFrom(ntp(), contents)) { | |
| 924 if (supports_instant) | |
| 925 RemoveFromBlacklist(ntp_->instant_url()); | |
| 926 else | |
| 927 BlacklistAndResetNTP(); | |
| 928 | |
| 929 content::NotificationService::current()->Notify( | |
| 930 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, | |
| 931 content::Source<InstantController>(this), | |
| 932 content::NotificationService::NoDetails()); | |
| 933 | |
| 934 } else if (IsContentsFrom(overlay(), contents)) { | |
| 935 if (supports_instant) | |
| 936 RemoveFromBlacklist(overlay_->instant_url()); | |
| 937 else | |
| 938 BlacklistAndResetOverlay(); | |
| 939 | |
| 940 content::NotificationService::current()->Notify( | |
| 941 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED, | |
| 942 content::Source<InstantController>(this), | |
| 943 content::NotificationService::NoDetails()); | |
| 944 } | |
| 945 } | |
| 946 | |
| 947 void InstantController::InstantPageRenderViewGone( | |
| 948 const content::WebContents* contents) { | |
| 949 if (IsContentsFrom(overlay(), contents)) | |
| 950 BlacklistAndResetOverlay(); | |
| 951 else if (IsContentsFrom(ntp(), contents)) | |
| 952 BlacklistAndResetNTP(); | |
| 953 else | |
| 954 NOTREACHED(); | |
| 955 } | |
| 956 | |
| 957 void InstantController::InstantPageAboutToNavigateMainFrame( | |
| 958 const content::WebContents* contents, | |
| 959 const GURL& url) { | |
| 960 DCHECK(IsContentsFrom(overlay(), contents)); | |
| 961 | |
| 962 // If the page does not yet support Instant, we allow redirects and other | |
| 963 // navigations to go through since the Instant URL can redirect - e.g. to | |
| 964 // country specific pages. | |
| 965 if (!overlay_->supports_instant()) | |
| 966 return; | |
| 967 | |
| 968 GURL instant_url(overlay_->instant_url()); | |
| 969 | |
| 970 // If we are navigating to the Instant URL, do nothing. | |
| 971 if (url == instant_url) | |
| 972 return; | |
| 973 | |
| 974 // Commit the navigation if either: | |
| 975 // - The page is in NTP mode (so it could only navigate on a user click) or | |
| 976 // - The page is not in NTP mode and we are navigating to a URL with a | |
| 977 // different host or path than the Instant URL. This enables the instant | |
| 978 // page when it is showing search results to change the query parameters | |
| 979 // and fragments of the URL without it navigating. | |
| 980 if (model_.mode().is_ntp() || | |
| 981 (url.host() != instant_url.host() || url.path() != instant_url.path())) { | |
| 982 CommitIfPossible(INSTANT_COMMIT_NAVIGATED); | |
| 983 } | |
| 984 } | |
| 985 | |
| 986 void InstantController::SetSuggestions( | |
| 987 const content::WebContents* contents, | |
| 988 const std::vector<InstantSuggestion>& suggestions) { | |
| 989 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions"); | |
| 990 | |
| 991 // Ignore if the message is from an unexpected source. | |
| 992 if (IsContentsFrom(ntp(), contents)) | |
| 993 return; | |
| 994 if (instant_tab_ && !IsContentsFrom(instant_tab(), contents)) | |
| 995 return; | |
| 996 if (IsContentsFrom(overlay(), contents) && | |
| 997 !allow_overlay_to_show_search_suggestions_) | |
| 998 return; | |
| 999 | |
| 1000 InstantSuggestion suggestion; | |
| 1001 if (!suggestions.empty()) | |
| 1002 suggestion = suggestions[0]; | |
| 1003 | |
| 1004 if (instant_tab_ && search_mode_.is_search_results() && | |
| 1005 suggestion.behavior == INSTANT_COMPLETE_REPLACE) { | |
| 1006 // Update |last_omnibox_text_| so that the controller commits the proper | |
| 1007 // query if the user focuses the omnibox and presses Enter. | |
| 1008 last_omnibox_text_ = suggestion.text; | |
| 1009 last_suggestion_ = InstantSuggestion(); | |
| 1010 last_match_was_search_ = suggestion.type == INSTANT_SUGGESTION_SEARCH; | |
| 1011 // This means a committed page in state search called setValue(). We should | |
| 1012 // update the omnibox to reflect what the search page says. | |
| 1013 browser_->SetInstantSuggestion(suggestion); | |
| 1014 return; | |
| 1015 } | |
| 1016 | |
| 1017 // Ignore if we are not currently accepting search suggestions. | |
| 1018 if (!search_mode_.is_search_suggestions() || last_omnibox_text_.empty()) | |
| 1019 return; | |
| 1020 | |
| 1021 if (suggestion.behavior == INSTANT_COMPLETE_REPLACE) { | |
| 1022 // We don't get an Update() when changing the omnibox due to a REPLACE | |
| 1023 // suggestion (so that we don't inadvertently cause the overlay to change | |
| 1024 // what it's showing, as the user arrows up/down through the page-provided | |
| 1025 // suggestions). So, update these state variables here. | |
| 1026 last_omnibox_text_ = suggestion.text; | |
| 1027 last_suggestion_ = InstantSuggestion(); | |
| 1028 last_match_was_search_ = suggestion.type == INSTANT_SUGGESTION_SEARCH; | |
| 1029 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 1030 "ReplaceSuggestion text='%s' type=%d", | |
| 1031 UTF16ToUTF8(suggestion.text).c_str(), suggestion.type)); | |
| 1032 browser_->SetInstantSuggestion(suggestion); | |
| 1033 } else { | |
| 1034 bool is_valid_suggestion = true; | |
| 1035 | |
| 1036 // If the page is trying to set inline autocompletion in verbatim mode, | |
| 1037 // instead try suggesting the exact omnibox text. This makes the omnibox | |
| 1038 // interpret user text as an URL if possible while preventing unwanted | |
| 1039 // autocompletion during backspacing. | |
| 1040 if (suggestion.behavior == INSTANT_COMPLETE_NOW && last_verbatim_) | |
| 1041 suggestion.text = last_omnibox_text_; | |
| 1042 | |
| 1043 // Suggestion text should be a full URL for URL suggestions, or the | |
| 1044 // completion of a query for query suggestions. | |
| 1045 if (suggestion.type == INSTANT_SUGGESTION_URL) { | |
| 1046 // If the suggestion is not a valid URL, perhaps it's something like | |
| 1047 // "foo.com". Try prefixing "http://". If it still isn't valid, drop it. | |
| 1048 if (!GURL(suggestion.text).is_valid()) { | |
| 1049 suggestion.text.insert(0, ASCIIToUTF16("http://")); | |
| 1050 if (!GURL(suggestion.text).is_valid()) | |
| 1051 is_valid_suggestion = false; | |
| 1052 } | |
| 1053 } else if (StartsWith(suggestion.text, last_omnibox_text_, true)) { | |
| 1054 // The user typed an exact prefix of the suggestion. | |
| 1055 suggestion.text.erase(0, last_omnibox_text_.size()); | |
| 1056 } else if (!NormalizeAndStripPrefix(&suggestion.text, last_omnibox_text_)) { | |
| 1057 // Unicode normalize and case-fold the user text and suggestion. If the | |
| 1058 // user text is a prefix, suggest the normalized, case-folded completion; | |
| 1059 // for instance, if the user types 'i' and the suggestion is 'INSTANT', | |
| 1060 // suggest 'nstant'. Otherwise, the user text really isn't a prefix, so | |
| 1061 // suggest nothing. | |
| 1062 is_valid_suggestion = false; | |
| 1063 } | |
| 1064 | |
| 1065 // Don't suggest gray text if there already was inline autocompletion. | |
| 1066 // http://crbug.com/162303 | |
| 1067 if (suggestion.behavior == INSTANT_COMPLETE_NEVER && | |
| 1068 last_omnibox_text_has_inline_autocompletion_) | |
| 1069 is_valid_suggestion = false; | |
| 1070 | |
| 1071 if (is_valid_suggestion) { | |
| 1072 last_suggestion_ = suggestion; | |
| 1073 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 1074 "SetInstantSuggestion: text='%s' behavior=%d", | |
| 1075 UTF16ToUTF8(suggestion.text).c_str(), suggestion.behavior)); | |
| 1076 browser_->SetInstantSuggestion(suggestion); | |
| 1077 } else { | |
| 1078 last_suggestion_ = InstantSuggestion(); | |
| 1079 } | |
| 1080 } | |
| 1081 | |
| 1082 // Extended mode pages will call ShowOverlay() when they are ready. | |
| 1083 if (!extended_enabled_) | |
| 1084 ShowOverlay(INSTANT_SHOWN_QUERY_SUGGESTIONS, 100, INSTANT_SIZE_PERCENT); | |
| 1085 } | |
| 1086 | |
| 1087 void InstantController::ShowInstantOverlay(const content::WebContents* contents, | |
| 1088 InstantShownReason reason, | |
| 1089 int height, | |
| 1090 InstantSizeUnits units) { | |
| 1091 if (extended_enabled_ && IsContentsFrom(overlay(), contents)) | |
| 1092 ShowOverlay(reason, height, units); | |
| 1093 } | |
| 1094 | |
| 1095 void InstantController::FocusOmnibox(const content::WebContents* contents) { | |
| 1096 if (!extended_enabled_) | |
| 1097 return; | |
| 1098 | |
| 1099 DCHECK(IsContentsFrom(instant_tab(), contents)); | |
| 1100 browser_->FocusOmnibox(true); | |
| 1101 } | |
| 1102 | |
| 1103 void InstantController::StartCapturingKeyStrokes( | |
| 1104 const content::WebContents* contents) { | |
| 1105 if (!extended_enabled_) | |
| 1106 return; | |
| 1107 | |
| 1108 DCHECK(IsContentsFrom(instant_tab(), contents)); | |
| 1109 browser_->FocusOmnibox(false); | |
| 1110 } | |
| 1111 | |
| 1112 void InstantController::StopCapturingKeyStrokes( | |
| 1113 content::WebContents* contents) { | |
| 1114 // Nothing to do if omnibox doesn't have invisible focus. | |
| 1115 if (!extended_enabled_ || omnibox_focus_state_ != OMNIBOX_FOCUS_INVISIBLE) | |
| 1116 return; | |
| 1117 | |
| 1118 DCHECK(IsContentsFrom(instant_tab(), contents)); | |
| 1119 contents->GetView()->Focus(); | |
| 1120 } | |
| 1121 | |
| 1122 void InstantController::NavigateToURL(const content::WebContents* contents, | |
| 1123 const GURL& url, | |
| 1124 content::PageTransition transition, | |
| 1125 WindowOpenDisposition disposition) { | |
| 1126 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 1127 "NavigateToURL: url='%s'", url.spec().c_str())); | |
| 1128 | |
| 1129 // TODO(samarth): handle case where contents are no longer "active" (e.g. user | |
| 1130 // has switched tabs). | |
| 1131 if (!extended_enabled_) | |
| 1132 return; | |
| 1133 if (overlay_) | |
| 1134 HideOverlay(); | |
| 1135 browser_->OpenURL(url, transition, disposition); | |
| 1136 } | |
| 1137 | |
| 1138 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { | |
| 1139 // If the overlay is showing custom NTP content, don't hide it, commit it | |
| 1140 // (no matter where the user clicked) or try to recreate it. | |
| 1141 if (model_.mode().is_ntp()) | |
| 1142 return; | |
| 1143 | |
| 1144 if (model_.mode().is_default()) { | |
| 1145 // Correct search terms if the user clicked on the committed results page | |
| 1146 // while showing an autocomplete suggestion | |
| 1147 if (instant_tab_ && !last_suggestion_.text.empty() && | |
| 1148 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER && | |
| 1149 IsViewInContents(GetViewGainingFocus(view_gaining_focus), | |
| 1150 instant_tab_->contents())) { | |
| 1151 // Commit the omnibox's suggested grey text as if the user had typed it. | |
| 1152 browser_->CommitSuggestedText(true); | |
| 1153 | |
| 1154 // Update the state so that next query from hitting Enter from the | |
| 1155 // omnibox is correct. | |
| 1156 last_omnibox_text_ += last_suggestion_.text; | |
| 1157 last_suggestion_ = InstantSuggestion(); | |
| 1158 } | |
| 1159 // If the overlay is not showing at all, recreate it if it's stale. | |
| 1160 ReloadOverlayIfStale(); | |
| 1161 MaybeSwitchToRemoteOverlay(); | |
| 1162 return; | |
| 1163 } | |
| 1164 | |
| 1165 // The overlay is showing search suggestions. If GetOverlayContents() is NULL, | |
| 1166 // we are in the commit path. Don't do anything. | |
| 1167 if (!GetOverlayContents()) | |
| 1168 return; | |
| 1169 | |
| 1170 #if defined(OS_MACOSX) | |
| 1171 // TODO(sreeram): See if Mac really needs this special treatment. | |
| 1172 if (!overlay_->is_pointer_down_from_activate()) | |
| 1173 HideOverlay(); | |
| 1174 #else | |
| 1175 if (IsFullHeight(model_)) | |
| 1176 CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); | |
| 1177 else if (!IsViewInContents(GetViewGainingFocus(view_gaining_focus), | |
| 1178 overlay_->contents())) | |
| 1179 HideOverlay(); | |
| 1180 #endif | |
| 1181 } | |
| 1182 | |
| 1183 void InstantController::ResetNTP() { | |
| 1184 ntp_.reset(); | |
| 1185 std::string instant_url; | |
| 1186 if (!GetInstantURL(browser_->profile(), false, &instant_url)) | |
| 1187 return; | |
| 1188 | |
| 1189 ntp_.reset(new InstantNTP(this, instant_url)); | |
| 1190 ntp_->InitContents(browser_->profile(), browser_->GetActiveWebContents(), | |
| 1191 base::Bind(&InstantController::ResetNTP, | |
| 1192 base::Unretained(this))); | |
| 1193 } | |
| 1194 | |
| 1195 bool InstantController::EnsureOverlayIsCurrent(bool ignore_blacklist) { | |
| 1196 // If there's no active tab, the browser is closing. | |
| 1197 const content::WebContents* active_tab = browser_->GetActiveWebContents(); | |
| 1198 if (!active_tab) | |
| 1199 return false; | |
| 1200 | |
| 1201 Profile* profile = Profile::FromBrowserContext( | |
| 1202 active_tab->GetBrowserContext()); | |
| 1203 std::string instant_url; | |
| 1204 if (!GetInstantURL(profile, ignore_blacklist, &instant_url)) { | |
| 1205 // If we are in extended mode, fallback to the local overlay. | |
| 1206 if (extended_enabled_) | |
| 1207 instant_url = chrome::search::kLocalOmniboxPopupURL; | |
| 1208 else | |
| 1209 return false; | |
| 1210 } | |
| 1211 | |
| 1212 if (!overlay_ || overlay_->instant_url() != instant_url) | |
| 1213 CreateOverlay(instant_url, active_tab); | |
| 1214 | |
| 1215 return true; | |
| 1216 } | |
| 1217 | |
| 1218 void InstantController::CreateOverlay(const std::string& instant_url, | |
| 1219 const content::WebContents* active_tab) { | |
| 1220 HideInternal(); | |
| 1221 overlay_.reset(new InstantOverlay(this, instant_url)); | |
| 1222 overlay_->InitContents(browser_->profile(), active_tab); | |
| 1223 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 1224 "CreateOverlay: instant_url='%s'", instant_url.c_str())); | |
| 1225 } | |
| 1226 | |
| 1227 void InstantController::MaybeSwitchToRemoteOverlay() { | |
| 1228 if (!overlay_ || omnibox_focus_state_ != OMNIBOX_FOCUS_NONE || | |
| 1229 !model_.mode().is_default()) { | |
| 1230 return; | |
| 1231 } | |
| 1232 | |
| 1233 EnsureOverlayIsCurrent(false); | |
| 1234 } | |
| 1235 | |
| 1236 void InstantController::ResetInstantTab() { | |
| 1237 // Do not wire up the InstantTab if Instant should only use local overlays, to | |
| 1238 // prevent it from sending data to the page. | |
| 1239 if (!search_mode_.is_origin_default() && !use_local_overlay_only_) { | |
| 1240 content::WebContents* active_tab = browser_->GetActiveWebContents(); | |
| 1241 if (!instant_tab_ || active_tab != instant_tab_->contents()) { | |
| 1242 instant_tab_.reset(new InstantTab(this)); | |
| 1243 instant_tab_->Init(active_tab); | |
| 1244 // Update theme info for this tab. | |
| 1245 browser_->UpdateThemeInfo(); | |
| 1246 instant_tab_->SetDisplayInstantResults(instant_enabled_); | |
| 1247 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | |
| 1248 instant_tab_->InitializeFonts(); | |
| 1249 instant_tab_->GrantChromeSearchAccessFromOrigin(active_tab->GetURL()); | |
| 1250 StartListeningToMostVisitedChanges(); | |
| 1251 instant_tab_->KeyCaptureChanged( | |
| 1252 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | |
| 1253 } | |
| 1254 | |
| 1255 // Hide the |overlay_| since we are now using |instant_tab_| instead. | |
| 1256 HideOverlay(); | |
| 1257 } else { | |
| 1258 instant_tab_.reset(); | |
| 1259 } | |
| 1260 } | |
| 1261 | |
| 1262 void InstantController::HideOverlay() { | |
| 1263 HideInternal(); | |
| 1264 ReloadOverlayIfStale(); | |
| 1265 MaybeSwitchToRemoteOverlay(); | |
| 1266 } | |
| 1267 | |
| 1268 void InstantController::HideInternal() { | |
| 1269 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); | |
| 1270 | |
| 1271 // If GetOverlayContents() returns NULL, either we're already in the desired | |
| 1272 // MODE_DEFAULT state, or we're in the commit path. For the latter, don't | |
| 1273 // change the state just yet; else we may hide the overlay unnecessarily. | |
| 1274 // Instead, the state will be set correctly after the commit is done. | |
| 1275 if (GetOverlayContents()) { | |
| 1276 model_.SetOverlayState(chrome::search::Mode(), 0, INSTANT_SIZE_PERCENT); | |
| 1277 allow_overlay_to_show_search_suggestions_ = false; | |
| 1278 | |
| 1279 // Send a message asking the overlay to clear out old results. | |
| 1280 overlay_->Update(string16(), 0, 0, true); | |
| 1281 } | |
| 1282 | |
| 1283 // Clear the first interaction timestamp for later use. | |
| 1284 first_interaction_time_ = base::Time(); | |
| 1285 } | |
| 1286 | |
| 1287 void InstantController::ShowOverlay(InstantShownReason reason, | |
| 1288 int height, | |
| 1289 InstantSizeUnits units) { | |
| 1290 // If we are on a committed search results page, the |overlay_| is not in use. | |
| 1291 if (instant_tab_) | |
| 1292 return; | |
| 1293 | |
| 1294 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 1295 "Show: reason=%d height=%d units=%d", reason, height, units)); | |
| 1296 | |
| 1297 // INSTANT_SHOWN_CUSTOM_NTP_CONTENT is no longer supported. | |
| 1298 // TODO(samarth): remove once the server has been updated. | |
| 1299 if (reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT) | |
| 1300 return; | |
| 1301 | |
| 1302 // Must have updated omnibox after the last HideOverlay() to show suggestions. | |
| 1303 if (reason == INSTANT_SHOWN_QUERY_SUGGESTIONS && | |
| 1304 !allow_overlay_to_show_search_suggestions_) | |
| 1305 return; | |
| 1306 | |
| 1307 // The page is trying to hide itself. Hide explicitly (i.e., don't use | |
| 1308 // HideOverlay()) so that it can change its mind. | |
| 1309 if (height == 0) { | |
| 1310 model_.SetOverlayState(chrome::search::Mode(), 0, INSTANT_SIZE_PERCENT); | |
| 1311 return; | |
| 1312 } | |
| 1313 | |
| 1314 // If the overlay is being shown for the first time since the user started | |
| 1315 // typing, record a histogram value. | |
| 1316 if (!first_interaction_time_.is_null() && model_.mode().is_default()) { | |
| 1317 base::TimeDelta delta = base::Time::Now() - first_interaction_time_; | |
| 1318 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta); | |
| 1319 } | |
| 1320 | |
| 1321 // Show at 100% height except in the following cases: | |
| 1322 // - The local overlay (omnibox popup) is being loaded. | |
| 1323 // - Instant is disabled. The page needs to be able to show only a dropdown. | |
| 1324 // - The page wants to show custom NTP content. | |
| 1325 // - The page is over a website other than search or an NTP, and is not | |
| 1326 // already showing at 100% height. | |
| 1327 if (overlay_->IsUsingLocalOverlay() || !instant_enabled_ || | |
| 1328 reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT || | |
| 1329 (search_mode_.is_origin_default() && !IsFullHeight(model_))) | |
| 1330 model_.SetOverlayState(search_mode_, height, units); | |
| 1331 else | |
| 1332 model_.SetOverlayState(search_mode_, 100, INSTANT_SIZE_PERCENT); | |
| 1333 | |
| 1334 // If the overlay is being shown at full height and the omnibox is not | |
| 1335 // focused, commit right away. | |
| 1336 if (IsFullHeight(model_) && omnibox_focus_state_ == OMNIBOX_FOCUS_NONE) | |
| 1337 CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); | |
| 1338 } | |
| 1339 | |
| 1340 void InstantController::SendPopupBoundsToPage() { | |
| 1341 if (last_popup_bounds_ == popup_bounds_ || !overlay_ || | |
| 1342 overlay_->is_pointer_down_from_activate()) | |
| 1343 return; | |
| 1344 | |
| 1345 last_popup_bounds_ = popup_bounds_; | |
| 1346 gfx::Rect overlay_bounds = browser_->GetInstantBounds(); | |
| 1347 gfx::Rect intersection = gfx::IntersectRects(popup_bounds_, overlay_bounds); | |
| 1348 | |
| 1349 // Translate into window coordinates. | |
| 1350 if (!intersection.IsEmpty()) { | |
| 1351 intersection.Offset(-overlay_bounds.origin().x(), | |
| 1352 -overlay_bounds.origin().y()); | |
| 1353 } | |
| 1354 | |
| 1355 // In the current Chrome UI, these must always be true so they sanity check | |
| 1356 // the above operations. In a future UI, these may be removed or adjusted. | |
| 1357 // There is no point in sanity-checking |intersection.y()| because the omnibox | |
| 1358 // can be placed anywhere vertically relative to the overlay (for example, in | |
| 1359 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds). | |
| 1360 DCHECK_LE(0, intersection.x()); | |
| 1361 DCHECK_LE(0, intersection.width()); | |
| 1362 DCHECK_LE(0, intersection.height()); | |
| 1363 | |
| 1364 overlay_->SetPopupBounds(intersection); | |
| 1365 } | |
| 1366 | |
| 1367 bool InstantController::GetInstantURL(Profile* profile, | |
| 1368 bool ignore_blacklist, | |
| 1369 std::string* instant_url) const { | |
| 1370 DCHECK(profile); | |
| 1371 instant_url->clear(); | |
| 1372 | |
| 1373 if (extended_enabled_ && use_local_overlay_only_) { | |
| 1374 *instant_url = chrome::search::kLocalOmniboxPopupURL; | |
| 1375 return true; | |
| 1376 } | |
| 1377 | |
| 1378 const GURL instant_url_obj = chrome::search::GetInstantURL(profile); | |
| 1379 if (!instant_url_obj.is_valid()) | |
| 1380 return false; | |
| 1381 | |
| 1382 *instant_url = instant_url_obj.spec(); | |
| 1383 | |
| 1384 if (!ignore_blacklist) { | |
| 1385 std::map<std::string, int>::const_iterator iter = | |
| 1386 blacklisted_urls_.find(*instant_url); | |
| 1387 if (iter != blacklisted_urls_.end() && | |
| 1388 iter->second > kMaxInstantSupportFailures) { | |
| 1389 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); | |
| 1390 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( | |
| 1391 "GetInstantURL: Instant URL blacklisted: url=%s", | |
| 1392 instant_url->c_str())); | |
| 1393 return false; | |
| 1394 } | |
| 1395 } | |
| 1396 | |
| 1397 return true; | |
| 1398 } | |
| 1399 | |
| 1400 void InstantController::BlacklistAndResetNTP() { | |
| 1401 ++blacklisted_urls_[ntp_->instant_url()]; | |
| 1402 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_ADDED_TO_BLACKLIST); | |
| 1403 delete ntp_->ReleaseContents().release(); | |
| 1404 MessageLoop::current()->DeleteSoon(FROM_HERE, ntp_.release()); | |
| 1405 ResetNTP(); | |
| 1406 } | |
| 1407 | |
| 1408 void InstantController::BlacklistAndResetOverlay() { | |
| 1409 ++blacklisted_urls_[overlay_->instant_url()]; | |
| 1410 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_ADDED_TO_BLACKLIST); | |
| 1411 HideInternal(); | |
| 1412 delete overlay_->ReleaseContents().release(); | |
| 1413 MessageLoop::current()->DeleteSoon(FROM_HERE, overlay_.release()); | |
| 1414 EnsureOverlayIsCurrent(false); | |
| 1415 } | |
| 1416 | |
| 1417 void InstantController::RemoveFromBlacklist(const std::string& url) { | |
| 1418 if (blacklisted_urls_.erase(url)) { | |
| 1419 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_REMOVED_FROM_BLACKLIST); | |
| 1420 } | |
| 1421 } | |
| 1422 | |
| 1423 void InstantController::StartListeningToMostVisitedChanges() { | |
| 1424 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | |
| 1425 if (top_sites) { | |
| 1426 if (!registrar_.IsRegistered( | |
| 1427 this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | |
| 1428 content::Source<history::TopSites>(top_sites))) { | |
| 1429 // TopSites updates itself after a delay. This is especially noticable | |
| 1430 // when your profile is empty. Ask TopSites to update itself when we're | |
| 1431 // about to show the new tab page. | |
| 1432 top_sites->SyncWithHistory(); | |
| 1433 | |
| 1434 RequestMostVisitedItems(); | |
| 1435 | |
| 1436 // Register for notification when TopSites changes. | |
| 1437 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | |
| 1438 content::Source<history::TopSites>(top_sites)); | |
| 1439 } else { | |
| 1440 // We are already registered, so just get and send the most visited data. | |
| 1441 RequestMostVisitedItems(); | |
| 1442 } | |
| 1443 } | |
| 1444 } | |
| 1445 | |
| 1446 void InstantController::RequestMostVisitedItems() { | |
| 1447 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | |
| 1448 if (top_sites) { | |
| 1449 top_sites->GetMostVisitedURLs( | |
| 1450 base::Bind(&InstantController::OnMostVisitedItemsReceived, | |
| 1451 weak_ptr_factory_.GetWeakPtr())); | |
| 1452 } | |
| 1453 } | |
| 1454 | |
| 1455 void InstantController::OnMostVisitedItemsReceived( | |
| 1456 const history::MostVisitedURLList& data) { | |
| 1457 std::vector<MostVisitedItem> most_visited_items; | |
| 1458 for (size_t i = 0; i < data.size(); i++) { | |
| 1459 const history::MostVisitedURL& url = data[i]; | |
| 1460 | |
| 1461 MostVisitedItem item; | |
| 1462 item.url = url.url; | |
| 1463 item.title = url.title; | |
| 1464 | |
| 1465 most_visited_items.push_back(item); | |
| 1466 } | |
| 1467 SendMostVisitedItems(most_visited_items); | |
| 1468 } | |
| 1469 | |
| 1470 void InstantController::SendMostVisitedItems( | |
| 1471 const std::vector<MostVisitedItem>& items) { | |
| 1472 if (overlay_) | |
| 1473 overlay_->SendMostVisitedItems(items); | |
| 1474 if (ntp_) | |
| 1475 ntp_->SendMostVisitedItems(items); | |
| 1476 if (instant_tab_) | |
| 1477 instant_tab_->SendMostVisitedItems(items); | |
| 1478 content::NotificationService::current()->Notify( | |
| 1479 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, | |
| 1480 content::Source<InstantController>(this), | |
| 1481 content::NotificationService::NoDetails()); | |
| 1482 } | |
| OLD | NEW |