1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/search_engines/search_provider_install_state_message_filter.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "chrome/browser/google/google_url_tracker_factory.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/search_engines/template_url_service_factory.h"
12#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
13#include "chrome/common/render_messages.h"
14#include "content/public/browser/render_process_host.h"
15#include "url/gurl.h"
16
17using content::BrowserThread;
18
19SearchProviderInstallStateMessageFilter::
20SearchProviderInstallStateMessageFilter(
21    int render_process_id,
22    Profile* profile)
23    : BrowserMessageFilter(ChromeMsgStart),
24      provider_data_(TemplateURLServiceFactory::GetForProfile(profile),
25                     UIThreadSearchTermsData(profile).GoogleBaseURLValue(),
26                     GoogleURLTrackerFactory::GetForProfile(profile),
27                     content::RenderProcessHost::FromID(render_process_id)),
28      is_off_the_record_(profile->IsOffTheRecord()),
29      weak_factory_(this) {
30  // This is initialized by RenderProcessHostImpl. Do not add any non-trivial
31  // initialization here. Instead do it lazily when required.
32  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
33}
34
35bool SearchProviderInstallStateMessageFilter::OnMessageReceived(
36    const IPC::Message& message) {
37  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
38  bool handled = true;
39  IPC_BEGIN_MESSAGE_MAP(SearchProviderInstallStateMessageFilter, message)
40    IPC_MESSAGE_HANDLER_DELAY_REPLY(
41        ChromeViewHostMsg_GetSearchProviderInstallState,
42        OnGetSearchProviderInstallState)
43    IPC_MESSAGE_UNHANDLED(handled = false)
44  IPC_END_MESSAGE_MAP()
45  return handled;
46}
47
48SearchProviderInstallStateMessageFilter::
49~SearchProviderInstallStateMessageFilter() {
50  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
51}
52
53search_provider::InstallState
54SearchProviderInstallStateMessageFilter::GetSearchProviderInstallState(
55    const GURL& page_location,
56    const GURL& requested_host) {
57  GURL requested_origin = requested_host.GetOrigin();
58
59  // Do the security check before any others to avoid information leaks.
60  if (page_location.GetOrigin() != requested_origin)
61    return search_provider::DENIED;
62
63  // In incognito mode, no search information is exposed. (This check must be
64  // done after the security check or else a web site can detect that the
65  // user is in incognito mode just by doing a cross origin request.)
66  if (is_off_the_record_)
67      return search_provider::NOT_INSTALLED;
68
69  switch (provider_data_.GetInstallState(requested_origin)) {
70    case SearchProviderInstallData::NOT_INSTALLED:
71      return search_provider::NOT_INSTALLED;
72
73    case SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT:
74      return search_provider::INSTALLED_BUT_NOT_DEFAULT;
75
76    case SearchProviderInstallData::INSTALLED_AS_DEFAULT:
77      return search_provider::INSTALLED_AS_DEFAULT;
78  }
79
80  NOTREACHED();
81  return search_provider::NOT_INSTALLED;
82}
83
84void
85SearchProviderInstallStateMessageFilter::OnGetSearchProviderInstallState(
86    const GURL& page_location,
87    const GURL& requested_host,
88    IPC::Message* reply_msg) {
89  provider_data_.CallWhenLoaded(
90      base::Bind(
91          &SearchProviderInstallStateMessageFilter::
92          ReplyWithProviderInstallState,
93          weak_factory_.GetWeakPtr(),
94          page_location,
95          requested_host,
96          reply_msg));
97}
98
99void SearchProviderInstallStateMessageFilter::ReplyWithProviderInstallState(
100    const GURL& page_location,
101    const GURL& requested_host,
102    IPC::Message* reply_msg) {
103  DCHECK(reply_msg);
104  search_provider::InstallState install_state =
105      GetSearchProviderInstallState(page_location, requested_host);
106
107  ChromeViewHostMsg_GetSearchProviderInstallState::WriteReplyParams(
108      reply_msg,
109      install_state);
110  Send(reply_msg);
111}
112