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