diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-03 03:05:11 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-03 03:05:11 +0000 |
commit | 4acc19a6f31abef9608546d10f107240603ca57e (patch) | |
tree | 632c914c428e94c05c8b9cb52cb183e62ae13e56 /chrome/browser/dom_ui | |
parent | 15936cdb983239ba2347e624af19e7305e416c7b (diff) | |
download | chromium_src-4acc19a6f31abef9608546d10f107240603ca57e.zip chromium_src-4acc19a6f31abef9608546d10f107240603ca57e.tar.gz chromium_src-4acc19a6f31abef9608546d10f107240603ca57e.tar.bz2 |
Move HTML dialogs out of their own tab contents type. Moved functions to new
file html_dialog_ui.*
Move WebContents view creation into the constructor, which makes a bunch of
extra calls to CreateView unnecessary.
Remove unused CallJavascriptFunction() functions in DOMUI.
Review URL: http://codereview.chromium.org/56065
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13065 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/dom_ui')
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_factory.cc | 12 | ||||
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_host.cc | 26 | ||||
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_host.h | 15 | ||||
-rw-r--r-- | chrome/browser/dom_ui/html_dialog_contents.cc | 77 | ||||
-rw-r--r-- | chrome/browser/dom_ui/html_dialog_contents.h | 76 | ||||
-rw-r--r-- | chrome/browser/dom_ui/html_dialog_ui.cc | 70 | ||||
-rw-r--r-- | chrome/browser/dom_ui/html_dialog_ui.h | 79 |
7 files changed, 159 insertions, 196 deletions
diff --git a/chrome/browser/dom_ui/dom_ui_factory.cc b/chrome/browser/dom_ui/dom_ui_factory.cc index 7a69c0b..75786d8 100644 --- a/chrome/browser/dom_ui/dom_ui_factory.cc +++ b/chrome/browser/dom_ui/dom_ui_factory.cc @@ -8,6 +8,7 @@ #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/html_dialog_ui.h" #include "chrome/browser/dom_ui/new_tab_ui.h" #include "chrome/browser/extensions/extensions_ui.h" #include "chrome/common/url_constants.h" @@ -24,8 +25,15 @@ // 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. + // Currently, any gears: URL means an HTML dialog. + if (url.SchemeIs(chrome::kGearsScheme)) { + if (new_ui) + *new_ui = new HtmlDialogUI(web_contents); + return true; + } + + // This will get called a lot to check all URLs, so do a quick check of other + // schemes (gears was handled above) to filter out most URLs. if (!url.SchemeIs(chrome::kChromeInternalScheme) && !url.SchemeIs(chrome::kChromeUIScheme)) return false; diff --git a/chrome/browser/dom_ui/dom_ui_host.cc b/chrome/browser/dom_ui/dom_ui_host.cc index 20ebc9f..f68105e 100644 --- a/chrome/browser/dom_ui/dom_ui_host.cc +++ b/chrome/browser/dom_ui/dom_ui_host.cc @@ -49,28 +49,6 @@ void DOMUIHost::RegisterMessageCallback(const std::string& name, message_callbacks_.insert(std::make_pair(name, callback)); } - -void DOMUIHost::CallJavascriptFunction(const std::wstring& function_name, - const Value& arg) { - std::string json; - JSONWriter::Write(&arg, false, &json); - std::wstring javascript = function_name + L"(" + UTF8ToWide(json) + L");"; - - ExecuteJavascript(javascript); -} - -void DOMUIHost::CallJavascriptFunction( - const std::wstring& function_name, - const Value& arg1, const Value& arg2) { - std::string json; - JSONWriter::Write(&arg1, false, &json); - std::wstring javascript = function_name + L"(" + UTF8ToWide(json); - JSONWriter::Write(&arg2, false, &json); - javascript += L"," + UTF8ToWide(json) + L");"; - - ExecuteJavascript(javascript); -} - void DOMUIHost::ProcessDOMUIMessage(const std::string& message, const std::string& content) { // Look up the callback for this message. @@ -101,7 +79,3 @@ WebPreferences DOMUIHost::GetWebkitPrefs() { return web_prefs; } - -void DOMUIHost::ExecuteJavascript(const std::wstring& javascript) { - render_view_host()->ExecuteJavascriptInWebFrame(std::wstring(), javascript); -} diff --git a/chrome/browser/dom_ui/dom_ui_host.h b/chrome/browser/dom_ui/dom_ui_host.h index b66fe9b..845aaf1 100644 --- a/chrome/browser/dom_ui/dom_ui_host.h +++ b/chrome/browser/dom_ui/dom_ui_host.h @@ -44,19 +44,6 @@ class DOMUIHost : public WebContents { void RegisterMessageCallback(const std::string& message, MessageCallback* callback); - - // 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 - // of the call, and should be thought of more like sending a message to - // the page. - // There are two function variants for one-arg and two-arg calls. - void CallJavascriptFunction(const std::wstring& function_name, - const Value& arg); - void CallJavascriptFunction(const std::wstring& function_name, - const Value& arg1, - const Value& arg2); - - // Overrides from WebContents. virtual void ProcessDOMUIMessage(const std::string& message, const std::string& content); @@ -76,8 +63,6 @@ class DOMUIHost : public WebContents { virtual ~DOMUIHost(); private: - // Execute a string of raw Javascript on the page. - void ExecuteJavascript(const std::wstring& javascript); // The DOMMessageHandlers we own. std::vector<DOMMessageHandler*> handlers_; diff --git a/chrome/browser/dom_ui/html_dialog_contents.cc b/chrome/browser/dom_ui/html_dialog_contents.cc deleted file mode 100644 index 7944a6a..0000000 --- a/chrome/browser/dom_ui/html_dialog_contents.cc +++ /dev/null @@ -1,77 +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/html_dialog_contents.h" - -#include "base/values.h" -#include "chrome/browser/renderer_host/render_view_host.h" - -const char kGearsScheme[] = "gears"; - -HtmlDialogContents::HtmlDialogContents(Profile* profile, - SiteInstance* instance, - RenderViewHostFactory* rvf) - : DOMUIHost(profile, instance, rvf), - delegate_(NULL) { - set_type(TAB_CONTENTS_HTML_DIALOG); -} - -HtmlDialogContents::~HtmlDialogContents() { -} - -void HtmlDialogContents::Init(HtmlDialogContentsDelegate* delegate) { - delegate_ = delegate; - - std::string dialog_args; - if (delegate_) - dialog_args = delegate_->GetDialogArgs(); - - DCHECK(render_view_host()); - render_view_host()->SetDOMUIProperty("dialogArguments", dialog_args); -} - -//////////////////////////////////////////////////////////////////////////////// -// DOMUIHost implementation: - -void HtmlDialogContents::AttachMessageHandlers() { - // Hook up the javascript function calls, also known as chrome.send("foo") - // calls in the HTML, to the actual C++ functions. - RegisterMessageCallback("DialogClose", - NewCallback(this, &HtmlDialogContents::OnDialogClosed)); -} - -// static -bool HtmlDialogContents::IsHtmlDialogUrl(const GURL& url) { - return url.SchemeIs(kGearsScheme); -} - -//////////////////////////////////////////////////////////////////////////////// -// Private: - -// Helper function to read the JSON string from the Value parameter. -std::string GetJsonResponse(const Value* content) { - if (!content || !content->IsType(Value::TYPE_LIST)) { - NOTREACHED(); - return ""; - } - const ListValue* args = static_cast<const ListValue*>(content); - if (args->GetSize() != 1) { - NOTREACHED(); - return ""; - } - - std::string result; - Value* value = NULL; - if (!args->Get(0, &value) || !value->GetAsString(&result)) { - NOTREACHED(); - return ""; - } - - return result; -} - -void HtmlDialogContents::OnDialogClosed(const Value* content) { - if (delegate_) - delegate_->OnDialogClosed(GetJsonResponse(content)); -} diff --git a/chrome/browser/dom_ui/html_dialog_contents.h b/chrome/browser/dom_ui/html_dialog_contents.h deleted file mode 100644 index cb0f215..0000000 --- a/chrome/browser/dom_ui/html_dialog_contents.h +++ /dev/null @@ -1,76 +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. - -#ifndef CHROME_BROWSER_DOM_UI_HTML_DIALOG_CONTENTS_H__ -#define CHROME_BROWSER_DOM_UI_HTML_DIALOG_CONTENTS_H__ - -#include "base/gfx/size.h" -#include "chrome/browser/dom_ui/dom_ui_host.h" - -// Implement this class to receive notifications. -class HtmlDialogContentsDelegate { - public: - // Returns true if the contents needs to be run in a modal dialog. - virtual bool IsDialogModal() const = 0; - // Returns the title of the dialog. - virtual std::wstring GetDialogTitle() const = 0; - // Get the HTML file path for the content to load in the dialog. - virtual GURL GetDialogContentURL() const = 0; - // Get the size of the dialog. - virtual void GetDialogSize(gfx::Size* size) const = 0; - // Gets the JSON string input to use when showing the dialog. - virtual std::string GetDialogArgs() const = 0; - // A callback to notify the delegate that the dialog closed. - virtual void OnDialogClosed(const std::string& json_retval) = 0; - - protected: - ~HtmlDialogContentsDelegate() {} -}; - -//////////////////////////////////////////////////////////////////////////////// -// -// HtmlDialogContents is a simple wrapper around DOMUIHost and is used to -// display file URL contents inside a modal HTML dialog. -// -//////////////////////////////////////////////////////////////////////////////// -class HtmlDialogContents : public DOMUIHost { - public: - struct HtmlDialogParams { - // The URL for the content that will be loaded in the dialog. - GURL url; - // Width of the dialog. - int width; - // Height of the dialog. - int height; - // The JSON input to pass to the dialog when showing it. - std::string json_input; - }; - - HtmlDialogContents(Profile* profile, - SiteInstance* instance, - RenderViewHostFactory* rvf); - virtual ~HtmlDialogContents(); - - // Initialize the HtmlDialogContents with the given delegate. Must be called - // after the RenderViewHost is created. - void Init(HtmlDialogContentsDelegate* d); - - // Overridden from DOMUIHost: - virtual void AttachMessageHandlers(); - - // Returns true of this URL should be handled by the HtmlDialogContents. - static bool IsHtmlDialogUrl(const GURL& url); - - private: - // JS message handlers: - void OnDialogClosed(const Value* content); - - // The delegate that knows how to display the dialog and receives the response - // back from the dialog. - HtmlDialogContentsDelegate* delegate_; - - DISALLOW_EVIL_CONSTRUCTORS(HtmlDialogContents); -}; - -#endif // CHROME_BROWSER_DOM_UI_HTML_DIALOG_CONTENTS_H__ diff --git a/chrome/browser/dom_ui/html_dialog_ui.cc b/chrome/browser/dom_ui/html_dialog_ui.cc new file mode 100644 index 0000000..ab86040d --- /dev/null +++ b/chrome/browser/dom_ui/html_dialog_ui.cc @@ -0,0 +1,70 @@ +// 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/html_dialog_ui.h" + +#include "base/singleton.h" +#include "base/values.h" +#include "chrome/browser/tab_contents/web_contents.h" +#include "chrome/browser/renderer_host/render_view_host.h" + +HtmlDialogUI::HtmlDialogUI(WebContents* web_contents) : DOMUI(web_contents) { +} + +HtmlDialogUI::~HtmlDialogUI() { + // Make sure we can't get any more callbacks. + GetPropertyAccessor().DeleteProperty(web_contents()->property_bag()); +} + +// static +PropertyAccessor<HtmlDialogUIDelegate*>& HtmlDialogUI::GetPropertyAccessor() { + return *Singleton< PropertyAccessor<HtmlDialogUIDelegate*> >::get(); +} + +//////////////////////////////////////////////////////////////////////////////// +// Private: + +// Helper function to read the JSON string from the Value parameter. +static std::string GetJsonResponse(const Value* content) { + if (!content || !content->IsType(Value::TYPE_LIST)) { + NOTREACHED(); + return std::string(); + } + const ListValue* args = static_cast<const ListValue*>(content); + if (args->GetSize() != 1) { + NOTREACHED(); + return std::string(); + } + + std::string result; + Value* value = NULL; + if (!args->Get(0, &value) || !value->GetAsString(&result)) { + NOTREACHED(); + return std::string(); + } + + return result; +} + +void HtmlDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { + // Hook up the javascript function calls, also known as chrome.send("foo") + // calls in the HTML, to the actual C++ functions. + RegisterMessageCallback("DialogClose", + NewCallback(this, &HtmlDialogUI::OnDialogClosed)); + + // Pass the arguments to the renderer supplied by the delegate. + std::string dialog_args; + HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty( + web_contents()->property_bag()); + if (delegate) + dialog_args = (*delegate)->GetDialogArgs(); + render_view_host->SetDOMUIProperty("dialogArguments", dialog_args); +} + +void HtmlDialogUI::OnDialogClosed(const Value* content) { + HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty( + web_contents()->property_bag()); + if (delegate) + (*delegate)->OnDialogClosed(GetJsonResponse(content)); +} diff --git a/chrome/browser/dom_ui/html_dialog_ui.h b/chrome/browser/dom_ui/html_dialog_ui.h new file mode 100644 index 0000000..070315f --- /dev/null +++ b/chrome/browser/dom_ui/html_dialog_ui.h @@ -0,0 +1,79 @@ +// 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. + +#ifndef CHROME_BROWSER_DOM_UI_HTML_DIALOG_UI_H_ +#define CHROME_BROWSER_DOM_UI_HTML_DIALOG_UI_H_ + +#include "base/gfx/size.h" +#include "chrome/browser/dom_ui/dom_ui.h" +#include "chrome/common/property_bag.h" +#include "googleurl/src/gurl.h" + +// Implement this class to receive notifications. +class HtmlDialogUIDelegate { + public: + // Returns true if the contents needs to be run in a modal dialog. + virtual bool IsDialogModal() const = 0; + + // Returns the title of the dialog. + virtual std::wstring GetDialogTitle() const = 0; + + // Get the HTML file path for the content to load in the dialog. + virtual GURL GetDialogContentURL() const = 0; + + // Get the size of the dialog. + virtual void GetDialogSize(gfx::Size* size) const = 0; + + // Gets the JSON string input to use when showing the dialog. + virtual std::string GetDialogArgs() const = 0; + + // A callback to notify the delegate that the dialog closed. + virtual void OnDialogClosed(const std::string& json_retval) = 0; + + protected: + ~HtmlDialogUIDelegate() {} +}; + +// Displays file URL contents inside a modal HTML dialog. +// +// This application really should not use WebContents + DOMUI. It should instead +// just embed a RenderView in a dialog and be done with it. +// +// Before loading a URL corresponding to this DOMUI, the caller should set its +// delegate as a property on the WebContents. This DOMUI will pick it up from +// there and call it back. This is a bit of a hack to allow the dialog to pass +// its delegate to the DOM UI without having nasty accessors on the WebContents. +// The correct design using RVH directly would avoid all of this. +class HtmlDialogUI : public DOMUI { + public: + struct HtmlDialogParams { + // The URL for the content that will be loaded in the dialog. + GURL url; + // Width of the dialog. + int width; + // Height of the dialog. + int height; + // The JSON input to pass to the dialog when showing it. + std::string json_input; + }; + + // When created, the property should already be set on the WebContents. + HtmlDialogUI(WebContents* web_contents); + virtual ~HtmlDialogUI(); + + // Returns the PropertyBag accessor object used to write the delegate pointer + // into the WebContents (see class-level comment above). + static PropertyAccessor<HtmlDialogUIDelegate*>& GetPropertyAccessor(); + + private: + // DOMUI overrides. + virtual void RenderViewCreated(RenderViewHost* render_view_host); + + // JS message handler. + void OnDialogClosed(const Value* content); + + DISALLOW_COPY_AND_ASSIGN(HtmlDialogUI); +}; + +#endif // CHROME_BROWSER_DOM_UI_HTML_DIALOG_UI_H_ |