OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/components_ui.h" | 5 #include "chrome/browser/ui/webui/components_ui.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/component_updater/component_updater_service.h" | 13 #include "chrome/browser/component_updater/component_updater_service.h" |
14 #include "chrome/browser/component_updater/crx_update_item.h" | |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
16 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
18 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
19 #include "content/public/browser/web_ui_data_source.h" | 20 #include "content/public/browser/web_ui_data_source.h" |
20 #include "content/public/browser/web_ui_message_handler.h" | 21 #include "content/public/browser/web_ui_message_handler.h" |
21 #include "grit/browser_resources.h" | 22 #include "grit/browser_resources.h" |
22 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
23 #include "grit/theme_resources.h" | 24 #include "grit/theme_resources.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
36 content::WebUIDataSource* source = | 37 content::WebUIDataSource* source = |
37 content::WebUIDataSource::Create(chrome::kChromeUIComponentsHost); | 38 content::WebUIDataSource::Create(chrome::kChromeUIComponentsHost); |
38 source->SetUseJsonJSFormatV2(); | 39 source->SetUseJsonJSFormatV2(); |
39 | 40 |
40 source->AddLocalizedString("componentsTitle", IDS_COMPONENTS_TITLE); | 41 source->AddLocalizedString("componentsTitle", IDS_COMPONENTS_TITLE); |
41 source->AddLocalizedString("componentsNoneInstalled", | 42 source->AddLocalizedString("componentsNoneInstalled", |
42 IDS_COMPONENTS_NONE_INSTALLED); | 43 IDS_COMPONENTS_NONE_INSTALLED); |
43 source->AddLocalizedString("componentVersion", IDS_COMPONENTS_VERSION); | 44 source->AddLocalizedString("componentVersion", IDS_COMPONENTS_VERSION); |
44 source->AddLocalizedString("checkUpdate", IDS_COMPONENTS_CHECK_FOR_UPDATE); | 45 source->AddLocalizedString("checkUpdate", IDS_COMPONENTS_CHECK_FOR_UPDATE); |
45 source->AddLocalizedString("noComponents", IDS_COMPONENTS_NO_COMPONENTS); | 46 source->AddLocalizedString("noComponents", IDS_COMPONENTS_NO_COMPONENTS); |
47 source->AddLocalizedString("statusLabel", IDS_COMPONENTS_STATUS_LABEL); | |
48 source->AddLocalizedString("checkingLabel", IDS_COMPONENTS_CHECKING_LABEL); | |
46 | 49 |
47 source->SetJsonPath("strings.js"); | 50 source->SetJsonPath("strings.js"); |
48 source->AddResourcePath("components.js", IDR_COMPONENTS_JS); | 51 source->AddResourcePath("components.js", IDR_COMPONENTS_JS); |
49 source->SetDefaultResource(IDR_COMPONENTS_HTML); | 52 source->SetDefaultResource(IDR_COMPONENTS_HTML); |
50 #if defined(OS_CHROMEOS) | 53 #if defined(OS_CHROMEOS) |
51 chromeos::AddAccountUITweaksLocalizedValues(source, profile); | 54 chromeos::AddAccountUITweaksLocalizedValues(source, profile); |
52 #endif | 55 #endif |
53 return source; | 56 return source; |
54 } | 57 } |
55 | 58 |
(...skipping 12 matching lines...) Expand all Loading... | |
68 // WebUIMessageHandler implementation. | 71 // WebUIMessageHandler implementation. |
69 virtual void RegisterMessages() OVERRIDE; | 72 virtual void RegisterMessages() OVERRIDE; |
70 | 73 |
71 // Callback for the "requestComponentsData" message. | 74 // Callback for the "requestComponentsData" message. |
72 void HandleRequestComponentsData(const base::ListValue* args); | 75 void HandleRequestComponentsData(const base::ListValue* args); |
73 | 76 |
74 // Callback for the "checkUpdate" message. | 77 // Callback for the "checkUpdate" message. |
75 void HandleCheckUpdate(const base::ListValue* args); | 78 void HandleCheckUpdate(const base::ListValue* args); |
76 | 79 |
77 private: | 80 private: |
78 void LoadComponents(); | |
79 | |
80 content::NotificationRegistrar registrar_; | 81 content::NotificationRegistrar registrar_; |
81 | 82 |
82 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler); | 83 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler); |
83 }; | 84 }; |
84 | 85 |
85 ComponentsDOMHandler::ComponentsDOMHandler() { | 86 ComponentsDOMHandler::ComponentsDOMHandler() { |
86 } | 87 } |
87 | 88 |
88 void ComponentsDOMHandler::RegisterMessages() { | 89 void ComponentsDOMHandler::RegisterMessages() { |
89 web_ui()->RegisterMessageCallback("requestComponentsData", | 90 web_ui()->RegisterMessageCallback( |
91 "requestComponentsData", | |
90 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData, | 92 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData, |
91 base::Unretained(this))); | 93 base::Unretained(this))); |
92 | 94 |
93 web_ui()->RegisterMessageCallback("checkUpdate", | 95 web_ui()->RegisterMessageCallback( |
96 "checkUpdate", | |
94 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate, | 97 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate, |
95 base::Unretained(this))); | 98 base::Unretained(this))); |
96 } | 99 } |
97 | 100 |
98 void ComponentsDOMHandler::HandleRequestComponentsData( | 101 void ComponentsDOMHandler::HandleRequestComponentsData( |
99 const base::ListValue* args) { | 102 const base::ListValue* args) { |
100 LoadComponents(); | 103 base::ListValue* list = ComponentsUI::LoadComponents(); |
104 base::DictionaryValue result; | |
105 result.Set("components", list); | |
106 web_ui()->CallJavascriptFunction("returnComponentsData", result); | |
101 } | 107 } |
102 | 108 |
103 // This function is called when user presses button from html UI. | 109 // This function is called when user presses button from html UI. |
104 // TODO(shrikant): We need to make this button available based on current | 110 // 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 | 111 // 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) | 112 // button. (https://code.google.com/p/chromium/issues/detail?id=272540) |
107 void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue* args) { | 113 void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue* args) { |
108 if (args->GetSize() != 1) { | 114 if (args->GetSize() != 1) { |
109 NOTREACHED(); | 115 NOTREACHED(); |
110 return; | 116 return; |
111 } | 117 } |
112 | 118 |
113 std::string component_id; | 119 std::string component_id; |
114 if (!args->GetString(0, &component_id)) { | 120 if (!args->GetString(0, &component_id)) { |
115 NOTREACHED(); | 121 NOTREACHED(); |
116 return; | 122 return; |
117 } | 123 } |
118 | 124 |
119 ComponentsUI::OnDemandUpdate(component_id); | 125 ComponentsUI::OnDemandUpdate(component_id); |
120 } | 126 } |
121 | 127 |
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); | |
127 | |
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]; | |
132 | |
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); | |
137 | |
138 component_list->Append(component_entry); | |
139 } | |
140 | |
141 base::DictionaryValue results; | |
142 results.Set("components", component_list); | |
143 web_ui()->CallJavascriptFunction("returnComponentsData", results); | |
144 } | |
145 | |
146 } // namespace | 128 } // namespace |
147 | 129 |
148 /////////////////////////////////////////////////////////////////////////////// | 130 /////////////////////////////////////////////////////////////////////////////// |
149 // | 131 // |
150 // ComponentsUI | 132 // ComponentsUI |
151 // | 133 // |
152 /////////////////////////////////////////////////////////////////////////////// | 134 /////////////////////////////////////////////////////////////////////////////// |
153 | 135 |
154 ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 136 ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
155 web_ui->AddMessageHandler(new ComponentsDOMHandler()); | 137 web_ui->AddMessageHandler(new ComponentsDOMHandler()); |
156 | 138 |
157 // Set up the chrome://components/ source. | 139 // Set up the chrome://components/ source. |
158 Profile* profile = Profile::FromWebUI(web_ui); | 140 Profile* profile = Profile::FromWebUI(web_ui); |
159 content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource(profile)); | 141 content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource(profile)); |
142 component_updater::ComponentUpdateService* cus = | |
143 g_browser_process->component_updater(); | |
144 cus->AddObserver(this); | |
145 } | |
146 | |
147 ComponentsUI::~ComponentsUI() { | |
148 component_updater::ComponentUpdateService* cus = | |
149 g_browser_process->component_updater(); | |
150 if (cus) | |
151 cus->RemoveObserver(this); | |
160 } | 152 } |
161 | 153 |
162 // static | 154 // static |
163 void ComponentsUI::OnDemandUpdate(const std::string& component_id) { | 155 void ComponentsUI::OnDemandUpdate(const std::string& component_id) { |
164 component_updater::ComponentUpdateService* cus = | 156 component_updater::ComponentUpdateService* cus = |
165 g_browser_process->component_updater(); | 157 g_browser_process->component_updater(); |
166 cus->GetOnDemandUpdater().OnDemandUpdate(component_id); | 158 cus->GetOnDemandUpdater().OnDemandUpdate(component_id); |
167 } | 159 } |
168 | 160 |
169 // static | 161 // static |
162 base::ListValue* ComponentsUI::LoadComponents() { | |
163 component_updater::ComponentUpdateService* cus = | |
164 g_browser_process->component_updater(); | |
165 std::vector<std::string> component_ids; | |
166 component_ids = cus->GetComponentIDs(); | |
167 | |
168 // Construct DictionaryValues to return to UI. | |
169 base::ListValue* component_list = new base::ListValue(); | |
170 for (size_t j = 0; j < component_ids.size(); ++j) { | |
171 const component_updater::CrxUpdateItem* item = | |
172 cus->GetComponentDetails(component_ids[j]); | |
173 if (item) { | |
174 base::DictionaryValue* component_entry = new base::DictionaryValue(); | |
175 component_entry->SetString("id", component_ids[j]); | |
176 component_entry->SetString("name", item->component.name); | |
177 component_entry->SetString("version", | |
178 item->component.version.GetString()); | |
179 | |
180 component_entry->SetString("status", | |
181 ServiceStatusToString(item->status)); | |
182 | |
183 component_list->Append(component_entry); | |
184 } | |
185 } | |
186 | |
187 return component_list; | |
188 } | |
189 | |
190 // static | |
170 base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes( | 191 base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes( |
171 ui::ScaleFactor scale_factor) { | 192 ui::ScaleFactor scale_factor) { |
172 return ResourceBundle::GetSharedInstance(). | 193 return ResourceBundle::GetSharedInstance(). |
173 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor); | 194 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor); |
174 } | 195 } |
196 | |
197 base::string16 ComponentsUI::ComponentEventToString(Events event) { | |
198 switch (event) { | |
199 case COMPONENT_UPDATER_STARTED: | |
James Hawkins
2014/05/28 23:01:11
Optional: Storing these in maps may make these met
Shrikant Kelkar
2014/05/28 23:14:45
Will keep them as it is for now.
| |
200 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_STARTED); | |
201 case COMPONENT_UPDATER_SLEEPING: | |
202 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_SLEEPING); | |
203 case COMPONENT_UPDATE_FOUND: | |
204 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_FOUND); | |
205 case COMPONENT_UPDATE_READY: | |
206 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_READY); | |
207 case COMPONENT_UPDATED: | |
208 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_UPDATED); | |
209 case COMPONENT_NOT_UPDATED: | |
210 return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_NOTUPDATED); | |
211 } | |
212 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN); | |
213 } | |
214 | |
215 base::string16 ComponentsUI::ServiceStatusToString( | |
216 component_updater::CrxUpdateItem::Status status) { | |
217 switch (status) { | |
218 case component_updater::CrxUpdateItem::kNew: | |
219 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NEW); | |
220 case component_updater::CrxUpdateItem::kChecking: | |
221 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_CHECKING); | |
222 case component_updater::CrxUpdateItem::kCanUpdate: | |
223 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATE); | |
224 case component_updater::CrxUpdateItem::kDownloadingDiff: | |
225 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL_DIFF); | |
226 case component_updater::CrxUpdateItem::kDownloading: | |
227 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL); | |
228 case component_updater::CrxUpdateItem::kUpdatingDiff: | |
229 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDT_DIFF); | |
230 case component_updater::CrxUpdateItem::kUpdating: | |
231 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATING); | |
232 case component_updater::CrxUpdateItem::kUpdated: | |
233 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATED); | |
234 case component_updater::CrxUpdateItem::kUpToDate: | |
235 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPTODATE); | |
236 case component_updater::CrxUpdateItem::kNoUpdate: | |
237 return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NOUPDATE); | |
238 } | |
239 return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN); | |
240 } | |
241 | |
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 parameters.SetString("id", id); | |
247 web_ui()->CallJavascriptFunction("onComponentEvent", parameters); | |
248 } | |
OLD | NEW |