[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 1 | // Copyright (c) 2009 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 | |
[email protected] | 98ce1ee | 2009-09-30 22:00:50 | [diff] [blame] | 5 | #include <string> |
| 6 | |
[email protected] | 2fa20c67 | 2009-12-02 22:42:57 | [diff] [blame] | 7 | #include "app/l10n_util.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 8 | #include "base/callback.h" |
[email protected] | 9fd9092f | 2010-03-08 23:28:41 | [diff] [blame] | 9 | #include "base/utf_string_conversions.h" |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | 4f2b1c4 | 2009-09-10 02:04:38 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 12 | #include "chrome/browser/dom_ui/tips_handler.h" |
| 13 | #include "chrome/browser/profile.h" |
| 14 | #include "chrome/browser/web_resource/web_resource_service.h" |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 15 | #include "chrome/common/pref_names.h" |
[email protected] | 90945981 | 2009-06-26 20:55:01 | [diff] [blame] | 16 | #include "chrome/common/web_resource/web_resource_unpacker.h" |
| 17 | #include "chrome/common/url_constants.h" |
| 18 | #include "googleurl/src/gurl.h" |
[email protected] | 2fa20c67 | 2009-12-02 22:42:57 | [diff] [blame] | 19 | #include "grit/generated_resources.h" |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 20 | |
[email protected] | 2bc2de6 | 2009-06-29 23:37:42 | [diff] [blame] | 21 | DOMMessageHandler* TipsHandler::Attach(DOMUI* dom_ui) { |
[email protected] | cf4e8bf | 2009-07-01 16:55:15 | [diff] [blame] | 22 | dom_ui_ = dom_ui; |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 23 | tips_cache_ = dom_ui_->GetProfile()->GetPrefs()-> |
[email protected] | 61a523f | 2009-08-26 00:32:11 | [diff] [blame] | 24 | GetMutableDictionary(prefs::kNTPTipsCache); |
[email protected] | cf4e8bf | 2009-07-01 16:55:15 | [diff] [blame] | 25 | return DOMMessageHandler::Attach(dom_ui); |
[email protected] | 2bc2de6 | 2009-06-29 23:37:42 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void TipsHandler::RegisterMessages() { |
| 29 | dom_ui_->RegisterMessageCallback("getTips", |
| 30 | NewCallback(this, &TipsHandler::HandleGetTips)); |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void TipsHandler::HandleGetTips(const Value* content) { |
[email protected] | 2fa20c67 | 2009-12-02 22:42:57 | [diff] [blame] | 34 | // List containing the tips to be displayed. |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 35 | ListValue list_value; |
| 36 | |
| 37 | // Holds the web resource data found in the preferences cache. |
[email protected] | 61a523f | 2009-08-26 00:32:11 | [diff] [blame] | 38 | ListValue* wr_list; |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 39 | |
[email protected] | 61a523f | 2009-08-26 00:32:11 | [diff] [blame] | 40 | // These values hold the data for each web resource item. |
| 41 | int current_tip_index; |
| 42 | std::string current_tip; |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 43 | |
[email protected] | 6401e9a | 2009-10-07 00:00:12 | [diff] [blame] | 44 | // If tips are not correct for our language, do not send. Wait for update. |
[email protected] | 4f2b1c4 | 2009-09-10 02:04:38 | [diff] [blame] | 45 | // We need to check here because the new tab page calls for tips before |
| 46 | // the tip service starts up. |
| 47 | PrefService* current_prefs = dom_ui_->GetProfile()->GetPrefs(); |
| 48 | if (current_prefs->HasPrefPath(prefs::kNTPTipsServer)) { |
| 49 | std::wstring server = current_prefs->GetString(prefs::kNTPTipsServer); |
[email protected] | 9986ee16 | 2009-11-19 16:25:04 | [diff] [blame] | 50 | std::wstring locale = |
| 51 | ASCIIToWide(g_browser_process->GetApplicationLocale()); |
| 52 | if (!EndsWith(server, locale, false)) { |
[email protected] | 4f2b1c4 | 2009-09-10 02:04:38 | [diff] [blame] | 53 | dom_ui_->CallJavascriptFunction(L"tips", list_value); |
| 54 | return; |
| 55 | } |
| 56 | } |
| 57 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 58 | if (tips_cache_ != NULL && !tips_cache_->empty()) { |
[email protected] | 61a523f | 2009-08-26 00:32:11 | [diff] [blame] | 59 | if (tips_cache_->GetInteger( |
| 60 | WebResourceService::kCurrentTipPrefName, ¤t_tip_index) && |
| 61 | tips_cache_->GetList( |
[email protected] | 33d3c05 | 2009-09-18 21:07:19 | [diff] [blame] | 62 | WebResourceService::kTipCachePrefName, &wr_list) && |
| 63 | wr_list && wr_list->GetSize() > 0) { |
[email protected] | 2fa20c67 | 2009-12-02 22:42:57 | [diff] [blame] | 64 | if (wr_list->GetSize() <= static_cast<size_t>(current_tip_index)) { |
| 65 | // Check to see whether the home page is set to NTP; if not, add tip |
| 66 | // to set home page before resetting tip index to 0. |
[email protected] | 61a523f | 2009-08-26 00:32:11 | [diff] [blame] | 67 | current_tip_index = 0; |
[email protected] | 8fe6e1c | 2010-05-27 08:21:54 | [diff] [blame] | 68 | const PrefService::Preference* pref = |
| 69 | dom_ui_->GetProfile()->GetPrefs()->FindPreference( |
| 70 | prefs::kHomePageIsNewTabPage); |
| 71 | bool value; |
| 72 | if (pref && !pref->IsManaged() && |
| 73 | pref->GetValue()->GetAsBoolean(&value) && !value) { |
[email protected] | 563007b | 2009-12-03 20:28:36 | [diff] [blame] | 74 | SendTip(WideToUTF8(l10n_util::GetString( |
[email protected] | 2fa20c67 | 2009-12-02 22:42:57 | [diff] [blame] | 75 | IDS_NEW_TAB_MAKE_THIS_HOMEPAGE)), L"set_homepage_tip", |
| 76 | current_tip_index); |
| 77 | return; |
| 78 | } |
| 79 | } |
[email protected] | 61a523f | 2009-08-26 00:32:11 | [diff] [blame] | 80 | if (wr_list->GetString(current_tip_index, ¤t_tip)) { |
[email protected] | 2fa20c67 | 2009-12-02 22:42:57 | [diff] [blame] | 81 | SendTip(current_tip, L"tip_html_text", current_tip_index + 1); |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
[email protected] | 2fa20c67 | 2009-12-02 22:42:57 | [diff] [blame] | 85 | } |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 86 | |
[email protected] | 2fa20c67 | 2009-12-02 22:42:57 | [diff] [blame] | 87 | void TipsHandler::SendTip(std::string tip, std::wstring tip_type, |
| 88 | int tip_index) { |
| 89 | // List containing the tips to be displayed. |
| 90 | ListValue list_value; |
| 91 | DictionaryValue* tip_dict = new DictionaryValue(); |
| 92 | tip_dict->SetString(tip_type, tip); |
| 93 | list_value.Append(tip_dict); |
| 94 | tips_cache_->SetInteger(WebResourceService::kCurrentTipPrefName, |
| 95 | tip_index); |
[email protected] | 6a3d3e6 | 2009-06-25 23:32:48 | [diff] [blame] | 96 | // Send list of web resource items back out to the DOM. |
| 97 | dom_ui_->CallJavascriptFunction(L"tips", list_value); |
| 98 | } |
| 99 | |
| 100 | // static |
| 101 | void TipsHandler::RegisterUserPrefs(PrefService* prefs) { |
| 102 | prefs->RegisterDictionaryPref(prefs::kNTPTipsCache); |
| 103 | prefs->RegisterStringPref(prefs::kNTPTipsServer, |
| 104 | WebResourceService::kDefaultResourceServer); |
| 105 | } |
| 106 | |
[email protected] | 90945981 | 2009-06-26 20:55:01 | [diff] [blame] | 107 | bool TipsHandler::IsValidURL(const std::wstring& url_string) { |
| 108 | GURL url(WideToUTF8(url_string)); |
| 109 | return !url.is_empty() && (url.SchemeIs(chrome::kHttpScheme) || |
| 110 | url.SchemeIs(chrome::kHttpsScheme)); |
| 111 | } |