| 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 "base/string_util.h" | 5 #include "base/string_util.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chrome/browser/dom_ui/tips_handler.h" | 7 #include "chrome/browser/dom_ui/tips_handler.h" |
| 8 #include "chrome/browser/profile.h" | 8 #include "chrome/browser/profile.h" |
| 9 #include "chrome/browser/web_resource/web_resource_service.h" | 9 #include "chrome/browser/web_resource/web_resource_service.h" |
| 10 #include "chrome/common/pref_names.h" |
| 10 #include "chrome/common/web_resource/web_resource_unpacker.h" | 11 #include "chrome/common/web_resource/web_resource_unpacker.h" |
| 11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/url_constants.h" |
| 13 #include "googleurl/src/gurl.h" |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 const int kNumTipsToShow = 2; | 17 const int kNumTipsToShow = 2; |
| 16 | 18 |
| 17 // TODO(mrc): l10n | 19 // TODO(mrc): l10n |
| 18 // This title should only appear the very first time Chrome is run with | 20 // This title should only appear the very first time Chrome is run with |
| 19 // web resources enabled; otherwise the cache should be populated. | 21 // web resources enabled; otherwise the cache should be populated. |
| 20 static const wchar_t* kTipsTitleAtStartup = | 22 static const wchar_t* kTipsTitleAtStartup = |
| 21 L"Tips and recommendations to help you discover interesting websites."; | 23 L"Tips and recommendations to help you discover interesting websites."; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 DictionaryValue* tip_dict = new DictionaryValue(); | 55 DictionaryValue* tip_dict = new DictionaryValue(); |
| 54 tip_dict->SetString(WebResourceService::kWebResourceTitle, title); | 56 tip_dict->SetString(WebResourceService::kWebResourceTitle, title); |
| 55 list_value.Append(tip_dict); | 57 list_value.Append(tip_dict); |
| 56 } else { | 58 } else { |
| 57 int tip_counter = 0; | 59 int tip_counter = 0; |
| 58 while (tips_cache_->GetDictionary(IntToWString(tip_counter++), &wr_dict)) { | 60 while (tips_cache_->GetDictionary(IntToWString(tip_counter++), &wr_dict)) { |
| 59 DictionaryValue* tip_dict = new DictionaryValue(); | 61 DictionaryValue* tip_dict = new DictionaryValue(); |
| 60 if (wr_dict && | 62 if (wr_dict && |
| 61 wr_dict->GetSize() > 0 && | 63 wr_dict->GetSize() > 0 && |
| 62 wr_dict->GetString(WebResourceService::kWebResourceTitle, &title) && | 64 wr_dict->GetString(WebResourceService::kWebResourceTitle, &title) && |
| 63 wr_dict->GetString(WebResourceService::kWebResourceURL, &url)) { | 65 wr_dict->GetString(WebResourceService::kWebResourceURL, &url) && |
| 66 IsValidURL(url)) { |
| 64 tip_dict->SetString(WebResourceService::kWebResourceTitle, title); | 67 tip_dict->SetString(WebResourceService::kWebResourceTitle, title); |
| 65 tip_dict->SetString(WebResourceService::kWebResourceURL, url); | 68 tip_dict->SetString(WebResourceService::kWebResourceURL, url); |
| 66 list_value.Append(tip_dict); | 69 list_value.Append(tip_dict); |
| 67 } | 70 } |
| 68 } | 71 } |
| 69 } | 72 } |
| 70 | 73 |
| 71 // Send list of web resource items back out to the DOM. | 74 // Send list of web resource items back out to the DOM. |
| 72 dom_ui_->CallJavascriptFunction(L"tips", list_value); | 75 dom_ui_->CallJavascriptFunction(L"tips", list_value); |
| 73 } | 76 } |
| 74 | 77 |
| 75 // static | 78 // static |
| 76 void TipsHandler::RegisterUserPrefs(PrefService* prefs) { | 79 void TipsHandler::RegisterUserPrefs(PrefService* prefs) { |
| 77 prefs->RegisterDictionaryPref(prefs::kNTPTipsCache); | 80 prefs->RegisterDictionaryPref(prefs::kNTPTipsCache); |
| 78 prefs->RegisterStringPref(prefs::kNTPTipsServer, | 81 prefs->RegisterStringPref(prefs::kNTPTipsServer, |
| 79 WebResourceService::kDefaultResourceServer); | 82 WebResourceService::kDefaultResourceServer); |
| 80 } | 83 } |
| 81 | 84 |
| 85 bool TipsHandler::IsValidURL(const std::wstring& url_string) { |
| 86 GURL url(WideToUTF8(url_string)); |
| 87 return !url.is_empty() && (url.SchemeIs(chrome::kHttpScheme) || |
| 88 url.SchemeIs(chrome::kHttpsScheme)); |
| 89 } |
| 82 | 90 |
| OLD | NEW |