Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / signin / merge_session_load_page.cc
1 // Copyright 2014 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/signin/merge_session_load_page.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/system/tray/system_tray_delegate.h"
10 #include "base/i18n/rtl.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager_factory.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/renderer_preferences_util.h"
21 #include "chrome/browser/tab_contents/tab_util.h"
22 #include "chrome/browser/ui/zoom/zoom_controller.h"
23 #include "chrome/common/extensions/extension_constants.h"
24 #include "chrome/common/url_constants.h"
25 #include "chrome/grit/browser_resources.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/interstitial_page.h"
29 #include "content/public/browser/notification_types.h"
30 #include "content/public/browser/web_contents.h"
31 #include "extensions/browser/extension_system.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/extension_icon_set.h"
34 #include "net/base/escape.h"
35 #include "ui/base/l10n/l10n_util.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/base/webui/jstemplate_builder.h"
38
39 using content::BrowserThread;
40 using content::InterstitialPage;
41 using content::WebContents;
42
43 namespace {
44
45 // Delay time for showing interstitial page.
46 const int kShowDelayTimeMS = 1000;
47
48 // Maximum time for showing interstitial page.
49 const int kTotalWaitTimeMS = 10000;
50
51 }  // namespace
52
53 namespace chromeos {
54
55 MergeSessionLoadPage::MergeSessionLoadPage(
56     WebContents* web_contents,
57     const GURL& url,
58     const MergeSessionThrottle::CompletionCallback& callback)
59     : callback_(callback),
60       proceeded_(false),
61       web_contents_(web_contents),
62       url_(url) {
63   interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this);
64 }
65
66 MergeSessionLoadPage::~MergeSessionLoadPage() {
67   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68 }
69
70 void MergeSessionLoadPage::Show() {
71   OAuth2LoginManager* manager = GetOAuth2LoginManager();
72   if (manager && manager->ShouldBlockTabLoading()) {
73     manager->AddObserver(this);
74     interstitial_page_->Show();
75   } else {
76     interstitial_page_->Proceed();
77   }
78 }
79
80 std::string MergeSessionLoadPage::GetHTMLContents() {
81   base::DictionaryValue strings;
82   strings.SetString("title", web_contents_->GetTitle());
83   // Set the timeout to show the page.
84   strings.SetInteger("show_delay_time", kShowDelayTimeMS);
85   strings.SetInteger("total_wait_time", kTotalWaitTimeMS);
86   // TODO(zelidrag): Flip the message to IDS_MERGE_SESSION_LOAD_HEADLINE
87   // after merge.
88   strings.SetString("heading",
89                     l10n_util::GetStringUTF16(IDS_TAB_LOADING_TITLE));
90
91   bool rtl = base::i18n::IsRTL();
92   strings.SetString("textdirection", rtl ? "rtl" : "ltr");
93
94   base::StringPiece html(
95       ResourceBundle::GetSharedInstance().GetRawDataResource(
96           IDR_MERGE_SESSION_LOAD_HTML));
97   return webui::GetI18nTemplateHtml(html, &strings);
98 }
99
100 void MergeSessionLoadPage::OverrideRendererPrefs(
101       content::RendererPreferences* prefs) {
102   Profile* profile = Profile::FromBrowserContext(
103       web_contents_->GetBrowserContext());
104   renderer_preferences_util::UpdateFromSystemSettings(
105       prefs, profile, web_contents_);
106 }
107
108 void MergeSessionLoadPage::OnProceed() {
109   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
110   proceeded_ = true;
111   NotifyBlockingPageComplete();
112 }
113
114 void MergeSessionLoadPage::OnDontProceed() {
115   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
116   // Ignore if it's already proceeded.
117   if (proceeded_)
118     return;
119   NotifyBlockingPageComplete();
120 }
121
122 void MergeSessionLoadPage::CommandReceived(const std::string& cmd) {
123   std::string command(cmd);
124   // The Jasonified response has quotes, remove them.
125   if (command.length() > 1 && command[0] == '"')
126     command = command.substr(1, command.length() - 2);
127
128   if (command == "proceed") {
129     interstitial_page_->Proceed();
130   } else {
131     DVLOG(1) << "Unknown command:" << cmd;
132   }
133 }
134
135 void MergeSessionLoadPage::NotifyBlockingPageComplete() {
136   OAuth2LoginManager* manager = GetOAuth2LoginManager();
137   if (manager)
138     manager->RemoveObserver(this);
139
140   if (!callback_.is_null()) {
141     BrowserThread::PostTask(
142         BrowserThread::IO, FROM_HERE, callback_);
143   }
144 }
145
146 OAuth2LoginManager* MergeSessionLoadPage::GetOAuth2LoginManager() {
147   content::BrowserContext* browser_context = web_contents_->GetBrowserContext();
148   if (!browser_context)
149     return NULL;
150
151   Profile* profile = Profile::FromBrowserContext(browser_context);
152   if (!profile)
153     return NULL;
154
155   return OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile);
156 }
157
158 void MergeSessionLoadPage::OnSessionRestoreStateChanged(
159     Profile* user_profile, OAuth2LoginManager::SessionRestoreState state) {
160   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
161
162   OAuth2LoginManager* manager = GetOAuth2LoginManager();
163   DVLOG(1) << "Merge session should "
164            << (!manager->ShouldBlockTabLoading() ?
165                   " NOT " : "")
166            << " be blocking now, "
167            << state;
168   if (!manager->ShouldBlockTabLoading()) {
169     manager->RemoveObserver(this);
170     interstitial_page_->Proceed();
171   }
172 }
173
174 }  // namespace chromeos