OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/ui/webui/new_profile_ui.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "base/time.h" |
| 9 #include "chrome/browser/defaults.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 13 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 14 #include "chrome/browser/ui/webui/new_profile_dom_handler.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/common/url_constants.h" |
| 17 #include "content/browser/tab_contents/tab_contents.h" |
| 18 #include "grit/browser_resources.h" |
| 19 #include "grit/chromium_strings.h" |
| 20 #include "grit/generated_resources.h" |
| 21 #include "grit/theme_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kStringsJsFile[] = "strings.js"; |
| 27 const char kNewProfileJsFile[] = "new_profile.js"; |
| 28 |
| 29 // Checks if this is the current time within a given date range? |
| 30 bool InDateRange(double begin, double end) { |
| 31 base::Time start_time = base::Time::FromDoubleT(begin); |
| 32 base::Time end_time = base::Time::FromDoubleT(end); |
| 33 return start_time < base::Time::Now() && end_time > base::Time::Now(); |
| 34 } |
| 35 |
| 36 /////////////////////////////////////////////////////////////////////////////// |
| 37 // |
| 38 // NewProfileUIHTMLSource |
| 39 // |
| 40 /////////////////////////////////////////////////////////////////////////////// |
| 41 |
| 42 class NewProfileUIHTMLSource : public ChromeWebUIDataSource { |
| 43 public: |
| 44 explicit NewProfileUIHTMLSource(Profile* profile); |
| 45 |
| 46 // Called when the network layer has requested a resource underneath |
| 47 // the path we registered. |
| 48 virtual void StartDataRequest(const std::string& path, |
| 49 bool is_incognito, |
| 50 int request_id); |
| 51 |
| 52 virtual std::string GetMimeType(const std::string&) const; |
| 53 |
| 54 static int PathToIDR(const std::string& path); |
| 55 |
| 56 private: |
| 57 ~NewProfileUIHTMLSource() {} |
| 58 DISALLOW_COPY_AND_ASSIGN(NewProfileUIHTMLSource); |
| 59 }; |
| 60 |
| 61 NewProfileUIHTMLSource::NewProfileUIHTMLSource(Profile* profile) |
| 62 : ChromeWebUIDataSource(chrome::kChromeUINewProfileHost) { |
| 63 AddLocalizedString("title", IDS_NEW_PROFILE_SETUP_TITLE); |
| 64 AddLocalizedString("createProfile", IDS_NEW_PROFILE_SETUP_CREATE_PROFILE); |
| 65 AddLocalizedString("cancelProfile", IDS_NEW_PROFILE_SETUP_CANCEL_PROFILE); |
| 66 AddLocalizedString("profileNameLabel", IDS_NEW_PROFILE_SETUP_NAME_LABEL); |
| 67 AddLocalizedString("profileIconLabel", IDS_NEW_PROFILE_SETUP_ICON_LABEL); |
| 68 AddLocalizedString("summaryConclusion", |
| 69 IDS_NEW_PROFILE_SETUP_SUMMARY_CONCLUSION); |
| 70 |
| 71 const string16 short_product_name = |
| 72 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
| 73 AddString("summaryTitle", l10n_util::GetStringFUTF16( |
| 74 IDS_NEW_PROFILE_SETUP_SUMMARY_TITLE, short_product_name)); |
| 75 AddString("summaryBody", l10n_util::GetStringFUTF16( |
| 76 IDS_NEW_PROFILE_SETUP_SUMMARY_BODY, short_product_name)); |
| 77 |
| 78 // If the user has preferences for a start and end time for a custom logo, |
| 79 // and the time now is between these two times, show the custom logo. |
| 80 // TODO(sail): We should also update the logo if the theme changes or |
| 81 // if these prefs change. |
| 82 // TODO(sail): This code is copy pasted from the NTP code. This should be |
| 83 // refactored. |
| 84 bool hasCustomLogo = |
| 85 profile->GetPrefs()->FindPreference(prefs::kNTPCustomLogoStart) && |
| 86 profile->GetPrefs()->FindPreference(prefs::kNTPCustomLogoEnd) && |
| 87 InDateRange(profile->GetPrefs()->GetDouble(prefs::kNTPCustomLogoStart), |
| 88 profile->GetPrefs()->GetDouble(prefs::kNTPCustomLogoEnd)); |
| 89 AddString("customlogo", ASCIIToUTF16(hasCustomLogo ? "true" : "false")); |
| 90 } |
| 91 |
| 92 void NewProfileUIHTMLSource::StartDataRequest(const std::string& path, |
| 93 bool is_incognito, |
| 94 int request_id) { |
| 95 if (path == kStringsJsFile) { |
| 96 SendLocalizedStringsAsJSON(request_id); |
| 97 } else { |
| 98 int idr = path == kNewProfileJsFile ? IDR_NEW_PROFILE_JS : |
| 99 IDR_NEW_PROFILE_HTML; |
| 100 SendFromResourceBundle(request_id, idr); |
| 101 } |
| 102 } |
| 103 |
| 104 std::string NewProfileUIHTMLSource::GetMimeType(const std::string& path) const { |
| 105 if (path == kStringsJsFile || path == kNewProfileJsFile) |
| 106 return "application/javascript"; |
| 107 |
| 108 return "text/html"; |
| 109 } |
| 110 |
| 111 } // namespace |
| 112 |
| 113 /////////////////////////////////////////////////////////////////////////////// |
| 114 // |
| 115 // NewProfileUI |
| 116 // |
| 117 /////////////////////////////////////////////////////////////////////////////// |
| 118 |
| 119 NewProfileUI::NewProfileUI(TabContents* contents) : ChromeWebUI(contents) { |
| 120 should_hide_url_ = true; |
| 121 |
| 122 NewProfileDOMHandler* handler = new NewProfileDOMHandler(); |
| 123 AddMessageHandler(handler); |
| 124 handler->Attach(this); |
| 125 |
| 126 // Set up the new profile source. |
| 127 NewProfileUIHTMLSource* html_source = |
| 128 new NewProfileUIHTMLSource(GetProfile()); |
| 129 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); |
| 130 } |
OLD | NEW |