Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chrome/browser/chromeos/offline/offline_load_page.cc

Issue 2931005: Show offline interstitial page when offline and reload when reconnected to network. (Closed)
Patch Set: " Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/offline/offline_load_page.h"
6
7 #include "app/l10n_util.h"
8 #include "app/resource_bundle.h"
9 #include "base/histogram.h"
10 #include "base/i18n/rtl.h"
11 #include "base/string_piece.h"
12 #include "base/values.h"
13 #include "chrome/browser/browser.h"
14 #include "chrome/browser/chrome_thread.h"
15 #include "chrome/browser/tab_contents/navigation_controller.h"
16 #include "chrome/browser/tab_contents/navigation_entry.h"
17 #include "chrome/browser/tab_contents/tab_contents.h"
18 #include "chrome/browser/tab_contents/tab_util.h"
19 #include "chrome/common/jstemplate_builder.h"
20 #include "chrome/common/notification_type.h"
21 #include "grit/browser_resources.h"
22 #include "grit/generated_resources.h"
23
24 namespace {
25
26 // Maximum time to show a blank page.
27 const int kMaxBlankPeriod = 3000;
28
29 // A utility function to set the dictionary's value given by |resource_id|.
30 void SetString(DictionaryValue* strings, const wchar_t* name, int resource_id) {
31 strings->SetString(name, l10n_util::GetString(resource_id));
32 }
33
34 } // namespace
35
36 namespace chromeos {
37
38 // static
39 void OfflineLoadPage::Show(int process_host_id, int render_view_id,
40 const GURL& url, Delegate* delegate) {
41 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
42 if (NetworkStateNotifier::is_connected()) {
43 // Check again in UI thread and proceed if it's connected.
44 delegate->OnBlockingPageComplete(true);
45 } else {
46 TabContents* tab_contents =
47 tab_util::GetTabContentsByID(process_host_id, render_view_id);
48 DCHECK(tab_contents);
49 (new OfflineLoadPage(tab_contents, url, delegate))->Show();
50 }
51 }
52
53 OfflineLoadPage::OfflineLoadPage(TabContents* tab_contents,
54 const GURL& url,
55 Delegate* delegate)
56 : InterstitialPage(tab_contents, true, url),
57 delegate_(delegate) {
58 registrar_.Add(this, NotificationType::NETWORK_STATE_CHANGED,
59 NotificationService::AllSources());
60 }
61
62 std::string OfflineLoadPage::GetHTMLContents() {
63 DictionaryValue strings;
64 SetString(&strings, L"headLine", IDS_OFFLINE_LOAD_HEADLINE);
65 SetString(&strings, L"description", IDS_OFFLINE_LOAD_DESCRIPTION);
66 SetString(&strings, L"load_button", IDS_OFFLINE_LOAD_BUTTON);
67 SetString(&strings, L"back_button", IDS_OFFLINE_BACK_BUTTON);
68
69 // TODO(oshima): tab()->GetTitle() always return url. This has to be
70 // a cached title.
71 strings.SetString(L"title", UTF16ToWide(tab()->GetTitle()));
72 strings.SetString(L"textdirection", base::i18n::IsRTL() ? L"rtl" : L"ltr");
73 strings.SetString(L"display_go_back",
74 tab()->controller().CanGoBack() ? L"inline" : L"none");
75 int64 time_to_wait = std::max(
76 static_cast<int64>(0),
77 kMaxBlankPeriod -
78 NetworkStateNotifier::GetOfflineDuration().InMilliseconds());
79 strings.SetString(L"on_load",
80 StringPrintf(L"startTimer(%ld)", time_to_wait));
81
82 // TODO(oshima): thumbnail is not working yet. fix this.
83 const std::string url = "chrome://thumb/" + GetURL().spec();
84 strings.SetString(L"thumbnailUrl", "url(" + url + ")");
85
86 base::StringPiece html(
87 ResourceBundle::GetSharedInstance().GetRawDataResource(
88 IDR_OFFLINE_LOAD_HTML));
89 return jstemplate_builder::GetI18nTemplateHtml(html, &strings);
90 }
91
92 void OfflineLoadPage::CommandReceived(const std::string& cmd) {
93 std::string command(cmd);
94 // The Jasonified response has quotes, remove them.
95 if (command.length() > 1 && command[0] == '"') {
96 command = command.substr(1, command.length() - 2);
97 }
98 // TODO(oshima): record action for metrics.
99 if (command == "proceed") {
100 Proceed();
101 } else if (command == "dontproceed") {
102 DontProceed();
103 } else {
104 LOG(WARNING) << "Unknown command:" << cmd;
105 }
106 }
107
108 void OfflineLoadPage::Proceed() {
109 delegate_->OnBlockingPageComplete(true);
110 InterstitialPage::Proceed();
111 }
112
113 void OfflineLoadPage::DontProceed() {
114 delegate_->OnBlockingPageComplete(false);
115 InterstitialPage::DontProceed();
116 }
117
118 void OfflineLoadPage::Observe(NotificationType type,
119 const NotificationSource& source,
120 const NotificationDetails& details) {
121 if (type.value == NotificationType::NETWORK_STATE_CHANGED) {
122 chromeos::NetworkStateDetails* state_details =
123 Details<chromeos::NetworkStateDetails>(details).ptr();
124 DLOG(INFO) << "NetworkStateChanaged notification received: state="
125 << state_details->state();
126 if (state_details->state() ==
127 chromeos::NetworkStateDetails::CONNECTED) {
128 registrar_.Remove(this, NotificationType::NETWORK_STATE_CHANGED,
129 NotificationService::AllSources());
130 Proceed();
131 }
132 } else {
133 InterstitialPage::Observe(type, source, details);
134 }
135 }
136
137 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/offline/offline_load_page.h ('k') | chrome/browser/chromeos/offline/offline_load_page_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698