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