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