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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 virtual void RegisterMessages() OVERRIDE; | 70 virtual void RegisterMessages() OVERRIDE; |
70 | 71 |
71 // Callback for the "requestComponentsData" message. | 72 // Callback for the "requestComponentsData" message. |
72 void HandleRequestComponentsData(const base::ListValue* args); | 73 void HandleRequestComponentsData(const base::ListValue* args); |
73 | 74 |
74 // Callback for the "checkUpdate" message. | 75 // Callback for the "checkUpdate" message. |
75 void HandleCheckUpdate(const base::ListValue* args); | 76 void HandleCheckUpdate(const base::ListValue* args); |
76 | 77 |
77 private: | 78 private: |
78 void LoadComponents(); | 79 void LoadComponents(); |
| 80 std::string ServiceStatusToString( |
| 81 component_updater::CrxUpdateItem::Status status); |
79 | 82 |
80 content::NotificationRegistrar registrar_; | 83 content::NotificationRegistrar registrar_; |
81 | 84 |
82 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler); | 85 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler); |
83 }; | 86 }; |
84 | 87 |
85 ComponentsDOMHandler::ComponentsDOMHandler() { | 88 ComponentsDOMHandler::ComponentsDOMHandler() { |
86 } | 89 } |
87 | 90 |
88 void ComponentsDOMHandler::RegisterMessages() { | 91 void ComponentsDOMHandler::RegisterMessages() { |
89 web_ui()->RegisterMessageCallback("requestComponentsData", | 92 web_ui()->RegisterMessageCallback( |
| 93 "requestComponentsData", |
90 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData, | 94 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData, |
91 base::Unretained(this))); | 95 base::Unretained(this))); |
92 | 96 |
93 web_ui()->RegisterMessageCallback("checkUpdate", | 97 web_ui()->RegisterMessageCallback( |
| 98 "checkUpdate", |
94 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate, | 99 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate, |
95 base::Unretained(this))); | 100 base::Unretained(this))); |
96 } | 101 } |
97 | 102 |
98 void ComponentsDOMHandler::HandleRequestComponentsData( | 103 void ComponentsDOMHandler::HandleRequestComponentsData( |
99 const base::ListValue* args) { | 104 const base::ListValue* args) { |
100 LoadComponents(); | 105 LoadComponents(); |
101 } | 106 } |
102 | 107 |
103 // This function is called when user presses button from html UI. | 108 // This function is called when user presses button from html UI. |
(...skipping 11 matching lines...) Expand all Loading... |
115 NOTREACHED(); | 120 NOTREACHED(); |
116 return; | 121 return; |
117 } | 122 } |
118 | 123 |
119 ComponentsUI::OnDemandUpdate(component_id); | 124 ComponentsUI::OnDemandUpdate(component_id); |
120 } | 125 } |
121 | 126 |
122 void ComponentsDOMHandler::LoadComponents() { | 127 void ComponentsDOMHandler::LoadComponents() { |
123 component_updater::ComponentUpdateService* cus = | 128 component_updater::ComponentUpdateService* cus = |
124 g_browser_process->component_updater(); | 129 g_browser_process->component_updater(); |
125 std::vector<component_updater::CrxComponentInfo> components; | 130 std::vector<std::string> component_ids; |
126 cus->GetComponents(&components); | 131 cus->GetComponentIDs(&component_ids); |
127 | 132 |
128 // Construct DictionaryValues to return to UI. | 133 // Construct DictionaryValues to return to UI. |
129 base::ListValue* component_list = new base::ListValue(); | 134 base::ListValue* component_list = new base::ListValue(); |
130 for (size_t j = 0; j < components.size(); ++j) { | 135 for (size_t j = 0; j < component_ids.size(); ++j) { |
131 const component_updater::CrxComponentInfo& component = components[j]; | 136 component_updater::CrxUpdateItem* item; |
| 137 cus->GetComponentDetails(component_ids[j], &item); |
132 | 138 |
133 base::DictionaryValue* component_entry = new base::DictionaryValue(); | 139 base::DictionaryValue* component_entry = new base::DictionaryValue(); |
134 component_entry->SetString("id", component.id); | 140 component_entry->SetString("id", component_ids[j]); |
135 component_entry->SetString("name", component.name); | 141 component_entry->SetString("name", item->component.name); |
136 component_entry->SetString("version", component.version); | 142 component_entry->SetString("version", item->component.version.GetString()); |
| 143 |
| 144 component_entry->SetString("status", |
| 145 ServiceStatusToString(item->status).c_str()); |
137 | 146 |
138 component_list->Append(component_entry); | 147 component_list->Append(component_entry); |
139 } | 148 } |
140 | 149 |
141 base::DictionaryValue results; | 150 base::DictionaryValue results; |
142 results.Set("components", component_list); | 151 results.Set("components", component_list); |
143 web_ui()->CallJavascriptFunction("returnComponentsData", results); | 152 web_ui()->CallJavascriptFunction("returnComponentsData", results); |
144 } | 153 } |
145 | 154 |
| 155 std::string ComponentsDOMHandler::ServiceStatusToString( |
| 156 component_updater::CrxUpdateItem::Status status) { |
| 157 switch (status) { |
| 158 case component_updater::CrxUpdateItem::kNew: |
| 159 return "new"; |
| 160 case component_updater::CrxUpdateItem::kChecking: |
| 161 return "checking"; |
| 162 case component_updater::CrxUpdateItem::kCanUpdate: |
| 163 return "update"; |
| 164 case component_updater::CrxUpdateItem::kDownloadingDiff: |
| 165 return "downloading_diff"; |
| 166 case component_updater::CrxUpdateItem::kDownloading: |
| 167 return "downloading"; |
| 168 case component_updater::CrxUpdateItem::kUpdatingDiff: |
| 169 return "updating_diff"; |
| 170 case component_updater::CrxUpdateItem::kUpdating: |
| 171 return "updating"; |
| 172 case component_updater::CrxUpdateItem::kUpdated: |
| 173 return "updated"; |
| 174 case component_updater::CrxUpdateItem::kUpToDate: |
| 175 return "uptodate"; |
| 176 case component_updater::CrxUpdateItem::kNoUpdate: |
| 177 return "no_update"; |
| 178 case component_updater::CrxUpdateItem::kLastStatus: |
| 179 default: |
| 180 return "Unknown"; |
| 181 } |
| 182 return "Unknown"; |
| 183 } |
| 184 |
146 } // namespace | 185 } // namespace |
147 | 186 |
148 /////////////////////////////////////////////////////////////////////////////// | 187 /////////////////////////////////////////////////////////////////////////////// |
149 // | 188 // |
150 // ComponentsUI | 189 // ComponentsUI |
151 // | 190 // |
152 /////////////////////////////////////////////////////////////////////////////// | 191 /////////////////////////////////////////////////////////////////////////////// |
153 | 192 |
154 ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 193 ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
155 web_ui->AddMessageHandler(new ComponentsDOMHandler()); | 194 web_ui->AddMessageHandler(new ComponentsDOMHandler()); |
156 | 195 |
157 // Set up the chrome://components/ source. | 196 // Set up the chrome://components/ source. |
158 Profile* profile = Profile::FromWebUI(web_ui); | 197 Profile* profile = Profile::FromWebUI(web_ui); |
159 content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource(profile)); | 198 content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource(profile)); |
| 199 component_updater::ComponentUpdateService* cus = |
| 200 g_browser_process->component_updater(); |
| 201 cus->AddObserver(this); |
160 } | 202 } |
161 | 203 |
162 // static | 204 // static |
163 void ComponentsUI::OnDemandUpdate(const std::string& component_id) { | 205 void ComponentsUI::OnDemandUpdate(const std::string& component_id) { |
164 component_updater::ComponentUpdateService* cus = | 206 component_updater::ComponentUpdateService* cus = |
165 g_browser_process->component_updater(); | 207 g_browser_process->component_updater(); |
166 cus->OnDemandUpdate(component_id); | 208 cus->OnDemandUpdate(component_id); |
167 } | 209 } |
168 | 210 |
169 // static | 211 // static |
170 base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes( | 212 base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes( |
171 ui::ScaleFactor scale_factor) { | 213 ui::ScaleFactor scale_factor) { |
172 return ResourceBundle::GetSharedInstance(). | 214 return ResourceBundle::GetSharedInstance(). |
173 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor); | 215 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor); |
174 } | 216 } |
| 217 |
| 218 void ComponentsUI::ComponentEventToString(Events event, |
| 219 std::string* converted_event) { |
| 220 DCHECK(converted_event != NULL); |
| 221 switch (event) { |
| 222 case COMPONENT_UPDATER_STARTED: |
| 223 *converted_event = "UpdaterStarted"; |
| 224 return; |
| 225 case COMPONENT_UPDATER_SLEEPING: |
| 226 *converted_event = "UpdaterSleeping"; |
| 227 return; |
| 228 case COMPONENT_UPDATE_FOUND: |
| 229 *converted_event = "UpdateFound"; |
| 230 return; |
| 231 case COMPONENT_UPDATE_READY: |
| 232 *converted_event = "UpdateReady"; |
| 233 return; |
| 234 case COMPONENT_UPDATED: |
| 235 *converted_event = "ComponentUpdated"; |
| 236 return; |
| 237 case COMPONENT_NOT_UPDATED: |
| 238 *converted_event = "ComponentNotUpdated"; |
| 239 return; |
| 240 default: |
| 241 *converted_event = "Unknown"; |
| 242 return; |
| 243 } |
| 244 } |
| 245 |
| 246 void ComponentsUI::OnEvent(Events event, const std::string& id) { |
| 247 std::string converted_event; |
| 248 ComponentEventToString(event, &converted_event); |
| 249 base::DictionaryValue parameters; |
| 250 parameters.SetString("event", converted_event.c_str()); |
| 251 if (!id.empty()) |
| 252 parameters.SetString("id", id.c_str()); |
| 253 web_ui()->CallJavascriptFunction("onComponentEvent", parameters); |
| 254 } |
OLD | NEW |