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 "components/component_updater/crx_update_item.h"
20 #include "content/public/browser/web_ui.h"
21 #include "content/public/browser/web_ui_data_source.h"
22 #include "content/public/browser/web_ui_message_handler.h"
23 #include "grit/browser_resources.h"
24 #include "grit/theme_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/ui/webui/chromeos/ui_account_tweaks.h"
32 using content::WebUIMessageHandler
;
36 content::WebUIDataSource
* CreateComponentsUIHTMLSource(Profile
* profile
) {
37 content::WebUIDataSource
* source
=
38 content::WebUIDataSource::Create(chrome::kChromeUIComponentsHost
);
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
);
46 source
->AddLocalizedString("statusLabel", IDS_COMPONENTS_STATUS_LABEL
);
47 source
->AddLocalizedString("checkingLabel", IDS_COMPONENTS_CHECKING_LABEL
);
49 source
->SetJsonPath("strings.js");
50 source
->AddResourcePath("components.js", IDR_COMPONENTS_JS
);
51 source
->SetDefaultResource(IDR_COMPONENTS_HTML
);
52 #if defined(OS_CHROMEOS)
53 chromeos::AddAccountUITweaksLocalizedValues(source
, profile
);
58 ////////////////////////////////////////////////////////////////////////////////
60 // ComponentsDOMHandler
62 ////////////////////////////////////////////////////////////////////////////////
64 // The handler for Javascript messages for the chrome://components/ page.
65 class ComponentsDOMHandler
: public WebUIMessageHandler
{
67 ComponentsDOMHandler();
68 virtual ~ComponentsDOMHandler() {}
70 // WebUIMessageHandler implementation.
71 virtual void RegisterMessages() override
;
73 // Callback for the "requestComponentsData" message.
74 void HandleRequestComponentsData(const base::ListValue
* args
);
76 // Callback for the "checkUpdate" message.
77 void HandleCheckUpdate(const base::ListValue
* args
);
80 content::NotificationRegistrar registrar_
;
82 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler
);
85 ComponentsDOMHandler::ComponentsDOMHandler() {
88 void ComponentsDOMHandler::RegisterMessages() {
89 web_ui()->RegisterMessageCallback(
90 "requestComponentsData",
91 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData
,
92 base::Unretained(this)));
94 web_ui()->RegisterMessageCallback(
96 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate
,
97 base::Unretained(this)));
100 void ComponentsDOMHandler::HandleRequestComponentsData(
101 const base::ListValue
* args
) {
102 base::ListValue
* list
= ComponentsUI::LoadComponents();
103 base::DictionaryValue result
;
104 result
.Set("components", list
);
105 web_ui()->CallJavascriptFunction("returnComponentsData", result
);
108 // This function is called when user presses button from html UI.
109 // TODO(shrikant): We need to make this button available based on current
110 // state e.g. If component state is currently updating then we need to disable
111 // button. (https://code.google.com/p/chromium/issues/detail?id=272540)
112 void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue
* args
) {
113 if (args
->GetSize() != 1) {
118 std::string component_id
;
119 if (!args
->GetString(0, &component_id
)) {
124 ComponentsUI::OnDemandUpdate(component_id
);
129 ///////////////////////////////////////////////////////////////////////////////
133 ///////////////////////////////////////////////////////////////////////////////
135 ComponentsUI::ComponentsUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
136 web_ui
->AddMessageHandler(new ComponentsDOMHandler());
138 // Set up the chrome://components/ source.
139 Profile
* profile
= Profile::FromWebUI(web_ui
);
140 content::WebUIDataSource::Add(profile
, CreateComponentsUIHTMLSource(profile
));
141 component_updater::ComponentUpdateService
* cus
=
142 g_browser_process
->component_updater();
143 cus
->AddObserver(this);
146 ComponentsUI::~ComponentsUI() {
147 component_updater::ComponentUpdateService
* cus
=
148 g_browser_process
->component_updater();
150 cus
->RemoveObserver(this);
154 void ComponentsUI::OnDemandUpdate(const std::string
& component_id
) {
155 component_updater::ComponentUpdateService
* cus
=
156 g_browser_process
->component_updater();
157 cus
->GetOnDemandUpdater().OnDemandUpdate(component_id
);
161 base::ListValue
* ComponentsUI::LoadComponents() {
162 component_updater::ComponentUpdateService
* cus
=
163 g_browser_process
->component_updater();
164 std::vector
<std::string
> component_ids
;
165 component_ids
= cus
->GetComponentIDs();
167 // Construct DictionaryValues to return to UI.
168 base::ListValue
* component_list
= new base::ListValue();
169 for (size_t j
= 0; j
< component_ids
.size(); ++j
) {
170 component_updater::CrxUpdateItem item
;
171 if (cus
->GetComponentDetails(component_ids
[j
], &item
)) {
172 base::DictionaryValue
* component_entry
= new base::DictionaryValue();
173 component_entry
->SetString("id", component_ids
[j
]);
174 component_entry
->SetString("name", item
.component
.name
);
175 component_entry
->SetString("version", item
.component
.version
.GetString());
176 component_entry
->SetString("status", ServiceStatusToString(item
.status
));
177 component_list
->Append(component_entry
);
181 return component_list
;
185 base::RefCountedMemory
* ComponentsUI::GetFaviconResourceBytes(
186 ui::ScaleFactor scale_factor
) {
187 return ResourceBundle::GetSharedInstance().
188 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON
, scale_factor
);
191 base::string16
ComponentsUI::ComponentEventToString(Events event
) {
193 case COMPONENT_UPDATER_STARTED
:
194 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_STARTED
);
195 case COMPONENT_UPDATER_SLEEPING
:
196 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_SLEEPING
);
197 case COMPONENT_UPDATE_FOUND
:
198 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_FOUND
);
199 case COMPONENT_UPDATE_READY
:
200 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_READY
);
201 case COMPONENT_UPDATED
:
202 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_UPDATED
);
203 case COMPONENT_NOT_UPDATED
:
204 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_NOTUPDATED
);
205 case COMPONENT_UPDATE_DOWNLOADING
:
206 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_DOWNLOADING
);
208 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN
);
211 base::string16
ComponentsUI::ServiceStatusToString(
212 component_updater::CrxUpdateItem::Status status
) {
214 case component_updater::CrxUpdateItem::kNew
:
215 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NEW
);
216 case component_updater::CrxUpdateItem::kChecking
:
217 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_CHECKING
);
218 case component_updater::CrxUpdateItem::kCanUpdate
:
219 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATE
);
220 case component_updater::CrxUpdateItem::kDownloadingDiff
:
221 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL_DIFF
);
222 case component_updater::CrxUpdateItem::kDownloading
:
223 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL
);
224 case component_updater::CrxUpdateItem::kUpdatingDiff
:
225 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDT_DIFF
);
226 case component_updater::CrxUpdateItem::kUpdating
:
227 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATING
);
228 case component_updater::CrxUpdateItem::kUpdated
:
229 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATED
);
230 case component_updater::CrxUpdateItem::kUpToDate
:
231 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPTODATE
);
232 case component_updater::CrxUpdateItem::kNoUpdate
:
233 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NOUPDATE
);
234 case component_updater::CrxUpdateItem::kLastStatus
:
235 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN
);
237 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN
);
240 void ComponentsUI::OnEvent(Events event
, const std::string
& id
) {
241 base::DictionaryValue parameters
;
242 parameters
.SetString("event", ComponentEventToString(event
));
244 using component_updater::ComponentUpdateService
;
245 if (event
== ComponentUpdateService::Observer::COMPONENT_UPDATED
) {
246 ComponentUpdateService
* cus
= g_browser_process
->component_updater();
247 component_updater::CrxUpdateItem item
;
248 if (cus
->GetComponentDetails(id
, &item
))
249 parameters
.SetString("version", item
.component
.version
.GetString());
251 parameters
.SetString("id", id
);
253 web_ui()->CallJavascriptFunction("onComponentEvent", parameters
);