terms_of_service_screen.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2013 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/chromeos/login/screens/terms_of_service_screen.h" 6 7#include <string> 8 9#include "base/location.h" 10#include "base/logging.h" 11#include "base/prefs/pref_service.h" 12#include "base/time/time.h" 13#include "chrome/browser/browser_process.h" 14#include "chrome/browser/chromeos/login/screens/screen_observer.h" 15#include "chrome/browser/chromeos/login/wizard_controller.h" 16#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 17#include "chrome/browser/profiles/profile.h" 18#include "chrome/browser/profiles/profile_manager.h" 19#include "chrome/common/pref_names.h" 20#include "net/http/http_response_headers.h" 21#include "net/url_request/url_fetcher.h" 22#include "net/url_request/url_request_context_getter.h" 23#include "net/url_request/url_request_status.h" 24#include "url/gurl.h" 25 26namespace chromeos { 27 28TermsOfServiceScreen::TermsOfServiceScreen(ScreenObserver* screen_observer, 29 TermsOfServiceScreenActor* actor) 30 : WizardScreen(screen_observer), 31 actor_(actor) { 32 DCHECK(actor_); 33 if (actor_) 34 actor_->SetDelegate(this); 35} 36 37TermsOfServiceScreen::~TermsOfServiceScreen() { 38 if (actor_) 39 actor_->SetDelegate(NULL); 40} 41 42void TermsOfServiceScreen::PrepareToShow() { 43} 44 45void TermsOfServiceScreen::Show() { 46 if (!actor_) 47 return; 48 49 // Set the domain name whose Terms of Service are being shown. 50 policy::BrowserPolicyConnectorChromeOS* connector = 51 g_browser_process->platform_part()->browser_policy_connector_chromeos(); 52 actor_->SetDomain(connector->GetEnterpriseDomain()); 53 54 // Show the screen. 55 actor_->Show(); 56 57 // Start downloading the Terms of Service. 58 StartDownload(); 59} 60 61void TermsOfServiceScreen::Hide() { 62 if (actor_) 63 actor_->Hide(); 64} 65 66std::string TermsOfServiceScreen::GetName() const { 67 return WizardController::kTermsOfServiceScreenName; 68} 69 70void TermsOfServiceScreen::OnDecline() { 71 get_screen_observer()->OnExit(ScreenObserver::TERMS_OF_SERVICE_DECLINED); 72} 73 74void TermsOfServiceScreen::OnAccept() { 75 get_screen_observer()->OnExit(ScreenObserver::TERMS_OF_SERVICE_ACCEPTED); 76} 77 78void TermsOfServiceScreen::OnActorDestroyed(TermsOfServiceScreenActor* actor) { 79 if (actor_ == actor) 80 actor_ = NULL; 81} 82 83void TermsOfServiceScreen::StartDownload() { 84 const PrefService* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs(); 85 // If an URL from which the Terms of Service can be downloaded has not been 86 // set, show an error message to the user. 87 std::string terms_of_service_url = 88 prefs->GetString(prefs::kTermsOfServiceURL); 89 if (terms_of_service_url.empty()) { 90 if (actor_) 91 actor_->OnLoadError(); 92 return; 93 } 94 95 // Start downloading the Terms of Service. 96 terms_of_service_fetcher_.reset(net::URLFetcher::Create( 97 GURL(terms_of_service_url), net::URLFetcher::GET, this)); 98 terms_of_service_fetcher_->SetRequestContext( 99 g_browser_process->system_request_context()); 100 // Request a text/plain MIME type as only plain-text Terms of Service are 101 // accepted. 102 terms_of_service_fetcher_->AddExtraRequestHeader("Accept: text/plain"); 103 // Retry up to three times if network changes are detected during the 104 // download. 105 terms_of_service_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); 106 terms_of_service_fetcher_->Start(); 107 108 // Abort the download attempt if it takes longer than one minute. 109 download_timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(1), 110 this, &TermsOfServiceScreen::OnDownloadTimeout); 111} 112 113void TermsOfServiceScreen::OnDownloadTimeout() { 114 // Abort the download attempt. 115 terms_of_service_fetcher_.reset(); 116 117 // Show an error message to the user. 118 if (actor_) 119 actor_->OnLoadError(); 120} 121 122void TermsOfServiceScreen::OnURLFetchComplete(const net::URLFetcher* source) { 123 if (source != terms_of_service_fetcher_.get()) { 124 NOTREACHED() << "Callback from foreign URL fetcher"; 125 return; 126 } 127 128 download_timer_.Stop(); 129 130 // Destroy the fetcher when this method returns. 131 scoped_ptr<net::URLFetcher> fetcher(terms_of_service_fetcher_.Pass()); 132 if (!actor_) 133 return; 134 135 std::string terms_of_service; 136 std::string mime_type; 137 // If the Terms of Service could not be downloaded, do not have a MIME type of 138 // text/plain or are empty, show an error message to the user. 139 if (!source->GetStatus().is_success() || 140 !source->GetResponseHeaders()->GetMimeType(&mime_type) || 141 mime_type != "text/plain" || 142 !source->GetResponseAsString(&terms_of_service)) { 143 actor_->OnLoadError(); 144 } else { 145 // If the Terms of Service were downloaded successfully, show them to the 146 // user. 147 actor_->OnLoadSuccess(terms_of_service); 148 } 149} 150 151} // namespace chromeos 152