summaryrefslogtreecommitdiffstats
path: root/chrome/browser/dom_ui
diff options
context:
space:
mode:
authorarv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-26 18:34:51 +0000
committerarv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-26 18:34:51 +0000
commitaefdb477a4fb4ee11b85d2e8fa265aea531d8bf5 (patch)
treeb2b2e0fe070505b2a8145988afa320b8cbe4ed5a /chrome/browser/dom_ui
parent30ebee97923efa86769332a801f92efcd6aa8121 (diff)
downloadchromium_src-aefdb477a4fb4ee11b85d2e8fa265aea531d8bf5.zip
chromium_src-aefdb477a4fb4ee11b85d2e8fa265aea531d8bf5.tar.gz
chromium_src-aefdb477a4fb4ee11b85d2e8fa265aea531d8bf5.tar.bz2
Initial add of app launcher DOM UI.
BUG=None TEST=Go to chrome://apps Review URL: http://codereview.chromium.org/1353005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42786 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/dom_ui')
-rw-r--r--chrome/browser/dom_ui/app_launcher_ui.cc169
-rw-r--r--chrome/browser/dom_ui/app_launcher_ui.h70
-rw-r--r--chrome/browser/dom_ui/dom_ui_factory.cc5
3 files changed, 243 insertions, 1 deletions
diff --git a/chrome/browser/dom_ui/app_launcher_ui.cc b/chrome/browser/dom_ui/app_launcher_ui.cc
new file mode 100644
index 0000000..607e170
--- /dev/null
+++ b/chrome/browser/dom_ui/app_launcher_ui.cc
@@ -0,0 +1,169 @@
+// Copyright (c) 2010 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/app_launcher_ui.h"
+
+#include "app/resource_bundle.h"
+#include "base/base64.h"
+#include "base/message_loop.h"
+#include "base/ref_counted_memory.h"
+#include "base/singleton.h"
+#include "base/string_piece.h"
+#include "base/string_util.h"
+#include "base/values.h"
+#include "chrome/browser/chrome_thread.h"
+#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
+#include "chrome/browser/extensions/extensions_service.h"
+#include "chrome/browser/profile.h"
+#include "chrome/browser/tab_contents/tab_contents.h"
+#include "chrome/common/notification_type.h"
+#include "chrome/common/jstemplate_builder.h"
+#include "chrome/common/url_constants.h"
+#include "grit/browser_resources.h"
+#include "grit/theme_resources.h"
+
+namespace {
+
+bool TreatAsApp(const Extension* extension) {
+ return !extension->GetFullLaunchURL().is_empty();
+}
+
+} // namespace
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// AppLauncherUIHTMLSource
+//
+////////////////////////////////////////////////////////////////////////////////
+
+AppLauncherUIHTMLSource::AppLauncherUIHTMLSource()
+ : DataSource(chrome::kChromeUIAppsHost, MessageLoop::current()) {
+}
+
+void AppLauncherUIHTMLSource::StartDataRequest(const std::string& path,
+ bool is_off_the_record, int request_id) {
+ DictionaryValue localized_strings;
+ // TODO(arv): What strings do we need?
+ localized_strings.SetString(L"title", L"App Launcher");
+ SetFontAndTextDirection(&localized_strings);
+
+ static const base::StringPiece app_launcher_html(
+ ResourceBundle::GetSharedInstance().GetRawDataResource(
+ IDR_APP_LAUNCHER_HTML));
+ std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
+ app_launcher_html, &localized_strings);
+ jstemplate_builder::AppendJsTemplateSourceHtml(&full_html);
+
+ scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
+ html_bytes->data.resize(full_html.size());
+ std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
+
+ SendResponse(request_id, html_bytes);
+}
+
+std::string AppLauncherUIHTMLSource::GetMimeType(
+ const std::string& path) const {
+ return "text/html";
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// AppLauncherHandler
+//
+////////////////////////////////////////////////////////////////////////////////
+
+AppLauncherDOMHandler::AppLauncherDOMHandler(
+ ExtensionsService* extension_service)
+ : extensions_service_(extension_service) {
+}
+
+AppLauncherDOMHandler::~AppLauncherDOMHandler() {}
+
+DOMMessageHandler* AppLauncherDOMHandler::Attach(DOMUI* dom_ui) {
+ // TODO(arv): Add initialization code to the Apps store etc.
+ return DOMMessageHandler::Attach(dom_ui);
+}
+
+void AppLauncherDOMHandler::RegisterMessages() {
+ dom_ui_->RegisterMessageCallback("getAll",
+ NewCallback(this, &AppLauncherDOMHandler::HandleGetAll));
+ dom_ui_->RegisterMessageCallback("launch",
+ NewCallback(this, &AppLauncherDOMHandler::HandleLaunch));
+}
+
+// static
+void AppLauncherDOMHandler::CreateAppInfo(Extension* extension,
+ DictionaryValue* value) {
+ value->Clear();
+ value->SetString(L"id", extension->id());
+ value->SetString(L"name", extension->name());
+ value->SetString(L"description", extension->description());
+ value->SetString(L"launch_url", extension->GetFullLaunchURL().spec());
+
+ // TODO(arv): Get the icon from the extension
+ std::string file_contents =
+ ResourceBundle::GetSharedInstance().GetDataResource(
+ IDR_EXTENSION_DEFAULT_ICON);
+ std::string base64_encoded;
+ base::Base64Encode(file_contents, &base64_encoded);
+ GURL icon_url("data:image/png;base64," + base64_encoded);
+ value->SetString(L"icon", icon_url.spec());
+}
+
+void AppLauncherDOMHandler::HandleGetAll(const Value* value) {
+ ListValue list;
+ const ExtensionList* extensions = extensions_service_->extensions();
+ for (ExtensionList::const_iterator it = extensions->begin();
+ it != extensions->end(); ++it) {
+ if (TreatAsApp(*it)) {
+ DictionaryValue* app_info = new DictionaryValue();
+ CreateAppInfo(*it, app_info);
+ list.Append(app_info);
+ }
+ }
+
+ dom_ui_->CallJavascriptFunction(L"getAllCallback", list);
+}
+
+void AppLauncherDOMHandler::HandleLaunch(const Value* value) {
+ if (!value->IsType(Value::TYPE_LIST)) {
+ NOTREACHED();
+ return;
+ }
+
+ std::string url;
+ const ListValue* list = static_cast<const ListValue*>(value);
+ if (list->GetSize() == 0 || !list->GetString(0, &url)) {
+ NOTREACHED();
+ return;
+ }
+
+ TabContents* tab_contents = dom_ui_->tab_contents();
+ tab_contents->OpenURL(GURL(url), GURL(), NEW_FOREGROUND_TAB,
+ PageTransition::LINK);
+}
+////////////////////////////////////////////////////////////////////////////////
+//
+// AppLauncherUI
+//
+////////////////////////////////////////////////////////////////////////////////
+
+AppLauncherUI::AppLauncherUI(TabContents* contents) : DOMUI(contents) {
+ ExtensionsService *extension_service =
+ GetProfile()->GetOriginalProfile()->GetExtensionsService();
+
+ AppLauncherDOMHandler* handler = new AppLauncherDOMHandler(extension_service);
+ AddMessageHandler(handler);
+ handler->Attach(this);
+
+ AppLauncherUIHTMLSource* html_source = new AppLauncherUIHTMLSource();
+
+ // Set up the chrome://bookmarks/ source.
+ ChromeThread::PostTask(
+ ChromeThread::IO, FROM_HERE,
+ NewRunnableMethod(
+ Singleton<ChromeURLDataManager>::get(),
+ &ChromeURLDataManager::AddDataSource,
+ make_scoped_refptr(html_source)));
+}
diff --git a/chrome/browser/dom_ui/app_launcher_ui.h b/chrome/browser/dom_ui/app_launcher_ui.h
new file mode 100644
index 0000000..9a0e31d
--- /dev/null
+++ b/chrome/browser/dom_ui/app_launcher_ui.h
@@ -0,0 +1,70 @@
+// Copyright (c) 2010 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_APP_LAUNCHER_UI_H_
+#define CHROME_BROWSER_DOM_UI_APP_LAUNCHER_UI_H_
+
+#include <string>
+
+#include "base/ref_counted.h"
+#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
+#include "chrome/browser/dom_ui/dom_ui.h"
+
+class Extension;
+class ExtensionsService;
+class GURL;
+class RefCountedMemory;
+
+// The HTML source for the apps view.
+class AppLauncherUIHTMLSource : public ChromeURLDataManager::DataSource {
+ public:
+ AppLauncherUIHTMLSource();
+
+ // Called when the network layer has requested a resource underneath
+ // the path we registered.
+ virtual void StartDataRequest(const std::string& path,
+ bool is_off_the_record,
+ int request_id);
+ virtual std::string GetMimeType(const std::string& path) const;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(AppLauncherUIHTMLSource);
+};
+
+// The handler for Javascript messages related to the "apps" view.
+class AppLauncherDOMHandler : public DOMMessageHandler {
+ public:
+ explicit AppLauncherDOMHandler(ExtensionsService* extension_service);
+ virtual ~AppLauncherDOMHandler();
+
+ // DOMMessageHandler implementation.
+ virtual DOMMessageHandler* Attach(DOMUI* dom_ui);
+ virtual void RegisterMessages();
+
+ // Populate a dictionary with the information from an extension.
+ static void CreateAppInfo(Extension* extension, DictionaryValue* value);
+
+ // Callback for the "getAll" message.
+ void HandleGetAll(const Value* value);
+
+ // Callback for the "launch" message.
+ void HandleLaunch(const Value* value);
+
+ private:
+ // The apps are represented in the extensions model.
+ scoped_refptr<ExtensionsService> extensions_service_;
+
+ DISALLOW_COPY_AND_ASSIGN(AppLauncherDOMHandler);
+};
+
+// This class is used to hook up chrome://applauncher/.
+class AppLauncherUI : public DOMUI {
+ public:
+ explicit AppLauncherUI(TabContents* contents);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(AppLauncherUI);
+};
+
+#endif // CHROME_BROWSER_DOM_UI_APP_LAUNCHER_UI_H_
diff --git a/chrome/browser/dom_ui/dom_ui_factory.cc b/chrome/browser/dom_ui/dom_ui_factory.cc
index dbf3729..a108213 100644
--- a/chrome/browser/dom_ui/dom_ui_factory.cc
+++ b/chrome/browser/dom_ui/dom_ui_factory.cc
@@ -5,6 +5,7 @@
#include "chrome/browser/dom_ui/dom_ui_factory.h"
#include "chrome/browser/chrome_thread.h"
+#include "chrome/browser/dom_ui/app_launcher_ui.h"
#include "chrome/browser/dom_ui/bookmarks_ui.h"
#include "chrome/browser/dom_ui/downloads_ui.h"
#include "chrome/browser/dom_ui/devtools_ui.h"
@@ -89,7 +90,9 @@ static DOMUIFactoryFunction GetDOMUIFactoryFunction(const GURL& url) {
// We must compare hosts only since some of the DOM UIs append extra stuff
// after the host name.
- if (url.host() == chrome::kChromeUIBookmarksHost)
+ if (url.host() == chrome::kChromeUIAppsHost)
+ return &NewDOMUI<AppLauncherUI>;
+ if (url.host() == chrome::kChromeUIBookmarksHost)
return &NewDOMUI<BookmarksUI>;
if (url.host() == chrome::kChromeUIDevToolsHost)
return &NewDOMUI<DevToolsUI>;