Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / sync_file_system_internals / extension_statuses_handler.cc
blob6daeeaf43cbc99ac9cfbef4b82ade0bb27c48d72
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/sync_file_system_internals/extension_statuses_handler.h"
7 #include <map>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sync_file_system/sync_file_system_service.h"
16 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h"
17 #include "content/public/browser/web_ui.h"
18 #include "content/public/browser/web_ui_data_source.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/common/extension.h"
22 using sync_file_system::SyncFileSystemServiceFactory;
23 using sync_file_system::SyncServiceState;
25 namespace syncfs_internals {
27 namespace {
29 void ConvertExtensionStatusToDictionary(
30 const base::WeakPtr<ExtensionService>& extension_service,
31 const base::Callback<void(const base::ListValue&)>& callback,
32 const std::map<GURL, std::string>& status_map) {
33 if (!extension_service) {
34 callback.Run(base::ListValue());
35 return;
38 base::ListValue list;
39 for (std::map<GURL, std::string>::const_iterator itr = status_map.begin();
40 itr != status_map.end();
41 ++itr) {
42 std::string extension_id = itr->first.HostNoBrackets();
44 // Join with human readable extension name.
45 const extensions::Extension* extension =
46 extension_service->GetExtensionById(extension_id, true);
47 if (!extension)
48 continue;
50 base::DictionaryValue* dict = new base::DictionaryValue;
51 dict->SetString("extensionID", extension_id);
52 dict->SetString("extensionName", extension->name());
53 dict->SetString("status", itr->second);
54 list.Append(dict);
57 callback.Run(list);
60 } // namespace
62 ExtensionStatusesHandler::ExtensionStatusesHandler(Profile* profile)
63 : profile_(profile),
64 weak_ptr_factory_(this) {}
66 ExtensionStatusesHandler::~ExtensionStatusesHandler() {}
68 void ExtensionStatusesHandler::RegisterMessages() {
69 web_ui()->RegisterMessageCallback(
70 "getExtensionStatuses",
71 base::Bind(&ExtensionStatusesHandler::GetExtensionStatuses,
72 base::Unretained(this)));
75 // static
76 void ExtensionStatusesHandler::GetExtensionStatusesAsDictionary(
77 Profile* profile,
78 const base::Callback<void(const base::ListValue&)>& callback) {
79 DCHECK(profile);
81 sync_file_system::SyncFileSystemService* sync_service =
82 SyncFileSystemServiceFactory::GetForProfile(profile);
83 if (!sync_service) {
84 callback.Run(base::ListValue());
85 return;
88 ExtensionService* extension_service =
89 extensions::ExtensionSystem::Get(profile)->extension_service();
90 if (!extension_service) {
91 callback.Run(base::ListValue());
92 return;
95 sync_service->GetExtensionStatusMap(base::Bind(
96 &ConvertExtensionStatusToDictionary,
97 extension_service->AsWeakPtr(), callback));
100 void ExtensionStatusesHandler::GetExtensionStatuses(
101 const base::ListValue* args) {
102 DCHECK(args);
103 GetExtensionStatusesAsDictionary(
104 profile_,
105 base::Bind(&ExtensionStatusesHandler::DidGetExtensionStatuses,
106 weak_ptr_factory_.GetWeakPtr()));
109 void ExtensionStatusesHandler::DidGetExtensionStatuses(
110 const base::ListValue& list) {
111 web_ui()->CallJavascriptFunction("ExtensionStatuses.onGetExtensionStatuses",
112 list);
115 } // namespace syncfs_internals