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.
5 #include "chrome/browser/chromeos/login/screens/terms_of_service_screen.h"
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/policy/browser_policy_connector.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"
28 TermsOfServiceScreen::TermsOfServiceScreen(ScreenObserver* screen_observer,
29 TermsOfServiceScreenActor* actor)
30 : WizardScreen(screen_observer),
34 actor_->SetDelegate(this);
37 TermsOfServiceScreen::~TermsOfServiceScreen() {
39 actor_->SetDelegate(NULL);
42 void TermsOfServiceScreen::PrepareToShow() {
45 void TermsOfServiceScreen::Show() {
49 // Set the domain name whose Terms of Service are being shown.
51 g_browser_process->browser_policy_connector()->GetEnterpriseDomain());
56 // Start downloading the Terms of Service.
60 void TermsOfServiceScreen::Hide() {
65 std::string TermsOfServiceScreen::GetName() const {
66 return WizardController::kTermsOfServiceScreenName;
69 void TermsOfServiceScreen::OnDecline() {
70 get_screen_observer()->OnExit(ScreenObserver::TERMS_OF_SERVICE_DECLINED);
73 void TermsOfServiceScreen::OnAccept() {
74 get_screen_observer()->OnExit(ScreenObserver::TERMS_OF_SERVICE_ACCEPTED);
77 void TermsOfServiceScreen::OnActorDestroyed(TermsOfServiceScreenActor* actor) {
82 void TermsOfServiceScreen::StartDownload() {
83 const PrefService* prefs = ProfileManager::GetDefaultProfile()->GetPrefs();
84 // If an URL from which the Terms of Service can be downloaded has not been
85 // set, show an error message to the user.
86 std::string terms_of_service_url =
87 prefs->GetString(prefs::kTermsOfServiceURL);
88 if (terms_of_service_url.empty()) {
90 actor_->OnLoadError();
94 // Start downloading the Terms of Service.
95 terms_of_service_fetcher_.reset(net::URLFetcher::Create(
96 GURL(terms_of_service_url), net::URLFetcher::GET, this));
97 terms_of_service_fetcher_->SetRequestContext(
98 g_browser_process->system_request_context());
99 // Request a text/plain MIME type as only plain-text Terms of Service are
101 terms_of_service_fetcher_->AddExtraRequestHeader("Accept: text/plain");
102 // Retry up to three times if network changes are detected during the
104 terms_of_service_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3);
105 terms_of_service_fetcher_->Start();
107 // Abort the download attempt if it takes longer than one minute.
108 download_timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(1),
109 this, &TermsOfServiceScreen::OnDownloadTimeout);
112 void TermsOfServiceScreen::OnDownloadTimeout() {
113 // Abort the download attempt.
114 terms_of_service_fetcher_.reset();
116 // Show an error message to the user.
118 actor_->OnLoadError();
121 void TermsOfServiceScreen::OnURLFetchComplete(const net::URLFetcher* source) {
122 if (source != terms_of_service_fetcher_.get()) {
123 NOTREACHED() << "Callback from foreign URL fetcher";
127 download_timer_.Stop();
129 // Destroy the fetcher when this method returns.
130 scoped_ptr<net::URLFetcher> fetcher(terms_of_service_fetcher_.Pass());
134 std::string terms_of_service;
135 std::string mime_type;
136 // If the Terms of Service could not be downloaded, do not have a MIME type of
137 // text/plain or are empty, show an error message to the user.
138 if (!source->GetStatus().is_success() ||
139 !source->GetResponseHeaders()->GetMimeType(&mime_type) ||
140 mime_type != "text/plain" ||
141 !source->GetResponseAsString(&terms_of_service)) {
142 actor_->OnLoadError();
144 // If the Terms of Service were downloaded successfully, show them to the
146 actor_->OnLoadSuccess(terms_of_service);
150 } // namespace chromeos