summaryrefslogtreecommitdiffstats
path: root/chrome/browser/instant/instant_controller.cc
blob: 4f8ba0185e950b99b5384f76fc52e08bb3158bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/instant/instant_controller.h"

#include "base/command_line.h"
#include "base/i18n/case_conversion.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autocomplete/autocomplete_provider.h"
#include "chrome/browser/google/google_util.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/history/history_tab_helper.h"
#include "chrome/browser/instant/instant_loader.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser_instant_controller.h"
#include "chrome/browser/ui/search/search.h"
#include "chrome/browser/ui/search/search_tab_helper.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "net/base/escape.h"
#include "unicode/normalizer2.h"
#include "unicode/unistr.h"

#if defined(TOOLKIT_VIEWS)
#include "ui/views/widget/widget.h"
#endif

namespace {

// An artificial delay (in milliseconds) we introduce before telling the Instant
// page about the new omnibox bounds, in cases where the bounds shrink. This is
// to avoid the page jumping up/down very fast in response to bounds changes.
const int kUpdateBoundsDelayMS = 1000;

// The maximum number of times we'll load a non-Instant-supporting search engine
// before we give up and blacklist it for the rest of the browsing session.
const int kMaxInstantSupportFailures = 10;

// If an Instant page has not been used in these many milliseconds, it is
// reloaded so that the page does not become stale.
const int kStaleLoaderTimeoutMS = 3 * 3600 * 1000;

std::string ModeToString(InstantController::Mode mode) {
  switch (mode) {
    case InstantController::EXTENDED: return "_Extended";
    case InstantController::INSTANT:  return "_Instant";
    case InstantController::DISABLED: return "_Disabled";
  }

  NOTREACHED();
  return std::string();
}

void AddSessionStorageHistogram(InstantController::Mode mode,
                                const TabContents* tab1,
                                const TabContents* tab2) {
  base::Histogram* histogram = base::BooleanHistogram::FactoryGet(
      "Instant.SessionStorageNamespace" + ModeToString(mode),
      base::Histogram::kUmaTargetedHistogramFlag);
  const content::SessionStorageNamespaceMap& session_storage_map1 =
      tab1->web_contents()->GetController().GetSessionStorageNamespaceMap();
  const content::SessionStorageNamespaceMap& session_storage_map2 =
      tab2->web_contents()->GetController().GetSessionStorageNamespaceMap();
  bool is_session_storage_the_same =
      session_storage_map1.size() == session_storage_map2.size();
  if (is_session_storage_the_same) {
    // The size is the same, so let's check that all entries match.
    for (content::SessionStorageNamespaceMap::const_iterator
             it1 = session_storage_map1.begin(),
             it2 = session_storage_map2.begin();
         it1 != session_storage_map1.end() &&
             it2 != session_storage_map2.end();
         ++it1, ++it2) {
      if (it1->first != it2->first || it1->second != it2->second) {
        is_session_storage_the_same = false;
        break;
      }
    }
  }
  histogram->AddBoolean(is_session_storage_the_same);
}

InstantController::Mode GetModeForProfile(Profile* profile) {
  if (chrome::search::IsInstantExtendedAPIEnabled(profile))
    return InstantController::EXTENDED;

  if (!profile || profile->IsOffTheRecord() || !profile->GetPrefs() ||
      !profile->GetPrefs()->GetBoolean(prefs::kInstantEnabled))
    return InstantController::DISABLED;

  return InstantController::INSTANT;
}

string16 Normalize(const string16& str) {
  UErrorCode status = U_ZERO_ERROR;
  const icu::Normalizer2* normalizer =
      icu::Normalizer2::getInstance(NULL, "nfkc_cf", UNORM2_COMPOSE, status);
  if (normalizer == NULL || U_FAILURE(status))
    return str;
  icu::UnicodeString norm_str(normalizer->normalize(
      icu::UnicodeString(FALSE, str.c_str(), str.size()), status));
  if (U_FAILURE(status))
    return str;
  return string16(norm_str.getBuffer(), norm_str.length());
}

bool NormalizeAndStripPrefix(string16* text, const string16& prefix) {
  const string16 norm_prefix = Normalize(prefix);
  string16 norm_text = Normalize(*text);
  if (norm_prefix.size() <= norm_text.size() &&
      norm_text.compare(0, norm_prefix.size(), norm_prefix) == 0) {
    *text = norm_text.erase(0, norm_prefix.size());
    return true;
  }
  return false;
}

InstantModel::PreviewState GetNewPreviewState(InstantShownReason reason) {
  return reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT ?
      InstantModel::CUSTOM_NTP_CONTENT : InstantModel::QUERY_RESULTS;
}

}  // namespace

InstantController::~InstantController() {
}

// static
InstantController* InstantController::CreateInstant(
    Profile* profile,
    chrome::BrowserInstantController* browser) {
  const Mode mode = GetModeForProfile(profile);
  return mode == DISABLED ? NULL : new InstantController(browser, mode);
}

// static
bool InstantController::IsExtendedAPIEnabled(Profile* profile) {
  return GetModeForProfile(profile) == EXTENDED;
}

// static
bool InstantController::IsInstantEnabled(Profile* profile) {
  const Mode mode = GetModeForProfile(profile);
  return mode == EXTENDED || mode == INSTANT;
}

// static
void InstantController::RegisterUserPrefs(PrefService* prefs) {
  prefs->RegisterBooleanPref(prefs::kInstantConfirmDialogShown, false,
                             PrefService::SYNCABLE_PREF);
  prefs->RegisterBooleanPref(prefs::kInstantEnabled, false,
                             PrefService::SYNCABLE_PREF);
}

bool InstantController::Update(const AutocompleteMatch& match,
                               const string16& user_text,
                               const string16& full_text,
                               bool verbatim) {
  const TabContents* active_tab = browser_->GetActiveTabContents();

  // We could get here with no active tab if the Browser is closing.
  if (!active_tab) {
    Hide();
    return false;
  }

  std::string instant_url;
  Profile* profile = active_tab->profile();

  // If the match's TemplateURL is valid, it's a search query; use it. If it's
  // not valid, it's likely a URL; in EXTENDED mode, try using the default
  // search engine's TemplateURL instead.
  const GURL& tab_url = active_tab->web_contents()->GetURL();
  if (GetInstantURL(match.GetTemplateURL(profile, false), tab_url,
                    &instant_url)) {
    ResetLoader(instant_url, active_tab);
  } else if (mode_ != EXTENDED || !CreateDefaultLoader()) {
    Hide();
    return false;
  }

  if (full_text.empty()) {
    // In extended mode, blank the preview instead of flashing the NTP when the
    // user backspaces out the omnibox text. (Assume they still intend to edit,
    // so showing the NTP would just be distracting.)
    if (mode_ == EXTENDED)
      SendBlankQuery();
    else
      Hide();
    return false;
  }

  // Track the non-Instant search URL for this query.
  url_for_history_ = match.destination_url;
  last_transition_type_ = match.transition;
  last_match_was_search_ = AutocompleteMatch::IsSearchType(match.type);

  // If the query text hasn't changed, don't send an update to the loader. Just
  // reuse the last suggestion, as it's still valid.
  if (user_text == last_user_text_ && verbatim == last_verbatim_) {
    browser_->SetInstantSuggestion(last_suggestion_);
    return last_match_was_search_;
  }

  // The last suggestion and its associated parameters can be preserved if the
  // user continues typing the same query as the suggested text is showing.
  // Suggestions are only reused with INSTANT_COMPLETE_NEVER behavior.
  bool reused_suggestion = false;
  if (last_suggestion_.behavior == INSTANT_COMPLETE_NEVER) {
    if (StartsWith(user_text, last_user_text_, false)) {
      // If the new user text is longer than the last user text, we need to
      // normalize any added characters.
      reused_suggestion = NormalizeAndStripPrefix(&last_suggestion_.text,
          string16(user_text, last_user_text_.size()));
    } else if (StartsWith(last_user_text_, user_text, false)) {
      // If the new user text is a prefix of the last user text, no
      // normalization is necessary.
      last_suggestion_.text.insert(0, last_user_text_, user_text.size(),
                                   last_user_text_.size() - user_text.size());
      reused_suggestion = true;
    }
  }

  last_user_text_ = user_text;
  last_full_text_ = full_text;
  last_verbatim_ = verbatim;
  if (!reused_suggestion)
    last_suggestion_ = InstantSuggestion();
  if (model_.preview_state() == InstantModel::NOT_READY) {
    model_.SetPreviewState(InstantModel::AWAITING_SUGGESTIONS,
                           0, INSTANT_SIZE_PERCENT);
  }

  // Store the first interaction time for use with latency histograms.
  if (first_interaction_time_.is_null())
    first_interaction_time_ = base::Time::Now();

  loader_->Update(mode_ == EXTENDED ? user_text : full_text, verbatim);

  content::NotificationService::current()->Notify(
      chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED,
      content::Source<InstantController>(this),
      content::NotificationService::NoDetails());

  // We don't have new suggestions yet, but we can either reuse the existing
  // suggestion or reset the existing "gray text".
  browser_->SetInstantSuggestion(last_suggestion_);

  // Though we may have handled a URL match above, we return false here, so that
  // omnibox prerendering can kick in. TODO(sreeram): Remove this (and always
  // return true) once we are able to commit URLs as well.
  return last_match_was_search_;
}

// TODO(tonyg): This method only fires when the omnibox bounds change. It also
// needs to fire when the preview bounds change (e.g.: open/close info bar).
void InstantController::SetOmniboxBounds(const gfx::Rect& bounds) {
  if (omnibox_bounds_ == bounds)
    return;

  omnibox_bounds_ = bounds;
  if (omnibox_bounds_.height() > last_omnibox_bounds_.height()) {
    update_bounds_timer_.Stop();
    SendBoundsToPage();
  } else if (!update_bounds_timer_.IsRunning()) {
    update_bounds_timer_.Start(FROM_HERE,
        base::TimeDelta::FromMilliseconds(kUpdateBoundsDelayMS), this,
        &InstantController::SendBoundsToPage);
  }
}

void InstantController::HandleAutocompleteResults(
    const std::vector<AutocompleteProvider*>& providers) {
  if (mode_ != EXTENDED || !GetPreviewContents())
    return;

  std::vector<InstantAutocompleteResult> results;
  for (ACProviders::const_iterator provider = providers.begin();
       provider != providers.end(); ++provider) {
    for (ACMatches::const_iterator match = (*provider)->matches().begin();
         match != (*provider)->matches().end(); ++match) {
      InstantAutocompleteResult result;
      result.provider = UTF8ToUTF16((*provider)->GetName());
      result.type = UTF8ToUTF16(AutocompleteMatch::TypeToString(match->type));
      result.description = match->description;
      result.destination_url = UTF8ToUTF16(match->destination_url.spec());
      result.relevance = match->relevance;
      results.push_back(result);
    }
  }

  loader_->SendAutocompleteResults(results);
}

bool InstantController::OnUpOrDownKeyPressed(int count) {
  if (mode_ != EXTENDED || !GetPreviewContents())
    return false;

  loader_->OnUpOrDownKeyPressed(count);
  return true;
}

void InstantController::OnEscapeKeyPressed() {
  if (mode_ == EXTENDED &&
      active_tab_mode_.is_search_suggestions() &&
      active_tab_mode_.is_origin_ntp() &&
      loader_.get() && loader_->HasShownCustomNTPContent()) {
    // We are showing search suggestions on top of an extended mode NTP where
    // the page previously showed custom NTP content. The user pressed escape
    // so will return to the NTP. To avoid janky flicker, assume the page will
    // show custom NTP content again, and do not hide it.

    // TODO(jered): Still clearing the uncommitted query here because otherwise
    // the page behaves improperly on repeating the same query immediately. This
    // behavior was not apparent when we used to Hide() in this case. Find out
    // why, and then remove this call or this TODO.
    SendBlankQuery();
    return;
  }

  if (model_.preview_state() != InstantModel::CUSTOM_NTP_CONTENT)
    Hide();
}

TabContents* InstantController::GetPreviewContents() const {
  return loader_.get() ? loader_->preview_contents() : NULL;
}

void InstantController::Hide() {
  // The only time when the preview is not already in the desired NOT_READY
  // state and GetPreviewContents() returns NULL is when we are in the commit
  // path. In that case, don't change the state just yet; otherwise we may
  // cause the preview to hide unnecessarily. Instead, the state will be set
  // correctly after the commit is done.
  if (GetPreviewContents())
    model_.SetPreviewState(InstantModel::NOT_READY, 0, INSTANT_SIZE_PERCENT);

  // Clear the first interaction timestamp for later use.
  first_interaction_time_ = base::Time();

  if (GetPreviewContents() && !last_full_text_.empty()) {
    // Send a blank query to ask the preview to clear out old results.
    SendBlankQuery();
  }
}

void InstantController::SendBlankQuery() {
  last_full_text_.clear();
  last_user_text_.clear();
  loader_->Update(last_full_text_, true);
}

bool InstantController::IsCurrent() const {
  return model_.preview_state() == InstantModel::QUERY_RESULTS &&
         GetPreviewContents() &&
         loader_->supports_instant() && last_match_was_search_;
}

void InstantController::CommitCurrentPreview(InstantCommitType type) {
  TabContents* preview = loader_->ReleasePreviewContents(type, last_full_text_);

  if (mode_ == EXTENDED) {
    // Consider what's happening:
    //   1. The user has typed a query in the omnibox and committed it (either
    //      by pressing Enter or clicking on the preview).
    //   2. We commit the preview to the tab strip, and tell the page.
    //   3. The page will update the URL hash fragment with the query terms.
    // After steps 1 and 3, the omnibox will show the query terms. However, if
    // the URL we are committing at step 2 doesn't already have query terms, it
    // will flash for a brief moment as a plain URL. So, avoid that flicker by
    // pretending that the plain URL is actually the typed query terms.
    // TODO(samarth,beaudoin): Instead of this hack, we should add a new field
    // to NavigationEntry to keep track of what the correct query, if any, is.
    content::NavigationEntry* entry =
        preview->web_contents()->GetController().GetVisibleEntry();
    std::string url = entry->GetVirtualURL().spec();
    if (!google_util::IsInstantExtendedAPIGoogleSearchUrl(url) &&
        google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN,
                                       google_util::ALLOW_NON_STANDARD_PORTS)) {
      entry->SetVirtualURL(GURL(
          url + "#q=" +
          net::EscapeQueryParamValue(UTF16ToUTF8(last_full_text_), true)));
      chrome::search::SearchTabHelper::FromWebContents(
          preview->web_contents())->NavigationEntryUpdated();
    }
  }

  // If the preview page has navigated since the last Update(), we need to add
  // the navigation to history ourselves. Else, the page will navigate after
  // commit, and it will be added to history in the usual manner.
  const history::HistoryAddPageArgs& last_navigation =
      loader_->last_navigation();
  if (!last_navigation.url.is_empty()) {
    content::NavigationEntry* entry =
        preview->web_contents()->GetController().GetActiveEntry();
    DCHECK_EQ(last_navigation.url, entry->GetURL());

    // Add the page to history.
    HistoryTabHelper* history_tab_helper =
        HistoryTabHelper::FromWebContents(preview->web_contents());
    history_tab_helper->UpdateHistoryForNavigation(last_navigation);

    // Update the page title.
    history_tab_helper->UpdateHistoryPageTitle(*entry);
  }

  // Add a fake history entry with a non-Instant search URL, so that search
  // terms extraction (for autocomplete history matches) works.
  if (HistoryService* history = HistoryServiceFactory::GetForProfile(
          preview->profile(), Profile::EXPLICIT_ACCESS)) {
    history->AddPage(url_for_history_, base::Time::Now(), NULL, 0, GURL(),
                     history::RedirectList(), last_transition_type_,
                     history::SOURCE_BROWSED, false);
  }

  DeleteLoader();

  preview->web_contents()->GetController().PruneAllButActive();

  if (type != INSTANT_COMMIT_PRESSED_ALT_ENTER) {
    const TabContents* active_tab = browser_->GetActiveTabContents();
    AddSessionStorageHistogram(mode_, active_tab, preview);
    preview->web_contents()->GetController().CopyStateFromAndPrune(
        &active_tab->web_contents()->GetController());
  }

  // Browser takes ownership of the preview.
  browser_->CommitInstant(preview, type == INSTANT_COMMIT_PRESSED_ALT_ENTER);

  content::NotificationService::current()->Notify(
      chrome::NOTIFICATION_INSTANT_COMMITTED,
      content::Source<content::WebContents>(preview->web_contents()),
      content::NotificationService::NoDetails());

  model_.SetPreviewState(InstantModel::NOT_READY, 0, INSTANT_SIZE_PERCENT);

  // Try to create another loader immediately so that it is ready for the next
  // user interaction.
  CreateDefaultLoader();
}

void InstantController::OnAutocompleteLostFocus(
    gfx::NativeView view_gaining_focus) {
  is_omnibox_focused_ = false;

  // If there is no preview, nothing to do.
  if (!GetPreviewContents())
    return;

  // It's bizarre if custom NTP content disappears when the user focuses
  // outside the omnibox.
  if (model_.preview_state() == InstantModel::CUSTOM_NTP_CONTENT)
    return;

  // If the preview is not showing, only need to check for loader staleness.
  if (model_.preview_state() == InstantModel::NOT_READY) {
    MaybeOnStaleLoader();
    return;
  }

#if defined(OS_MACOSX)
  if (!loader_->IsPointerDownFromActivate()) {
    Hide();
    MaybeOnStaleLoader();
  }
#else
  content::RenderWidgetHostView* rwhv =
      GetPreviewContents()->web_contents()->GetRenderWidgetHostView();
  if (!view_gaining_focus || !rwhv) {
    Hide();
    MaybeOnStaleLoader();
    return;
  }

#if defined(TOOLKIT_VIEWS)
  // For views the top level widget is always focused. If the focus change
  // originated in views determine the child Widget from the view that is being
  // focused.
  views::Widget* widget =
      views::Widget::GetWidgetForNativeView(view_gaining_focus);
  if (widget) {
    views::FocusManager* focus_manager = widget->GetFocusManager();
    if (focus_manager && focus_manager->is_changing_focus() &&
        focus_manager->GetFocusedView() &&
        focus_manager->GetFocusedView()->GetWidget()) {
      view_gaining_focus =
          focus_manager->GetFocusedView()->GetWidget()->GetNativeView();
    }
  }
#endif

  gfx::NativeView tab_view =
      GetPreviewContents()->web_contents()->GetNativeView();

  // Focus is going to the renderer.
  if (rwhv->GetNativeView() == view_gaining_focus ||
      tab_view == view_gaining_focus) {

    // If the mouse is not down, focus is not going to the renderer. Someone
    // else moved focus and we shouldn't commit.
    if (!loader_->IsPointerDownFromActivate()) {
      Hide();
      MaybeOnStaleLoader();
    }

    return;
  }

  // Walk up the view hierarchy. If the view gaining focus is a subview of the
  // WebContents view (such as a windowed plugin or http auth dialog), we want
  // to keep the preview contents. Otherwise, focus has gone somewhere else,
  // such as the JS inspector, and we want to cancel the preview.
  gfx::NativeView view_gaining_focus_ancestor = view_gaining_focus;
  while (view_gaining_focus_ancestor &&
         view_gaining_focus_ancestor != tab_view) {
    view_gaining_focus_ancestor =
        platform_util::GetParent(view_gaining_focus_ancestor);
  }

  if (view_gaining_focus_ancestor) {
    CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST);
    return;
  }

  Hide();
  MaybeOnStaleLoader();
#endif
}

void InstantController::OnAutocompleteGotFocus() {
  is_omnibox_focused_ = true;
  CreateDefaultLoader();
}

void InstantController::OnActiveTabModeChanged(chrome::search::Mode new_mode) {
  const bool is_ntp_changed = active_tab_mode_.is_ntp() != new_mode.is_ntp();
  active_tab_mode_ = new_mode;
  if (is_ntp_changed && GetPreviewContents()) {
    loader_->OnActiveTabModeChanged(new_mode.is_ntp());
    // On navigation away from the NTP, hide custom content. However keep
    // the preview shown if searching, since it will soon show a suggestions
    // dropdown and hiding it would cause flicker.
    if (new_mode.is_default() &&
        model_.preview_state() == InstantModel::CUSTOM_NTP_CONTENT)
      Hide();
  }
}

bool InstantController::commit_on_pointer_release() const {
  return GetPreviewContents() && loader_->IsPointerDownFromActivate();
}

void InstantController::SetSuggestions(
    InstantLoader* loader,
    const std::vector<InstantSuggestion>& suggestions) {
  if (loader_ != loader || model_.preview_state() == InstantModel::NOT_READY)
    return;

  InstantSuggestion suggestion;
  if (!suggestions.empty())
    suggestion = suggestions[0];

  if (suggestion.behavior == INSTANT_COMPLETE_REPLACE) {
    // We don't get an Update() when changing the omnibox due to a REPLACE
    // suggestion (so that we don't inadvertently cause the preview to change
    // what it's showing, as the user arrows up/down through the page-provided
    // suggestions). So, update these state variables here.
    last_full_text_ = suggestion.text;
    last_user_text_.clear();
    last_verbatim_ = true;
    last_suggestion_ = InstantSuggestion();
    last_match_was_search_ = suggestion.type == INSTANT_SUGGESTION_SEARCH;
    browser_->SetInstantSuggestion(suggestion);
  } else {
    // Suggestion text should be a full URL for URL suggestions, or the
    // completion of a query for query suggestions.
    if (suggestion.type == INSTANT_SUGGESTION_URL) {
      // If the suggestion is not a valid URL, perhaps it's something like
      // "foo.com". Try prefixing "http://". If it still isn't valid, drop it.
      if (!GURL(suggestion.text).is_valid()) {
        suggestion.text.insert(0, ASCIIToUTF16("http://"));
        if (!GURL(suggestion.text).is_valid())
          suggestion = InstantSuggestion();
      }
    } else if (StartsWith(suggestion.text, last_user_text_, true)) {
      // The user typed an exact prefix of the suggestion.
      suggestion.text.erase(0, last_user_text_.size());
    } else if (!NormalizeAndStripPrefix(&suggestion.text, last_user_text_)) {
      // Unicode normalize and case-fold the user text and suggestion. If the
      // user text is a prefix, suggest the normalized, case-folded completion;
      // for instance, if the user types 'i' and the suggestion is 'INSTANT',
      // suggestion 'nstant'. Otherwise, the user text really isn't a prefix,
      // so suggest nothing.
      suggestion = InstantSuggestion();
    }

    last_suggestion_ = suggestion;

    // Set the suggested text if the suggestion behavior is
    // INSTANT_COMPLETE_NEVER irrespective of verbatim because in this case
    // the suggested text does not get committed if the user presses enter.
    if (suggestion.behavior == INSTANT_COMPLETE_NEVER || !last_verbatim_)
      browser_->SetInstantSuggestion(suggestion);
  }

  Show(INSTANT_SHOWN_QUERY_SUGGESTIONS, 100, INSTANT_SIZE_PERCENT);
}

void InstantController::CommitInstantLoader(InstantLoader* loader) {
  if (loader_ != loader ||
      model_.preview_state() != InstantModel::QUERY_RESULTS)
    return;

  CommitCurrentPreview(INSTANT_COMMIT_FOCUS_LOST);
}

void InstantController::ShowInstantPreview(InstantLoader* loader,
                                           InstantShownReason reason,
                                           int height,
                                           InstantSizeUnits units) {
  if (loader_ != loader || mode_ != EXTENDED)
    return;

  Show(reason, height, units);
}

void InstantController::InstantLoaderPreviewLoaded(InstantLoader* loader) {
}

void InstantController::InstantSupportDetermined(InstantLoader* loader,
                                                 bool supports_instant) {
  if (supports_instant) {
    blacklisted_urls_.erase(loader->instant_url());
  } else {
    ++blacklisted_urls_[loader->instant_url()];
    if (loader_ == loader)
      DeleteLoader();
  }

  content::NotificationService::current()->Notify(
      chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED,
      content::Source<InstantController>(this),
      content::NotificationService::NoDetails());
}

void InstantController::SwappedTabContents(InstantLoader* loader) {
  if (loader_ == loader)
    model_.SetPreviewContents(GetPreviewContents());
}

void InstantController::InstantLoaderContentsFocused(InstantLoader* loader) {
#if defined(USE_AURA)
  // On aura the omnibox only receives a focus lost if we initiate the focus
  // change. This does that.
  if (model_.preview_state() != InstantModel::NOT_READY)
    browser_->InstantPreviewFocused();
#endif
}

InstantController::InstantController(chrome::BrowserInstantController* browser,
                                     Mode mode)
    : browser_(browser),
      model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
      mode_(mode),
      last_verbatim_(false),
      last_transition_type_(content::PAGE_TRANSITION_LINK),
      last_match_was_search_(false),
      is_omnibox_focused_(false) {
}

void InstantController::ResetLoader(const std::string& instant_url,
                                    const TabContents* active_tab) {
  if (GetPreviewContents() && loader_->instant_url() != instant_url)
    DeleteLoader();

  if (!GetPreviewContents()) {
    loader_.reset(new InstantLoader(this, instant_url, active_tab));
    loader_->Init();

    // Ensure the searchbox API has the correct context.
    loader_->OnActiveTabModeChanged(active_tab_mode_.is_ntp());

    // Reset the loader timer.
    stale_loader_timer_.Stop();
    stale_loader_timer_.Start(
        FROM_HERE,
        base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this,
        &InstantController::OnStaleLoader);
  }
}

bool InstantController::CreateDefaultLoader() {
  const TabContents* active_tab = browser_->GetActiveTabContents();

  // We could get here with no active tab if the Browser is closing.
  if (!active_tab)
    return false;

  const TemplateURL* template_url =
      TemplateURLServiceFactory::GetForProfile(active_tab->profile())->
                                 GetDefaultSearchProvider();
  const GURL& tab_url = active_tab->web_contents()->GetURL();
  std::string instant_url;
  if (!GetInstantURL(template_url, tab_url, &instant_url))
    return false;

  ResetLoader(instant_url, active_tab);
  return true;
}

void InstantController::OnStaleLoader() {
  // If the loader is showing, do not delete it. It will get deleted the next
  // time the autocomplete loses focus.
  if (model_.preview_state() != InstantModel::NOT_READY)
    return;

  DeleteLoader();
  CreateDefaultLoader();
}

void InstantController::MaybeOnStaleLoader() {
  if (!stale_loader_timer_.IsRunning())
    OnStaleLoader();
}

void InstantController::DeleteLoader() {
  last_full_text_.clear();
  last_user_text_.clear();
  last_verbatim_ = false;
  last_suggestion_ = InstantSuggestion();
  last_match_was_search_ = false;
  last_omnibox_bounds_ = gfx::Rect();
  url_for_history_ = GURL();
  if (GetPreviewContents())
    model_.SetPreviewState(InstantModel::NOT_READY, 0, INSTANT_SIZE_PERCENT);
  first_interaction_time_ = base::Time();

  // Schedule the deletion for later, since we may have gotten here from a call
  // within a |loader_| method (i.e., it's still on the stack). If we deleted
  // the loader immediately, things would still be fine so long as the caller
  // doesn't access any instance members after we return, but why rely on that?
  MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release());
}

void InstantController::Show(InstantShownReason reason,
                             int height,
                             InstantSizeUnits units) {
  // Must be on NTP to show NTP content.
  if (reason == INSTANT_SHOWN_CUSTOM_NTP_CONTENT &&
      !active_tab_mode_.is_ntp())
    return;

  // Must have updated omnibox after most recent Hide() to show suggestions.
  if (reason == INSTANT_SHOWN_QUERY_SUGGESTIONS &&
      model_.preview_state() == InstantModel::NOT_READY)
    return;

  // If the preview is being shown because of the first set of suggestions to
  // arrive for this query editing session, record a histogram value.
  if (!first_interaction_time_.is_null() &&
      model_.preview_state() == InstantModel::AWAITING_SUGGESTIONS) {
    base::TimeDelta delta = base::Time::Now() - first_interaction_time_;
    UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta);
  }

  model_.SetPreviewState(GetNewPreviewState(reason), height, units);
}

void InstantController::SendBoundsToPage() {
  if (last_omnibox_bounds_ == omnibox_bounds_ ||
      model_.preview_state() == InstantModel::NOT_READY ||
      !GetPreviewContents() || loader_->IsPointerDownFromActivate())
    return;

  last_omnibox_bounds_ = omnibox_bounds_;
  gfx::Rect preview_bounds = browser_->GetInstantBounds();
  gfx::Rect intersection = gfx::IntersectRects(omnibox_bounds_, preview_bounds);

  // Translate into window coordinates.
  if (!intersection.IsEmpty()) {
    intersection.Offset(-preview_bounds.origin().x(),
                        -preview_bounds.origin().y());
  }

  // In the current Chrome UI, these must always be true so they sanity check
  // the above operations. In a future UI, these may be removed or adjusted.
  // There is no point in sanity-checking |intersection.y()| because the omnibox
  // can be placed anywhere vertically relative to the preview (for example, in
  // Mac fullscreen mode, the omnibox is fully enclosed by the preview bounds).
  DCHECK_LE(0, intersection.x());
  DCHECK_LE(0, intersection.width());
  DCHECK_LE(0, intersection.height());

  loader_->SetOmniboxBounds(intersection);
}

bool InstantController::GetInstantURL(const TemplateURL* template_url,
                                      const GURL& tab_url,
                                      std::string* instant_url) const {
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(switches::kInstantURL)) {
    *instant_url = command_line->GetSwitchValueASCII(switches::kInstantURL);
    return template_url != NULL;
  }

  if (!template_url)
    return false;

  const TemplateURLRef& instant_url_ref = template_url->instant_url_ref();
  if (!instant_url_ref.IsValid())
    return false;

  // Even if the URL template doesn't have search terms, it may have other
  // components (such as {google:baseURL}) that need to be replaced.
  *instant_url = instant_url_ref.ReplaceSearchTerms(
      TemplateURLRef::SearchTermsArgs(string16()));

  // Extended mode should always use HTTPS. TODO(sreeram): This section can be
  // removed if TemplateURLs supported "https://{google:host}/..." instead of
  // only supporting "{google:baseURL}...".
  if (mode_ == EXTENDED) {
    GURL url_obj(*instant_url);
    if (!url_obj.is_valid())
      return false;

    if (!url_obj.SchemeIsSecure()) {
      const std::string new_scheme = "https";
      const std::string new_port = "443";
      GURL::Replacements secure;
      secure.SetSchemeStr(new_scheme);
      secure.SetPortStr(new_port);
      url_obj = url_obj.ReplaceComponents(secure);

      if (!url_obj.is_valid())
        return false;

      *instant_url = url_obj.spec();
    }
  }

  std::map<std::string, int>::const_iterator iter =
      blacklisted_urls_.find(*instant_url);
  if (iter != blacklisted_urls_.end() &&
      iter->second > kMaxInstantSupportFailures)
    return false;

  return true;
}