- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / screens / terms_of_service_screen.cc
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/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"
24 #include "url/gurl.h"
25
26 namespace chromeos {
27
28 TermsOfServiceScreen::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
37 TermsOfServiceScreen::~TermsOfServiceScreen() {
38   if (actor_)
39     actor_->SetDelegate(NULL);
40 }
41
42 void TermsOfServiceScreen::PrepareToShow() {
43 }
44
45 void TermsOfServiceScreen::Show() {
46   if (!actor_)
47     return;
48
49   // Set the domain name whose Terms of Service are being shown.
50   actor_->SetDomain(
51       g_browser_process->browser_policy_connector()->GetEnterpriseDomain());
52
53   // Show the screen.
54   actor_->Show();
55
56   // Start downloading the Terms of Service.
57   StartDownload();
58 }
59
60 void TermsOfServiceScreen::Hide() {
61   if (actor_)
62     actor_->Hide();
63 }
64
65 std::string TermsOfServiceScreen::GetName() const {
66   return WizardController::kTermsOfServiceScreenName;
67 }
68
69 void TermsOfServiceScreen::OnDecline() {
70   get_screen_observer()->OnExit(ScreenObserver::TERMS_OF_SERVICE_DECLINED);
71 }
72
73 void TermsOfServiceScreen::OnAccept() {
74   get_screen_observer()->OnExit(ScreenObserver::TERMS_OF_SERVICE_ACCEPTED);
75 }
76
77 void TermsOfServiceScreen::OnActorDestroyed(TermsOfServiceScreenActor* actor) {
78   if (actor_ == actor)
79     actor_ = NULL;
80 }
81
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()) {
89     if (actor_)
90       actor_->OnLoadError();
91     return;
92   }
93
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
100   // accepted.
101   terms_of_service_fetcher_->AddExtraRequestHeader("Accept: text/plain");
102   // Retry up to three times if network changes are detected during the
103   // download.
104   terms_of_service_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3);
105   terms_of_service_fetcher_->Start();
106
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);
110 }
111
112 void TermsOfServiceScreen::OnDownloadTimeout() {
113   // Abort the download attempt.
114   terms_of_service_fetcher_.reset();
115
116   // Show an error message to the user.
117   if (actor_)
118     actor_->OnLoadError();
119 }
120
121 void TermsOfServiceScreen::OnURLFetchComplete(const net::URLFetcher* source) {
122   if (source != terms_of_service_fetcher_.get()) {
123     NOTREACHED() << "Callback from foreign URL fetcher";
124     return;
125   }
126
127   download_timer_.Stop();
128
129   // Destroy the fetcher when this method returns.
130   scoped_ptr<net::URLFetcher> fetcher(terms_of_service_fetcher_.Pass());
131   if (!actor_)
132     return;
133
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();
143   } else {
144     // If the Terms of Service were downloaded successfully, show them to the
145     // user.
146     actor_->OnLoadSuccess(terms_of_service);
147   }
148 }
149
150 }  // namespace chromeos