Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / components_ui.cc
bloba929a1677a10b6fa0571e06935e94d7ffe381978
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/ui/browser.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "components/component_updater/component_updater_service.h"
19 #include "content/public/browser/web_ui.h"
20 #include "content/public/browser/web_ui_data_source.h"
21 #include "content/public/browser/web_ui_message_handler.h"
22 #include "grit/browser_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(Profile* profile) {
36 content::WebUIDataSource* source =
37 content::WebUIDataSource::Create(chrome::kChromeUIComponentsHost);
39 source->AddLocalizedString("componentsTitle", IDS_COMPONENTS_TITLE);
40 source->AddLocalizedString("componentsNoneInstalled",
41 IDS_COMPONENTS_NONE_INSTALLED);
42 source->AddLocalizedString("componentVersion", IDS_COMPONENTS_VERSION);
43 source->AddLocalizedString("checkUpdate", IDS_COMPONENTS_CHECK_FOR_UPDATE);
44 source->AddLocalizedString("noComponents", IDS_COMPONENTS_NO_COMPONENTS);
45 source->AddLocalizedString("statusLabel", IDS_COMPONENTS_STATUS_LABEL);
46 source->AddLocalizedString("checkingLabel", IDS_COMPONENTS_CHECKING_LABEL);
48 source->SetJsonPath("strings.js");
49 source->AddResourcePath("components.js", IDR_COMPONENTS_JS);
50 source->SetDefaultResource(IDR_COMPONENTS_HTML);
51 #if defined(OS_CHROMEOS)
52 chromeos::AddAccountUITweaksLocalizedValues(source, profile);
53 #endif
54 return source;
57 ////////////////////////////////////////////////////////////////////////////////
59 // ComponentsDOMHandler
61 ////////////////////////////////////////////////////////////////////////////////
63 // The handler for Javascript messages for the chrome://components/ page.
64 class ComponentsDOMHandler : public WebUIMessageHandler {
65 public:
66 ComponentsDOMHandler();
67 ~ComponentsDOMHandler() override {}
69 // WebUIMessageHandler implementation.
70 void RegisterMessages() override;
72 // Callback for the "requestComponentsData" message.
73 void HandleRequestComponentsData(const base::ListValue* args);
75 // Callback for the "checkUpdate" message.
76 void HandleCheckUpdate(const base::ListValue* args);
78 private:
79 content::NotificationRegistrar registrar_;
81 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler);
84 ComponentsDOMHandler::ComponentsDOMHandler() {
87 void ComponentsDOMHandler::RegisterMessages() {
88 web_ui()->RegisterMessageCallback(
89 "requestComponentsData",
90 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData,
91 base::Unretained(this)));
93 web_ui()->RegisterMessageCallback(
94 "checkUpdate",
95 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate,
96 base::Unretained(this)));
99 void ComponentsDOMHandler::HandleRequestComponentsData(
100 const base::ListValue* args) {
101 base::ListValue* list = ComponentsUI::LoadComponents();
102 base::DictionaryValue result;
103 result.Set("components", list);
104 web_ui()->CallJavascriptFunction("returnComponentsData", result);
107 // This function is called when user presses button from html UI.
108 // TODO(shrikant): We need to make this button available based on current
109 // state e.g. If component state is currently updating then we need to disable
110 // button. (https://code.google.com/p/chromium/issues/detail?id=272540)
111 void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue* args) {
112 if (args->GetSize() != 1) {
113 NOTREACHED();
114 return;
117 std::string component_id;
118 if (!args->GetString(0, &component_id)) {
119 NOTREACHED();
120 return;
123 ComponentsUI::OnDemandUpdate(component_id);
126 } // namespace
128 ///////////////////////////////////////////////////////////////////////////////
130 // ComponentsUI
132 ///////////////////////////////////////////////////////////////////////////////
134 ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
135 web_ui->AddMessageHandler(new ComponentsDOMHandler());
137 // Set up the chrome://components/ source.
138 Profile* profile = Profile::FromWebUI(web_ui);
139 content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource(profile));
140 component_updater::ComponentUpdateService* cus =
141 g_browser_process->component_updater();
142 cus->AddObserver(this);
145 ComponentsUI::~ComponentsUI() {
146 component_updater::ComponentUpdateService* cus =
147 g_browser_process->component_updater();
148 if (cus)
149 cus->RemoveObserver(this);
152 // static
153 void ComponentsUI::OnDemandUpdate(const std::string& component_id) {
154 component_updater::ComponentUpdateService* cus =
155 g_browser_process->component_updater();
156 cus->GetOnDemandUpdater().OnDemandUpdate(component_id);
159 // static
160 base::ListValue* ComponentsUI::LoadComponents() {
161 component_updater::ComponentUpdateService* cus =
162 g_browser_process->component_updater();
163 std::vector<std::string> component_ids;
164 component_ids = cus->GetComponentIDs();
166 // Construct DictionaryValues to return to UI.
167 base::ListValue* component_list = new base::ListValue();
168 for (size_t j = 0; j < component_ids.size(); ++j) {
169 update_client::CrxUpdateItem item;
170 if (cus->GetComponentDetails(component_ids[j], &item)) {
171 base::DictionaryValue* component_entry = new base::DictionaryValue();
172 component_entry->SetString("id", component_ids[j]);
173 component_entry->SetString("name", item.component.name);
174 component_entry->SetString("version", item.component.version.GetString());
175 component_entry->SetString("status", ServiceStatusToString(item.state));
176 component_list->Append(component_entry);
180 return component_list;
183 // static
184 base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes(
185 ui::ScaleFactor scale_factor) {
186 return ResourceBundle::GetSharedInstance().
187 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor);
190 base::string16 ComponentsUI::ComponentEventToString(Events event) {
191 switch (event) {
192 case Events::COMPONENT_CHECKING_FOR_UPDATES:
193 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_STARTED);
194 case Events::COMPONENT_WAIT:
195 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_SLEEPING);
196 case Events::COMPONENT_UPDATE_FOUND:
197 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_FOUND);
198 case Events::COMPONENT_UPDATE_READY:
199 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_READY);
200 case Events::COMPONENT_UPDATED:
201 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_UPDATED);
202 case Events::COMPONENT_NOT_UPDATED:
203 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_NOTUPDATED);
204 case Events::COMPONENT_UPDATE_DOWNLOADING:
205 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_DOWNLOADING);
207 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
210 base::string16 ComponentsUI::ServiceStatusToString(
211 update_client::CrxUpdateItem::State state) {
212 // TODO(sorin): handle kDownloaded. For now, just handle it as kUpdating.
213 switch (state) {
214 case update_client::CrxUpdateItem::State::kNew:
215 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NEW);
216 case update_client::CrxUpdateItem::State::kChecking:
217 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_CHECKING);
218 case update_client::CrxUpdateItem::State::kCanUpdate:
219 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATE);
220 case update_client::CrxUpdateItem::State::kDownloadingDiff:
221 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL_DIFF);
222 case update_client::CrxUpdateItem::State::kDownloading:
223 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL);
224 case update_client::CrxUpdateItem::State::kUpdatingDiff:
225 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDT_DIFF);
226 case update_client::CrxUpdateItem::State::kUpdating:
227 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATING);
228 case update_client::CrxUpdateItem::State::kDownloaded:
229 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DOWNLOADED);
230 case update_client::CrxUpdateItem::State::kUpdated:
231 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATED);
232 case update_client::CrxUpdateItem::State::kUpToDate:
233 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPTODATE);
234 case update_client::CrxUpdateItem::State::kNoUpdate:
235 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NOUPDATE);
236 case update_client::CrxUpdateItem::State::kLastStatus:
237 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
239 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
242 void ComponentsUI::OnEvent(Events event, const std::string& id) {
243 base::DictionaryValue parameters;
244 parameters.SetString("event", ComponentEventToString(event));
245 if (!id.empty()) {
246 if (event == Events::COMPONENT_UPDATED) {
247 auto cus = g_browser_process->component_updater();
248 update_client::CrxUpdateItem item;
249 if (cus->GetComponentDetails(id, &item))
250 parameters.SetString("version", item.component.version.GetString());
252 parameters.SetString("id", id);
254 web_ui()->CallJavascriptFunction("onComponentEvent", parameters);