| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | |
| 8 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 9 #include "base/values.h" | 8 #include "base/values.h" |
| 10 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/dom_ui/tips_handler.h" | 10 #include "chrome/browser/dom_ui/tips_handler.h" |
| 12 #include "chrome/browser/profile.h" | 11 #include "chrome/browser/profile.h" |
| 13 #include "chrome/browser/web_resource/web_resource_service.h" | 12 #include "chrome/browser/web_resource/web_resource_service.h" |
| 14 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/common/web_resource/web_resource_unpacker.h" | 14 #include "chrome/common/web_resource/web_resource_unpacker.h" |
| 16 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 17 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 18 #include "grit/generated_resources.h" | |
| 19 | 17 |
| 20 DOMMessageHandler* TipsHandler::Attach(DOMUI* dom_ui) { | 18 DOMMessageHandler* TipsHandler::Attach(DOMUI* dom_ui) { |
| 21 dom_ui_ = dom_ui; | 19 dom_ui_ = dom_ui; |
| 22 tips_cache_ = dom_ui_->GetProfile()->GetPrefs()-> | 20 tips_cache_ = dom_ui_->GetProfile()->GetPrefs()-> |
| 23 GetMutableDictionary(prefs::kNTPTipsCache); | 21 GetMutableDictionary(prefs::kNTPTipsCache); |
| 24 return DOMMessageHandler::Attach(dom_ui); | 22 return DOMMessageHandler::Attach(dom_ui); |
| 25 } | 23 } |
| 26 | 24 |
| 27 void TipsHandler::RegisterMessages() { | 25 void TipsHandler::RegisterMessages() { |
| 28 dom_ui_->RegisterMessageCallback("getTips", | 26 dom_ui_->RegisterMessageCallback("getTips", |
| 29 NewCallback(this, &TipsHandler::HandleGetTips)); | 27 NewCallback(this, &TipsHandler::HandleGetTips)); |
| 30 } | 28 } |
| 31 | 29 |
| 32 void TipsHandler::HandleGetTips(const Value* content) { | 30 void TipsHandler::HandleGetTips(const Value* content) { |
| 33 // List containing the tips to be displayed. | 31 // List containing the tips to be displayed. |
| 34 ListValue list_value; | 32 ListValue list_value; |
| 35 | 33 |
| 36 // Holds the web resource data found in the preferences cache. | 34 // Holds the web resource data found in the preferences cache. |
| 37 ListValue* wr_list; | 35 ListValue* wr_list; |
| 38 | 36 |
| 39 // These values hold the data for each web resource item. | 37 // These values hold the data for each web resource item. |
| 40 int current_tip_index; | 38 int current_tip_index; |
| 41 std::string current_tip; | 39 std::string current_tip; |
| 42 | 40 |
| 43 // If tips are not correct for our language, do not send. Wait for update. | 41 // If tips are not correct for our language, do not send. Wait for update. |
| 44 // We need to check here because the new tab page calls for tips before | 42 // We need to check here because the new tab page calls for tips before |
| 45 // the tip service starts up. | 43 // the tip service starts up. |
| 46 PrefService* current_prefs = dom_ui_->GetProfile()->GetPrefs(); | 44 PrefService* current_prefs = dom_ui_->GetProfile()->GetPrefs(); |
| 47 if (current_prefs->HasPrefPath(prefs::kNTPTipsServer)) { | 45 if (current_prefs->HasPrefPath(prefs::kNTPTipsServer)) { |
| 48 std::wstring server = current_prefs->GetString(prefs::kNTPTipsServer); | 46 std::wstring server = current_prefs->GetString(prefs::kNTPTipsServer); |
| 49 std::wstring locale = | 47 std::wstring locale = |
| 50 ASCIIToWide(g_browser_process->GetApplicationLocale()); | 48 ASCIIToWide(g_browser_process->GetApplicationLocale()); |
| 51 if (!EndsWith(server, locale, false)) { | 49 if (!EndsWith(server, locale, false)) { |
| 52 dom_ui_->CallJavascriptFunction(L"tips", list_value); | 50 dom_ui_->CallJavascriptFunction(L"tips", list_value); |
| 53 return; | 51 return; |
| 54 } | 52 } |
| 55 } | 53 } |
| 56 | 54 |
| 57 if (tips_cache_ != NULL && !tips_cache_->empty()) { | 55 if (tips_cache_ != NULL && !tips_cache_->empty()) { |
| 58 if (tips_cache_->GetInteger( | 56 if (tips_cache_->GetInteger( |
| 59 WebResourceService::kCurrentTipPrefName, ¤t_tip_index) && | 57 WebResourceService::kCurrentTipPrefName, ¤t_tip_index) && |
| 60 tips_cache_->GetList( | 58 tips_cache_->GetList( |
| 61 WebResourceService::kTipCachePrefName, &wr_list) && | 59 WebResourceService::kTipCachePrefName, &wr_list) && |
| 62 wr_list && wr_list->GetSize() > 0) { | 60 wr_list && wr_list->GetSize() > 0) { |
| 63 if (wr_list->GetSize() <= static_cast<size_t>(current_tip_index)) { | 61 if (wr_list->GetSize() <= static_cast<size_t>(current_tip_index)) |
| 64 // Check to see whether the home page is set to NTP; if not, add tip | |
| 65 // to set home page before resetting tip index to 0. | |
| 66 current_tip_index = 0; | 62 current_tip_index = 0; |
| 67 if (!dom_ui_->GetProfile()->GetPrefs()->GetBoolean( | |
| 68 prefs::kHomePageIsNewTabPage)) { | |
| 69 SendTip(WideToASCII(l10n_util::GetString( | |
| 70 IDS_NEW_TAB_MAKE_THIS_HOMEPAGE)), L"set_homepage_tip", | |
| 71 current_tip_index); | |
| 72 return; | |
| 73 } | |
| 74 } | |
| 75 if (wr_list->GetString(current_tip_index, ¤t_tip)) { | 63 if (wr_list->GetString(current_tip_index, ¤t_tip)) { |
| 76 SendTip(current_tip, L"tip_html_text", current_tip_index + 1); | 64 DictionaryValue* tip_dict = new DictionaryValue(); |
| 65 tip_dict->SetString(L"tip_html_text", current_tip); |
| 66 list_value.Append(tip_dict); |
| 67 tips_cache_->SetInteger(WebResourceService::kCurrentTipPrefName, |
| 68 current_tip_index + 1); |
| 77 } | 69 } |
| 78 } | 70 } |
| 79 } | 71 } |
| 80 } | |
| 81 | 72 |
| 82 void TipsHandler::SendTip(std::string tip, std::wstring tip_type, | |
| 83 int tip_index) { | |
| 84 // List containing the tips to be displayed. | |
| 85 ListValue list_value; | |
| 86 DictionaryValue* tip_dict = new DictionaryValue(); | |
| 87 tip_dict->SetString(tip_type, tip); | |
| 88 list_value.Append(tip_dict); | |
| 89 tips_cache_->SetInteger(WebResourceService::kCurrentTipPrefName, | |
| 90 tip_index); | |
| 91 // Send list of web resource items back out to the DOM. | 73 // Send list of web resource items back out to the DOM. |
| 92 dom_ui_->CallJavascriptFunction(L"tips", list_value); | 74 dom_ui_->CallJavascriptFunction(L"tips", list_value); |
| 93 } | 75 } |
| 94 | 76 |
| 95 // static | 77 // static |
| 96 void TipsHandler::RegisterUserPrefs(PrefService* prefs) { | 78 void TipsHandler::RegisterUserPrefs(PrefService* prefs) { |
| 97 prefs->RegisterDictionaryPref(prefs::kNTPTipsCache); | 79 prefs->RegisterDictionaryPref(prefs::kNTPTipsCache); |
| 98 prefs->RegisterStringPref(prefs::kNTPTipsServer, | 80 prefs->RegisterStringPref(prefs::kNTPTipsServer, |
| 99 WebResourceService::kDefaultResourceServer); | 81 WebResourceService::kDefaultResourceServer); |
| 100 } | 82 } |
| 101 | 83 |
| 102 bool TipsHandler::IsValidURL(const std::wstring& url_string) { | 84 bool TipsHandler::IsValidURL(const std::wstring& url_string) { |
| 103 GURL url(WideToUTF8(url_string)); | 85 GURL url(WideToUTF8(url_string)); |
| 104 return !url.is_empty() && (url.SchemeIs(chrome::kHttpScheme) || | 86 return !url.is_empty() && (url.SchemeIs(chrome::kHttpScheme) || |
| 105 url.SchemeIs(chrome::kHttpsScheme)); | 87 url.SchemeIs(chrome::kHttpsScheme)); |
| 106 } | 88 } |
| OLD | NEW |