| OLD | NEW |
| (Empty) |
| 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_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "chrome/browser/google/google_url_tracker_factory.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #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/common/search_provider.mojom.h" | |
| 14 #include "content/public/browser/render_process_host.h" | |
| 15 #include "services/shell/public/cpp/interface_registry.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 using content::BrowserThread; | |
| 19 | |
| 20 SearchProviderInstallStateImpl::SearchProviderInstallStateImpl( | |
| 21 int render_process_id, | |
| 22 Profile* profile) | |
| 23 : provider_data_(TemplateURLServiceFactory::GetForProfile(profile), | |
| 24 UIThreadSearchTermsData(profile).GoogleBaseURLValue(), | |
| 25 GoogleURLTrackerFactory::GetForProfile(profile), | |
| 26 content::RenderProcessHost::FromID(render_process_id)), | |
| 27 is_off_the_record_(profile->IsOffTheRecord()), | |
| 28 binding_(this), | |
| 29 weak_factory_(this) { | |
| 30 // This is initialized by RenderProcessHostImpl. Do not add any non-trivial | |
| 31 // initialization here. Instead do it lazily when required. | |
| 32 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 33 } | |
| 34 | |
| 35 SearchProviderInstallStateImpl::~SearchProviderInstallStateImpl() { | |
| 36 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 37 } | |
| 38 | |
| 39 // static | |
| 40 void SearchProviderInstallStateImpl::Create( | |
| 41 int render_process_id, | |
| 42 Profile* profile, | |
| 43 chrome::mojom::SearchProviderInstallStateRequest request) { | |
| 44 // BindOnIOThread takes ownership on the impl object: | |
| 45 BrowserThread::PostTask( | |
| 46 BrowserThread::IO, FROM_HERE, | |
| 47 base::Bind(&SearchProviderInstallStateImpl::BindOnIOThread, | |
| 48 base::Unretained(new SearchProviderInstallStateImpl( | |
| 49 render_process_id, profile)), | |
| 50 base::Passed(&request))); | |
| 51 } | |
| 52 | |
| 53 void SearchProviderInstallStateImpl::BindOnIOThread( | |
| 54 chrome::mojom::SearchProviderInstallStateRequest request) { | |
| 55 binding_.Bind(std::move(request)); | |
| 56 } | |
| 57 | |
| 58 chrome::mojom::InstallState | |
| 59 SearchProviderInstallStateImpl::GetSearchProviderInstallState( | |
| 60 const GURL& page_location, | |
| 61 const GURL& requested_host) { | |
| 62 GURL requested_origin = requested_host.GetOrigin(); | |
| 63 | |
| 64 // Do the security check before any others to avoid information leaks. | |
| 65 if (page_location.GetOrigin() != requested_origin) | |
| 66 return chrome::mojom::InstallState::DENIED; | |
| 67 | |
| 68 // In incognito mode, no search information is exposed. (This check must be | |
| 69 // done after the security check or else a web site can detect that the | |
| 70 // user is in incognito mode just by doing a cross origin request.) | |
| 71 if (is_off_the_record_) | |
| 72 return chrome::mojom::InstallState::NOT_INSTALLED; | |
| 73 | |
| 74 switch (provider_data_.GetInstallState(requested_origin)) { | |
| 75 case SearchProviderInstallData::NOT_INSTALLED: | |
| 76 return chrome::mojom::InstallState::NOT_INSTALLED; | |
| 77 | |
| 78 case SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT: | |
| 79 return chrome::mojom::InstallState::INSTALLED_BUT_NOT_DEFAULT; | |
| 80 | |
| 81 case SearchProviderInstallData::INSTALLED_AS_DEFAULT: | |
| 82 return chrome::mojom::InstallState::INSTALLED_AS_DEFAULT; | |
| 83 } | |
| 84 | |
| 85 NOTREACHED(); | |
| 86 return chrome::mojom::InstallState::NOT_INSTALLED; | |
| 87 } | |
| 88 | |
| 89 void SearchProviderInstallStateImpl::GetInstallState( | |
| 90 const GURL& page_location, | |
| 91 const GURL& requested_host, | |
| 92 const GetInstallStateCallback& callback) { | |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 94 provider_data_.CallWhenLoaded(base::Bind( | |
| 95 &SearchProviderInstallStateImpl::ReplyWithProviderInstallState, | |
| 96 weak_factory_.GetWeakPtr(), page_location, requested_host, callback)); | |
| 97 } | |
| 98 | |
| 99 void SearchProviderInstallStateImpl::ReplyWithProviderInstallState( | |
| 100 const GURL& page_location, | |
| 101 const GURL& requested_host, | |
| 102 const GetInstallStateCallback& callback) { | |
| 103 chrome::mojom::InstallState install_state = | |
| 104 GetSearchProviderInstallState(page_location, requested_host); | |
| 105 | |
| 106 callback.Run(install_state); | |
| 107 } | |
| OLD | NEW |