blob: 161fb6c5fa099c26b461637eff5b810025e80658 [file] [log] [blame]
[email protected]6a3d3e62009-06-25 23:32:481// 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]98ce1ee2009-09-30 22:00:505#include <string>
6
[email protected]2fa20c672009-12-02 22:42:577#include "app/l10n_util.h"
[email protected]2041cf342010-02-19 03:15:598#include "base/callback.h"
[email protected]9fd9092f2010-03-08 23:28:419#include "base/utf_string_conversions.h"
[email protected]6a3d3e62009-06-25 23:32:4810#include "base/values.h"
[email protected]4f2b1c42009-09-10 02:04:3811#include "chrome/browser/browser_process.h"
[email protected]6a3d3e62009-06-25 23:32:4812#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]6a3d3e62009-06-25 23:32:4815#include "chrome/common/pref_names.h"
[email protected]909459812009-06-26 20:55:0116#include "chrome/common/web_resource/web_resource_unpacker.h"
17#include "chrome/common/url_constants.h"
18#include "googleurl/src/gurl.h"
[email protected]2fa20c672009-12-02 22:42:5719#include "grit/generated_resources.h"
[email protected]6a3d3e62009-06-25 23:32:4820
[email protected]2bc2de62009-06-29 23:37:4221DOMMessageHandler* TipsHandler::Attach(DOMUI* dom_ui) {
[email protected]cf4e8bf2009-07-01 16:55:1522 dom_ui_ = dom_ui;
[email protected]6a3d3e62009-06-25 23:32:4823 tips_cache_ = dom_ui_->GetProfile()->GetPrefs()->
[email protected]61a523f2009-08-26 00:32:1124 GetMutableDictionary(prefs::kNTPTipsCache);
[email protected]cf4e8bf2009-07-01 16:55:1525 return DOMMessageHandler::Attach(dom_ui);
[email protected]2bc2de62009-06-29 23:37:4226}
27
28void TipsHandler::RegisterMessages() {
29 dom_ui_->RegisterMessageCallback("getTips",
30 NewCallback(this, &TipsHandler::HandleGetTips));
[email protected]6a3d3e62009-06-25 23:32:4831}
32
33void TipsHandler::HandleGetTips(const Value* content) {
[email protected]2fa20c672009-12-02 22:42:5734 // List containing the tips to be displayed.
[email protected]6a3d3e62009-06-25 23:32:4835 ListValue list_value;
36
37 // Holds the web resource data found in the preferences cache.
[email protected]61a523f2009-08-26 00:32:1138 ListValue* wr_list;
[email protected]6a3d3e62009-06-25 23:32:4839
[email protected]61a523f2009-08-26 00:32:1140 // These values hold the data for each web resource item.
41 int current_tip_index;
42 std::string current_tip;
[email protected]6a3d3e62009-06-25 23:32:4843
[email protected]6401e9a2009-10-07 00:00:1244 // If tips are not correct for our language, do not send. Wait for update.
[email protected]4f2b1c42009-09-10 02:04:3845 // 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]9986ee162009-11-19 16:25:0450 std::wstring locale =
51 ASCIIToWide(g_browser_process->GetApplicationLocale());
52 if (!EndsWith(server, locale, false)) {
[email protected]4f2b1c42009-09-10 02:04:3853 dom_ui_->CallJavascriptFunction(L"tips", list_value);
54 return;
55 }
56 }
57
[email protected]4dad9ad82009-11-25 20:47:5258 if (tips_cache_ != NULL && !tips_cache_->empty()) {
[email protected]61a523f2009-08-26 00:32:1159 if (tips_cache_->GetInteger(
60 WebResourceService::kCurrentTipPrefName, &current_tip_index) &&
61 tips_cache_->GetList(
[email protected]33d3c052009-09-18 21:07:1962 WebResourceService::kTipCachePrefName, &wr_list) &&
63 wr_list && wr_list->GetSize() > 0) {
[email protected]2fa20c672009-12-02 22:42:5764 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]61a523f2009-08-26 00:32:1167 current_tip_index = 0;
[email protected]8fe6e1c2010-05-27 08:21:5468 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]563007b2009-12-03 20:28:3674 SendTip(WideToUTF8(l10n_util::GetString(
[email protected]2fa20c672009-12-02 22:42:5775 IDS_NEW_TAB_MAKE_THIS_HOMEPAGE)), L"set_homepage_tip",
76 current_tip_index);
77 return;
78 }
79 }
[email protected]61a523f2009-08-26 00:32:1180 if (wr_list->GetString(current_tip_index, &current_tip)) {
[email protected]2fa20c672009-12-02 22:42:5781 SendTip(current_tip, L"tip_html_text", current_tip_index + 1);
[email protected]6a3d3e62009-06-25 23:32:4882 }
83 }
84 }
[email protected]2fa20c672009-12-02 22:42:5785}
[email protected]6a3d3e62009-06-25 23:32:4886
[email protected]2fa20c672009-12-02 22:42:5787void 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]6a3d3e62009-06-25 23:32:4896 // Send list of web resource items back out to the DOM.
97 dom_ui_->CallJavascriptFunction(L"tips", list_value);
98}
99
100// static
101void TipsHandler::RegisterUserPrefs(PrefService* prefs) {
102 prefs->RegisterDictionaryPref(prefs::kNTPTipsCache);
103 prefs->RegisterStringPref(prefs::kNTPTipsServer,
104 WebResourceService::kDefaultResourceServer);
105}
106
[email protected]909459812009-06-26 20:55:01107bool 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}