diff options
Diffstat (limited to 'chrome/browser/dom_ui')
21 files changed, 933 insertions, 1114 deletions
diff --git a/chrome/browser/dom_ui/chrome_url_data_manager.cc b/chrome/browser/dom_ui/chrome_url_data_manager.cc index b84cf75..113580a 100644 --- a/chrome/browser/dom_ui/chrome_url_data_manager.cc +++ b/chrome/browser/dom_ui/chrome_url_data_manager.cc @@ -104,7 +104,8 @@ void RegisterURLRequestChromeJob() { // time being we need to serve the same content from chrome-ui://inspector // for the Console Debugger and in-process Web Inspector. chrome_url_data_manager.AddFileSource("inspector", inspector_dir); - chrome_url_data_manager.AddFileSource(chrome::kDevToolsHost, inspector_dir); + chrome_url_data_manager.AddFileSource(chrome::kChromeUIDevToolsHost, + inspector_dir); } URLRequest::RegisterProtocolFactory(kChromeURLScheme, @@ -120,7 +121,7 @@ void UnregisterURLRequestChromeJob() { std::wstring inspector_dir; if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { chrome_url_data_manager.RemoveFileSource("inspector"); - chrome_url_data_manager.RemoveFileSource(chrome::kDevToolsHost); + chrome_url_data_manager.RemoveFileSource(chrome::kChromeUIDevToolsHost); } } diff --git a/chrome/browser/dom_ui/debugger_ui.cc b/chrome/browser/dom_ui/debugger_ui.cc index 3af0faa..c8b4e74 100644 --- a/chrome/browser/dom_ui/debugger_ui.cc +++ b/chrome/browser/dom_ui/debugger_ui.cc @@ -19,6 +19,8 @@ #include "grit/debugger_resources.h" +namespace { + class DebuggerHTMLSource : public ChromeURLDataManager::DataSource { public: // Creates our datasource and sets our user message to a specific message @@ -80,7 +82,7 @@ class DebuggerHTMLSource : public ChromeURLDataManager::DataSource { } private: - DISALLOW_EVIL_CONSTRUCTORS(DebuggerHTMLSource); + DISALLOW_COPY_AND_ASSIGN(DebuggerHTMLSource); }; @@ -117,12 +119,9 @@ class DebuggerHandler : public DOMMessageHandler { DISALLOW_COPY_AND_ASSIGN(DebuggerHandler); }; +} // namespace -DebuggerUI::DebuggerUI(DOMUIContents* contents) - : DOMUI(contents) { -} - -void DebuggerUI::Init() { +DebuggerUI::DebuggerUI(WebContents* contents) : DOMUI(contents) { AddMessageHandler(new DebuggerHandler(this)); DebuggerHTMLSource* html_source = new DebuggerHTMLSource(); @@ -131,18 +130,3 @@ void DebuggerUI::Init() { &ChromeURLDataManager::AddDataSource, html_source)); } - -// static -bool DebuggerUI::IsDebuggerUrl(const GURL& url) { - return url.SchemeIs(chrome::kChromeUIScheme) && - url.host() == chrome::kInspectorHost; -} - -// static -GURL DebuggerUI::GetBaseURL() { - // DebuggerUI is accessible from chrome-ui://inspector. - std::string url = chrome::kChromeUIScheme; - url += chrome::kStandardSchemeSeparator; - url += chrome::kInspectorHost; - return GURL(url); -} diff --git a/chrome/browser/dom_ui/debugger_ui.h b/chrome/browser/dom_ui/debugger_ui.h index 3b12ec9..8cefc66 100644 --- a/chrome/browser/dom_ui/debugger_ui.h +++ b/chrome/browser/dom_ui/debugger_ui.h @@ -11,15 +11,7 @@ class DebuggerUI : public DOMUI { public: - DebuggerUI(DOMUIContents* contents); - - // DOMUI Implementation - virtual void Init(); - - // Return the URL for the front page of this UI. - static GURL GetBaseURL(); - - static bool IsDebuggerUrl(const GURL& url); + DebuggerUI(WebContents* contents); private: DISALLOW_COPY_AND_ASSIGN(DebuggerUI); diff --git a/chrome/browser/dom_ui/devtools_ui.cc b/chrome/browser/dom_ui/devtools_ui.cc index 61bfc06..daf2de6 100644 --- a/chrome/browser/dom_ui/devtools_ui.cc +++ b/chrome/browser/dom_ui/devtools_ui.cc @@ -6,13 +6,9 @@ #include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/common/render_messages.h" +#include "chrome/common/url_constants.h" -// DevToolsUI is accessible from chrome-ui://devtools. -static const char kDevToolsHost[] = "devtools"; - -// static -GURL DevToolsUI::GetBaseURL() { - return GURL(DOMUIContents::GetScheme() + "://" + kDevToolsHost); +DevToolsUI::DevToolsUI(WebContents* contents) : DOMUI(contents) { } void DevToolsUI::RenderViewCreated(RenderViewHost* render_view_host) { diff --git a/chrome/browser/dom_ui/devtools_ui.h b/chrome/browser/dom_ui/devtools_ui.h index 4abfaba..afc9fe5 100644 --- a/chrome/browser/dom_ui/devtools_ui.h +++ b/chrome/browser/dom_ui/devtools_ui.h @@ -9,13 +9,9 @@ class DevToolsUI : public DOMUI { public: - explicit DevToolsUI(DOMUIContents* contents) : DOMUI(contents) {} + explicit DevToolsUI(WebContents* contents); - // Return the URL for the front page of this UI. - static GURL GetBaseURL(); - - // DOMUI Implementation - virtual void Init() {} + // DOMUI overrides. virtual void RenderViewCreated(RenderViewHost* render_view_host); private: diff --git a/chrome/browser/dom_ui/dom_ui.cc b/chrome/browser/dom_ui/dom_ui.cc index 98a88e1..ea9e1a5 100644 --- a/chrome/browser/dom_ui/dom_ui.cc +++ b/chrome/browser/dom_ui/dom_ui.cc @@ -8,12 +8,17 @@ #include "base/json_writer.h" #include "base/string_util.h" #include "base/values.h" +#include "chrome/browser/tab_contents/web_contents.h" #include "chrome/common/l10n_util.h" - -/////////////////////////////////////////////////////////////////////////////// -// DOMMessageHandler - -DOMUI::DOMUI(DOMUIContents* contents) : contents_(contents) { +#include "chrome/common/stl_util-inl.h" + +DOMUI::DOMUI(WebContents* contents) + : hide_favicon_(false), + force_bookmark_bar_visible_(false), + focus_location_bar_by_default_(false), + should_hide_url_(false), + link_transition_type_(PageTransition::LINK), + web_contents_(contents) { } DOMUI::~DOMUI() { @@ -79,10 +84,8 @@ void DOMUI::RegisterMessageCallback(const std::string &message, message_callbacks_.insert(std::make_pair(message, callback)); } -void DOMUI::RequestOpenURL(const GURL& url, - const GURL& /* referer */, - WindowOpenDisposition disposition) { - get_contents()->OpenURL(url, GURL(), disposition, PageTransition::LINK); +Profile* DOMUI::GetProfile() { + return web_contents()->profile(); } // DOMUI, protected: ---------------------------------------------------------- @@ -94,9 +97,8 @@ void DOMUI::AddMessageHandler(DOMMessageHandler* handler) { // DOMUI, private: ------------------------------------------------------------ void DOMUI::ExecuteJavascript(const std::wstring& javascript) { - DCHECK(contents_); - contents_->render_view_host()->ExecuteJavascriptInWebFrame(std::wstring(), - javascript); + web_contents()->render_view_host()->ExecuteJavascriptInWebFrame( + std::wstring(), javascript); } /////////////////////////////////////////////////////////////////////////////// diff --git a/chrome/browser/dom_ui/dom_ui.h b/chrome/browser/dom_ui/dom_ui.h index 1d59bfc..4be503c 100644 --- a/chrome/browser/dom_ui/dom_ui.h +++ b/chrome/browser/dom_ui/dom_ui.h @@ -2,25 +2,32 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_DOM_UI_H__ -#define CHROME_BROWSER_DOM_UI_H__ +#ifndef CHROME_BROWSER_DOM_UI_DOM_UI_H_ +#define CHROME_BROWSER_DOM_UI_DOM_UI_H_ +#include <map> +#include <string> +#include <vector> + +#include "base/string16.h" #include "base/task.h" -#include "chrome/browser/dom_ui/dom_ui_contents.h" +#include "chrome/common/page_transition_types.h" +#include "webkit/glue/window_open_disposition.h" class DictionaryValue; class DOMMessageHandler; +class GURL; +class Profile; class RenderViewHost; class Value; +class WebContents; // A DOMUI sets up the datasources and message handlers for a given HTML-based -// UI. It is contained by a DOMUIContents. +// UI. It is contained by a DOMUIManager. class DOMUI { public: - DOMUI(DOMUIContents* contents); - + explicit DOMUI(WebContents* contents); virtual ~DOMUI(); - virtual void Init() = 0; virtual void RenderViewCreated(RenderViewHost* render_view_host) {} @@ -30,8 +37,45 @@ class DOMUI { // Used by DOMMessageHandlers. typedef Callback1<const Value*>::Type MessageCallback; - void RegisterMessageCallback (const std::string& message, - MessageCallback* callback); + void RegisterMessageCallback(const std::string& message, + MessageCallback* callback); + + // Returns true if the favicon should be hidden for the current tab. + bool hide_favicon() const { + return hide_favicon_; + } + + // Returns true if the bookmark bar should be forced to being visible, + // overriding the user's preference. + bool force_bookmark_bar_visible() const { + return force_bookmark_bar_visible_; + } + + // Returns true if the location bar should be focused by default rather than + // the page contents. Some pages will want to use this to encourage the user + // to type in the URL bar. + bool focus_location_bar_by_default() const { + return focus_location_bar_by_default_; + } + + // Returns true if the page's URL should be hidden. Some DOM UI pages + // like the new tab page will want to hide it. + bool should_hide_url() const { + return should_hide_url_; + } + + // Gets a custom tab title provided by the DOM UI. If there is no title + // override, the string will be empty which should trigger the default title + // behavior for the tab. + const string16& overridden_title() const { + return overridden_title_; + } + + // Returns the transition type that should be used for link clicks on this + // DOM UI. This will default to LINK but may be overridden. + const PageTransition::Type link_transition_type() const { + return link_transition_type_; + } // Call a Javascript function by sending its name and arguments down to // the renderer. This is asynchronous; there's no way to get the result @@ -45,31 +89,29 @@ class DOMUI { const Value& arg1, const Value& arg2); - // Overriddable control over the contents. - // Favicon should be displayed normally. - virtual bool ShouldDisplayFavIcon() { return true; } - // No special bookmark bar behavior - virtual bool IsBookmarkBarAlwaysVisible() { return false; } - // Defaults to focusing the page. - virtual void SetInitialFocus() { contents_->Focus(); } - // Whether we want to display the page's URL. - virtual bool ShouldDisplayURL() { return true; } - // Hide the referrer. - virtual void RequestOpenURL(const GURL& url, const GURL&, - WindowOpenDisposition disposition); - - DOMUIContents* get_contents() { return contents_; } - Profile* get_profile() { return contents_->profile(); } + WebContents* web_contents() { return web_contents_; } + + Profile* GetProfile(); protected: void AddMessageHandler(DOMMessageHandler* handler); - DOMUIContents* contents_; + // Options that may be overridden by individual DOM UI implementations. The + // bool options default to false. See the public getters for more information. + bool hide_favicon_; + bool force_bookmark_bar_visible_; + bool focus_location_bar_by_default_; + bool should_hide_url_; + string16 overridden_title_; // Defaults to empty string. + PageTransition::Type link_transition_type_; // Defaults to LINK. private: // Execute a string of raw Javascript on the page. void ExecuteJavascript(const std::wstring& javascript); + // Non-owning pointer to the WebContents this DOMUI is associated with. + WebContents* web_contents_; + // The DOMMessageHandlers we own. std::vector<DOMMessageHandler*> handlers_; @@ -107,4 +149,4 @@ class DOMMessageHandler { DISALLOW_COPY_AND_ASSIGN(DOMMessageHandler); }; -#endif // CHROME_BROWSER_DOM_UI_H__ +#endif // CHROME_BROWSER_DOM_UI_DOM_UI_H_ diff --git a/chrome/browser/dom_ui/dom_ui_contents.cc b/chrome/browser/dom_ui/dom_ui_contents.cc deleted file mode 100644 index 90e04dd..0000000 --- a/chrome/browser/dom_ui/dom_ui_contents.cc +++ /dev/null @@ -1,300 +0,0 @@ -// 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 "chrome/browser/dom_ui/dom_ui_contents.h" - -#include "chrome/browser/dom_ui/debugger_ui.h" -#include "chrome/browser/dom_ui/devtools_ui.h" -#include "chrome/browser/dom_ui/dom_ui.h" -#include "chrome/browser/dom_ui/downloads_ui.h" -#include "chrome/browser/dom_ui/history_ui.h" -#include "chrome/browser/dom_ui/new_tab_ui.h" -#include "chrome/browser/extensions/extensions_ui.h" -#include "chrome/browser/renderer_host/render_view_host.h" -#include "chrome/browser/tab_contents/navigation_entry.h" -#include "chrome/browser/tab_contents/web_contents_view.h" -#include "chrome/common/l10n_util.h" -#include "chrome/common/resource_bundle.h" -#include "chrome/common/url_constants.h" - -#include "grit/generated_resources.h" - -// The path used in internal URLs to thumbnail data. -static const char kThumbnailPath[] = "thumb"; - -// The path used in internal URLs to favicon data. -static const char kFavIconPath[] = "favicon"; - -/////////////////////////////////////////////////////////////////////////////// -// FavIconSource - -FavIconSource::FavIconSource(Profile* profile) - : DataSource(kFavIconPath, MessageLoop::current()), profile_(profile) {} - -void FavIconSource::StartDataRequest(const std::string& path, int request_id) { - HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); - if (hs) { - HistoryService::Handle handle; - if (path.size() > 8 && path.substr(0, 8) == "iconurl/") { - handle = hs->GetFavIcon( - GURL(path.substr(8)), - &cancelable_consumer_, - NewCallback(this, &FavIconSource::OnFavIconDataAvailable)); - } else { - handle = hs->GetFavIconForURL( - GURL(path), - &cancelable_consumer_, - NewCallback(this, &FavIconSource::OnFavIconDataAvailable)); - } - // Attach the ChromeURLDataManager request ID to the history request. - cancelable_consumer_.SetClientData(hs, handle, request_id); - } else { - SendResponse(request_id, NULL); - } -} - -void FavIconSource::OnFavIconDataAvailable( - HistoryService::Handle request_handle, - bool know_favicon, - scoped_refptr<RefCountedBytes> data, - bool expired, - GURL icon_url) { - HistoryService* hs = - profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); - int request_id = cancelable_consumer_.GetClientData(hs, request_handle); - - if (know_favicon && data.get() && !data->data.empty()) { - // Forward the data along to the networking system. - SendResponse(request_id, data); - } else { - if (!default_favicon_.get()) { - default_favicon_ = new RefCountedBytes; - ResourceBundle::GetSharedInstance().LoadImageResourceBytes( - IDR_DEFAULT_FAVICON, &default_favicon_->data); - } - - SendResponse(request_id, default_favicon_); - } -} - -/////////////////////////////////////////////////////////////////////////////// -// ThumbnailSource - -ThumbnailSource::ThumbnailSource(Profile* profile) - : DataSource(kThumbnailPath, MessageLoop::current()), profile_(profile) {} - -void ThumbnailSource::StartDataRequest(const std::string& path, - int request_id) { - HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); - if (hs) { - HistoryService::Handle handle = hs->GetPageThumbnail( - GURL(path), - &cancelable_consumer_, - NewCallback(this, &ThumbnailSource::OnThumbnailDataAvailable)); - // Attach the ChromeURLDataManager request ID to the history request. - cancelable_consumer_.SetClientData(hs, handle, request_id); - } else { - // Tell the caller that no thumbnail is available. - SendResponse(request_id, NULL); - } -} - -void ThumbnailSource::OnThumbnailDataAvailable( - HistoryService::Handle request_handle, - scoped_refptr<RefCountedBytes> data) { - HistoryService* hs = - profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); - int request_id = cancelable_consumer_.GetClientData(hs, request_handle); - // Forward the data along to the networking system. - if (data.get() && !data->data.empty()) { - SendResponse(request_id, data); - } else { - if (!default_thumbnail_.get()) { - default_thumbnail_ = new RefCountedBytes; - ResourceBundle::GetSharedInstance().LoadImageResourceBytes( - IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); - } - - SendResponse(request_id, default_thumbnail_); - } -} - -/////////////////////////////////////////////////////////////////////////////// -// DOMUIContents - -// This is the top-level URL handler for chrome-ui: URLs, and exposed in -// our header file. The individual DOMUIs provide a chrome-ui:// HTML source -// at the same host/path. -bool DOMUIContentsCanHandleURL(GURL* url, - TabContentsType* result_type) { - // chrome-internal is a scheme we used to use for the new tab page. - if (!url->SchemeIs(chrome::kChromeUIScheme) && - !url->SchemeIs(chrome::kChromeInternalScheme)) - return false; - - *result_type = TAB_CONTENTS_DOM_UI; - return true; -} - -DOMUIContents::DOMUIContents(Profile* profile, - SiteInstance* instance, - RenderViewHostFactory* render_view_factory) - : WebContents(profile, - instance, - render_view_factory, - MSG_ROUTING_NONE, - NULL), - current_ui_(NULL), - current_url_(GURL()) { - set_type(TAB_CONTENTS_DOM_UI); -} - -DOMUIContents::~DOMUIContents() { - if (current_ui_) - delete current_ui_; -} - -bool DOMUIContents::CreateRenderViewForRenderManager( - RenderViewHost* render_view_host) { - // Be sure to enable DOM UI bindings on the RenderViewHost before - // CreateRenderView is called. Since a cross-site transition may be - // involved, this may or may not be the same RenderViewHost that we had when - // we were created. - render_view_host->AllowDOMUIBindings(); - return WebContents::CreateRenderViewForRenderManager(render_view_host); -} - -WebPreferences DOMUIContents::GetWebkitPrefs() { - // Get the users preferences then force image loading to always be on. - WebPreferences web_prefs = WebContents::GetWebkitPrefs(); - web_prefs.loads_images_automatically = true; - web_prefs.javascript_enabled = true; - - return web_prefs; -} - -void DOMUIContents::RenderViewCreated(RenderViewHost* render_view_host) { - if (current_ui_) - current_ui_->RenderViewCreated(render_view_host); -} - -bool DOMUIContents::ShouldDisplayFavIcon() { - if (InitCurrentUI(false)) - return current_ui_->ShouldDisplayFavIcon(); - return true; -} - -bool DOMUIContents::IsBookmarkBarAlwaysVisible() { - if (InitCurrentUI(false)) - return current_ui_->IsBookmarkBarAlwaysVisible(); - return false; -} - -void DOMUIContents::SetInitialFocus() { - if (InitCurrentUI(false)) - current_ui_->SetInitialFocus(); - else if (current_ui_) - current_ui_->get_contents()->view()->SetInitialFocus(); -} - -const string16& DOMUIContents::GetTitle() const { - // Workaround for new tab page - we may be asked for a title before - // the content is ready, and we don't even want to display a 'loading...' - // message, so we force it here. - if (controller()->GetActiveEntry() && - controller()->GetActiveEntry()->url().host() == - NewTabUI::GetBaseURL().host()) { - static string16* newtab_title = new string16(WideToUTF16Hack( - l10n_util::GetString(IDS_NEW_TAB_TITLE))); - return *newtab_title; - } - return WebContents::GetTitle(); -} - -bool DOMUIContents::ShouldDisplayURL() { - if (InitCurrentUI(false)) - return current_ui_->ShouldDisplayURL(); - return TabContents::ShouldDisplayURL(); -} - -void DOMUIContents::RequestOpenURL(const GURL& url, const GURL& referrer, - WindowOpenDisposition disposition) { - if (InitCurrentUI(false)) - current_ui_->RequestOpenURL(url, referrer, disposition); - else - WebContents::RequestOpenURL(url, referrer, disposition); -} - -bool DOMUIContents::NavigateToPendingEntry(bool reload) { - InitCurrentUI(reload); - // Let WebContents do whatever it's meant to do. - return WebContents::NavigateToPendingEntry(reload); -} - -void DOMUIContents::ProcessDOMUIMessage(const std::string& message, - const std::string& content) { - DCHECK(current_ui_); - current_ui_->ProcessDOMUIMessage(message, content); -} - -bool DOMUIContents::InitCurrentUI(bool reload) { - if (!controller()->GetActiveEntry()) - return false; - - GURL url = controller()->GetActiveEntry()->url(); - - if (url.is_empty() || !url.is_valid()) - return false; - - if (reload || url.host() != current_url_.host()) { - // Shut down our existing DOMUI. - delete current_ui_; - current_ui_ = NULL; - - // Set up a new DOMUI. - current_ui_ = GetDOMUIForURL(url); - if (current_ui_) { - current_ui_->Init(); - current_url_ = url; - } - } - - if (current_ui_) - return true; - - return false; -} - -// static -const std::string DOMUIContents::GetScheme() { - return chrome::kChromeUIScheme; -} - -DOMUI* DOMUIContents::GetDOMUIForURL(const GURL &url) { - if (url.host() == NewTabUI::GetBaseURL().host() || - url.SchemeIs(chrome::kChromeInternalScheme)) { - return new NewTabUI(this); - } - if (url.host() == HistoryUI::GetBaseURL().host()) { - return new HistoryUI(this); - } - if (url.host() == DownloadsUI::GetBaseURL().host()) { - return new DownloadsUI(this); - } -#if defined(OS_WIN) -// TODO(port): include this once these are converted to HTML - if (url.host() == ExtensionsUI::GetBaseURL().host()) { - return new ExtensionsUI(this); - } - if (url.host() == DebuggerUI::GetBaseURL().host()) { - return new DebuggerUI(this); - } - if (url.host() == DevToolsUI::GetBaseURL().host()) { - return new DevToolsUI(this); - } -#else - NOTIMPLEMENTED(); -#endif - return NULL; -} diff --git a/chrome/browser/dom_ui/dom_ui_contents.h b/chrome/browser/dom_ui/dom_ui_contents.h deleted file mode 100644 index f1796f2..0000000 --- a/chrome/browser/dom_ui/dom_ui_contents.h +++ /dev/null @@ -1,148 +0,0 @@ -// 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. - -// Contains code for managing local HTML UI at chrome-ui:// URLs. - -#ifndef CHROME_BROWSER_DOM_UI_CONTENTS_H_ -#define CHROME_BROWSER_DOM_UI_CONTENTS_H_ - -#include "chrome/browser/dom_ui/chrome_url_data_manager.h" -#include "chrome/browser/tab_contents/web_contents.h" -#include "webkit/glue/webpreferences.h" - -class DOMUI; -class RenderViewHost; - -// FavIconSource is the gateway between network-level chrome: -// requests for favicons and the history backend that serves these. -class FavIconSource : public ChromeURLDataManager::DataSource { - public: - explicit FavIconSource(Profile* profile); - - // Called when the network layer has requested a resource underneath - // the path we registered. - virtual void StartDataRequest(const std::string& path, int request_id); - - virtual std::string GetMimeType(const std::string&) const { - // We need to explicitly return a mime type, otherwise if the user tries to - // drag the image they get no extension. - return "image/png"; - } - - // Called when favicon data is available from the history backend. - void OnFavIconDataAvailable( - HistoryService::Handle request_handle, - bool know_favicon, - scoped_refptr<RefCountedBytes> data, - bool expired, - GURL url); - - private: - Profile* const profile_; - CancelableRequestConsumerT<int, 0> cancelable_consumer_; - - // Raw PNG representation of the favicon to show when the favicon - // database doesn't have a favicon for a webpage. - scoped_refptr<RefCountedBytes> default_favicon_; - - DISALLOW_COPY_AND_ASSIGN(FavIconSource); -}; - -// ThumbnailSource is the gateway between network-level chrome: -// requests for thumbnails and the history backend that serves these. -class ThumbnailSource : public ChromeURLDataManager::DataSource { - public: - explicit ThumbnailSource(Profile* profile); - - // Called when the network layer has requested a resource underneath - // the path we registered. - virtual void StartDataRequest(const std::string& path, int request_id); - - virtual std::string GetMimeType(const std::string&) const { - // We need to explicitly return a mime type, otherwise if the user tries to - // drag the image they get no extension. - return "image/png"; - } - - // Called when thumbnail data is available from the history backend. - void OnThumbnailDataAvailable( - HistoryService::Handle request_handle, - scoped_refptr<RefCountedBytes> data); - - private: - Profile* const profile_; - CancelableRequestConsumerT<int, 0> cancelable_consumer_; - - // Raw PNG representation of the thumbnail to show when the thumbnail - // database doesn't have a thumbnail for a webpage. - scoped_refptr<RefCountedBytes> default_thumbnail_; - - DISALLOW_COPY_AND_ASSIGN(ThumbnailSource); -}; - -// Exposed for use by BrowserURLHandler. -bool DOMUIContentsCanHandleURL(GURL* url, TabContentsType* result_type); - -class DOMUIContents : public WebContents { - public: - DOMUIContents(Profile* profile, - SiteInstance* instance, - RenderViewHostFactory* render_view_factory); - ~DOMUIContents(); - - // - // WebContents overrides - // - virtual void ProcessDOMUIMessage(const std::string& message, - const std::string& content); - virtual bool CreateRenderViewForRenderManager( - RenderViewHost* render_view_host); - // Override this method so we can ensure that javascript and image loading - // are always on even for DOMUIHost tabs. - virtual WebPreferences GetWebkitPrefs(); - // We don't want a favicon on the new tab page. - virtual bool ShouldDisplayFavIcon(); - // The bookmark bar is always visible on the new tab. - virtual bool IsBookmarkBarAlwaysVisible(); - // Whether we want to display the page's URL. - virtual bool ShouldDisplayURL(); - // Get the title for this page. - virtual const string16& GetTitle() const; - // We may wish to control what happens when a URL is opened. - virtual void RequestOpenURL(const GURL& url, const GURL& referrer, - WindowOpenDisposition disposition); - virtual DOMUIContents* AsDOMUIContents() { return this; } - - virtual void RenderViewCreated(RenderViewHost* render_view_host); - - // - // TabContents overrides - // - virtual void UpdateHistoryForNavigation(const GURL& url, - const ViewHostMsg_FrameNavigate_Params& params) { } - virtual bool NavigateToPendingEntry(bool reload); - - // Gives the DOMUI an opportunity to chose where the focus should go. - void SetInitialFocus(); - - // Return the scheme used. We currently use chrome-ui: - static const std::string GetScheme(); - - private: - // Return a DOM UI for the provided URL. - DOMUI* GetDOMUIForURL(const GURL& url); - - // Initialize our current UI. - bool InitCurrentUI(bool reload); - - // The DOMUI we own and show. - DOMUI* current_ui_; - - // URL that our current_ui_ is serving. - GURL current_url_; - - DISALLOW_COPY_AND_ASSIGN(DOMUIContents); -}; - -#endif // CHROME_BROWSER_DOM_UI_CONTENTS_H_ diff --git a/chrome/browser/dom_ui/dom_ui_factory.cc b/chrome/browser/dom_ui/dom_ui_factory.cc new file mode 100644 index 0000000..228aa8c --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_factory.cc @@ -0,0 +1,102 @@ +// Copyright (c) 2009 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 "chrome/browser/dom_ui/dom_ui_factory.h"
+
+#include "chrome/browser/dom_ui/downloads_ui.h"
+#include "chrome/browser/dom_ui/debugger_ui.h"
+#include "chrome/browser/dom_ui/devtools_ui.h"
+#include "chrome/browser/dom_ui/history_ui.h"
+#include "chrome/browser/dom_ui/new_tab_ui.h"
+#include "chrome/browser/extensions/extensions_ui.h"
+#include "chrome/common/url_constants.h"
+#include "googleurl/src/gurl.h"
+
+// Backend for both querying for and creating new DOMUI objects. If you're just
+// querying whether there's a DOM UI for the given URL, pass NULL for both the
+// web contents and the new_ui. The return value will indiacate whether a DOM UI
+// exists for the given URL.
+//
+// If you want to create a DOM UI, pass non-NULL pointers for both web_contents
+// and new_ui. The *new_ui pointer will be filled with the created UI if it
+// succeeds (indicated by a return value of true). The caller owns the *new_ui
+// pointer.
+static bool CreateDOMUI(const GURL& url, WebContents* web_contents,
+ DOMUI** new_ui) {
+ // This will get called a lot to check all URLs, so do a quick check of the
+ // scheme to filter out most URLs.
+ if (!url.SchemeIs(chrome::kChromeInternalScheme) &&
+ !url.SchemeIs(chrome::kChromeUIScheme))
+ return false;
+
+ // Special case the new tab page. In older versions of Chrome, the new tab
+ // page was hosted at chrome-internal:<blah>. This might be in people's saved
+ // sessions or bookmarks, so we say any URL with that scheme triggers the new
+ // tab page.
+ if (url.host() == chrome::kChromeUINewTabHost ||
+ url.SchemeIs(chrome::kChromeInternalScheme)) {
+ if (new_ui)
+ *new_ui = new NewTabUI(web_contents);
+ return true;
+ }
+
+ // We must compare hosts only since some of the DOM UIs append extra stuff
+ // after the host name.
+ if (url.host() == chrome::kChromeUIHistoryHost) {
+ if (new_ui)
+ *new_ui = new HistoryUI(web_contents);
+ return true;
+ }
+
+ if (url.host() == chrome::kChromeUIDownloadsHost) {
+ if (new_ui)
+ *new_ui = new DownloadsUI(web_contents);
+ return true;
+ }
+
+#if defined(OS_WIN)
+
+ // TODO(port): it should be possible to include these now.
+ if (url.host() == chrome::kChromeUIExtensionsHost) {
+ if (new_ui)
+ *new_ui = new ExtensionsUI(web_contents);
+ return true;
+ }
+
+ if (url.host() == chrome::kChromeUIInspectorHost) {
+ if (new_ui)
+ *new_ui = new DebuggerUI(web_contents);
+ return true;
+ }
+
+ if (url.host() == chrome::kChromeUIDevToolsHost) {
+ if (new_ui)
+ *new_ui = new DevToolsUI(web_contents);
+ return true;
+ }
+
+#endif
+
+ return false;
+}
+
+// static
+bool DOMUIFactory::HasDOMUIScheme(const GURL& url) {
+ return url.SchemeIs(chrome::kChromeInternalScheme) ||
+ url.SchemeIs(chrome::kChromeUIScheme);
+}
+
+// static
+bool DOMUIFactory::UseDOMUIForURL(const GURL& url) {
+ return CreateDOMUI(url, NULL, NULL);
+}
+
+// static
+DOMUI* DOMUIFactory::CreateDOMUIForURL(WebContents* web_contents,
+ const GURL& url) {
+ DOMUI* dom_ui;
+ if (!CreateDOMUI(url, web_contents, &dom_ui))
+ return NULL;
+ return dom_ui;
+}
diff --git a/chrome/browser/dom_ui/dom_ui_factory.h b/chrome/browser/dom_ui/dom_ui_factory.h new file mode 100644 index 0000000..1641a8a --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_factory.h @@ -0,0 +1,33 @@ +// Copyright (c) 2009 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.
+
+#ifndef CHROME_BROWSER_DOM_UI_DOM_UI_FACTORY_H_
+#define CHROME_BROWSER_DOM_UI_DOM_UI_FACTORY_H_
+
+class DOMUI;
+class GURL;
+class WebContents;
+
+class DOMUIFactory {
+ public:
+ // Returns true if the given URL's scheme would trigger the DOM UI system.
+ // This is a less precise test than UseDONUIForURL, which tells you whether
+ // that specific URL matches a known one. This one is faster and can be used
+ // to determine security policy.
+ static bool HasDOMUIScheme(const GURL& url);
+
+ // Returns true if the given URL will use the DOM UI system.
+ static bool UseDOMUIForURL(const GURL& url);
+
+ // Allocates a new DOMUI object for the given URL, and returns it. If the URL
+ // is not a DOM UI URL, then it will return NULL. When non-NULL, ownership of
+ // the returned pointer is passed to the caller.
+ static DOMUI* CreateDOMUIForURL(WebContents* web_contents, const GURL& url);
+
+ private:
+ // Class is for scoping only.
+ DOMUIFactory() {};
+};
+
+#endif // CHROME_BROWSER_DOM_UI_DOM_UI_FACTORY_H_
diff --git a/chrome/browser/dom_ui/dom_ui_favicon_source.cc b/chrome/browser/dom_ui/dom_ui_favicon_source.cc new file mode 100644 index 0000000..985ded4 --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_favicon_source.cc @@ -0,0 +1,63 @@ +// Copyright (c) 2009 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 "chrome/browser/dom_ui/dom_ui_favicon_source.h"
+
+#include "chrome/browser/profile.h"
+#include "chrome/common/resource_bundle.h"
+#include "chrome/common/url_constants.h"
+#include "grit/theme_resources.h" +
+DOMUIFavIconSource::DOMUIFavIconSource(Profile* profile)
+ : DataSource(chrome::kChromeUIFavIconPath, MessageLoop::current()),
+ profile_(profile) {
+}
+
+void DOMUIFavIconSource::StartDataRequest(const std::string& path,
+ int request_id) {
+ HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
+ if (hs) {
+ HistoryService::Handle handle;
+ if (path.size() > 8 && path.substr(0, 8) == "iconurl/") {
+ handle = hs->GetFavIcon(
+ GURL(path.substr(8)),
+ &cancelable_consumer_,
+ NewCallback(this, &DOMUIFavIconSource::OnFavIconDataAvailable));
+ } else {
+ handle = hs->GetFavIconForURL(
+ GURL(path),
+ &cancelable_consumer_,
+ NewCallback(this, &DOMUIFavIconSource::OnFavIconDataAvailable));
+ }
+ // Attach the ChromeURLDataManager request ID to the history request.
+ cancelable_consumer_.SetClientData(hs, handle, request_id);
+ } else {
+ SendResponse(request_id, NULL);
+ }
+}
+
+void DOMUIFavIconSource::OnFavIconDataAvailable(
+ HistoryService::Handle request_handle,
+ bool know_favicon,
+ scoped_refptr<RefCountedBytes> data,
+ bool expired,
+ GURL icon_url) {
+ HistoryService* hs =
+ profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
+ int request_id = cancelable_consumer_.GetClientData(hs, request_handle);
+
+ if (know_favicon && data.get() && !data->data.empty()) {
+ // Forward the data along to the networking system.
+ SendResponse(request_id, data);
+ } else {
+ if (!default_favicon_.get()) {
+ default_favicon_ = new RefCountedBytes;
+ ResourceBundle::GetSharedInstance().LoadImageResourceBytes(
+ IDR_DEFAULT_FAVICON, &default_favicon_->data);
+ }
+
+ SendResponse(request_id, default_favicon_);
+ }
+}
+
diff --git a/chrome/browser/dom_ui/dom_ui_favicon_source.h b/chrome/browser/dom_ui/dom_ui_favicon_source.h new file mode 100644 index 0000000..e946b1e --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_favicon_source.h @@ -0,0 +1,52 @@ +// Copyright (c) 2009 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.
+
+#ifndef CHROME_BROWSER_DOM_UI_DOM_UI_FAVICON_SOURCE_H_
+#define CHROME_BROWSER_DOM_UI_DOM_UI_FAVICON_SOURCE_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/scoped_ptr.h"
+#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
+#include "chrome/browser/history/history.h"
+
+class GURL;
+class Profile;
+
+// FavIconSource is the gateway between network-level chrome:
+// requests for favicons and the history backend that serves these.
+class DOMUIFavIconSource : public ChromeURLDataManager::DataSource {
+ public:
+ explicit DOMUIFavIconSource(Profile* profile);
+
+ // Called when the network layer has requested a resource underneath
+ // the path we registered.
+ virtual void StartDataRequest(const std::string& path, int request_id);
+
+ virtual std::string GetMimeType(const std::string&) const {
+ // We need to explicitly return a mime type, otherwise if the user tries to
+ // drag the image they get no extension.
+ return "image/png";
+ }
+
+ // Called when favicon data is available from the history backend.
+ void OnFavIconDataAvailable(HistoryService::Handle request_handle,
+ bool know_favicon,
+ scoped_refptr<RefCountedBytes> data,
+ bool expired,
+ GURL url);
+
+ private:
+ Profile* profile_;
+ CancelableRequestConsumerT<int, 0> cancelable_consumer_;
+
+ // Raw PNG representation of the favicon to show when the favicon
+ // database doesn't have a favicon for a webpage.
+ scoped_refptr<RefCountedBytes> default_favicon_;
+
+ DISALLOW_COPY_AND_ASSIGN(DOMUIFavIconSource);
+};
+
+#endif // CHROME_BROWSER_DOM_UI_DOM_UI_FAVICON_SOURCE_H_
diff --git a/chrome/browser/dom_ui/dom_ui_thumbnail_source.cc b/chrome/browser/dom_ui/dom_ui_thumbnail_source.cc new file mode 100644 index 0000000..5f1724e3 --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_thumbnail_source.cc @@ -0,0 +1,53 @@ +// Copyright (c) 2009 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 "chrome/browser/dom_ui/dom_ui_thumbnail_source.h"
+
+#include "chrome/browser/profile.h"
+#include "chrome/common/resource_bundle.h"
+#include "chrome/common/url_constants.h"
+#include "googleurl/src/gurl.h"
+#include "grit/theme_resources.h"
+
+DOMUIThumbnailSource::DOMUIThumbnailSource(Profile* profile)
+ : DataSource(chrome::kChromeUIThumbnailPath, MessageLoop::current()),
+ profile_(profile) {
+}
+
+void DOMUIThumbnailSource::StartDataRequest(const std::string& path,
+ int request_id) {
+ HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
+ if (hs) {
+ HistoryService::Handle handle = hs->GetPageThumbnail(
+ GURL(path),
+ &cancelable_consumer_,
+ NewCallback(this, &DOMUIThumbnailSource::OnThumbnailDataAvailable));
+ // Attach the ChromeURLDataManager request ID to the history request.
+ cancelable_consumer_.SetClientData(hs, handle, request_id);
+ } else {
+ // Tell the caller that no thumbnail is available.
+ SendResponse(request_id, NULL);
+ }
+}
+
+void DOMUIThumbnailSource::OnThumbnailDataAvailable(
+ HistoryService::Handle request_handle,
+ scoped_refptr<RefCountedBytes> data) {
+ HistoryService* hs =
+ profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
+ int request_id = cancelable_consumer_.GetClientData(hs, request_handle);
+ // Forward the data along to the networking system.
+ if (data.get() && !data->data.empty()) {
+ SendResponse(request_id, data);
+ } else {
+ if (!default_thumbnail_.get()) {
+ default_thumbnail_ = new RefCountedBytes;
+ ResourceBundle::GetSharedInstance().LoadImageResourceBytes(
+ IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data);
+ }
+
+ SendResponse(request_id, default_thumbnail_);
+ }
+}
+
diff --git a/chrome/browser/dom_ui/dom_ui_thumbnail_source.h b/chrome/browser/dom_ui/dom_ui_thumbnail_source.h new file mode 100644 index 0000000..c4f7063 --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_thumbnail_source.h @@ -0,0 +1,48 @@ +// Copyright (c) 2009 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.
+
+#ifndef CHROME_BROWSER_DOM_UI_DOM_UI_THUMBNAIL_SOURCE_H_
+#define CHROME_BROWSER_DOM_UI_DOM_UI_THUMBNAIL_SOURCE_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/scoped_ptr.h"
+#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
+#include "chrome/browser/history/history.h"
+
+class Profile;
+
+// ThumbnailSource is the gateway between network-level chrome:
+// requests for thumbnails and the history backend that serves these.
+class DOMUIThumbnailSource : public ChromeURLDataManager::DataSource {
+ public:
+ explicit DOMUIThumbnailSource(Profile* profile);
+
+ // Called when the network layer has requested a resource underneath
+ // the path we registered.
+ virtual void StartDataRequest(const std::string& path, int request_id);
+
+ virtual std::string GetMimeType(const std::string&) const {
+ // We need to explicitly return a mime type, otherwise if the user tries to
+ // drag the image they get no extension.
+ return "image/png";
+ }
+
+ // Called when thumbnail data is available from the history backend.
+ void OnThumbnailDataAvailable(HistoryService::Handle request_handle,
+ scoped_refptr<RefCountedBytes> data);
+
+ private:
+ Profile* profile_;
+ CancelableRequestConsumerT<int, 0> cancelable_consumer_;
+
+ // Raw PNG representation of the thumbnail to show when the thumbnail
+ // database doesn't have a thumbnail for a webpage.
+ scoped_refptr<RefCountedBytes> default_thumbnail_;
+
+ DISALLOW_COPY_AND_ASSIGN(DOMUIThumbnailSource);
+};
+
+#endif // CHROME_BROWSER_DOM_UI_DOM_UI_THUMBNAIL_SOURCE_H_
diff --git a/chrome/browser/dom_ui/downloads_ui.cc b/chrome/browser/dom_ui/downloads_ui.cc index f8d9515..5ee67bd 100644 --- a/chrome/browser/dom_ui/downloads_ui.cc +++ b/chrome/browser/dom_ui/downloads_ui.cc @@ -9,7 +9,9 @@ #include "base/thread.h" #include "base/time_format.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/dom_ui/chrome_url_data_manager.h" #include "chrome/browser/dom_ui/fileicon_source.h" +#include "chrome/browser/download/download_manager.h" #if defined(OS_WIN) // TODO(port): re-enable when download_util is ported #include "chrome/browser/download/download_util.h" @@ -19,11 +21,11 @@ #include "chrome/common/jstemplate_builder.h" #include "chrome/common/l10n_util.h" #include "chrome/common/time_format.h" +#include "chrome/common/url_constants.h" #include "grit/browser_resources.h" #include "grit/generated_resources.h" -// DownloadsUI is accessible from chrome-ui://downloads. -static const char kDownloadsHost[] = "downloads"; +namespace { // Maximum number of downloads to show. TODO(glen): Remove this and instead // stuff the downloads down the pipe slowly. @@ -35,8 +37,23 @@ static const int kMaxDownloads = 150; // /////////////////////////////////////////////////////////////////////////////// +class DownloadsUIHTMLSource : public ChromeURLDataManager::DataSource { + public: + DownloadsUIHTMLSource(); + + // Called when the network layer has requested a resource underneath + // the path we registered. + virtual void StartDataRequest(const std::string& path, int request_id); + virtual std::string GetMimeType(const std::string&) const { + return "text/html"; + } + + private: + DISALLOW_COPY_AND_ASSIGN(DownloadsUIHTMLSource); +}; + DownloadsUIHTMLSource::DownloadsUIHTMLSource() - : DataSource(kDownloadsHost, MessageLoop::current()) { + : DataSource(chrome::kChromeUIDownloadsHost, MessageLoop::current()) { } void DownloadsUIHTMLSource::StartDataRequest(const std::string& path, @@ -100,6 +117,82 @@ void DownloadsUIHTMLSource::StartDataRequest(const std::string& path, // /////////////////////////////////////////////////////////////////////////////// +// The handler for Javascript messages related to the "downloads" view, +// also observes changes to the download manager. +class DownloadsDOMHandler : public DOMMessageHandler, + public DownloadManager::Observer, + public DownloadItem::Observer { + public: + explicit DownloadsDOMHandler(DOMUI* dom_ui, DownloadManager* dlm); + virtual ~DownloadsDOMHandler(); + + void Init(); + + // DownloadItem::Observer interface + virtual void OnDownloadUpdated(DownloadItem* download); + + // DownloadManager::Observer interface + virtual void ModelChanged(); + virtual void SetDownloads(std::vector<DownloadItem*>& downloads); + + // Callback for the "getDownloads" message. + void HandleGetDownloads(const Value* value); + + // Callback for the "openFile" message - opens the file in the shell. + void HandleOpenFile(const Value* value); + + // Callback for the "drag" message - initiates a file object drag. + void HandleDrag(const Value* value); + + // Callback for the "saveDangerous" message - specifies that the user + // wishes to save a dangerous file. + void HandleSaveDangerous(const Value* value); + + // Callback for the "discardDangerous" message - specifies that the user + // wishes to discard (remove) a dangerous file. + void HandleDiscardDangerous(const Value* value); + + // Callback for the "show" message - shows the file in explorer. + void HandleShow(const Value* value); + + // Callback for the "pause" message - pauses the file download. + void HandlePause(const Value* value); + + // Callback for the "cancel" message - cancels the download. + void HandleCancel(const Value* value); + + private: + // Send the current list of downloads to the page. + void SendCurrentDownloads(); + + // Creates a representation of a download in a format that the downloads + // HTML page can understand. + DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id); + + // Clear all download items and their observers. + void ClearDownloadItems(); + + // Return the download that corresponds to a given id. + DownloadItem* GetDownloadById(int id); + + // Return the download that is referred to in a given value. + DownloadItem* GetDownloadByValue(const Value* value); + + // Current search text. + std::wstring search_text_; + + // Our model + DownloadManager* download_manager_; + + // The current set of visible DownloadItems for this view received from the + // DownloadManager. DownloadManager owns the DownloadItems. The vector is + // kept in order, sorted by ascending start time. + typedef std::vector<DownloadItem*> OrderedDownloads; + OrderedDownloads download_items_; + + DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); +}; + // Sort DownloadItems into descending order by their start time. class DownloadItemSorter : public std::binary_function<DownloadItem*, DownloadItem*, @@ -120,20 +213,20 @@ DownloadsDOMHandler::DownloadsDOMHandler(DOMUI* dom_ui, DownloadManager* dlm) NewCallback(this, &DownloadsDOMHandler::HandleOpenFile)); dom_ui_->RegisterMessageCallback("drag", - NewCallback(this, &DownloadsDOMHandler::HandleDrag)); + NewCallback(this, &DownloadsDOMHandler::HandleDrag)); dom_ui_->RegisterMessageCallback("saveDangerous", - NewCallback(this, &DownloadsDOMHandler::HandleSaveDangerous)); + NewCallback(this, &DownloadsDOMHandler::HandleSaveDangerous)); dom_ui_->RegisterMessageCallback("discardDangerous", - NewCallback(this, &DownloadsDOMHandler::HandleDiscardDangerous)); + NewCallback(this, &DownloadsDOMHandler::HandleDiscardDangerous)); dom_ui_->RegisterMessageCallback("show", - NewCallback(this, &DownloadsDOMHandler::HandleShow)); + NewCallback(this, &DownloadsDOMHandler::HandleShow)); dom_ui_->RegisterMessageCallback("togglepause", - NewCallback(this, &DownloadsDOMHandler::HandlePause)); + NewCallback(this, &DownloadsDOMHandler::HandlePause)); dom_ui_->RegisterMessageCallback("resume", - NewCallback(this, &DownloadsDOMHandler::HandlePause)); + NewCallback(this, &DownloadsDOMHandler::HandlePause)); dom_ui_->RegisterMessageCallback("cancel", - NewCallback(this, &DownloadsDOMHandler::HandleCancel)); + NewCallback(this, &DownloadsDOMHandler::HandleCancel)); // Create our fileicon data source. g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, @@ -159,8 +252,8 @@ void DownloadsDOMHandler::OnDownloadUpdated(DownloadItem* download) { // errors between the UI and the download_items_ list (we may wish to use // something other than 'id'). OrderedDownloads::iterator it = find(download_items_.begin(), - download_items_.end(), - download); + download_items_.end(), + download); if (it == download_items_.end()) return; const int id = static_cast<int>(it - download_items_.begin()); @@ -346,17 +439,16 @@ DownloadItem* DownloadsDOMHandler::GetDownloadByValue(const Value* value) { return NULL; } +} // namespace + /////////////////////////////////////////////////////////////////////////////// // // DownloadsUI // /////////////////////////////////////////////////////////////////////////////// -DownloadsUI::DownloadsUI(DOMUIContents* contents) : DOMUI(contents) { -} - -void DownloadsUI::Init() { - DownloadManager* dlm = get_profile()->GetDownloadManager(); +DownloadsUI::DownloadsUI(WebContents* contents) : DOMUI(contents) { + DownloadManager* dlm = GetProfile()->GetDownloadManager(); DownloadsDOMHandler* handler = new DownloadsDOMHandler(this, dlm); AddMessageHandler(handler); @@ -370,11 +462,3 @@ void DownloadsUI::Init() { &ChromeURLDataManager::AddDataSource, html_source)); } - -// static -GURL DownloadsUI::GetBaseURL() { - std::string url = DOMUIContents::GetScheme(); - url += "://"; - url += kDownloadsHost; - return GURL(url); -} diff --git a/chrome/browser/dom_ui/downloads_ui.h b/chrome/browser/dom_ui/downloads_ui.h index baaca9a..ed435e1 100644 --- a/chrome/browser/dom_ui/downloads_ui.h +++ b/chrome/browser/dom_ui/downloads_ui.h @@ -5,116 +5,13 @@ #ifndef CHROME_BROWSER_DOM_UI_DOWNLOADS_UI_H_ #define CHROME_BROWSER_DOM_UI_DOWNLOADS_UI_H_ -#include "chrome/browser/dom_ui/chrome_url_data_manager.h" #include "chrome/browser/dom_ui/dom_ui.h" -#include "chrome/browser/dom_ui/dom_ui_contents.h" -#include "chrome/browser/download/download_manager.h" - -class GURL; - -class DownloadsUIHTMLSource : public ChromeURLDataManager::DataSource { - public: - DownloadsUIHTMLSource(); - - // Called when the network layer has requested a resource underneath - // the path we registered. - virtual void StartDataRequest(const std::string& path, int request_id); - virtual std::string GetMimeType(const std::string&) const { - return "text/html"; - } - - private: - DISALLOW_COPY_AND_ASSIGN(DownloadsUIHTMLSource); -}; - -// The handler for Javascript messages related to the "downloads" view, -// also observes changes to the download manager. -class DownloadsDOMHandler : public DOMMessageHandler, - public DownloadManager::Observer, - public DownloadItem::Observer { - public: - explicit DownloadsDOMHandler(DOMUI* dom_ui, DownloadManager* dlm); - virtual ~DownloadsDOMHandler(); - - void Init(); - - // DownloadItem::Observer interface - virtual void OnDownloadUpdated(DownloadItem* download); - - // DownloadManager::Observer interface - virtual void ModelChanged(); - virtual void SetDownloads(std::vector<DownloadItem*>& downloads); - - // Callback for the "getDownloads" message. - void HandleGetDownloads(const Value* value); - - // Callback for the "openFile" message - opens the file in the shell. - void HandleOpenFile(const Value* value); - - // Callback for the "drag" message - initiates a file object drag. - void HandleDrag(const Value* value); - - // Callback for the "saveDangerous" message - specifies that the user - // wishes to save a dangerous file. - void HandleSaveDangerous(const Value* value); - - // Callback for the "discardDangerous" message - specifies that the user - // wishes to discard (remove) a dangerous file. - void HandleDiscardDangerous(const Value* value); - - // Callback for the "show" message - shows the file in explorer. - void HandleShow(const Value* value); - - // Callback for the "pause" message - pauses the file download. - void HandlePause(const Value* value); - - // Callback for the "cancel" message - cancels the download. - void HandleCancel(const Value* value); - - private: - // Send the current list of downloads to the page. - void SendCurrentDownloads(); - - // Creates a representation of a download in a format that the downloads - // HTML page can understand. - DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id); - - // Clear all download items and their observers. - void ClearDownloadItems(); - - // Return the download that corresponds to a given id. - DownloadItem* GetDownloadById(int id); - - // Return the download that is referred to in a given value. - DownloadItem* GetDownloadByValue(const Value* value); - - // Current search text. - std::wstring search_text_; - - // Our model - DownloadManager* download_manager_; - - // The current set of visible DownloadItems for this view received from the - // DownloadManager. DownloadManager owns the DownloadItems. The vector is - // kept in order, sorted by ascending start time. - typedef std::vector<DownloadItem*> OrderedDownloads; - OrderedDownloads download_items_; - - DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); -}; class DownloadsUI : public DOMUI { public: - explicit DownloadsUI(DOMUIContents* contents); - - // Return the URL for the front page of this UI. - static GURL GetBaseURL(); - - // DOMUI Implementation - virtual void Init(); + explicit DownloadsUI(WebContents* contents); private: - DISALLOW_COPY_AND_ASSIGN(DownloadsUI); }; diff --git a/chrome/browser/dom_ui/history_ui.cc b/chrome/browser/dom_ui/history_ui.cc index 8bc2013..9309732 100644 --- a/chrome/browser/dom_ui/history_ui.cc +++ b/chrome/browser/dom_ui/history_ui.cc @@ -12,6 +12,7 @@ #include "base/time_format.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/dom_ui/dom_ui_favicon_source.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/history/history_types.h" #include "chrome/browser/profile.h" @@ -20,18 +21,13 @@ #include "chrome/common/notification_service.h" #include "chrome/common/resource_bundle.h" #include "chrome/common/time_format.h" +#include "chrome/common/url_constants.h" #include "net/base/escape.h" #include "grit/browser_resources.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" -using base::Time; -using base::TimeDelta; - -// HistoryUI is accessible from chrome-ui://history. -static const char kHistoryHost[] = "history"; - // Maximum number of search results to return in a given search. We should // eventually remove this. static const int kMaxSearchResults = 100; @@ -43,7 +39,7 @@ static const int kMaxSearchResults = 100; //////////////////////////////////////////////////////////////////////////////// HistoryUIHTMLSource::HistoryUIHTMLSource() - : DataSource(kHistoryHost, MessageLoop::current()) { + : DataSource(chrome::kChromeUIHistoryHost, MessageLoop::current()) { } void HistoryUIHTMLSource::StartDataRequest(const std::string& path, @@ -113,12 +109,12 @@ BrowsingHistoryHandler::BrowsingHistoryHandler(DOMUI* dom_ui) g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(&chrome_url_data_manager, &ChromeURLDataManager::AddDataSource, - new FavIconSource(dom_ui_->get_profile()))); + new DOMUIFavIconSource(dom_ui_->GetProfile()))); // Get notifications when history is cleared. NotificationService* service = NotificationService::current(); service->AddObserver(this, NotificationType::HISTORY_URLS_DELETED, - Source<Profile>(dom_ui_->get_profile())); + Source<Profile>(dom_ui_->GetProfile())); } BrowsingHistoryHandler::~BrowsingHistoryHandler() { @@ -126,7 +122,7 @@ BrowsingHistoryHandler::~BrowsingHistoryHandler() { NotificationService* service = NotificationService::current(); service->RemoveObserver(this, NotificationType::HISTORY_URLS_DELETED, - Source<Profile>(dom_ui_->get_profile())); + Source<Profile>(dom_ui_->GetProfile())); if (remover_.get()) remover_->RemoveObserver(this); @@ -142,10 +138,10 @@ void BrowsingHistoryHandler::HandleGetHistory(const Value* value) { // Set our query options. history::QueryOptions options; - options.begin_time = Time::Now().LocalMidnight(); - options.begin_time -= TimeDelta::FromDays(day); - options.end_time = Time::Now().LocalMidnight(); - options.end_time -= TimeDelta::FromDays(day - 1); + options.begin_time = base::Time::Now().LocalMidnight(); + options.begin_time -= base::TimeDelta::FromDays(day); + options.end_time = base::Time::Now().LocalMidnight(); + options.end_time -= base::TimeDelta::FromDays(day - 1); // As we're querying per-day, we can turn entry repeats off. options.most_recent_visit_only = true; @@ -154,7 +150,7 @@ void BrowsingHistoryHandler::HandleGetHistory(const Value* value) { search_text_ = std::wstring(); HistoryService* hs = - dom_ui_->get_profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); + dom_ui_->GetProfile()->GetHistoryService(Profile::EXPLICIT_ACCESS); hs->QueryHistory(search_text_, options, &cancelable_consumer_, @@ -181,7 +177,7 @@ void BrowsingHistoryHandler::HandleSearchHistory(const Value* value) { // Need to remember the query string for our results. search_text_ = query; HistoryService* hs = - dom_ui_->get_profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); + dom_ui_->GetProfile()->GetHistoryService(Profile::EXPLICIT_ACCESS); hs->QueryHistory(search_text_, options, &cancelable_consumer_, @@ -198,14 +194,15 @@ void BrowsingHistoryHandler::HandleDeleteDay(const Value* value) { cancelable_consumer_.CancelAllRequests(); // Get time. - Time time; - bool success = Time::FromString(ExtractStringValue(value).c_str(), &time); + base::Time time; + bool success = base::Time::FromString(ExtractStringValue(value).c_str(), + &time); DCHECK(success); - Time begin_time = time.LocalMidnight(); - Time end_time = begin_time + TimeDelta::FromDays(1); + base::Time begin_time = time.LocalMidnight(); + base::Time end_time = begin_time + base::TimeDelta::FromDays(1); - remover_.reset(new BrowsingDataRemover(dom_ui_->get_profile(), + remover_.reset(new BrowsingDataRemover(dom_ui_->GetProfile(), begin_time, end_time)); remover_->AddObserver(this); @@ -225,7 +222,7 @@ void BrowsingHistoryHandler::QueryComplete( history::QueryResults* results) { ListValue results_value; - Time midnight_today = Time::Now().LocalMidnight(); + base::Time midnight_today = base::Time::Now().LocalMidnight(); for (size_t i = 0; i < results->size(); ++i) { history::URLResult const &page = (*results)[i]; @@ -262,7 +259,7 @@ void BrowsingHistoryHandler::QueryComplete( page_value->SetString(L"snippet", page.snippet().text()); } page_value->SetBoolean(L"starred", - dom_ui_->get_profile()->GetBookmarkModel()->IsBookmarked(page.url())); + dom_ui_->GetProfile()->GetBookmarkModel()->IsBookmarked(page.url())); results_value.Append(page_value); } @@ -307,17 +304,17 @@ history::QueryOptions BrowsingHistoryHandler::CreateMonthQueryOptions( // Configure the begin point of the search to the start of the // current month. - Time::Exploded exploded; - Time::Now().LocalMidnight().LocalExplode(&exploded); + base::Time::Exploded exploded; + base::Time::Now().LocalMidnight().LocalExplode(&exploded); exploded.day_of_month = 1; if (month == 0) { - options.begin_time = Time::FromLocalExploded(exploded); + options.begin_time = base::Time::FromLocalExploded(exploded); // Set the end time of this first search to null (which will // show results from the future, should the user's clock have // been set incorrectly). - options.end_time = Time(); + options.end_time = base::Time(); } else { // Set the end-time of this search to the end of the month that is // |depth| months before the search end point. The end time is not @@ -328,7 +325,7 @@ history::QueryOptions BrowsingHistoryHandler::CreateMonthQueryOptions( exploded.month += 12; exploded.year--; } - options.end_time = Time::FromLocalExploded(exploded); + options.end_time = base::Time::FromLocalExploded(exploded); // Set the begin-time of the search to the start of the month // that is |depth| months prior to search_start_. @@ -338,7 +335,7 @@ history::QueryOptions BrowsingHistoryHandler::CreateMonthQueryOptions( exploded.month = 12; exploded.year--; } - options.begin_time = Time::FromLocalExploded(exploded); + options.begin_time = base::Time::FromLocalExploded(exploded); } return options; @@ -362,10 +359,7 @@ void BrowsingHistoryHandler::Observe(NotificationType type, // //////////////////////////////////////////////////////////////////////////////// -HistoryUI::HistoryUI(DOMUIContents* contents) : DOMUI(contents) { -} - -void HistoryUI::Init() { +HistoryUI::HistoryUI(WebContents* contents) : DOMUI(contents) { AddMessageHandler(new BrowsingHistoryHandler(this)); HistoryUIHTMLSource* html_source = new HistoryUIHTMLSource(); @@ -378,16 +372,7 @@ void HistoryUI::Init() { } // static -GURL HistoryUI::GetBaseURL() { - std::string url = DOMUIContents::GetScheme(); - url += "://"; - url += kHistoryHost; - return GURL(url); -} - -// static -const GURL HistoryUI::GetHistoryURLWithSearchText( - const std::wstring& text) { - return GURL(GetBaseURL().spec() + "#q=" + - EscapeQueryParamValue(WideToUTF8(text))); +const GURL HistoryUI::GetHistoryURLWithSearchText(const std::wstring& text) { + return GURL(std::string(chrome::kChromeUIHistoryURL) + "#q=" + + EscapeQueryParamValue(WideToUTF8(text))); } diff --git a/chrome/browser/dom_ui/history_ui.h b/chrome/browser/dom_ui/history_ui.h index 3bc550e..41bba82 100644 --- a/chrome/browser/dom_ui/history_ui.h +++ b/chrome/browser/dom_ui/history_ui.h @@ -2,15 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_DOM_UI_HISTORY_UI_H__ -#define CHROME_BROWSER_DOM_UI_HISTORY_UI_H__ +#ifndef CHROME_BROWSER_DOM_UI_HISTORY_UI_H_ +#define CHROME_BROWSER_DOM_UI_HISTORY_UI_H_ #include "base/scoped_ptr.h" #include "chrome/browser/browsing_data_remover.h" #include "chrome/browser/dom_ui/chrome_url_data_manager.h" #include "chrome/browser/dom_ui/dom_ui.h" -#include "chrome/browser/dom_ui/dom_ui_contents.h" #include "chrome/browser/cancelable_request.h" +#include "chrome/browser/history/history.h" class GURL; @@ -81,20 +81,13 @@ class BrowsingHistoryHandler : public DOMMessageHandler, class HistoryUI : public DOMUI { public: - explicit HistoryUI(DOMUIContents* contents); - - // Return the URL for the front page of this UI. - static GURL GetBaseURL(); + explicit HistoryUI(WebContents* contents); // Return the URL for a given search term. static const GURL GetHistoryURLWithSearchText(const std::wstring& text); - // DOMUI Implementation - virtual void Init(); - private: - DISALLOW_COPY_AND_ASSIGN(HistoryUI); }; -#endif // CHROME_BROWSER_DOM_UI_HISTORY_UI_H__ +#endif // CHROME_BROWSER_DOM_UI_HISTORY_UI_H_ diff --git a/chrome/browser/dom_ui/new_tab_ui.cc b/chrome/browser/dom_ui/new_tab_ui.cc index 21cffa7..d3183d5 100644 --- a/chrome/browser/dom_ui/new_tab_ui.cc +++ b/chrome/browser/dom_ui/new_tab_ui.cc @@ -8,18 +8,23 @@ #include "base/histogram.h" #include "base/string_piece.h" +#include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/bookmarks/bookmark_utils.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/dom_ui/dom_ui_contents.h" +#include "chrome/browser/dom_ui/dom_ui_favicon_source.h" +#include "chrome/browser/dom_ui/dom_ui_thumbnail_source.h" #include "chrome/browser/dom_ui/history_ui.h" #include "chrome/browser/history/page_usage_data.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/profile.h" #include "chrome/browser/renderer_host/render_view_host.h" +#include "chrome/browser/search_engines/template_url_model.h" #include "chrome/browser/sessions/session_types.h" #include "chrome/browser/tab_contents/navigation_entry.h" +#include "chrome/browser/tab_contents/web_contents.h" #include "chrome/browser/search_engines/template_url.h" +#include "chrome/browser/sessions/tab_restore_service.h" #include "chrome/browser/user_data_manager.h" #include "chrome/common/jstemplate_builder.h" #include "chrome/common/l10n_util.h" @@ -36,6 +41,8 @@ #include "grit/generated_resources.h" #include "grit/locale_settings.h" +namespace { + // The number of most visited pages we show. const int kMostVisitedPages = 9; @@ -43,19 +50,72 @@ const int kMostVisitedPages = 9; const int kMostVisitedScope = 90; // The number of recent bookmarks we show. -static const int kRecentBookmarks = 9; +const int kRecentBookmarks = 9; // The number of search URLs to show. -static const int kSearchURLs = 3; +const int kSearchURLs = 3; // Strings sent to the page via jstemplates used to set the direction of the // HTML document based on locale. -static const wchar_t kRTLHtmlTextDirection[] = L"rtl"; -static const wchar_t kDefaultHtmlTextDirection[] = L"ltr"; +const wchar_t kRTLHtmlTextDirection[] = L"rtl"; +const wchar_t kDefaultHtmlTextDirection[] = L"ltr"; -bool NewTabHTMLSource::first_view_ = true; +// Adds "url", "title", and "direction" keys on incoming dictionary, setting +// title as the url as a fallback on empty title. +void SetURLTitleAndDirection(DictionaryValue* dictionary, + const string16& title, + const GURL& gurl) { + std::wstring wstring_url = UTF8ToWide(gurl.spec()); + dictionary->SetString(L"url", wstring_url); -namespace { + std::wstring wstring_title = UTF16ToWide(title); + + bool using_url_as_the_title = false; + std::wstring title_to_set(wstring_title); + if (title_to_set.empty()) { + using_url_as_the_title = true; + title_to_set = wstring_url; + } + + // We set the "dir" attribute of the title, so that in RTL locales, a LTR + // title is rendered left-to-right and truncated from the right. For example, + // the title of http://msdn.microsoft.com/en-us/default.aspx is "MSDN: + // Microsoft developer network". In RTL locales, in the [New Tab] page, if + // the "dir" of this title is not specified, it takes Chrome UI's + // directionality. So the title will be truncated as "soft developer + // network". Setting the "dir" attribute as "ltr" renders the truncated title + // as "MSDN: Microsoft D...". As another example, the title of + // http://yahoo.com is "Yahoo!". In RTL locales, in the [New Tab] page, the + // title will be rendered as "!Yahoo" if its "dir" attribute is not set to + // "ltr". + // + // Since the title can contain BiDi text, we need to mark the text as either + // RTL or LTR, depending on the characters in the string. If we use the URL + // as the title, we mark the title as LTR since URLs are always treated as + // left to right strings. Simply setting the title's "dir" attribute works + // fine for rendering and truncating the title. However, it does not work for + // entire title within a tooltip when the mouse is over the title link.. For + // example, without LRE-PDF pair, the title "Yahoo!" will be rendered as + // "!Yahoo" within the tooltip when the mouse is over the title link. + std::wstring direction = kDefaultHtmlTextDirection; + if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) { + if (using_url_as_the_title) { + l10n_util::WrapStringWithLTRFormatting(&title_to_set); + } else { + if (l10n_util::StringContainsStrongRTLChars(wstring_title)) { + l10n_util::WrapStringWithRTLFormatting(&title_to_set); + direction = kRTLHtmlTextDirection; + } else { + l10n_util::WrapStringWithLTRFormatting(&title_to_set); + } + } + } + dictionary->SetString(L"title", title_to_set); + dictionary->SetString(L"direction", direction); +} + +//////////////////////////////////////////////////////////////////////////////// +// PaintTimer // To measure end-to-end performance of the new tab page, we observe paint // messages and wait for the page to stop repainting. @@ -115,67 +175,37 @@ class PaintTimer : public RenderWidgetHost::PaintObserver { DISALLOW_COPY_AND_ASSIGN(PaintTimer); }; -// Adds "url", "title", and "direction" keys on incoming dictionary, setting -// title as the url as a fallback on empty title. -void SetURLTitleAndDirection(DictionaryValue* dictionary, - const string16& title, - const GURL& gurl) { - std::wstring wstring_url = UTF8ToWide(gurl.spec()); - dictionary->SetString(L"url", wstring_url); +/////////////////////////////////////////////////////////////////////////////// +// NewTabHTMLSource - std::wstring wstring_title = UTF16ToWide(title); +class NewTabHTMLSource : public ChromeURLDataManager::DataSource { + public: + NewTabHTMLSource(); - bool using_url_as_the_title = false; - std::wstring title_to_set(wstring_title); - if (title_to_set.empty()) { - using_url_as_the_title = true; - title_to_set = wstring_url; - } + // Called when the network layer has requested a resource underneath + // the path we registered. + virtual void StartDataRequest(const std::string& path, int request_id); - // We set the "dir" attribute of the title, so that in RTL locales, a LTR - // title is rendered left-to-right and truncated from the right. For example, - // the title of http://msdn.microsoft.com/en-us/default.aspx is "MSDN: - // Microsoft developer network". In RTL locales, in the [New Tab] page, if - // the "dir" of this title is not specified, it takes Chrome UI's - // directionality. So the title will be truncated as "soft developer - // network". Setting the "dir" attribute as "ltr" renders the truncated title - // as "MSDN: Microsoft D...". As another example, the title of - // http://yahoo.com is "Yahoo!". In RTL locales, in the [New Tab] page, the - // title will be rendered as "!Yahoo" if its "dir" attribute is not set to - // "ltr". - // - // Since the title can contain BiDi text, we need to mark the text as either - // RTL or LTR, depending on the characters in the string. If we use the URL - // as the title, we mark the title as LTR since URLs are always treated as - // left to right strings. Simply setting the title's "dir" attribute works - // fine for rendering and truncating the title. However, it does not work for - // entire title within a tooltip when the mouse is over the title link.. For - // example, without LRE-PDF pair, the title "Yahoo!" will be rendered as - // "!Yahoo" within the tooltip when the mouse is over the title link. - std::wstring direction = kDefaultHtmlTextDirection; - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) { - if (using_url_as_the_title) { - l10n_util::WrapStringWithLTRFormatting(&title_to_set); - } else { - if (l10n_util::StringContainsStrongRTLChars(wstring_title)) { - l10n_util::WrapStringWithRTLFormatting(&title_to_set); - direction = kRTLHtmlTextDirection; - } else { - l10n_util::WrapStringWithLTRFormatting(&title_to_set); - } - } + virtual std::string GetMimeType(const std::string&) const { + return "text/html"; } - dictionary->SetString(L"title", title_to_set); - dictionary->SetString(L"direction", direction); -} -} // end anonymous namespace + // Setters and getters for first_view. + static void set_first_view(bool first_view) { first_view_ = first_view; } + static bool first_view() { return first_view_; } -/////////////////////////////////////////////////////////////////////////////// -// NewTabHTMLSource + private: + // Whether this is the is the first viewing of the new tab page and + // we think it is the user's startup page. + static bool first_view_; + + DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource); +}; + +bool NewTabHTMLSource::first_view_ = true; NewTabHTMLSource::NewTabHTMLSource() - : DataSource(chrome::kNewTabHost, MessageLoop::current()) { + : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()) { } void NewTabHTMLSource::StartDataRequest(const std::string& path, @@ -253,8 +283,26 @@ void NewTabHTMLSource::StartDataRequest(const std::string& path, /////////////////////////////////////////////////////////////////////////////// // IncognitoTabHTMLSource +class IncognitoTabHTMLSource : public ChromeURLDataManager::DataSource { + public: + // Creates our datasource and sets our user message to a specific message + // from our string bundle. + IncognitoTabHTMLSource(); + + // Called when the network layer has requested a resource underneath + // the path we registered. + virtual void StartDataRequest(const std::string& path, int request_id); + + virtual std::string GetMimeType(const std::string&) const { + return "text/html"; + } + + private: + DISALLOW_COPY_AND_ASSIGN(IncognitoTabHTMLSource); +}; + IncognitoTabHTMLSource::IncognitoTabHTMLSource() - : DataSource(chrome::kNewTabHost, MessageLoop::current()) { + : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()) { } void IncognitoTabHTMLSource::StartDataRequest(const std::string& path, @@ -287,6 +335,43 @@ void IncognitoTabHTMLSource::StartDataRequest(const std::string& path, /////////////////////////////////////////////////////////////////////////////// // MostVisitedHandler +// The handler for Javascript messages related to the "most visited" view. +class MostVisitedHandler : public DOMMessageHandler, + public NotificationObserver { + public: + explicit MostVisitedHandler(DOMUI* dom_ui); + virtual ~MostVisitedHandler(); + + // Callback for the "getMostVisited" message. + void HandleGetMostVisited(const Value* value); + + // NotificationObserver implementation. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + + const std::vector<GURL>& most_visited_urls() const { + return most_visited_urls_; + } + + private: + // Callback from the history system when the most visited list is available. + void OnSegmentUsageAvailable(CancelableRequestProvider::Handle handle, + std::vector<PageUsageData*>* data); + + DOMUI* dom_ui_; + + // Our consumer for the history service. + CancelableRequestConsumerTSimple<PageUsageData*> cancelable_consumer_; + + // The most visited URLs, in priority order. + // Only used for matching up clicks on the page to which most visited entry + // was clicked on for metrics purposes. + std::vector<GURL> most_visited_urls_; + + DISALLOW_COPY_AND_ASSIGN(MostVisitedHandler); +}; + MostVisitedHandler::MostVisitedHandler(DOMUI* dom_ui) : DOMMessageHandler(dom_ui), dom_ui_(dom_ui) { @@ -300,27 +385,27 @@ MostVisitedHandler::MostVisitedHandler(DOMUI* dom_ui) g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(&chrome_url_data_manager, &ChromeURLDataManager::AddDataSource, - new ThumbnailSource(dom_ui->get_profile()))); + new DOMUIThumbnailSource(dom_ui->GetProfile()))); g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(&chrome_url_data_manager, &ChromeURLDataManager::AddDataSource, - new FavIconSource(dom_ui->get_profile()))); + new DOMUIFavIconSource(dom_ui->GetProfile()))); // Get notifications when history is cleared. NotificationService* service = NotificationService::current(); service->AddObserver(this, NotificationType::HISTORY_URLS_DELETED, - Source<Profile>(dom_ui_->get_profile())); + Source<Profile>(dom_ui_->GetProfile())); } MostVisitedHandler::~MostVisitedHandler() { NotificationService* service = NotificationService::current(); service->RemoveObserver(this, NotificationType::HISTORY_URLS_DELETED, - Source<Profile>(dom_ui_->get_profile())); + Source<Profile>(dom_ui_->GetProfile())); } void MostVisitedHandler::HandleGetMostVisited(const Value* value) { HistoryService* hs = - dom_ui_->get_profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); + dom_ui_->GetProfile()->GetHistoryService(Profile::EXPLICIT_ACCESS); hs->QuerySegmentUsageSince( &cancelable_consumer_, base::Time::Now() - base::TimeDelta::FromDays(kMostVisitedScope), @@ -359,6 +444,31 @@ void MostVisitedHandler::Observe(NotificationType type, /////////////////////////////////////////////////////////////////////////////// // TemplateURLHandler +// The handler for Javascript messages related to the "common searches" view. +class TemplateURLHandler : public DOMMessageHandler, + public TemplateURLModelObserver { + public: + explicit TemplateURLHandler(DOMUI* dom_ui); + virtual ~TemplateURLHandler(); + + // Callback for the "getMostSearched" message, sent when the page requests + // the list of available searches. + void HandleGetMostSearched(const Value* content); + // Callback for the "doSearch" message, sent when the user wants to + // run a search. Content of the message is an array containing + // [<the search keyword>, <the search term>]. + void HandleDoSearch(const Value* content); + + // TemplateURLModelObserver implementation. + virtual void OnTemplateURLModelChanged(); + + private: + DOMUI* dom_ui_; + TemplateURLModel* template_url_model_; // Owned by profile. + + DISALLOW_COPY_AND_ASSIGN(TemplateURLHandler); +}; + TemplateURLHandler::TemplateURLHandler(DOMUI* dom_ui) : DOMMessageHandler(dom_ui), dom_ui_(dom_ui), @@ -378,7 +488,7 @@ void TemplateURLHandler::HandleGetMostSearched(const Value* content) { // The page Javascript has requested the list of keyword searches. // Start loading them from the template URL backend. if (!template_url_model_) { - template_url_model_ = dom_ui_->get_profile()->GetTemplateURLModel(); + template_url_model_ = dom_ui_->GetProfile()->GetTemplateURLModel(); template_url_model_->AddObserver(this); } if (template_url_model_->loaded()) { @@ -435,7 +545,7 @@ void TemplateURLHandler::HandleDoSearch(const Value* content) { if (url.is_valid()) { // Load the URL. - dom_ui_->get_contents()->OpenURL(url, GURL(), CURRENT_TAB, + dom_ui_->web_contents()->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::LINK); // Record the user action @@ -456,7 +566,7 @@ void TemplateURLHandler::HandleDoSearch(const Value* content) { if (urls[i] == template_url) { UserMetrics::RecordComputedAction( StringPrintf(L"NTP_SearchURL%d", item_number), - dom_ui_->get_profile()); + dom_ui_->GetProfile()); break; } @@ -494,6 +604,51 @@ void TemplateURLHandler::OnTemplateURLModelChanged() { /////////////////////////////////////////////////////////////////////////////// // RecentlyBookmarkedHandler +class RecentlyBookmarkedHandler : public DOMMessageHandler, + public BookmarkModelObserver { + public: + explicit RecentlyBookmarkedHandler(DOMUI* dom_ui); + ~RecentlyBookmarkedHandler(); + + // Callback which navigates to the bookmarks page. + void HandleShowBookmarkPage(const Value*); + + // Callback for the "getRecentlyBookmarked" message. + // It takes no arguments. + void HandleGetRecentlyBookmarked(const Value*); + + private: + void SendBookmarksToPage(); + + // BookmarkModelObserver methods. These invoke SendBookmarksToPage. + virtual void Loaded(BookmarkModel* model); + virtual void BookmarkNodeAdded(BookmarkModel* model, + BookmarkNode* parent, + int index); + virtual void BookmarkNodeRemoved(BookmarkModel* model, + BookmarkNode* parent, + int index); + virtual void BookmarkNodeChanged(BookmarkModel* model, + BookmarkNode* node); + + // These won't effect what is shown, so they do nothing. + virtual void BookmarkNodeMoved(BookmarkModel* model, + BookmarkNode* old_parent, + int old_index, + BookmarkNode* new_parent, + int new_index) {} + virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, + BookmarkNode* node) {} + virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model, + BookmarkNode* node) {} + + DOMUI* dom_ui_; + // The model we're getting bookmarks from. The model is owned by the Profile. + BookmarkModel* model_; + + DISALLOW_COPY_AND_ASSIGN(RecentlyBookmarkedHandler); +}; + RecentlyBookmarkedHandler::RecentlyBookmarkedHandler(DOMUI* dom_ui) : DOMMessageHandler(dom_ui), dom_ui_(dom_ui), @@ -510,7 +665,7 @@ RecentlyBookmarkedHandler::~RecentlyBookmarkedHandler() { void RecentlyBookmarkedHandler::HandleGetRecentlyBookmarked(const Value*) { if (!model_) { - model_ = dom_ui_->get_profile()->GetBookmarkModel(); + model_ = dom_ui_->GetProfile()->GetBookmarkModel(); model_->AddObserver(this); } // If the model is loaded, synchronously send the bookmarks down. Otherwise @@ -558,6 +713,48 @@ void RecentlyBookmarkedHandler::BookmarkNodeChanged(BookmarkModel* model, /////////////////////////////////////////////////////////////////////////////// // RecentlyClosedTabsHandler +class RecentlyClosedTabsHandler : public DOMMessageHandler, + public TabRestoreService::Observer { + public: + explicit RecentlyClosedTabsHandler(DOMUI* dom_ui); + virtual ~RecentlyClosedTabsHandler(); + + // Callback for the "reopenTab" message. Rewrites the history of the + // currently displayed tab to be the one in TabRestoreService with a + // history of a session passed in through the content pointer. + void HandleReopenTab(const Value* content); + + // Callback for the "getRecentlyClosedTabs" message. + void HandleGetRecentlyClosedTabs(const Value* content); + + // Observer callback for TabRestoreService::Observer. Sends data on + // recently closed tabs to the javascript side of this page to + // display to the user. + virtual void TabRestoreServiceChanged(TabRestoreService* service); + + // Observer callback to notice when our associated TabRestoreService + // is destroyed. + virtual void TabRestoreServiceDestroyed(TabRestoreService* service); + + private: + // Converts a closed tab to the value sent down to the NTP. Returns true on + // success, false if the value shouldn't be sent down. + bool TabToValue(const TabRestoreService::Tab& tab, + DictionaryValue* dictionary); + + // Converts a closed window to the value sent down to the NTP. Returns true + // on success, false if the value shouldn't be sent down. + bool WindowToValue(const TabRestoreService::Window& window, + DictionaryValue* dictionary); + + DOMUI* dom_ui_; + + // TabRestoreService that we are observing. + TabRestoreService* tab_restore_service_; + + DISALLOW_COPY_AND_ASSIGN(RecentlyClosedTabsHandler); +}; + RecentlyClosedTabsHandler::RecentlyClosedTabsHandler(DOMUI* dom_ui) : DOMMessageHandler(dom_ui), dom_ui_(dom_ui), @@ -575,7 +772,7 @@ RecentlyClosedTabsHandler::~RecentlyClosedTabsHandler() { } void RecentlyClosedTabsHandler::HandleReopenTab(const Value* content) { - NavigationController* controller = dom_ui_->get_contents()->controller(); + NavigationController* controller = dom_ui_->web_contents()->controller(); Browser* browser = Browser::GetBrowserForController( controller, NULL); if (!browser) @@ -608,7 +805,7 @@ void RecentlyClosedTabsHandler::HandleReopenTab(const Value* content) { void RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs( const Value* content) { if (!tab_restore_service_) { - tab_restore_service_ = dom_ui_->get_profile()->GetTabRestoreService(); + tab_restore_service_ = dom_ui_->GetProfile()->GetTabRestoreService(); // GetTabRestoreService() can return NULL (i.e., when in Off the // Record mode) @@ -666,7 +863,7 @@ bool RecentlyClosedTabsHandler::TabToValue( const TabNavigation& current_navigation = tab.navigations.at(tab.current_navigation_index); - if (current_navigation.url() == NewTabUI::GetBaseURL()) + if (current_navigation.url() == GURL(chrome::kChromeUINewTabURL)) return false; SetURLTitleAndDirection(dictionary, current_navigation.title(), @@ -704,6 +901,22 @@ bool RecentlyClosedTabsHandler::WindowToValue( /////////////////////////////////////////////////////////////////////////////// // HistoryHandler +class HistoryHandler : public DOMMessageHandler { + public: + explicit HistoryHandler(DOMUI* dom_ui); + + // Callback which navigates to the history page. + void HandleShowHistoryPage(const Value*); + + // Callback which navigates to the history page and performs a search. + void HandleSearchHistoryPage(const Value* content); + + private: + DOMUI* dom_ui_; + + DISALLOW_COPY_AND_ASSIGN(HistoryHandler); +}; + HistoryHandler::HistoryHandler(DOMUI* dom_ui) : DOMMessageHandler(dom_ui), dom_ui_(dom_ui) { @@ -714,11 +927,11 @@ HistoryHandler::HistoryHandler(DOMUI* dom_ui) } void HistoryHandler::HandleShowHistoryPage(const Value*) { - NavigationController* controller = dom_ui_->get_contents()->controller(); + NavigationController* controller = dom_ui_->web_contents()->controller(); if (controller) { - UserMetrics::RecordAction(L"NTP_ShowHistory", - dom_ui_->get_profile()); - controller->LoadURL(HistoryUI::GetBaseURL(), GURL(), PageTransition::LINK); + UserMetrics::RecordAction(L"NTP_ShowHistory", dom_ui_->GetProfile()); + controller->LoadURL(GURL(chrome::kChromeUINewTabURL), GURL(), + PageTransition::LINK); // We are deleted by LoadURL, so do not call anything else. } } @@ -733,13 +946,12 @@ void HistoryHandler::HandleSearchHistoryPage(const Value* content) { static_cast<const StringValue*>(list_member); std::wstring wstring_value; if (string_value->GetAsString(&wstring_value)) { - UserMetrics::RecordAction(L"NTP_SearchHistory", - dom_ui_->get_profile()); + UserMetrics::RecordAction(L"NTP_SearchHistory", dom_ui_->GetProfile()); #if defined(OS_WIN) // TODO(port): include this once history is converted to HTML NavigationController* controller = - dom_ui_->get_contents()->controller(); + dom_ui_->web_contents()->controller(); controller->LoadURL( HistoryUI::GetHistoryURLWithSearchText(wstring_value), GURL(), @@ -756,6 +968,25 @@ void HistoryHandler::HandleSearchHistoryPage(const Value* content) { /////////////////////////////////////////////////////////////////////////////// // MetricsHandler +// Let the page contents record UMA actions. Only use when you can't do it from +// C++. For example, we currently use it to let the NTP log the postion of the +// Most Visited or Bookmark the user clicked on, as we don't get that +// information through RequestOpenURL. You will need to update the metrics +// dashboard with the action names you use, as our processor won't catch that +// information (treat it as RecordComputedMetrics) +class MetricsHandler : public DOMMessageHandler { + public: + explicit MetricsHandler(DOMUI* dom_ui); + + // Callback which records a user action. + void HandleMetrics(const Value* content); + + private: + DOMUI* dom_ui_; + + DISALLOW_COPY_AND_ASSIGN(MetricsHandler); +}; + MetricsHandler::MetricsHandler(DOMUI* dom_ui) : DOMMessageHandler(dom_ui), dom_ui_(dom_ui) { @@ -773,49 +1004,44 @@ void MetricsHandler::HandleMetrics(const Value* content) { static_cast<const StringValue*>(list_member); std::wstring wstring_value; if (string_value->GetAsString(&wstring_value)) { - UserMetrics::RecordComputedAction(wstring_value, - dom_ui_->get_profile()); + UserMetrics::RecordComputedAction(wstring_value, dom_ui_->GetProfile()); } } } } +} // namespace + /////////////////////////////////////////////////////////////////////////////// -// NewTabUIContents - -NewTabUI::NewTabUI(DOMUIContents* contents) : - DOMUI(contents), - motd_message_id_(0), - incognito_(false), - most_visited_handler_(NULL) { - if (get_profile()->IsOffTheRecord()) - incognito_ = true; +// NewTabUI + +NewTabUI::NewTabUI(WebContents* contents) + : DOMUI(contents), + motd_message_id_(0), + incognito_(false) { + // Override some options on the DOM UI. + hide_favicon_ = true; + force_bookmark_bar_visible_ = true; + focus_location_bar_by_default_ = true; + should_hide_url_ = true; + overridden_title_ = WideToUTF16Hack(l10n_util::GetString(IDS_NEW_TAB_TITLE)); + + // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more + // highly. Note this means we're including clicks on not only most visited + // thumbnails, but also clicks on recently bookmarked. + link_transition_type_ = PageTransition::AUTO_BOOKMARK; if (NewTabHTMLSource::first_view() && - (get_profile()->GetPrefs()->GetInteger(prefs::kRestoreOnStartup) != 0 || - !get_profile()->GetPrefs()->GetBoolean(prefs::kHomePageIsNewTabPage)) - ) { + (GetProfile()->GetPrefs()->GetInteger(prefs::kRestoreOnStartup) != 0 || + !GetProfile()->GetPrefs()->GetBoolean(prefs::kHomePageIsNewTabPage))) { NewTabHTMLSource::set_first_view(false); } - contents->render_view_host()->set_paint_observer(new PaintTimer); -} + web_contents()->render_view_host()->set_paint_observer(new PaintTimer); -void NewTabUI::Init() { - // Regretfully, DataSources are global, instead of - // per-TabContents. Because of the motd_message_id_ member, each - // NewTabUIContents instance could theoretically have a different - // message. Moving this from the constructor to here means that we - // reconnect this source each time we reload so we should no longer - // have the bug where we open a normal new tab page (no motd), open - // another OTR new tab page (blurb motd describing what 'incognito' - // means), refresh the normal new page (which now displays the - // incognito blurb because that was the last NewTabHTMLSource hooked - // up). - // - // This is a workaround until http://b/issue?id=1230312 is fixed. + if (GetProfile()->IsOffTheRecord()) { + incognito_ = true; - if (incognito_) { IncognitoTabHTMLSource* html_source = new IncognitoTabHTMLSource(); g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, @@ -824,8 +1050,7 @@ void NewTabUI::Init() { html_source)); } else { AddMessageHandler(new TemplateURLHandler(this)); - most_visited_handler_ = new MostVisitedHandler(this); - AddMessageHandler(most_visited_handler_); // Takes ownership. + AddMessageHandler(new MostVisitedHandler(this)); AddMessageHandler(new RecentlyBookmarkedHandler(this)); AddMessageHandler(new RecentlyClosedTabsHandler(this)); AddMessageHandler(new HistoryHandler(this)); @@ -844,25 +1069,3 @@ void NewTabUI::Init() { html_source)); } } - -// static -GURL NewTabUI::GetBaseURL() { - return GURL(chrome::kChromeUINewTabURL); -} - -void NewTabUI::SetInitialFocus() { - if (get_contents()->delegate()) - get_contents()->delegate()->SetFocusToLocationBar(); -} - -void NewTabUI::RequestOpenURL(const GURL& url, - const GURL&, - WindowOpenDisposition disposition) { - // The user opened a URL on the page (including "open in new window"). - // We count all such clicks as AUTO_BOOKMARK, which increments the site's - // visit count (which is used for ranking the most visited entries). - // Note this means we're including clicks on not only most visited - // thumbnails, but also clicks on recently bookmarked. - get_contents()->OpenURL(url, GURL(), disposition, - PageTransition::AUTO_BOOKMARK); -} diff --git a/chrome/browser/dom_ui/new_tab_ui.h b/chrome/browser/dom_ui/new_tab_ui.h index 3d915b7..4a59201 100644 --- a/chrome/browser/dom_ui/new_tab_ui.h +++ b/chrome/browser/dom_ui/new_tab_ui.h @@ -2,275 +2,20 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H__ -#define CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H__ +#ifndef CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H_ +#define CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H_ -#include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/dom_ui/dom_ui.h" -#include "chrome/browser/dom_ui/chrome_url_data_manager.h" -#include "chrome/browser/history/history.h" -#include "chrome/browser/search_engines/template_url_model.h" -#include "chrome/browser/sessions/tab_restore_service.h" -class DictionaryValue; class GURL; class Profile; -class Value; -enum TabContentsType; - -// The following classes aren't used outside of new_tab_ui.cc but are -// put here for clarity. - -class NewTabHTMLSource : public ChromeURLDataManager::DataSource { - public: - NewTabHTMLSource(); - - // Called when the network layer has requested a resource underneath - // the path we registered. - virtual void StartDataRequest(const std::string& path, int request_id); - - virtual std::string GetMimeType(const std::string&) const { - return "text/html"; - } - - // Setters and getters for first_view. - static void set_first_view(bool first_view) { first_view_ = first_view; } - static bool first_view() { return first_view_; } - private: - // Whether this is the is the first viewing of the new tab page and - // we think it is the user's startup page. - static bool first_view_; - - DISALLOW_EVIL_CONSTRUCTORS(NewTabHTMLSource); -}; - -class IncognitoTabHTMLSource : public ChromeURLDataManager::DataSource { - public: - // Creates our datasource and sets our user message to a specific message - // from our string bundle. - IncognitoTabHTMLSource(); - - // Called when the network layer has requested a resource underneath - // the path we registered. - virtual void StartDataRequest(const std::string& path, int request_id); - - virtual std::string GetMimeType(const std::string&) const { - return "text/html"; - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(IncognitoTabHTMLSource); -}; - -// The handler for Javascript messages related to the "most visited" view. -class MostVisitedHandler : public DOMMessageHandler, - public NotificationObserver { - public: - explicit MostVisitedHandler(DOMUI* dom_ui); - virtual ~MostVisitedHandler(); - - // Callback for the "getMostVisited" message. - void HandleGetMostVisited(const Value* value); - - // NotificationObserver implementation. - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details); - - const std::vector<GURL>& most_visited_urls() const { - return most_visited_urls_; - } - - private: - // Callback from the history system when the most visited list is available. - void OnSegmentUsageAvailable(CancelableRequestProvider::Handle handle, - std::vector<PageUsageData*>* data); - - DOMUI* dom_ui_; - - // Our consumer for the history service. - CancelableRequestConsumerTSimple<PageUsageData*> cancelable_consumer_; - - // The most visited URLs, in priority order. - // Only used for matching up clicks on the page to which most visited entry - // was clicked on for metrics purposes. - std::vector<GURL> most_visited_urls_; - - DISALLOW_EVIL_CONSTRUCTORS(MostVisitedHandler); -}; - -// The handler for Javascript messages related to the "common searches" view. -class TemplateURLHandler : public DOMMessageHandler, - public TemplateURLModelObserver { - public: - explicit TemplateURLHandler(DOMUI* dom_ui); - virtual ~TemplateURLHandler(); - - // Callback for the "getMostSearched" message, sent when the page requests - // the list of available searches. - void HandleGetMostSearched(const Value* content); - // Callback for the "doSearch" message, sent when the user wants to - // run a search. Content of the message is an array containing - // [<the search keyword>, <the search term>]. - void HandleDoSearch(const Value* content); - - // TemplateURLModelObserver implementation. - virtual void OnTemplateURLModelChanged(); - - private: - DOMUI* dom_ui_; - TemplateURLModel* template_url_model_; // Owned by profile. - - DISALLOW_EVIL_CONSTRUCTORS(TemplateURLHandler); -}; - -class RecentlyBookmarkedHandler : public DOMMessageHandler, - public BookmarkModelObserver { - public: - explicit RecentlyBookmarkedHandler(DOMUI* dom_ui); - ~RecentlyBookmarkedHandler(); - - // Callback which navigates to the bookmarks page. - void HandleShowBookmarkPage(const Value*); - - // Callback for the "getRecentlyBookmarked" message. - // It takes no arguments. - void HandleGetRecentlyBookmarked(const Value*); - - private: - void SendBookmarksToPage(); - - // BookmarkModelObserver methods. These invoke SendBookmarksToPage. - virtual void Loaded(BookmarkModel* model); - virtual void BookmarkNodeAdded(BookmarkModel* model, - BookmarkNode* parent, - int index); - virtual void BookmarkNodeRemoved(BookmarkModel* model, - BookmarkNode* parent, - int index); - virtual void BookmarkNodeChanged(BookmarkModel* model, - BookmarkNode* node); - - // These won't effect what is shown, so they do nothing. - virtual void BookmarkNodeMoved(BookmarkModel* model, - BookmarkNode* old_parent, - int old_index, - BookmarkNode* new_parent, - int new_index) {} - virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, - BookmarkNode* node) {} - virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model, - BookmarkNode* node) {} - - DOMUI* dom_ui_; - // The model we're getting bookmarks from. The model is owned by the Profile. - BookmarkModel* model_; - - DISALLOW_EVIL_CONSTRUCTORS(RecentlyBookmarkedHandler); -}; - -class RecentlyClosedTabsHandler : public DOMMessageHandler, - public TabRestoreService::Observer { - public: - explicit RecentlyClosedTabsHandler(DOMUI* dom_ui); - virtual ~RecentlyClosedTabsHandler(); - - // Callback for the "reopenTab" message. Rewrites the history of the - // currently displayed tab to be the one in TabRestoreService with a - // history of a session passed in through the content pointer. - void HandleReopenTab(const Value* content); - - // Callback for the "getRecentlyClosedTabs" message. - void HandleGetRecentlyClosedTabs(const Value* content); - - // Observer callback for TabRestoreService::Observer. Sends data on - // recently closed tabs to the javascript side of this page to - // display to the user. - virtual void TabRestoreServiceChanged(TabRestoreService* service); - - // Observer callback to notice when our associated TabRestoreService - // is destroyed. - virtual void TabRestoreServiceDestroyed(TabRestoreService* service); - - private: - // Converts a closed tab to the value sent down to the NTP. Returns true on - // success, false if the value shouldn't be sent down. - bool TabToValue(const TabRestoreService::Tab& tab, - DictionaryValue* dictionary); - - // Converts a closed window to the value sent down to the NTP. Returns true - // on success, false if the value shouldn't be sent down. - bool WindowToValue(const TabRestoreService::Window& window, - DictionaryValue* dictionary); - - DOMUI* dom_ui_; - - /// TabRestoreService that we are observing. - TabRestoreService* tab_restore_service_; - - DISALLOW_EVIL_CONSTRUCTORS(RecentlyClosedTabsHandler); -}; - -class HistoryHandler : public DOMMessageHandler { - public: - explicit HistoryHandler(DOMUI* dom_ui); - - // Callback which navigates to the history page. - void HandleShowHistoryPage(const Value*); - - // Callback which navigates to the history page and performs a search. - void HandleSearchHistoryPage(const Value* content); - - private: - DOMUI* dom_ui_; - DISALLOW_EVIL_CONSTRUCTORS(HistoryHandler); -}; - -// Let the page contents record UMA actions. Only use when you can't do it from -// C++. For example, we currently use it to let the NTP log the postion of the -// Most Visited or Bookmark the user clicked on, as we don't get that -// information through RequestOpenURL. You will need to update the metrics -// dashboard with the action names you use, as our processor won't catch that -// information (treat it as RecordComputedMetrics) -class MetricsHandler : public DOMMessageHandler { - public: - explicit MetricsHandler(DOMUI* dom_ui); - - // Callback which records a user action. - void HandleMetrics(const Value* content); - - private: - DOMUI* dom_ui_; - DISALLOW_EVIL_CONSTRUCTORS(MetricsHandler); -}; // The TabContents used for the New Tab page. class NewTabUI : public DOMUI { public: - explicit NewTabUI(DOMUIContents* contents); - - // Return the URL for the front page of this UI. - static GURL GetBaseURL(); - - // DOMUI Implementation - virtual void Init(); - - // Overridden from DOMUI. - // Favicon should not be displayed. - virtual bool ShouldDisplayFavIcon() { return false; } - // Bookmark bar should always be visible. - virtual bool IsBookmarkBarAlwaysVisible() { return true; } - // When NTP gets the initial focus, focus the URL bar. - virtual void SetInitialFocus(); - // Should not display our URL. - virtual bool ShouldDisplayURL() { return false; } - // Control what happens when a link is clicked. - virtual void RequestOpenURL(const GURL& url, const GURL& referrer, - WindowOpenDisposition disposition); + explicit NewTabUI(WebContents* manager); private: - DOMUIContents* contents_; - // The message id that should be displayed in this NewTabUIContents // instance's motd area. int motd_message_id_; @@ -279,11 +24,7 @@ class NewTabUI : public DOMUI { // what HTML to load. bool incognito_; - // A pointer to the handler for most visited. - // Owned by the DOMUIHost. - MostVisitedHandler* most_visited_handler_; - - DISALLOW_EVIL_CONSTRUCTORS(NewTabUI); + DISALLOW_COPY_AND_ASSIGN(NewTabUI); }; -#endif // CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H__ +#endif // CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H_ |