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"
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
{
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());
39 for (std::map
<GURL
, std::string
>::const_iterator itr
= status_map
.begin();
40 itr
!= status_map
.end();
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);
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
);
62 ExtensionStatusesHandler::ExtensionStatusesHandler(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)));
76 void ExtensionStatusesHandler::GetExtensionStatusesAsDictionary(
78 const base::Callback
<void(const base::ListValue
&)>& callback
) {
81 sync_file_system::SyncFileSystemService
* sync_service
=
82 SyncFileSystemServiceFactory::GetForProfile(profile
);
84 callback
.Run(base::ListValue());
88 ExtensionService
* extension_service
=
89 extensions::ExtensionSystem::Get(profile
)->extension_service();
90 if (!extension_service
) {
91 callback
.Run(base::ListValue());
95 sync_service
->GetExtensionStatusMap(base::Bind(
96 &ConvertExtensionStatusToDictionary
,
97 extension_service
->AsWeakPtr(), callback
));
100 void ExtensionStatusesHandler::GetExtensionStatuses(
101 const base::ListValue
* args
) {
103 GetExtensionStatusesAsDictionary(
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",
115 } // namespace syncfs_internals