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"
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"
31 using content::WebUIMessageHandler
;
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
);
57 ////////////////////////////////////////////////////////////////////////////////
59 // ComponentsDOMHandler
61 ////////////////////////////////////////////////////////////////////////////////
63 // The handler for Javascript messages for the chrome://components/ page.
64 class ComponentsDOMHandler
: public WebUIMessageHandler
{
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
);
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(
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) {
117 std::string component_id
;
118 if (!args
->GetString(0, &component_id
)) {
123 ComponentsUI::OnDemandUpdate(component_id
);
128 ///////////////////////////////////////////////////////////////////////////////
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();
149 cus
->RemoveObserver(this);
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
);
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
.status
));
176 component_list
->Append(component_entry
);
180 return component_list
;
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
) {
192 case COMPONENT_UPDATER_STARTED
:
193 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_STARTED
);
194 case COMPONENT_UPDATER_SLEEPING
:
195 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_SLEEPING
);
196 case COMPONENT_UPDATE_FOUND
:
197 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_FOUND
);
198 case COMPONENT_UPDATE_READY
:
199 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_READY
);
200 case COMPONENT_UPDATED
:
201 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_UPDATED
);
202 case COMPONENT_NOT_UPDATED
:
203 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_NOTUPDATED
);
204 case 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::Status status
) {
213 case update_client::CrxUpdateItem::kNew
:
214 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NEW
);
215 case update_client::CrxUpdateItem::kChecking
:
216 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_CHECKING
);
217 case update_client::CrxUpdateItem::kCanUpdate
:
218 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATE
);
219 case update_client::CrxUpdateItem::kDownloadingDiff
:
220 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL_DIFF
);
221 case update_client::CrxUpdateItem::kDownloading
:
222 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL
);
223 case update_client::CrxUpdateItem::kUpdatingDiff
:
224 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDT_DIFF
);
225 case update_client::CrxUpdateItem::kUpdating
:
226 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATING
);
227 case update_client::CrxUpdateItem::kUpdated
:
228 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATED
);
229 case update_client::CrxUpdateItem::kUpToDate
:
230 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPTODATE
);
231 case update_client::CrxUpdateItem::kNoUpdate
:
232 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NOUPDATE
);
233 case update_client::CrxUpdateItem::kLastStatus
:
234 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN
);
236 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN
);
239 void ComponentsUI::OnEvent(Events event
, const std::string
& id
) {
240 base::DictionaryValue parameters
;
241 parameters
.SetString("event", ComponentEventToString(event
));
243 using component_updater::ComponentUpdateService
;
244 if (event
== ComponentUpdateService::Observer::COMPONENT_UPDATED
) {
245 ComponentUpdateService
* cus
= g_browser_process
->component_updater();
246 update_client::CrxUpdateItem item
;
247 if (cus
->GetComponentDetails(id
, &item
))
248 parameters
.SetString("version", item
.component
.version
.GetString());
250 parameters
.SetString("id", id
);
252 web_ui()->CallJavascriptFunction("onComponentEvent", parameters
);