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/base_screen_delegate.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"
28 TermsOfServiceScreen::TermsOfServiceScreen(
29 BaseScreenDelegate
* base_screen_delegate
,
30 TermsOfServiceScreenActor
* actor
)
31 : BaseScreen(base_screen_delegate
), actor_(actor
) {
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.
50 policy::BrowserPolicyConnectorChromeOS
* connector
=
51 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
52 actor_
->SetDomain(connector
->GetEnterpriseDomain());
57 // Start downloading the Terms of Service.
61 void TermsOfServiceScreen::Hide() {
66 std::string
TermsOfServiceScreen::GetName() const {
67 return WizardController::kTermsOfServiceScreenName
;
70 void TermsOfServiceScreen::OnDecline() {
71 Finish(BaseScreenDelegate::TERMS_OF_SERVICE_DECLINED
);
74 void TermsOfServiceScreen::OnAccept() {
75 Finish(BaseScreenDelegate::TERMS_OF_SERVICE_ACCEPTED
);
78 void TermsOfServiceScreen::OnActorDestroyed(TermsOfServiceScreenActor
* actor
) {
83 void 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()) {
91 actor_
->OnLoadError();
95 // Start downloading the Terms of Service.
96 terms_of_service_fetcher_
= 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
102 terms_of_service_fetcher_
->AddExtraRequestHeader("Accept: text/plain");
103 // Retry up to three times if network changes are detected during the
105 terms_of_service_fetcher_
->SetAutomaticallyRetryOnNetworkChanges(3);
106 terms_of_service_fetcher_
->Start();
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
);
113 void TermsOfServiceScreen::OnDownloadTimeout() {
114 // Abort the download attempt.
115 terms_of_service_fetcher_
.reset();
117 // Show an error message to the user.
119 actor_
->OnLoadError();
122 void TermsOfServiceScreen::OnURLFetchComplete(const net::URLFetcher
* source
) {
123 if (source
!= terms_of_service_fetcher_
.get()) {
124 NOTREACHED() << "Callback from foreign URL fetcher";
128 download_timer_
.Stop();
130 // Destroy the fetcher when this method returns.
131 scoped_ptr
<net::URLFetcher
> fetcher(terms_of_service_fetcher_
.Pass());
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();
145 // If the Terms of Service were downloaded successfully, show them to the
147 actor_
->OnLoadSuccess(terms_of_service
);
151 } // namespace chromeos