Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / components_ui.cc
blobcbd4e2f2d0f96ee0eba66dd0b5ed2ee32393a184
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/components_ui.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/values.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/component_updater/component_updater_service.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/url_constants.h"
18 #include "content/public/browser/web_ui.h"
19 #include "content/public/browser/web_ui_data_source.h"
20 #include "content/public/browser/web_ui_message_handler.h"
21 #include "grit/browser_resources.h"
22 #include "grit/generated_resources.h"
23 #include "grit/theme_resources.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/resource/resource_bundle.h"
27 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/ui/webui/chromeos/ui_account_tweaks.h"
29 #endif
31 using content::WebUIMessageHandler;
33 namespace {
35 content::WebUIDataSource* CreateComponentsUIHTMLSource() {
36 content::WebUIDataSource* source =
37 content::WebUIDataSource::Create(chrome::kChromeUIComponentsHost);
38 source->SetUseJsonJSFormatV2();
40 source->AddLocalizedString("componentsTitle", IDS_COMPONENTS_TITLE);
41 source->AddLocalizedString("componentsNoneInstalled",
42 IDS_COMPONENTS_NONE_INSTALLED);
43 source->AddLocalizedString("componentVersion", IDS_COMPONENTS_VERSION);
44 source->AddLocalizedString("checkUpdate", IDS_COMPONENTS_CHECK_FOR_UPDATE);
45 source->AddLocalizedString("noComponents", IDS_COMPONENTS_NO_COMPONENTS);
47 source->SetJsonPath("strings.js");
48 source->AddResourcePath("components.js", IDR_COMPONENTS_JS);
49 source->SetDefaultResource(IDR_COMPONENTS_HTML);
50 #if defined(OS_CHROMEOS)
51 chromeos::AddAccountUITweaksLocalizedValues(source);
52 #endif
53 return source;
56 ////////////////////////////////////////////////////////////////////////////////
58 // ComponentsDOMHandler
60 ////////////////////////////////////////////////////////////////////////////////
62 // The handler for Javascript messages for the chrome://components/ page.
63 class ComponentsDOMHandler : public WebUIMessageHandler {
64 public:
65 ComponentsDOMHandler();
66 virtual ~ComponentsDOMHandler() {}
68 // WebUIMessageHandler implementation.
69 virtual void RegisterMessages() OVERRIDE;
71 // Callback for the "requestComponentsData" message.
72 void HandleRequestComponentsData(const base::ListValue* args);
74 // Callback for the "checkUpdate" message.
75 void HandleCheckUpdate(const base::ListValue* args);
77 private:
78 void LoadComponents();
80 content::NotificationRegistrar registrar_;
82 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler);
85 ComponentsDOMHandler::ComponentsDOMHandler() {
88 void ComponentsDOMHandler::RegisterMessages() {
89 web_ui()->RegisterMessageCallback("requestComponentsData",
90 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData,
91 base::Unretained(this)));
93 web_ui()->RegisterMessageCallback("checkUpdate",
94 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate,
95 base::Unretained(this)));
98 void ComponentsDOMHandler::HandleRequestComponentsData(
99 const base::ListValue* args) {
100 LoadComponents();
103 // This function is called when user presses button from html UI.
104 // TODO(shrikant): We need to make this button available based on current
105 // state e.g. If component state is currently updating then we need to disable
106 // button. (https://code.google.com/p/chromium/issues/detail?id=272540)
107 void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue* args) {
108 if (args->GetSize() != 1) {
109 NOTREACHED();
110 return;
113 std::string component_id;
114 if (!args->GetString(0, &component_id)) {
115 NOTREACHED();
116 return;
119 ComponentsUI::OnDemandUpdate(component_id);
122 void ComponentsDOMHandler::LoadComponents() {
123 component_updater::ComponentUpdateService* cus =
124 g_browser_process->component_updater();
125 std::vector<component_updater::CrxComponentInfo> components;
126 cus->GetComponents(&components);
128 // Construct DictionaryValues to return to UI.
129 base::ListValue* component_list = new base::ListValue();
130 for (size_t j = 0; j < components.size(); ++j) {
131 const component_updater::CrxComponentInfo& component = components[j];
133 base::DictionaryValue* component_entry = new base::DictionaryValue();
134 component_entry->SetString("id", component.id);
135 component_entry->SetString("name", component.name);
136 component_entry->SetString("version", component.version);
138 component_list->Append(component_entry);
141 base::DictionaryValue results;
142 results.Set("components", component_list);
143 web_ui()->CallJavascriptFunction("returnComponentsData", results);
146 } // namespace
148 ///////////////////////////////////////////////////////////////////////////////
150 // ComponentsUI
152 ///////////////////////////////////////////////////////////////////////////////
154 ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
155 web_ui->AddMessageHandler(new ComponentsDOMHandler());
157 // Set up the chrome://components/ source.
158 Profile* profile = Profile::FromWebUI(web_ui);
159 content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource());
162 // static
163 void ComponentsUI::OnDemandUpdate(const std::string& component_id) {
164 component_updater::ComponentUpdateService* cus =
165 g_browser_process->component_updater();
166 cus->OnDemandUpdate(component_id);
169 // static
170 base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes(
171 ui::ScaleFactor scale_factor) {
172 return ResourceBundle::GetSharedInstance().
173 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor);