blob: db3ec16b667fd9190c2b6b60c99d9c92daca6a71 [file] [log] [blame]
// Copyright 2018 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/ui/webui/app_management/app_management_ui.h"
#include <utility>
#include "base/bind.h"
#include "base/feature_list.h"
#include "chrome/browser/apps/app_service/app_icon_source.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/app_management/app_management_page_handler.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "ui/base/resource/resource_bundle.h"
namespace {
content::WebUIDataSource* CreateAppManagementUIHTMLSource(Profile* profile) {
content::WebUIDataSource* source =
content::WebUIDataSource::Create(chrome::kChromeUIAppLauncherPageHost);
source->AddLocalizedString("appListTitle", IDS_APP_MANAGEMENT_APP_LIST_TITLE);
source->AddLocalizedString("lessApps", IDS_APP_MANAGEMENT_LESS_APPS);
source->AddLocalizedString("moreApps", IDS_APP_MANAGEMENT_MORE_APPS);
source->AddLocalizedString("searchPrompt", IDS_APP_MANAGEMENT_SEARCH_PROMPT);
source->AddLocalizedString("title", IDS_APP_MANAGEMENT_TITLE);
source->AddResourcePath("app_management.mojom-lite.js",
IDR_APP_MANAGEMENT_MOJO_LITE_JS);
source->AddResourcePath("types.mojom-lite.js",
IDR_APP_MANAGEMENT_TYPES_MOJO_LITE_JS);
source->AddResourcePath("big_buffer.mojom-lite.js",
IDR_APP_MANAGEMENT_BIG_BUFFER_MOJO_LITE_JS);
source->AddResourcePath("bitmap.mojom-lite.js",
IDR_APP_MANAGEMENT_BITMAP_MOJO_LITE_JS);
source->AddResourcePath("image.mojom-lite.js",
IDR_APP_MANAGEMENT_IMAGE_MOJO_LITE_JS);
source->AddResourcePath("image_info.mojom-lite.js",
IDR_APP_MANAGEMENT_IMAGE_INFO_MOJO_LITE_JS);
source->AddResourcePath("app.html", IDR_APP_MANAGEMENT_APP_HTML);
source->AddResourcePath("app.js", IDR_APP_MANAGEMENT_APP_JS);
source->AddResourcePath("browser_proxy.html",
IDR_APP_MANAGEMENT_BROWSER_PROXY_HTML);
source->AddResourcePath("browser_proxy.js",
IDR_APP_MANAGEMENT_BROWSER_PROXY_JS);
source->AddResourcePath("constants.html", IDR_APP_MANAGEMENT_CONSTANTS_HTML);
source->AddResourcePath("constants.js", IDR_APP_MANAGEMENT_CONSTANTS_JS);
source->AddResourcePath("fake_page_handler.js",
IDR_APP_MANAGEMENT_FAKE_PAGE_HANDLER_JS);
source->AddResourcePath("item.html", IDR_APP_MANAGEMENT_ITEM_HTML);
source->AddResourcePath("item.js", IDR_APP_MANAGEMENT_ITEM_JS);
source->AddResourcePath("main_view.html", IDR_APP_MANAGEMENT_MAIN_VIEW_HTML);
source->AddResourcePath("main_view.js", IDR_APP_MANAGEMENT_MAIN_VIEW_JS);
source->AddResourcePath("shared_style.html",
IDR_APP_MANAGEMENT_SHARED_STYLE_HTML);
source->AddResourcePath("shared_vars.html",
IDR_APP_MANAGEMENT_SHARED_VARS_HTML);
source->SetDefaultResource(IDR_APP_MANAGEMENT_INDEX_HTML);
source->SetJsonPath("strings.js");
return source;
}
} // namespace
///////////////////////////////////////////////////////////////////////////////
//
// AppManagementUI
//
///////////////////////////////////////////////////////////////////////////////
AppManagementUI::AppManagementUI(content::WebUI* web_ui)
: ui::MojoWebUIController(web_ui, true), page_factory_binding_(this) {
Profile* profile = Profile::FromWebUI(web_ui);
// Set up the data source.
content::WebUIDataSource* source = CreateAppManagementUIHTMLSource(profile);
content::WebUIDataSource::Add(profile, source);
AddHandlerToRegistry(base::BindRepeating(
&AppManagementUI::BindPageHandlerFactory, base::Unretained(this)));
// Make the chrome://app-icon/ resource available.
if (profile) {
content::URLDataSource::Add(profile,
std::make_unique<apps::AppIconSource>(profile));
}
}
AppManagementUI::~AppManagementUI() = default;
bool AppManagementUI::IsEnabled() {
return base::FeatureList::IsEnabled(features::kAppManagement);
}
void AppManagementUI::BindPageHandlerFactory(
app_management::mojom::PageHandlerFactoryRequest request) {
if (page_factory_binding_.is_bound())
page_factory_binding_.Unbind();
page_factory_binding_.Bind(std::move(request));
}
void AppManagementUI::CreatePageHandler(
app_management::mojom::PagePtr page,
app_management::mojom::PageHandlerRequest request) {
DCHECK(page);
page_handler_.reset(new AppManagementPageHandler(std::move(request),
std::move(page), web_ui()));
}