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