diff options
author | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-18 23:02:54 +0000 |
---|---|---|
committer | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-18 23:02:54 +0000 |
commit | ea0c98cfa2849f95bf40238c9f476c9cebb22244 (patch) | |
tree | ec81fe0c664cb6c0adddbd5d392313e8fbb93ec5 /chrome/browser/dom_ui | |
parent | 1f70f0ca51d1c61d3a775507b2b69dcdf60e77df (diff) | |
download | chromium_src-ea0c98cfa2849f95bf40238c9f476c9cebb22244.zip chromium_src-ea0c98cfa2849f95bf40238c9f476c9cebb22244.tar.gz chromium_src-ea0c98cfa2849f95bf40238c9f476c9cebb22244.tar.bz2 |
First draft of web resource service; fetches data from a JSON feed
and stores it in user prefs, where it can be used by the new tab page.
BUG = http://crbug.com/13363
Review URL: http://codereview.chromium.org/125052
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18766 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/dom_ui')
-rw-r--r-- | chrome/browser/dom_ui/new_tab_ui.cc | 14 | ||||
-rw-r--r-- | chrome/browser/dom_ui/new_tab_ui.h | 3 | ||||
-rw-r--r-- | chrome/browser/dom_ui/web_resource_handler.cc | 95 | ||||
-rw-r--r-- | chrome/browser/dom_ui/web_resource_handler.h | 56 |
4 files changed, 168 insertions, 0 deletions
diff --git a/chrome/browser/dom_ui/new_tab_ui.cc b/chrome/browser/dom_ui/new_tab_ui.cc index 52b0297..e1dd96e 100644 --- a/chrome/browser/dom_ui/new_tab_ui.cc +++ b/chrome/browser/dom_ui/new_tab_ui.cc @@ -22,6 +22,7 @@ #include "chrome/browser/dom_ui/dom_ui_theme_source.h" #include "chrome/browser/dom_ui/downloads_dom_handler.h" #include "chrome/browser/dom_ui/history_ui.h" +#include "chrome/browser/dom_ui/web_resource_handler.h" #include "chrome/browser/history/page_usage_data.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/profile.h" @@ -291,6 +292,8 @@ void NewTabHTMLSource::StartDataRequest(const std::string& path, l10n_util::GetString(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_MULTIPLE)); localized_strings.SetString(L"attributionintro", l10n_util::GetString(IDS_NEW_TAB_ATTRIBUTION_INTRO)); + localized_strings.SetString(L"resourcecache", + l10n_util::GetString(IDS_NEW_TAB_WEB_RESOURCE_CACHE)); SetFontAndTextDirection(&localized_strings); @@ -1380,6 +1383,9 @@ NewTabUI::NewTabUI(TabContents* contents) downloads_handler->Init(); } + if (EnableWebResources()) + AddMessageHandler(new WebResourceHandler(this)); + AddMessageHandler(new TemplateURLHandler(this)); AddMessageHandler(new MostVisitedHandler(this)); AddMessageHandler(new RecentlyBookmarkedHandler(this)); @@ -1435,6 +1441,8 @@ void NewTabUI::Observe(NotificationType type, // static void NewTabUI::RegisterUserPrefs(PrefService* prefs) { MostVisitedHandler::RegisterUserPrefs(prefs); + if (NewTabUI::EnableWebResources()) + WebResourceHandler::RegisterUserPrefs(prefs); } // static @@ -1442,3 +1450,9 @@ bool NewTabUI::EnableNewNewTabPage() { const CommandLine* command_line = CommandLine::ForCurrentProcess(); return command_line->HasSwitch(switches::kNewNewTabPage); } + +bool NewTabUI::EnableWebResources() { + const CommandLine* command_line = CommandLine::ForCurrentProcess(); + return command_line->HasSwitch(switches::kWebResources); +} + diff --git a/chrome/browser/dom_ui/new_tab_ui.h b/chrome/browser/dom_ui/new_tab_ui.h index 6c3a2f1..851dbfa 100644 --- a/chrome/browser/dom_ui/new_tab_ui.h +++ b/chrome/browser/dom_ui/new_tab_ui.h @@ -28,6 +28,9 @@ class NewTabUI : public DOMUI, // Whether we should use the prototype new tab page. static bool EnableNewNewTabPage(); + // Whether we should enable the web resources backend service + static bool EnableWebResources(); + private: void Observe(NotificationType type, const NotificationSource& source, diff --git a/chrome/browser/dom_ui/web_resource_handler.cc b/chrome/browser/dom_ui/web_resource_handler.cc new file mode 100644 index 0000000..7895da4 --- /dev/null +++ b/chrome/browser/dom_ui/web_resource_handler.cc @@ -0,0 +1,95 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/values.h" +#include "chrome/browser/dom_ui/web_resource_handler.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/web_resource/web_resource_service.h" +#include "chrome/common/web_resource/web_resource_unpacker.h" +#include "chrome/common/pref_names.h" + +namespace { + + const int kNumWebResourcesToShow = 2; + + // TODO(mrc): l10n + // This title should only appear the very first time Chrome is run with + // web resources enabled; otherwise the cache should be populated. + static const wchar_t* kWebResourceTitleAtStartup = + L"New: Suggestion Box!"; + + // This snipp should only appear the very first time Chrome is run with + // web resources enabled; otherwise the cache should be populated. + static const wchar_t* kWebResourceSnippetAtStartup = + L"Tips and recommendations to help you discover interesting websites."; +} + +WebResourceHandler::WebResourceHandler(DOMUI* dom_ui) + : DOMMessageHandler(dom_ui), + dom_ui_(dom_ui) { + dom_ui->RegisterMessageCallback("getNextCachedWebResource", + NewCallback(this, &WebResourceHandler::HandleGetCachedWebResource)); + + web_resource_cache_ = dom_ui_->GetProfile()->GetPrefs()-> + GetDictionary(prefs::kNTPWebResourceCache); +} + +void WebResourceHandler::HandleGetCachedWebResource(const Value* content) { + // Eventually we will feed more than one web resource datum at a time + // to the NTP; for now, this is a list containing one item: the tip + // to be displayed. + ListValue list_value; + + // Holds the web resource data found in the preferences cache. + DictionaryValue* wr_dict; + + // Dictionary which will be sent back in a Javascript call. + DictionaryValue* tip_dict = new DictionaryValue(); + + // These values hold the data for each web resource item. As the web + // resource server solidifies, these may change. + std::wstring title; + std::wstring thumb; + std::wstring source; + std::wstring snipp; + std::wstring url; + + // This should only be true on the very first Chrome run; otherwise, + // the cache should be populated. + if (web_resource_cache_ == NULL || web_resource_cache_->GetSize() < 1) { + title = kWebResourceTitleAtStartup; + snipp = kWebResourceSnippetAtStartup; + } else { + // Right now, hard-coded to simply get the first item (marked "0") in the + // resource data stored in the cache. Fail silently if data is missing. + // TODO(mrc): If data is missing, iterate through cache. + web_resource_cache_->GetDictionary(L"0", &wr_dict); + if (wr_dict && + wr_dict->GetSize() > 0 && + wr_dict->GetString(WebResourceService::kWebResourceTitle, &title) && + wr_dict->GetString(WebResourceService::kWebResourceThumb, &thumb) && + wr_dict->GetString(WebResourceService::kWebResourceSource, &source) && + wr_dict->GetString(WebResourceService::kWebResourceSnippet, &snipp) && + wr_dict->GetString(WebResourceService::kWebResourceURL, &url)) { + tip_dict->SetString(WebResourceService::kWebResourceTitle, title); + tip_dict->SetString(WebResourceService::kWebResourceThumb, thumb); + tip_dict->SetString(WebResourceService::kWebResourceSource, source); + tip_dict->SetString(WebResourceService::kWebResourceSnippet, snipp); + tip_dict->SetString(WebResourceService::kWebResourceURL, url); + } + } + + list_value.Append(tip_dict); + + // Send list of snippets back out to the DOM. + dom_ui_->CallJavascriptFunction(L"nextWebResource", list_value); +} + +// static +void WebResourceHandler::RegisterUserPrefs(PrefService* prefs) { + prefs->RegisterDictionaryPref(prefs::kNTPWebResourceCache); + prefs->RegisterStringPref(prefs::kNTPWebResourceServer, + WebResourceService::kDefaultResourceServer); +} + diff --git a/chrome/browser/dom_ui/web_resource_handler.h b/chrome/browser/dom_ui/web_resource_handler.h new file mode 100644 index 0000000..01157f9 --- /dev/null +++ b/chrome/browser/dom_ui/web_resource_handler.h @@ -0,0 +1,56 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This class pulls data from a web resource (such as a JSON feed) which +// has been stored in the user's preferences file. Used mainly +// by the suggestions and tips area of the new tab page. + +// Current sketch of tip cache format, hardcoded for poptart data in +// basic text form: + +// "web_resource_cache": { +// "0": { +// "index": should become time field (or not) +// "snippet": the text of the item +// "source": text describing source (i.e., "New York Post") +// "thumbnail": URL of thumbnail on popgadget server +// "title": text giving title of item +// "url": link to item's page +// }, +// [up to number of items in kMaxWebResourceCacheSize] + +#ifndef CHROME_BROWSER_DOM_UI_WEB_RESOURCE_HANDLER_H_ +#define CHROME_BROWSER_DOM_UI_WEB_RESOURCE_HANDLER_H_ + +#include "chrome/browser/dom_ui/dom_ui.h" + +class DictionaryValue; +class DOMUI; +class PrefService; +class Value; + +class WebResourceHandler : public DOMMessageHandler { + public: + explicit WebResourceHandler(DOMUI* dom_ui); + + WebResourceHandler(); + + // Callback which pulls web resource data from the preferences. + void HandleGetCachedWebResource(const Value* content); + + // Register web resource cache with pref service. + static void RegisterUserPrefs(PrefService* prefs); + + private: + // So we can push data out to the page that has called this handler. + DOMUI* dom_ui_; + + // Filled with data from cache in preferences. + const DictionaryValue* web_resource_cache_; + + DISALLOW_COPY_AND_ASSIGN(WebResourceHandler); +}; + +#endif // CHROME_BROWSER_DOM_UI_WEB_RESOURCE_HANDLER_H_ + |