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