Home | History | Annotate | Download | only in search_engines
      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/profiles/profile.h"
     10 #include "chrome/common/render_messages.h"
     11 #include "content/public/browser/notification_source.h"
     12 #include "content/public/browser/notification_types.h"
     13 #include "content/public/browser/render_process_host.h"
     14 #include "content/public/browser/render_view_host.h"
     15 #include "url/gurl.h"
     16 
     17 using content::BrowserThread;
     18 
     19 SearchProviderInstallStateMessageFilter::
     20     SearchProviderInstallStateMessageFilter(
     21     int render_process_id,
     22     Profile* profile)
     23     : weak_factory_(this),
     24       provider_data_(profile, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
     25           content::Source<content::RenderProcessHost>(
     26               content::RenderProcessHost::FromID(render_process_id))),
     27       is_off_the_record_(profile->IsOffTheRecord()) {
     28   // This is initialized by RenderProcessHostImpl. Do not add any non-trivial
     29   // initialization here. Instead do it lazily when required.
     30   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     31 }
     32 
     33 bool SearchProviderInstallStateMessageFilter::OnMessageReceived(
     34     const IPC::Message& message,
     35     bool* message_was_ok) {
     36   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     37   bool handled = true;
     38   IPC_BEGIN_MESSAGE_MAP_EX(SearchProviderInstallStateMessageFilter, message,
     39                            *message_was_ok)
     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 
     48 SearchProviderInstallStateMessageFilter::
     49 ~SearchProviderInstallStateMessageFilter() {
     50   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     51 }
     52 
     53 search_provider::InstallState
     54 SearchProviderInstallStateMessageFilter::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 
     84 void
     85 SearchProviderInstallStateMessageFilter::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 
     99 void 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