Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / sync_file_system_internals / file_metadata_handler.cc
blobd4cfb9d4619d9afc11445e6be85030da46358369
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/file_metadata_handler.h"
7 #include <map>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/values.h"
12 #include "chrome/browser/extensions/api/sync_file_system/sync_file_system_api_helpers.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/sync_file_system/sync_file_system_service.h"
15 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h"
16 #include "chrome/browser/ui/webui/sync_file_system_internals/extension_statuses_handler.h"
17 #include "content/public/browser/web_ui.h"
18 #include "content/public/browser/web_ui_data_source.h"
19 #include "extensions/common/extension.h"
21 using sync_file_system::RemoteFileSyncService;
22 using sync_file_system::SyncFileSystemServiceFactory;
23 using sync_file_system::SyncServiceState;
25 namespace syncfs_internals {
27 FileMetadataHandler::FileMetadataHandler(Profile* profile)
28 : profile_(profile),
29 weak_factory_(this) {}
31 FileMetadataHandler::~FileMetadataHandler() {}
33 void FileMetadataHandler::RegisterMessages() {
34 web_ui()->RegisterMessageCallback(
35 "getExtensions",
36 base::Bind(&FileMetadataHandler::GetExtensions,
37 base::Unretained(this)));
38 web_ui()->RegisterMessageCallback(
39 "getFileMetadata",
40 base::Bind(&FileMetadataHandler::GetFileMetadata,
41 base::Unretained(this)));
44 void FileMetadataHandler::GetFileMetadata(
45 const base::ListValue* args) {
46 std::string extension_id;
47 if (!args->GetString(0, &extension_id) || extension_id.empty()) {
48 LOG(WARNING) << "GetFileMetadata() Extension ID wasn't given";
49 return;
52 // Extension ID from JS is just the host. Need to reformat it to chrome
53 // extension type GURL.
54 const GURL origin = extensions::Extension::GetBaseURLFromExtensionId(
55 extension_id);
57 // Get all metadata for the one specific origin.
58 sync_file_system::SyncFileSystemService* sync_service =
59 SyncFileSystemServiceFactory::GetForProfile(profile_);
60 if (!sync_service)
61 return;
62 sync_service->DumpFiles(origin,
63 base::Bind(&FileMetadataHandler::DidGetFileMetadata,
64 weak_factory_.GetWeakPtr()));
67 void FileMetadataHandler::GetExtensions(const base::ListValue* args) {
68 DCHECK(args);
69 ExtensionStatusesHandler::GetExtensionStatusesAsDictionary(
70 profile_,
71 base::Bind(&FileMetadataHandler::DidGetExtensions,
72 weak_factory_.GetWeakPtr()));
75 void FileMetadataHandler::DidGetExtensions(const base::ListValue& list) {
76 web_ui()->CallJavascriptFunction("FileMetadata.onGetExtensions", list);
79 void FileMetadataHandler::DidGetFileMetadata(const base::ListValue& files) {
80 web_ui()->CallJavascriptFunction("FileMetadata.onGetFileMetadata", files);
83 } // namespace syncfs_internals