12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/renderer/searchbox/searchbox.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <string>
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/chrome_switches.h"
1358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "chrome/common/favicon/favicon_url_parser.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/omnibox_focus_state.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/render_messages.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/url_constants.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/renderer/searchbox/searchbox_extension.h"
180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/favicon_base/favicon_types.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/renderer/render_view.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/escape.h"
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebDocument.h"
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebFrame.h"
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebView.h"
247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// The size of the InstantMostVisitedItem cache.
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochconst size_t kMaxInstantMostVisitedItemCacheSize = 100;
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true if items stored in |old_item_id_pairs| and |new_items| are
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// equal.
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool AreMostVisitedItemsEqual(
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::vector<InstantMostVisitedItemIDPair>& old_item_id_pairs,
35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::vector<InstantMostVisitedItem>& new_items) {
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (old_item_id_pairs.size() != new_items.size())
37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = 0; i < new_items.size(); ++i) {
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (new_items[i].url != old_item_id_pairs[i].second.url ||
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        new_items[i].title != old_item_id_pairs[i].second.title) {
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return false;
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return true;
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace internal {  // for testing
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// Parses |path| and fills in |id| with the InstantRestrictedID obtained from
5358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// the |path|. |render_view_id| is the ID of the associated RenderView.
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
5558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// |path| is a pair of |render_view_id| and |restricted_id|, and it is
5658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// contained in Instant Extended URLs. A valid |path| is in the form:
5758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// <render_view_id>/<restricted_id>
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
5958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// If the |path| is valid, returns true and fills in |id| with restricted_id
6058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// value. If the |path| is invalid, returns false and |id| is not set.
6158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochbool GetInstantRestrictedIDFromPath(int render_view_id,
6258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                    const std::string& path,
6358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                    InstantRestrictedID* id) {
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Check that the path is of Most visited item ID form.
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::vector<std::string> tokens;
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (Tokenize(path, "/", &tokens) != 2)
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int view_id = 0;
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id)
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return base::StringToInt(tokens[1], id);
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochbool GetRestrictedIDFromFaviconUrl(int render_view_id,
7658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                   const GURL& url,
7758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                   std::string* favicon_params,
7858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                   InstantRestrictedID* rid) {
7958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Strip leading slash.
8058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  std::string raw_path = url.path();
8158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  DCHECK_GT(raw_path.length(), (size_t) 0);
8258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  DCHECK_EQ(raw_path[0], '/');
8358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  raw_path = raw_path.substr(1);
8458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
8558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  chrome::ParsedFaviconPath parsed;
860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!chrome::ParseFaviconPath(raw_path, favicon_base::FAVICON, &parsed))
8758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    return false;
8858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
8958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // The part of the URL which details the favicon parameters should be returned
9058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // so the favicon URL can be reconstructed, by replacing the restricted_id
9158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // with the actual URL from which the favicon is being requested.
9258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  *favicon_params = raw_path.substr(0, parsed.path_index);
9358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
9458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // The part of the favicon URL which is supposed to contain the URL from
9558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // which the favicon is being requested (i.e., the page's URL) actually
9658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // contains a pair in the format "<view_id>/<restricted_id>". If the page's
9758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // URL is not in the expected format then the execution must be stopped,
9858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // returning |true|, indicating that the favicon URL should be translated
9958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // without the page's URL part, to prevent search providers from spoofing
10058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // the user's browsing history. For example, the following favicon URL
10158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // "chrome-search://favicon/http://www.secretsite.com" it is not in the
10258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // expected format "chrome-search://favicon/<view_id>/<restricted_id>" so
10358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // the pages's URL part ("http://www.secretsite.com") should be removed
10458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // entirely from the translated URL otherwise the search engine would know
10558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // if the user has visited that page (by verifying whether the favicon URL
10658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // returns an image for a particular page's URL); the translated URL in this
10758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // case would be "chrome-search://favicon/" which would simply return the
10858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // default favicon.
10958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  std::string id_part = raw_path.substr(parsed.path_index);
11058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  InstantRestrictedID id;
11158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (!GetInstantRestrictedIDFromPath(render_view_id, id_part, &id))
11258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    return true;
11358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
11458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  *rid = id;
11558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  return true;
11658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch}
11758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
11858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// Parses a thumbnail |url| and fills in |id| with the InstantRestrictedID
11958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// obtained from the |url|. |render_view_id| is the ID of the associated
12058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// RenderView.
12158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch//
12258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// Valid |url| forms:
12358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// chrome-search://thumb/<view_id>/<restricted_id>
12458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch//
12558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// If the |url| is valid, returns true and fills in |id| with restricted_id
12658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch// value. If the |url| is invalid, returns false and |id| is not set.
12758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochbool GetRestrictedIDFromThumbnailUrl(int render_view_id,
12858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                     const GURL& url,
12958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                     InstantRestrictedID* id) {
13058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Strip leading slash.
13158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  std::string path = url.path();
13258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  DCHECK_GT(path.length(), (size_t) 0);
13358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  DCHECK_EQ(path[0], '/');
13458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  path = path.substr(1);
13558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  return GetInstantRestrictedIDFromPath(render_view_id, path, id);
13758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch}
13858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace internal
140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SearchBox::SearchBox(content::RenderView* render_view)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : content::RenderViewObserver(render_view),
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::RenderViewObserverTracker<SearchBox>(render_view),
14446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    page_seq_no_(0),
1457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    app_launcher_enabled_(false),
1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    is_focused_(false),
1477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    is_input_in_progress_(false),
1487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    is_key_capture_enabled_(false),
149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    display_instant_results_(false),
1507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize),
1517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    query_(),
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    start_margin_(0) {
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SearchBox::~SearchBox() {
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void SearchBox::LogEvent(NTPLoggingEventType event) {
1593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  render_view()->Send(new ChromeViewHostMsg_LogEvent(
16046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, event));
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void SearchBox::LogMostVisitedImpression(int position,
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                         const base::string16& provider) {
165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  render_view()->Send(new ChromeViewHostMsg_LogMostVisitedImpression(
16646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, position, provider));
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void SearchBox::LogMostVisitedNavigation(int position,
170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                         const base::string16& provider) {
171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  render_view()->Send(new ChromeViewHostMsg_LogMostVisitedNavigation(
17246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, position, provider));
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void SearchBox::CheckIsUserSignedInToChromeAs(const base::string16& identity) {
1760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  render_view()->Send(new ChromeViewHostMsg_ChromeIdentityCheck(
17746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, identity));
1780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::DeleteMostVisitedItem(
1817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    InstantRestrictedID most_visited_item_id) {
1827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
18346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(),
18446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      page_seq_no_,
1857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      GetURLForMostVisitedItem(most_visited_item_id)));
186eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
187eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochbool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url,
1897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                   GURL* url) const {
19058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  std::string favicon_params;
19158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  InstantRestrictedID rid = -1;
19258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  bool success = internal::GetRestrictedIDFromFaviconUrl(
19358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      render_view()->GetRoutingID(), transient_url, &favicon_params, &rid);
19458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (!success)
1957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return false;
1967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
19758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  InstantMostVisitedItem item;
19858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  std::string item_url;
19958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (rid != -1 && GetMostVisitedItemWithID(rid, &item))
20058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    item_url = item.url.spec();
20158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
20258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  *url = GURL(base::StringPrintf("chrome-search://favicon/%s%s",
20358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                 favicon_params.c_str(),
20458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                 item_url.c_str()));
2057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return true;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochbool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url,
2097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                     GURL* url) const {
2107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  InstantRestrictedID rid = 0;
21158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (!internal::GetRestrictedIDFromThumbnailUrl(render_view()->GetRoutingID(),
21258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch                                                 transient_url, &rid)) {
2137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return false;
2147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
2157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  GURL most_visited_item_url(GetURLForMostVisitedItem(rid));
2177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (most_visited_item_url.is_empty())
2187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return false;
2197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  *url = GURL(base::StringPrintf("chrome-search://thumb/%s",
2207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                 most_visited_item_url.spec().c_str()));
2217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return true;
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::GetMostVisitedItems(
2257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    std::vector<InstantMostVisitedItemIDPair>* items) const {
2267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return most_visited_items_cache_.GetCurrentItems(items);
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochbool SearchBox::GetMostVisitedItemWithID(
2307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    InstantRestrictedID most_visited_item_id,
2317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    InstantMostVisitedItem* item) const {
2327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id,
2337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                           item);
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochconst ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() {
2377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return theme_info_;
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
240424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void SearchBox::Focus() {
241424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  render_view()->Send(new ChromeViewHostMsg_FocusOmnibox(
24246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, OMNIBOX_FOCUS_VISIBLE));
243424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
244424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SearchBox::NavigateToURL(const GURL& url,
246a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                              WindowOpenDisposition disposition,
2471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                              bool is_most_visited_item_url) {
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  render_view()->Send(new ChromeViewHostMsg_SearchBoxNavigate(
24946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, url,
2501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      disposition, is_most_visited_item_url));
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
253a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void SearchBox::Paste(const base::string16& text) {
254ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  render_view()->Send(new ChromeViewHostMsg_PasteAndOpenDropdown(
25546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, text));
256ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
257ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::SetVoiceSearchSupported(bool supported) {
2597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  render_view()->Send(new ChromeViewHostMsg_SetVoiceSearchSupported(
26046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, supported));
2617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
2627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::StartCapturingKeyStrokes() {
2647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  render_view()->Send(new ChromeViewHostMsg_FocusOmnibox(
26546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, OMNIBOX_FOCUS_INVISIBLE));
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::StopCapturingKeyStrokes() {
2697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  render_view()->Send(new ChromeViewHostMsg_FocusOmnibox(
27046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_, OMNIBOX_FOCUS_NONE));
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SearchBox::UndoAllMostVisitedDeletions() {
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  render_view()->Send(
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
276f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          render_view()->GetRoutingID(), page_seq_no_));
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::UndoMostVisitedDeletion(
2807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    InstantRestrictedID most_visited_item_id) {
2817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  render_view()->Send(new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
28246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      render_view()->GetRoutingID(), page_seq_no_,
2837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      GetURLForMostVisitedItem(most_visited_item_id)));
284868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool SearchBox::OnMessageReceived(const IPC::Message& message) {
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool handled = true;
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IPC_BEGIN_MESSAGE_MAP(SearchBox, message)
28946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    IPC_MESSAGE_HANDLER(ChromeViewMsg_SetPageSequenceNumber,
29046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                        OnSetPageSequenceNumber)
2910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    IPC_MESSAGE_HANDLER(ChromeViewMsg_ChromeIdentityCheckResult,
2920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                        OnChromeIdentityCheckResult)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant,
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        OnDetermineIfPageSupportsInstant)
295868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFocusChanged, OnFocusChanged)
2967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange)
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMostVisitedItemsChanged,
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        OnMostVisitedChanged)
2997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPromoInformation,
3007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                        OnPromoInformationReceived)
301424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSetDisplayInstantResults,
302424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                        OnSetDisplayInstantResults)
3037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSetInputInProgress,
3047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                        OnSetInputInProgress)
305424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSetSuggestionToPrefetch,
306424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                        OnSetSuggestionToPrefetch)
3077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit)
3087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxThemeChanged,
3097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                        OnThemeChanged)
310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxToggleVoiceSearch,
311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        OnToggleVoiceSearch)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IPC_MESSAGE_UNHANDLED(handled = false)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IPC_END_MESSAGE_MAP()
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return handled;
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
31746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void SearchBox::OnSetPageSequenceNumber(int page_seq_no) {
31846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  page_seq_no_ = page_seq_no;
31946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
32046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
321a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void SearchBox::OnChromeIdentityCheckResult(const base::string16& identity,
3220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                            bool identity_match) {
3230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
3240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    extensions_v8::SearchBoxExtension::DispatchChromeIdentityCheckResult(
3250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        render_view()->GetWebView()->mainFrame(), identity, identity_match);
3260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
3270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
3280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SearchBox::OnDetermineIfPageSupportsInstant() {
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant(
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        render_view()->GetWebView()->mainFrame());
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DVLOG(1) << render_view() << " PageSupportsInstant: " << result;
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined(
33546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        render_view()->GetRoutingID(), page_seq_no_, result));
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void SearchBox::OnFocusChanged(OmniboxFocusState new_focus_state,
340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                               OmniboxFocusChangeReason reason) {
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool key_capture_enabled = new_focus_state == OMNIBOX_FOCUS_INVISIBLE;
342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (key_capture_enabled != is_key_capture_enabled_) {
343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Tell the page if the key capture mode changed unless the focus state
344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // changed because of TYPING. This is because in that case, the browser
345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // hasn't really stopped capturing key strokes.
346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    //
347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // (More practically, if we don't do this check, the page would receive
348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // onkeycapturechange before the corresponding onchange, and the page would
349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // have no way of telling whether the keycapturechange happened because of
350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // some actual user action or just because they started typing.)
351868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (reason != OMNIBOX_FOCUS_CHANGE_TYPING &&
352868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        render_view()->GetWebView() &&
353868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        render_view()->GetWebView()->mainFrame()) {
354868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_key_capture_enabled_ = key_capture_enabled;
355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DVLOG(1) << render_view() << " OnKeyCaptureChange";
356868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      extensions_v8::SearchBoxExtension::DispatchKeyCaptureChange(
357868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          render_view()->GetWebView()->mainFrame());
358868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
359868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
360868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool is_focused = new_focus_state == OMNIBOX_FOCUS_VISIBLE;
361868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (is_focused != is_focused_) {
362868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    is_focused_ = is_focused;
363868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DVLOG(1) << render_view() << " OnFocusChange";
3647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (render_view()->GetWebView() &&
3657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        render_view()->GetWebView()->mainFrame()) {
3667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      extensions_v8::SearchBoxExtension::DispatchFocusChange(
3677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          render_view()->GetWebView()->mainFrame());
3687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
3697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
3717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SearchBox::OnMarginChange(int margin) {
3737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  start_margin_ = margin;
3747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
3757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    extensions_v8::SearchBoxExtension::DispatchMarginChange(
3767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        render_view()->GetWebView()->mainFrame());
3777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
3787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
3797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::OnMostVisitedChanged(
3817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    const std::vector<InstantMostVisitedItem>& items) {
3827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::vector<InstantMostVisitedItemIDPair> last_known_items;
3837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  GetMostVisitedItems(&last_known_items);
3847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (AreMostVisitedItemsEqual(last_known_items, items))
3867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return;  // Do not send duplicate onmostvisitedchange events.
3877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  most_visited_items_cache_.AddItems(items);
3897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
3907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged(
3917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        render_view()->GetWebView()->mainFrame());
3927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
3937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
3947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::OnPromoInformationReceived(bool is_app_launcher_enabled) {
3967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  app_launcher_enabled_ = is_app_launcher_enabled;
3977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
3987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
399424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void SearchBox::OnSetDisplayInstantResults(bool display_instant_results) {
400424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  display_instant_results_ = display_instant_results;
401424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
402424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
4037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void SearchBox::OnSetInputInProgress(bool is_input_in_progress) {
4047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (is_input_in_progress_ != is_input_in_progress) {
4057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    is_input_in_progress_ = is_input_in_progress;
4067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DVLOG(1) << render_view() << " OnSetInputInProgress";
4077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (render_view()->GetWebView() &&
4087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        render_view()->GetWebView()->mainFrame()) {
4097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (is_input_in_progress_) {
4107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        extensions_v8::SearchBoxExtension::DispatchInputStart(
4117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            render_view()->GetWebView()->mainFrame());
4127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      } else {
4137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        extensions_v8::SearchBoxExtension::DispatchInputCancel(
4147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            render_view()->GetWebView()->mainFrame());
4157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
4167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
420424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void SearchBox::OnSetSuggestionToPrefetch(const InstantSuggestion& suggestion) {
421424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  suggestion_ = suggestion;
422424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
423424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    DVLOG(1) << render_view() << " OnSetSuggestionToPrefetch";
424424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    extensions_v8::SearchBoxExtension::DispatchSuggestionChange(
425424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        render_view()->GetWebView()->mainFrame());
426424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
427424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
428424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
429a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void SearchBox::OnSubmit(const base::string16& query) {
4307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  query_ = query;
4317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
4327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DVLOG(1) << render_view() << " OnSubmit";
4337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    extensions_v8::SearchBoxExtension::DispatchSubmit(
4347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        render_view()->GetWebView()->mainFrame());
4357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
4367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!query.empty())
4377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    Reset();
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SearchBox::OnThemeChanged(const ThemeBackgroundInfo& theme_info) {
441eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Do not send duplicate notifications.
442eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (theme_info_ == theme_info)
443eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
444eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  theme_info_ = theme_info;
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    extensions_v8::SearchBoxExtension::DispatchThemeChange(
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        render_view()->GetWebView()->mainFrame());
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SearchBox::OnToggleVoiceSearch() {
4537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
4547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch(
4557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        render_view()->GetWebView()->mainFrame());
4567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4597dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochGURL SearchBox::GetURLForMostVisitedItem(InstantRestrictedID item_id) const {
4607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  InstantMostVisitedItem item;
4617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL();
462eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
463eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SearchBox::Reset() {
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  query_.clear();
466424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  suggestion_ = InstantSuggestion();
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  start_margin_ = 0;
468868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  is_focused_ = false;
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  is_key_capture_enabled_ = false;
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  theme_info_ = ThemeBackgroundInfo();
471868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
472