OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search_engines/search_provider_install_state_message_fi lter.h" | 5 #include "chrome/browser/search_engines/search_provider_install_state_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/supports_user_data.h" | |
9 #include "chrome/browser/google/google_url_tracker_factory.h" | 10 #include "chrome/browser/google/google_url_tracker_factory.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/search_engines/template_url_service_factory.h" | 12 #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/browser/search_engines/ui_thread_search_terms_data.h" |
13 #include "chrome/common/render_messages.h" | 14 #include "chrome/common/search_provider.mojom.h" |
14 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/common/service_registry.h" | |
15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
16 | 18 |
17 using content::BrowserThread; | 19 using content::BrowserThread; |
18 | 20 |
19 SearchProviderInstallStateMessageFilter:: | 21 // Adapter used to store the impl in user data and to make sure it's deleted on |
20 SearchProviderInstallStateMessageFilter( | 22 // the right thread. |
23 class SearchProviderInstallStateImplAdapter | |
24 : public base::SupportsUserData::Data { | |
25 public: | |
26 explicit SearchProviderInstallStateImplAdapter( | |
27 std::unique_ptr<SearchProviderInstallStateImpl, | |
28 content::BrowserThread::DeleteOnIOThread> filter); | |
29 ~SearchProviderInstallStateImplAdapter() override = default; | |
30 | |
31 SearchProviderInstallStateImpl* get() { return ptr_.get(); } | |
32 | |
33 private: | |
34 std::unique_ptr<SearchProviderInstallStateImpl, | |
35 content::BrowserThread::DeleteOnIOThread> | |
36 ptr_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallStateImplAdapter); | |
39 }; | |
40 | |
41 SearchProviderInstallStateImplAdapter::SearchProviderInstallStateImplAdapter( | |
42 std::unique_ptr<SearchProviderInstallStateImpl, | |
43 content::BrowserThread::DeleteOnIOThread> filter) | |
Sam McNally
2016/06/23 03:48:04
filter?
| |
44 : ptr_(std::move(filter)) {} | |
45 | |
46 // Key used to attach the filter to the RenderProcessHost. | |
47 static const char kRenderProcessHostKey[] = "kRenderProcessHostKey"; | |
48 | |
49 SearchProviderInstallStateImpl::SearchProviderInstallStateImpl( | |
21 int render_process_id, | 50 int render_process_id, |
22 Profile* profile) | 51 Profile* profile) |
23 : BrowserMessageFilter(ChromeMsgStart), | 52 : provider_data_(TemplateURLServiceFactory::GetForProfile(profile), |
24 provider_data_(TemplateURLServiceFactory::GetForProfile(profile), | |
25 UIThreadSearchTermsData(profile).GoogleBaseURLValue(), | 53 UIThreadSearchTermsData(profile).GoogleBaseURLValue(), |
26 GoogleURLTrackerFactory::GetForProfile(profile), | 54 GoogleURLTrackerFactory::GetForProfile(profile), |
27 content::RenderProcessHost::FromID(render_process_id)), | 55 content::RenderProcessHost::FromID(render_process_id)), |
28 is_off_the_record_(profile->IsOffTheRecord()), | 56 is_off_the_record_(profile->IsOffTheRecord()), |
29 weak_factory_(this) { | 57 weak_factory_(this) { |
30 // This is initialized by RenderProcessHostImpl. Do not add any non-trivial | 58 // This is initialized by RenderProcessHostImpl. Do not add any non-trivial |
31 // initialization here. Instead do it lazily when required. | 59 // initialization here. Instead do it lazily when required. |
32 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 60 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
33 } | 61 } |
34 | 62 |
35 bool SearchProviderInstallStateMessageFilter::OnMessageReceived( | 63 SearchProviderInstallStateImpl::~SearchProviderInstallStateImpl() { |
36 const IPC::Message& message) { | |
37 DCHECK_CURRENTLY_ON(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 | |
48 SearchProviderInstallStateMessageFilter:: | |
49 ~SearchProviderInstallStateMessageFilter() { | |
50 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 64 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
51 } | 65 } |
52 | 66 |
53 search_provider::InstallState | 67 void SearchProviderInstallStateImpl::InstallService( |
54 SearchProviderInstallStateMessageFilter::GetSearchProviderInstallState( | 68 content::RenderProcessHost* host) { |
69 int id = host->GetID(); | |
70 Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext()); | |
71 auto* adapter = new SearchProviderInstallStateImplAdapter( | |
72 std::unique_ptr<SearchProviderInstallStateImpl, | |
73 content::BrowserThread::DeleteOnIOThread>( | |
74 new SearchProviderInstallStateImpl(id, profile))); | |
75 host->GetServiceRegistry() | |
76 ->AddService<chrome::mojom::SearchProviderInstallState>( | |
77 base::Bind(&SearchProviderInstallStateImpl::Bind, | |
78 base::Unretained(adapter->get())), | |
79 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
80 host->SetUserData(kRenderProcessHostKey, adapter); | |
81 } | |
82 | |
83 void SearchProviderInstallStateImpl::Bind( | |
84 chrome::mojom::SearchProviderInstallStateRequest request) { | |
85 binding_set_.AddBinding(this, std::move(request)); | |
86 } | |
87 | |
88 chrome::mojom::InstallState | |
89 SearchProviderInstallStateImpl::GetSearchProviderInstallState( | |
55 const GURL& page_location, | 90 const GURL& page_location, |
56 const GURL& requested_host) { | 91 const GURL& requested_host) { |
57 GURL requested_origin = requested_host.GetOrigin(); | 92 GURL requested_origin = requested_host.GetOrigin(); |
58 | 93 |
59 // Do the security check before any others to avoid information leaks. | 94 // Do the security check before any others to avoid information leaks. |
60 if (page_location.GetOrigin() != requested_origin) | 95 if (page_location.GetOrigin() != requested_origin) |
61 return search_provider::DENIED; | 96 return chrome::mojom::InstallState::DENIED; |
62 | 97 |
63 // In incognito mode, no search information is exposed. (This check must be | 98 // 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 | 99 // 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.) | 100 // user is in incognito mode just by doing a cross origin request.) |
66 if (is_off_the_record_) | 101 if (is_off_the_record_) |
67 return search_provider::NOT_INSTALLED; | 102 return chrome::mojom::InstallState::NOT_INSTALLED; |
68 | 103 |
69 switch (provider_data_.GetInstallState(requested_origin)) { | 104 switch (provider_data_.GetInstallState(requested_origin)) { |
70 case SearchProviderInstallData::NOT_INSTALLED: | 105 case SearchProviderInstallData::NOT_INSTALLED: |
71 return search_provider::NOT_INSTALLED; | 106 return chrome::mojom::InstallState::NOT_INSTALLED; |
72 | 107 |
73 case SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT: | 108 case SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT: |
74 return search_provider::INSTALLED_BUT_NOT_DEFAULT; | 109 return chrome::mojom::InstallState::INSTALLED_BUT_NOT_DEFAULT; |
75 | 110 |
76 case SearchProviderInstallData::INSTALLED_AS_DEFAULT: | 111 case SearchProviderInstallData::INSTALLED_AS_DEFAULT: |
77 return search_provider::INSTALLED_AS_DEFAULT; | 112 return chrome::mojom::InstallState::INSTALLED_AS_DEFAULT; |
78 } | 113 } |
79 | 114 |
80 NOTREACHED(); | 115 NOTREACHED(); |
81 return search_provider::NOT_INSTALLED; | 116 return chrome::mojom::InstallState::NOT_INSTALLED; |
82 } | 117 } |
83 | 118 |
84 void | 119 void SearchProviderInstallStateImpl::GetInstallState( |
85 SearchProviderInstallStateMessageFilter::OnGetSearchProviderInstallState( | |
86 const GURL& page_location, | 120 const GURL& page_location, |
87 const GURL& requested_host, | 121 const GURL& requested_host, |
88 IPC::Message* reply_msg) { | 122 const GetInstallStateCallback& callback) { |
89 provider_data_.CallWhenLoaded( | 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
90 base::Bind( | 124 provider_data_.CallWhenLoaded(base::Bind( |
91 &SearchProviderInstallStateMessageFilter:: | 125 &SearchProviderInstallStateImpl::ReplyWithProviderInstallState, |
92 ReplyWithProviderInstallState, | 126 weak_factory_.GetWeakPtr(), page_location, requested_host, callback)); |
93 weak_factory_.GetWeakPtr(), | |
94 page_location, | |
95 requested_host, | |
96 reply_msg)); | |
97 } | 127 } |
98 | 128 |
99 void SearchProviderInstallStateMessageFilter::ReplyWithProviderInstallState( | 129 void SearchProviderInstallStateImpl::ReplyWithProviderInstallState( |
100 const GURL& page_location, | 130 const GURL& page_location, |
101 const GURL& requested_host, | 131 const GURL& requested_host, |
102 IPC::Message* reply_msg) { | 132 const GetInstallStateCallback& callback) { |
103 DCHECK(reply_msg); | 133 chrome::mojom::InstallState install_state = |
104 search_provider::InstallState install_state = | |
105 GetSearchProviderInstallState(page_location, requested_host); | 134 GetSearchProviderInstallState(page_location, requested_host); |
106 | 135 |
107 ChromeViewHostMsg_GetSearchProviderInstallState::WriteReplyParams( | 136 callback.Run(install_state); |
108 reply_msg, | |
109 install_state); | |
110 Send(reply_msg); | |
111 } | 137 } |
OLD | NEW |