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 "chrome/browser/google/google_url_tracker_factory.h" | 9 #include "chrome/browser/google/google_url_tracker_factory.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/search_engines/template_url_service_factory.h" | 11 #include "chrome/browser/search_engines/template_url_service_factory.h" |
12 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | 12 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
13 #include "chrome/common/render_messages.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" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 using content::BrowserThread; | 18 using content::BrowserThread; |
18 | 19 |
19 SearchProviderInstallStateMessageFilter:: | 20 SearchProviderInstallStateImpl::SearchProviderInstallStateImpl( |
20 SearchProviderInstallStateMessageFilter( | |
21 int render_process_id, | 21 int render_process_id, |
22 Profile* profile) | 22 Profile* profile) |
23 : BrowserMessageFilter(ChromeMsgStart), | 23 : provider_data_(TemplateURLServiceFactory::GetForProfile(profile), |
24 provider_data_(TemplateURLServiceFactory::GetForProfile(profile), | |
25 UIThreadSearchTermsData(profile).GoogleBaseURLValue(), | 24 UIThreadSearchTermsData(profile).GoogleBaseURLValue(), |
26 GoogleURLTrackerFactory::GetForProfile(profile), | 25 GoogleURLTrackerFactory::GetForProfile(profile), |
27 content::RenderProcessHost::FromID(render_process_id)), | 26 content::RenderProcessHost::FromID(render_process_id)), |
28 is_off_the_record_(profile->IsOffTheRecord()), | 27 is_off_the_record_(profile->IsOffTheRecord()), |
29 weak_factory_(this) { | 28 weak_factory_(this) { |
30 // This is initialized by RenderProcessHostImpl. Do not add any non-trivial | 29 // This is initialized by RenderProcessHostImpl. Do not add any non-trivial |
31 // initialization here. Instead do it lazily when required. | 30 // initialization here. Instead do it lazily when required. |
32 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 31 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
33 } | 32 } |
34 | 33 |
35 bool SearchProviderInstallStateMessageFilter::OnMessageReceived( | 34 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); | 35 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
51 } | 36 } |
52 | 37 |
38 void SearchProviderInstallStateImpl::InstallService( | |
39 content::RenderProcessHost* host) { | |
40 int id = host->GetID(); | |
41 Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext()); | |
42 auto* adapter = new SearchProviderInstallStateImplAdapter( | |
43 std::unique_ptr<SearchProviderInstallStateImpl, | |
44 content::BrowserThread::DeleteOnIOThread>( | |
45 new SearchProviderInstallStateImpl(id, profile))); | |
46 host->GetServiceRegistry()->AddService<mojom::SearchProviderInstallState>( | |
sky
2016/06/20 15:20:26
I'm not familiar with the threading requirements o
tibell
2016/06/21 01:31:32
I'm just preserving the same threading behavior as
| |
47 base::Bind(&SearchProviderInstallStateImpl::Bind, | |
48 base::Unretained(adapter->get())), | |
49 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
50 host->SetUserData(SearchProviderInstallStateImpl::kRenderProcessHostKey, | |
51 adapter); | |
52 } | |
53 | |
53 search_provider::InstallState | 54 search_provider::InstallState |
54 SearchProviderInstallStateMessageFilter::GetSearchProviderInstallState( | 55 SearchProviderInstallStateImpl::GetSearchProviderInstallState( |
55 const GURL& page_location, | 56 const GURL& page_location, |
56 const GURL& requested_host) { | 57 const GURL& requested_host) { |
57 GURL requested_origin = requested_host.GetOrigin(); | 58 GURL requested_origin = requested_host.GetOrigin(); |
58 | 59 |
59 // Do the security check before any others to avoid information leaks. | 60 // Do the security check before any others to avoid information leaks. |
60 if (page_location.GetOrigin() != requested_origin) | 61 if (page_location.GetOrigin() != requested_origin) |
61 return search_provider::DENIED; | 62 return search_provider::DENIED; |
62 | 63 |
63 // In incognito mode, no search information is exposed. (This check must be | 64 // 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 // 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 // user is in incognito mode just by doing a cross origin request.) |
66 if (is_off_the_record_) | 67 if (is_off_the_record_) |
67 return search_provider::NOT_INSTALLED; | 68 return search_provider::NOT_INSTALLED; |
68 | 69 |
69 switch (provider_data_.GetInstallState(requested_origin)) { | 70 switch (provider_data_.GetInstallState(requested_origin)) { |
70 case SearchProviderInstallData::NOT_INSTALLED: | 71 case SearchProviderInstallData::NOT_INSTALLED: |
71 return search_provider::NOT_INSTALLED; | 72 return search_provider::NOT_INSTALLED; |
72 | 73 |
73 case SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT: | 74 case SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT: |
74 return search_provider::INSTALLED_BUT_NOT_DEFAULT; | 75 return search_provider::INSTALLED_BUT_NOT_DEFAULT; |
75 | 76 |
76 case SearchProviderInstallData::INSTALLED_AS_DEFAULT: | 77 case SearchProviderInstallData::INSTALLED_AS_DEFAULT: |
77 return search_provider::INSTALLED_AS_DEFAULT; | 78 return search_provider::INSTALLED_AS_DEFAULT; |
78 } | 79 } |
79 | 80 |
80 NOTREACHED(); | 81 NOTREACHED(); |
81 return search_provider::NOT_INSTALLED; | 82 return search_provider::NOT_INSTALLED; |
82 } | 83 } |
83 | 84 |
84 void | 85 void SearchProviderInstallStateImpl::Bind( |
85 SearchProviderInstallStateMessageFilter::OnGetSearchProviderInstallState( | 86 mojom::SearchProviderInstallStateRequest request) { |
87 binding_set_.AddBinding(this, std::move(request)); | |
88 } | |
89 | |
90 const char SearchProviderInstallStateImpl::kRenderProcessHostKey[] = | |
91 "kRenderProcessHostKey"; | |
92 | |
93 void SearchProviderInstallStateImpl::GetInstallState( | |
86 const GURL& page_location, | 94 const GURL& page_location, |
87 const GURL& requested_host, | 95 const GURL& requested_host, |
88 IPC::Message* reply_msg) { | 96 const GetInstallStateCallback& callback) { |
89 provider_data_.CallWhenLoaded( | 97 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
90 base::Bind( | 98 provider_data_.CallWhenLoaded(base::Bind( |
91 &SearchProviderInstallStateMessageFilter:: | 99 &SearchProviderInstallStateImpl::ReplyWithProviderInstallState, |
92 ReplyWithProviderInstallState, | 100 weak_factory_.GetWeakPtr(), page_location, requested_host, callback)); |
93 weak_factory_.GetWeakPtr(), | |
94 page_location, | |
95 requested_host, | |
96 reply_msg)); | |
97 } | 101 } |
98 | 102 |
99 void SearchProviderInstallStateMessageFilter::ReplyWithProviderInstallState( | 103 void SearchProviderInstallStateImpl::ReplyWithProviderInstallState( |
100 const GURL& page_location, | 104 const GURL& page_location, |
101 const GURL& requested_host, | 105 const GURL& requested_host, |
102 IPC::Message* reply_msg) { | 106 const GetInstallStateCallback& callback) { |
103 DCHECK(reply_msg); | |
104 search_provider::InstallState install_state = | 107 search_provider::InstallState install_state = |
105 GetSearchProviderInstallState(page_location, requested_host); | 108 GetSearchProviderInstallState(page_location, requested_host); |
106 | 109 |
107 ChromeViewHostMsg_GetSearchProviderInstallState::WriteReplyParams( | 110 callback.Run(install_state); |
108 reply_msg, | |
109 install_state); | |
110 Send(reply_msg); | |
111 } | 111 } |
112 | |
113 SearchProviderInstallStateImplAdapter::SearchProviderInstallStateImplAdapter( | |
114 std::unique_ptr<SearchProviderInstallStateImpl, | |
115 content::BrowserThread::DeleteOnIOThread> filter) | |
116 : ptr_(std::move(filter)) {} | |
117 | |
118 SearchProviderInstallStateImplAdapter:: | |
119 ~SearchProviderInstallStateImplAdapter() = default; | |
OLD | NEW |