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