search_provider_install_state_message_filter.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2010 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/logging.h"
8#include "chrome/browser/profiles/profile.h"
9#include "chrome/common/notification_source.h"
10#include "chrome/common/notification_type.h"
11#include "chrome/common/render_messages.h"
12#include "chrome/common/render_messages_params.h"
13#include "content/browser/renderer_host/render_process_host.h"
14#include "content/browser/renderer_host/render_view_host.h"
15#include "googleurl/src/gurl.h"
16
17SearchProviderInstallStateMessageFilter::
18SearchProviderInstallStateMessageFilter(
19    int render_process_id,
20    Profile* profile)
21    : ALLOW_THIS_IN_INITIALIZER_LIST(
22        reply_with_provider_install_state_factory_(this)),
23      provider_data_(profile->GetWebDataService(Profile::EXPLICIT_ACCESS),
24                     NotificationType::RENDERER_PROCESS_TERMINATED,
25                     Source<RenderProcessHost>(
26                         RenderProcessHost::FromID(render_process_id))),
27      is_off_the_record_(profile->IsOffTheRecord()) {
28  // This is initialized by BrowserRenderProcessHost. Do not add any non-trivial
29  // initialization here. Instead do it lazily when required.
30  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
31}
32
33SearchProviderInstallStateMessageFilter::
34~SearchProviderInstallStateMessageFilter() {
35  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
36}
37
38bool SearchProviderInstallStateMessageFilter::OnMessageReceived(
39    const IPC::Message& message,
40    bool* message_was_ok) {
41  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
42  bool handled = true;
43  IPC_BEGIN_MESSAGE_MAP_EX(SearchProviderInstallStateMessageFilter, message,
44                           *message_was_ok)
45    IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_GetSearchProviderInstallState,
46                                    OnMsgGetSearchProviderInstallState)
47    IPC_MESSAGE_UNHANDLED(handled = false)
48  IPC_END_MESSAGE_MAP()
49  return handled;
50}
51
52ViewHostMsg_GetSearchProviderInstallState_Params
53SearchProviderInstallStateMessageFilter::GetSearchProviderInstallState(
54    const GURL& page_location,
55    const GURL& requested_host) {
56  GURL requested_origin = requested_host.GetOrigin();
57
58  // Do the security check before any others to avoid information leaks.
59  if (page_location.GetOrigin() != requested_origin)
60    return ViewHostMsg_GetSearchProviderInstallState_Params::Denied();
61
62  // In incognito mode, no search information is exposed. (This check must be
63  // done after the security check or else a web site can detect that the
64  // user is in incognito mode just by doing a cross origin request.)
65  if (is_off_the_record_)
66      return ViewHostMsg_GetSearchProviderInstallState_Params::NotInstalled();
67
68  switch (provider_data_.GetInstallState(requested_origin)) {
69    case SearchProviderInstallData::NOT_INSTALLED:
70      return ViewHostMsg_GetSearchProviderInstallState_Params::
71          NotInstalled();
72
73    case SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT:
74      return ViewHostMsg_GetSearchProviderInstallState_Params::
75          InstallButNotDefault();
76
77    case SearchProviderInstallData::INSTALLED_AS_DEFAULT:
78      return ViewHostMsg_GetSearchProviderInstallState_Params::
79          InstalledAsDefault();
80  }
81
82  NOTREACHED();
83  return ViewHostMsg_GetSearchProviderInstallState_Params::
84      NotInstalled();
85}
86
87void
88SearchProviderInstallStateMessageFilter::OnMsgGetSearchProviderInstallState(
89    const GURL& page_location,
90    const GURL& requested_host,
91    IPC::Message* reply_msg) {
92  provider_data_.CallWhenLoaded(
93      reply_with_provider_install_state_factory_.NewRunnableMethod(
94          &SearchProviderInstallStateMessageFilter::
95          ReplyWithProviderInstallState,
96          page_location,
97          requested_host,
98          reply_msg));
99}
100
101void SearchProviderInstallStateMessageFilter::ReplyWithProviderInstallState(
102    const GURL& page_location,
103    const GURL& requested_host,
104    IPC::Message* reply_msg) {
105  DCHECK(reply_msg);
106  ViewHostMsg_GetSearchProviderInstallState_Params install_state =
107      GetSearchProviderInstallState(page_location, requested_host);
108
109  ViewHostMsg_GetSearchProviderInstallState::WriteReplyParams(
110      reply_msg,
111      install_state);
112  Send(reply_msg);
113}
114