1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/ash/drive_internals/drive_internals_ui.h"
#include <stddef.h>
#include <stdint.h>
#include <array>
#include <fstream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "ash/constants/ash_features.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/strings/pattern.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/strings/to_string.h"
#include "base/system/sys_info.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/file_system_util.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/file_util_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/services/file_util/public/cpp/zip_file_creator.h"
#include "chromeos/ash/components/drivefs/drivefs_pinning_manager.h"
#include "components/download/content/public/all_download_item_notifier.h"
#include "components/download/public/common/download_item.h"
#include "components/drive/drive_pref_names.h"
#include "components/drive/event_logger.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "google_apis/common/time_util.h"
#include "google_apis/drive/drive_api_parser.h"
#include "net/base/filename_util.h"
namespace ash {
namespace {
using base::FileEnumerator;
using base::FilePath;
using base::Value;
using content::BrowserThread;
using drive::DriveIntegrationService;
using drive::prefs::kDriveFsBulkPinningEnabled;
using drivefs::pinning::PinningManager;
constexpr char kKey[] = "key";
constexpr char kValue[] = "value";
constexpr char kClass[] = "class";
constexpr std::array kLogLevelName = {"verbose", "info", "warning", "error"};
size_t SeverityToLogLevelNameIndex(logging::LogSeverity severity) {
if (severity <= logging::LOGGING_VERBOSE) {
return 0;
}
if (severity == logging::LOGGING_INFO) {
return 1;
}
if (severity == logging::LOGGING_WARNING) {
return 2;
}
return 3;
}
size_t LogMarkToLogLevelNameIndex(char mark) {
switch (mark) {
case 'F':
return 0;
case 'I':
return 1;
case 'E':
default:
return 3;
}
}
template <typename T>
std::string ToString(const T x) {
return (std::ostringstream() << drivefs::pinning::NiceNum << x).str();
}
std::string ToString(const drive::FileError error) {
std::string s = drive::FileErrorToString(error);
if (const std::string_view prefix = "FILE_ERROR_"; s.starts_with(prefix)) {
s.erase(0, prefix.size());
}
return s;
}
template <typename T>
std::string ToPercent(const T num, const T total) {
if (num >= 0 && total > 0) {
return (std::ostringstream() << (100 * num / total) << "%").str();
}
return "🤔";
}
// Gets metadata of all files and directories in |root_path|
// recursively. Stores the result as a list of dictionaries like:
//
// [{ path: 'GCache/v1/tmp/<local_id>',
// size: 12345,
// is_directory: false,
// last_modified: '2005-08-09T09:57:00-08:00',
// },...]
//
// The list is sorted by the path.
std::pair<Value::List, Value::Dict> GetGCacheContents(
const FilePath& root_path) {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
// Use this map to sort the result list by the path.
std::map<FilePath, Value::Dict> files;
const int options = (FileEnumerator::FILES | FileEnumerator::DIRECTORIES |
FileEnumerator::SHOW_SYM_LINKS);
FileEnumerator enumerator(root_path, true /* recursive */, options);
int64_t total_size = 0;
for (FilePath current = enumerator.Next(); !current.empty();
current = enumerator.Next()) {
FileEnumerator::FileInfo info = enumerator.GetInfo();
int64_t size = info.GetSize();
const bool is_directory = info.IsDirectory();
const bool is_symbolic_link = base::IsLink(info.GetName());
const base::Time last_modified = info.GetLastModifiedTime();
auto entry =
Value::Dict()
.Set("path", current.value())
// Use double instead of integer for large files.
.Set("size", static_cast<double>(size))
.Set("is_directory", is_directory)
.Set("is_symbolic_link", is_symbolic_link)
.Set("last_modified",
google_apis::util::FormatTimeAsStringLocaltime(last_modified))
// Print lower 9 bits in octal format.
.Set("permission",
base::StringPrintf("%03o", info.stat().st_mode & 0x1ff));
files[current] = std::move(entry);
total_size += size;
}
std::pair<Value::List, Value::Dict> result;
// Convert |files| into response.
for (auto& it : files) {
result.first.Append(std::move(it.second));
}
result.second.Set("total_size", static_cast<double>(total_size));
return result;
}
// Appends {'key': key, 'value': value, 'class': clazz} dictionary to the
// |list|.
void AppendKeyValue(Value::List& list,
std::string key,
std::string value,
std::string clazz = std::string()) {
// clang-format off
auto dict =
Value::Dict()
.Set(kKey, std::move(key))
.Set(kValue, std::move(value));
// clang-format on
if (!clazz.empty()) {
dict.Set(kClass, std::move(clazz));
}
list.Append(std::move(dict));
}
ino_t GetInodeValue(const FilePath& path) {
struct stat file_stats;
if (stat(path.value().c_str(), &file_stats) != 0) {
return 0;
}
return file_stats.st_ino;
}
std::pair<ino_t, Value::List> GetServiceLogContents(const FilePath& log_path,
ino_t inode,
int from_line_number) {
Value::List result;
std::ifstream log(log_path.value());
if (log.good()) {
ino_t new_inode = GetInodeValue(log_path);
if (new_inode != inode) {
// Apparently log was recreated. Re-read the log.
from_line_number = 0;
inode = new_inode;
}
base::Time time;
constexpr char kTimestampPattern[] = R"(????-??-??T??:??:??.???Z? )";
const size_t pattern_length = strlen(kTimestampPattern);
std::string line;
int line_number = 0;
size_t severity_index = 0;
while (log.good()) {
std::getline(log, line);
if (line.empty() || ++line_number <= from_line_number) {
continue;
}
std::string_view log_line = line;
if (base::MatchPattern(log_line.substr(0, pattern_length),
kTimestampPattern) &&
google_apis::util::GetTimeFromString(
log_line.substr(0, pattern_length - 2), &time)) {
severity_index = LogMarkToLogLevelNameIndex(line[pattern_length - 2]);
line = line.substr(pattern_length);
}
const char* const severity = kLogLevelName[severity_index];
AppendKeyValue(result,
google_apis::util::FormatTimeAsStringLocaltime(time),
base::StrCat({"[", severity, "] ", line}),
base::StrCat({"log-", severity}));
}
}
return {inode, std::move(result)};
}
bool GetDeveloperMode() {
std::string output;
if (!base::GetAppOutput({"/usr/bin/crossystem", "cros_debug"}, &output)) {
return false;
}
return output == "1";
}
class DriveInternalsWebUIHandler;
void ZipLogs(Profile* profile,
base::WeakPtr<DriveInternalsWebUIHandler> drive_internals);
// Class to handle messages from chrome://drive-internals.
class DriveInternalsWebUIHandler : public content::WebUIMessageHandler,
DriveIntegrationService::Observer {
public:
DriveInternalsWebUIHandler() = default;
void DownloadLogsZip(const FilePath& path) {
web_ui()->GetWebContents()->GetController().LoadURL(
net::FilePathToFileURL(path), {}, {}, {});
}
void OnZipDone() { MaybeCallJavascript("onZipDone", Value()); }
private:
void MaybeCallJavascript(const std::string& function,
Value data1,
Value data2 = {}) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (IsJavascriptAllowed()) {
CallJavascriptFunction(function, std::move(data1), std::move(data2));
}
}
// Hide or show a section of the page.
void SetSectionEnabled(const std::string& section, bool enable) {
MaybeCallJavascript("setSectionEnabled", Value(section), Value(enable));
}
// WebUIMessageHandler override.
void RegisterMessages() override {
web_ui()->RegisterMessageCallback(
"pageLoaded",
base::BindRepeating(&DriveInternalsWebUIHandler::OnPageLoaded,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"periodicUpdate",
base::BindRepeating(&DriveInternalsWebUIHandler::OnPeriodicUpdate,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"setBulkPinningVisible",
base::BindRepeating(&DriveInternalsWebUIHandler::SetBulkPinningVisible,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"setVerboseLoggingEnabled",
base::BindRepeating(
&DriveInternalsWebUIHandler::SetVerboseLoggingEnabled,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"setMirroringEnabled",
base::BindRepeating(&DriveInternalsWebUIHandler::SetMirroringEnabled,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"addSyncPath",
base::BindRepeating(&DriveInternalsWebUIHandler::ToggleSyncPath,
weak_ptr_factory_.GetWeakPtr(),
drivefs::mojom::MirrorPathStatus::kStart));
web_ui()->RegisterMessageCallback(
"removeSyncPath",
base::BindRepeating(&DriveInternalsWebUIHandler::ToggleSyncPath,
weak_ptr_factory_.GetWeakPtr(),
drivefs::mojom::MirrorPathStatus::kStop));
web_ui()->RegisterMessageCallback(
"enableTracing",
base::BindRepeating(&DriveInternalsWebUIHandler::SetTracingEnabled,
weak_ptr_factory_.GetWeakPtr(), true));
web_ui()->RegisterMessageCallback(
"disableTracing",
base::BindRepeating(&DriveInternalsWebUIHandler::SetTracingEnabled,
weak_ptr_factory_.GetWeakPtr(), false));
web_ui()->RegisterMessageCallback(
"restartDrive",
base::BindRepeating(&DriveInternalsWebUIHandler::RestartDrive,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"resetDriveFileSystem",
base::BindRepeating(&DriveInternalsWebUIHandler::ResetDriveFileSystem,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"zipLogs",
base::BindRepeating(&DriveInternalsWebUIHandler::ZipDriveFsLogs,
weak_ptr_factory_.GetWeakPtr()));
}
void RegisterDeveloperMessages() {
CHECK(developer_mode_);
web_ui()->RegisterMessageCallback(
"setStartupArguments",
base::BindRepeating(&DriveInternalsWebUIHandler::SetStartupArguments,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"enableNetworking",
base::BindRepeating(&DriveInternalsWebUIHandler::SetNetworkingEnabled,
weak_ptr_factory_.GetWeakPtr(), true));
web_ui()->RegisterMessageCallback(
"disableNetworking",
base::BindRepeating(&DriveInternalsWebUIHandler::SetNetworkingEnabled,
weak_ptr_factory_.GetWeakPtr(), false));
web_ui()->RegisterMessageCallback(
"enableForcePauseSyncing",
base::BindRepeating(&DriveInternalsWebUIHandler::ForcePauseSyncing,
weak_ptr_factory_.GetWeakPtr(), true));
web_ui()->RegisterMessageCallback(
"disableForcePauseSyncing",
base::BindRepeating(&DriveInternalsWebUIHandler::ForcePauseSyncing,
weak_ptr_factory_.GetWeakPtr(), false));
web_ui()->RegisterMessageCallback(
"dumpAccountSettings",
base::BindRepeating(&DriveInternalsWebUIHandler::DumpAccountSettings,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"loadAccountSettings",
base::BindRepeating(&DriveInternalsWebUIHandler::LoadAccountSettings,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"setBulkPinningEnabled",
base::BindRepeating(&DriveInternalsWebUIHandler::SetBulkPinningEnabled,
weak_ptr_factory_.GetWeakPtr()));
}
// Called when the page is first loaded.
void OnPageLoaded(const Value::List& args) {
AllowJavascript();
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
UpdateDriveRelatedPreferencesSection();
UpdateGCacheContentsSection();
UpdatePathConfigurationsSection();
UpdateConnectionStatusSection();
UpdateAboutResourceSection();
UpdateDeltaUpdateStatusSection();
UpdateCacheContentsSection();
UpdateInFlightOperationsSection();
UpdateDriveDebugSection();
UpdateMirrorSyncSection();
// When the drive-internals page is reloaded by the reload key, the page
// content is recreated, but this WebUI object is not (instead, OnPageLoaded
// is called again). In that case, we have to forget the last sent ID here,
// and resent whole the logs to the page.
last_sent_event_id_ = -1;
UpdateEventLogSection();
last_sent_line_number_ = 0;
service_log_file_inode_ = 0;
UpdateServiceLogSection();
}
// Called when the page requests periodic update.
void OnPeriodicUpdate(const Value::List& args) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
UpdateEventLogSection();
UpdateServiceLogSection();
UpdateInFlightOperationsSection();
}
//
// Updates respective sections.
//
void UpdateConnectionStatusSection() {
SetSectionEnabled("connection-status-section", true);
Value::Dict connection_status;
connection_status.Set(
"status", ToString(drive::util::GetDriveConnectionStatus(profile())));
MaybeCallJavascript("updateConnectionStatus",
Value(std::move(connection_status)));
}
void UpdateAboutResourceSection() {
// TODO(crbug.com/41421123): Maybe worth implementing.
SetSectionEnabled("account-information-section", false);
}
void UpdateDeltaUpdateStatusSection() {
// TODO(crbug.com/41421123): Maybe worth implementing.
SetSectionEnabled("delta-update-status-section", false);
}
void UpdateInFlightOperationsSection() {
// TODO(crbug.com/41421123): Maybe worth implementing.
SetSectionEnabled("in-flight-operations-section", false);
}
void UpdatePathConfigurationsSection() {
SetSectionEnabled("path-configurations-section", true);
Value::List paths;
AppendKeyValue(paths, "Downloads",
file_manager::util::GetDownloadsFolderForProfile(profile())
.AsUTF8Unsafe());
if (DriveIntegrationService* const service = GetIntegrationService()) {
AppendKeyValue(paths, "Drive",
service->GetMountPointPath().AsUTF8Unsafe());
}
const char* const kPathPreferences[] = {
prefs::kSelectFileLastDirectory,
prefs::kSaveFileDefaultDirectory,
prefs::kDownloadDefaultDirectory,
};
for (const char* key : kPathPreferences) {
AppendKeyValue(paths, key, GetPrefs()->GetFilePath(key).AsUTF8Unsafe());
}
MaybeCallJavascript("updatePathConfigurations", Value(std::move(paths)));
}
void UpdateDriveDebugSection() {
SetSectionEnabled("drive-debug", true);
const PrefService* const prefs = GetPrefs();
MaybeCallJavascript(
"updateBulkPinningVisible",
Value(prefs->GetBoolean(drive::prefs::kDriveFsBulkPinningVisible)));
MaybeCallJavascript(
"updateVerboseLogging",
Value(prefs->GetBoolean(drive::prefs::kDriveFsEnableVerboseLogging)));
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(GetDeveloperMode),
base::BindOnce(&DriveInternalsWebUIHandler::OnGetDeveloperMode,
weak_ptr_factory_.GetWeakPtr()));
// Propagate the amount of local free space in bytes.
FilePath home_path;
if (base::PathService::Get(base::DIR_HOME, &home_path)) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(&base::SysInfo::AmountOfFreeDiskSpace, home_path),
base::BindOnce(&DriveInternalsWebUIHandler::OnGetFreeDiskSpace,
weak_ptr_factory_.GetWeakPtr()));
} else {
LOG(ERROR) << "Home directory not found";
}
}
void UpdateMirrorSyncSection() {
if (!features::IsDriveFsMirroringEnabled()) {
SetSectionEnabled("mirror-sync-section", false);
return;
}
SetSectionEnabled("mirror-sync-section", true);
bool mirroring_enabled =
GetPrefs()->GetBoolean(drive::prefs::kDriveFsEnableMirrorSync);
MaybeCallJavascript("updateMirroring", Value(mirroring_enabled));
SetSectionEnabled("mirror-sync-paths", mirroring_enabled);
SetSectionEnabled("mirror-path-form", mirroring_enabled);
if (!mirroring_enabled) {
return;
}
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
service->GetSyncingPaths(
base::BindOnce(&DriveInternalsWebUIHandler::OnGetSyncingPaths,
weak_ptr_factory_.GetWeakPtr()));
}
void OnGetSyncingPaths(drive::FileError status,
const std::vector<FilePath>& paths) {
if (status != drive::FILE_ERROR_OK) {
LOG(ERROR) << "Error retrieving syncing paths: " << status;
return;
}
for (const FilePath& sync_path : paths) {
MaybeCallJavascript("onAddSyncPath", Value(sync_path.value()),
Value(ToString(drive::FILE_ERROR_OK)));
}
}
void ToggleSyncPath(drivefs::mojom::MirrorPathStatus status,
const Value::List& args) {
if (!features::IsDriveFsMirroringEnabled()) {
return;
}
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
if (args.size() == 1 && args[0].is_string()) {
const FilePath sync_path(args[0].GetString());
auto callback =
base::BindOnce((status == drivefs::mojom::MirrorPathStatus::kStart)
? &DriveInternalsWebUIHandler::OnAddSyncPath
: &DriveInternalsWebUIHandler::OnRemoveSyncPath,
weak_ptr_factory_.GetWeakPtr(), sync_path);
service->ToggleSyncForPath(sync_path, status, std::move(callback));
}
}
void OnAddSyncPath(const FilePath& sync_path, drive::FileError status) {
MaybeCallJavascript("onAddSyncPath", Value(sync_path.value()),
Value(ToString(status)));
}
void OnRemoveSyncPath(const FilePath& sync_path, drive::FileError status) {
MaybeCallJavascript("onRemoveSyncPath", Value(sync_path.value()),
Value(ToString(status)));
}
void UpdateBulkPinningDeveloperSection() {
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
const bool enabled = drive::util::IsDriveFsBulkPinningAvailable(profile());
SetSectionEnabled("bulk-pinning-section", enabled);
if (!enabled) {
return;
}
Observe(service);
MaybeCallJavascript(
"updateBulkPinning",
Value(GetPrefs()->GetBoolean(kDriveFsBulkPinningEnabled)));
if (PinningManager* const manager = service->GetPinningManager()) {
OnBulkPinProgress(manager->GetProgress());
}
}
void OnBulkPinProgress(const drivefs::pinning::Progress& progress) override {
using drivefs::pinning::HumanReadableSize;
auto dict =
Value::Dict()
.Set("enabled", GetPrefs()->GetBoolean(kDriveFsBulkPinningEnabled))
.Set("stage", drivefs::pinning::ToString(progress.stage))
.Set("free_space", ToString(HumanReadableSize(progress.free_space)))
.Set("required_space",
ToString(HumanReadableSize(progress.required_space)))
.Set("bytes_to_pin",
ToString(HumanReadableSize(progress.bytes_to_pin)))
.Set("pinned_bytes",
ToString(HumanReadableSize(progress.pinned_bytes)))
.Set("pinned_bytes_percent",
ToPercent(progress.pinned_bytes, progress.bytes_to_pin))
.Set("files_to_pin", ToString(progress.files_to_pin))
.Set("pinned_files", ToString(progress.pinned_files))
.Set("pinned_files_percent",
ToPercent(progress.pinned_files, progress.files_to_pin))
.Set("failed_files", ToString(progress.failed_files))
.Set("syncing_files", ToString(progress.syncing_files))
.Set("skipped_items", ToString(progress.skipped_items))
.Set("listed_items", ToString(progress.listed_items))
.Set("listed_dirs", ToString(progress.listed_dirs))
.Set("listed_files", ToString(progress.listed_files))
.Set("listed_docs", ToString(progress.listed_docs))
.Set("listed_shortcuts", ToString(progress.listed_shortcuts))
.Set("active_queries", ToString(progress.active_queries))
.Set("max_active_queries", ToString(progress.max_active_queries))
.Set("time_spent_listing_items",
drivefs::pinning::ToString(progress.time_spent_listing_items))
.Set("time_spent_pinning_files",
drivefs::pinning::ToString(progress.time_spent_pinning_files))
.Set("remaining_time",
drivefs::pinning::ToString(progress.remaining_time));
MaybeCallJavascript("onBulkPinningProgress", Value(std::move(dict)));
}
// Called when GetDeveloperMode() is complete.
void OnGetDeveloperMode(bool enabled) {
developer_mode_ = enabled;
if (!enabled) {
return;
}
RegisterDeveloperMessages();
// Get the startup arguments.
if (DriveIntegrationService* const service = GetIntegrationService()) {
service->GetStartupArguments(
base::BindOnce(&DriveInternalsWebUIHandler::OnGetStartupArguments,
weak_ptr_factory_.GetWeakPtr()));
}
}
// Called when GetStartupArguments() is complete.
void OnGetStartupArguments(const std::string& arguments) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(developer_mode_);
MaybeCallJavascript("updateStartupArguments", Value(arguments));
SetSectionEnabled("developer-mode-controls", true);
UpdateBulkPinningDeveloperSection();
}
// Called when AmountOfFreeDiskSpace() is complete.
void OnGetFreeDiskSpace(int64_t free_space) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
Value::Dict local_storage_summary;
local_storage_summary.Set("free_space", static_cast<double>(free_space));
MaybeCallJavascript("updateLocalStorageUsage",
Value(std::move(local_storage_summary)));
}
void UpdateDriveRelatedPreferencesSection() {
SetSectionEnabled("drive-related-preferences-section", true);
const char* const kDriveRelatedPreferences[] = {
drive::prefs::kDisableDrive,
drive::prefs::kDisableDriveOverCellular,
drive::prefs::kDriveFsWasLaunchedAtLeastOnce,
drive::prefs::kDriveFsPinnedMigrated,
drive::prefs::kDriveFsEnableVerboseLogging,
drive::prefs::kDriveFsEnableMirrorSync,
};
PrefService* const prefs = GetPrefs();
Value::List preferences;
for (const char* key : kDriveRelatedPreferences) {
// As of now, all preferences are boolean.
AppendKeyValue(preferences, key, base::ToString(prefs->GetBoolean(key)));
}
MaybeCallJavascript("updateDriveRelatedPreferences",
Value(std::move(preferences)));
}
void UpdateEventLogSection() {
SetSectionEnabled("event-log-section", true);
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
const std::vector<drive::EventLogger::Event> log =
service->GetLogger()->GetHistory();
Value::List list;
for (const drive::EventLogger::Event& event : log) {
// Skip events which were already sent.
if (event.id <= last_sent_event_id_) {
continue;
}
const char* const severity =
kLogLevelName[SeverityToLogLevelNameIndex(event.severity)];
AppendKeyValue(list,
google_apis::util::FormatTimeAsStringLocaltime(event.when),
base::StrCat({"[", severity, "] ", event.what}),
base::StrCat({"log-", severity}));
last_sent_event_id_ = event.id;
}
if (!list.empty()) {
MaybeCallJavascript("updateEventLog", Value(std::move(list)));
}
}
void UpdateServiceLogSection() {
SetSectionEnabled("service-log-section", true);
if (service_log_file_is_processing_) {
return;
}
service_log_file_is_processing_ = true;
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
FilePath log_path = service->GetDriveFsLogPath();
if (log_path.empty()) {
return;
}
MaybeCallJavascript(
"updateOtherServiceLogsUrl",
Value(net::FilePathToFileURL(log_path.DirName()).spec()));
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(&GetServiceLogContents, log_path,
service_log_file_inode_, last_sent_line_number_),
base::BindOnce(&DriveInternalsWebUIHandler::OnServiceLogRead,
weak_ptr_factory_.GetWeakPtr()));
}
// Called when service logs are read.
void OnServiceLogRead(std::pair<ino_t, Value::List> response) {
if (service_log_file_inode_ != response.first) {
service_log_file_inode_ = response.first;
last_sent_line_number_ = 0;
}
if (!response.second.empty()) {
last_sent_line_number_ += response.second.size();
MaybeCallJavascript("updateServiceLog",
Value(std::move(response.second)));
}
service_log_file_is_processing_ = false;
}
void UpdateCacheContentsSection() {
// TODO(crbug.com/41421123): Maybe worth implementing.
SetSectionEnabled("cache-contents-section", false);
}
void UpdateGCacheContentsSection() {
SetSectionEnabled("gcache-contents-section", true);
const FilePath root_path =
drive::util::GetCacheRootPath(profile()).DirName();
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(&GetGCacheContents, root_path),
base::BindOnce(&DriveInternalsWebUIHandler::OnGetGCacheContents,
weak_ptr_factory_.GetWeakPtr()));
}
// Called when GetGCacheContents() is complete.
void OnGetGCacheContents(std::pair<Value::List, Value::Dict> response) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
MaybeCallJavascript("updateGCacheContents",
Value(std::move(response.first)),
Value(std::move(response.second)));
}
// Called when the "Verbose Logging" checkbox on the page is changed.
void SetVerboseLoggingEnabled(const Value::List& args) {
AllowJavascript();
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
if (args.size() == 1 && args[0].is_bool()) {
bool enabled = args[0].GetBool();
GetPrefs()->SetBoolean(drive::prefs::kDriveFsEnableVerboseLogging,
enabled);
RestartDrive(Value::List());
}
}
void SetMirroringEnabled(const Value::List& args) {
AllowJavascript();
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
return;
}
if (args.size() == 1 && args[0].is_bool()) {
bool enabled = args[0].GetBool();
GetPrefs()->SetBoolean(drive::prefs::kDriveFsEnableMirrorSync, enabled);
SetSectionEnabled("mirror-sync-paths", enabled);
SetSectionEnabled("mirror-path-form", enabled);
}
}
// Called when the "bulk-pinning-visible" checkbox on the page is changed.
void SetBulkPinningVisible(const Value::List& args) {
AllowJavascript();
if (args.size() != 1 || !args[0].is_bool()) {
LOG(ERROR) << "Args in not a bool";
return;
}
const bool b = args[0].GetBool();
VLOG(1) << "Set pref " << drive::prefs::kDriveFsBulkPinningVisible << " to "
<< b;
GetPrefs()->SetBoolean(drive::prefs::kDriveFsBulkPinningVisible, b);
}
void SetBulkPinningEnabled(const Value::List& args) {
AllowJavascript();
if (args.size() != 1 || !args[0].is_bool()) {
LOG(ERROR) << "Args in not a bool";
return;
}
GetPrefs()->SetBoolean(kDriveFsBulkPinningEnabled, args[0].GetBool());
UpdateBulkPinningDeveloperSection();
drivefs::pinning::RecordBulkPinningEnabledSource(
drivefs::pinning::BulkPinningEnabledSource::kDriveInternal);
}
// Called when the "Startup Arguments" field on the page is submitted.
void SetStartupArguments(const Value::List& args) {
AllowJavascript();
CHECK(developer_mode_);
if (args.size() < 1 || !args[0].is_string()) {
OnSetStartupArguments(false);
return;
}
DriveIntegrationService* const service = GetIntegrationService();
if (!service) {
OnSetStartupArguments(false);
return;
}
service->SetStartupArguments(
args[0].GetString(),
base::BindOnce(&DriveInternalsWebUIHandler::OnSetStartupArguments,
weak_ptr_factory_.GetWeakPtr()));
}
void OnSetStartupArguments(bool success) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(developer_mode_);
if (success) {
RestartDrive(Value::List());
}
MaybeCallJavascript("updateStartupArgumentsStatus", Value(success));
}
void SetTracingEnabled(bool enabled, const Value::List& args) {
AllowJavascript();
if (DriveIntegrationService* const service = GetIntegrationService()) {
service->SetTracingEnabled(enabled);
}
}
void SetNetworkingEnabled(bool enabled, const Value::List& args) {
AllowJavascript();
CHECK(developer_mode_);
if (DriveIntegrationService* const service = GetIntegrationService()) {
service->SetNetworkingEnabled(enabled);
}
}
void ForcePauseSyncing(bool enabled, const Value::List& args) {
AllowJavascript();
CHECK(developer_mode_);
if (DriveIntegrationService* const service = GetIntegrationService()) {
service->ForcePauseSyncing(enabled);
}
}
void DumpAccountSettings(const Value::List& args) {
AllowJavascript();
CHECK(developer_mode_);
if (DriveIntegrationService* const service = GetIntegrationService()) {
service->DumpAccountSettings();
}
}
void LoadAccountSettings(const Value::List& args) {
AllowJavascript();
CHECK(developer_mode_);
if (DriveIntegrationService* const service = GetIntegrationService()) {
service->LoadAccountSettings();
}
}
// Called when the "Restart Drive" button on the page is pressed.
void RestartDrive(const Value::List& args) {
AllowJavascript();
if (DriveIntegrationService* const service = GetIntegrationService()) {
service->RestartDrive();
}
}
// Called when the corresponding button on the page is pressed.
void ResetDriveFileSystem(const Value::List& args) {
AllowJavascript();
if (DriveIntegrationService* const service = GetIntegrationService()) {
service->ClearCacheAndRemountFileSystem(
base::BindOnce(&DriveInternalsWebUIHandler::ResetFinished,
weak_ptr_factory_.GetWeakPtr()));
}
}
void ZipDriveFsLogs(const Value::List& args) {
AllowJavascript();
DriveIntegrationService* const service = GetIntegrationService();
if (!service || service->GetDriveFsLogPath().empty()) {
return;
}
ZipLogs(profile(), weak_ptr_factory_.GetWeakPtr());
}
// Called after file system reset for ResetDriveFileSystem is done.
void ResetFinished(bool success) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
MaybeCallJavascript("updateResetStatus", Value(success));
}
Profile* profile() { return Profile::FromWebUI(web_ui()); }
PrefService* GetPrefs() { return profile()->GetPrefs(); }
// Returns a DriveIntegrationService, if any.
// May return nullptr in guest/incognito mode.
DriveIntegrationService* GetIntegrationService() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DriveIntegrationService* const service =
drive::DriveIntegrationServiceFactory::FindForProfile(profile());
if (!service) {
LOG(ERROR) << "No DriveFS integration service";
return nullptr;
}
if (!service->is_enabled()) {
LOG(ERROR) << "DriveFS integration service is disabled";
return nullptr;
}
return service;
}
// The last event sent to the JavaScript side.
int last_sent_event_id_ = -1;
// The last line of service log sent to the JS side.
int last_sent_line_number_;
// The inode of the log file.
ino_t service_log_file_inode_;
// Service log file is being parsed.
bool service_log_file_is_processing_ = false;
// Whether developer mode is enabled for debug commands.
bool developer_mode_ = false;
base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_{this};
};
class LogsZipper : public download::AllDownloadItemNotifier::Observer {
public:
LogsZipper(Profile* profile,
base::WeakPtr<DriveInternalsWebUIHandler> drive_internals)
: profile_(profile),
logs_directory_(
drive::DriveIntegrationServiceFactory::FindForProfile(profile)
->GetDriveFsLogPath()
.DirName()),
zip_path_(logs_directory_.AppendASCII(kLogsZipName)),
drive_internals_(std::move(drive_internals)) {}
LogsZipper(const LogsZipper&) = delete;
LogsZipper& operator=(const LogsZipper&) = delete;
void Start() {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(&LogsZipper::EnumerateLogFiles, logs_directory_,
zip_path_),
base::BindOnce(&LogsZipper::ZipLogFiles, base::Unretained(this)));
}
private:
static constexpr char kLogsZipName[] = "drivefs_logs.zip";
void ZipLogFiles(const std::vector<FilePath>& files) {
const scoped_refptr<ZipFileCreator> creator =
base::MakeRefCounted<ZipFileCreator>(logs_directory_, files, zip_path_);
creator->SetCompletionCallback(base::BindOnce(
&LogsZipper::OnZipDone, base::Unretained(this), creator));
creator->Start(LaunchFileUtilService());
}
static std::vector<FilePath> EnumerateLogFiles(FilePath logs_path,
FilePath zip_path) {
// Note: this may be racy if multiple attempts to export logs are run
// concurrently, but it's a debug page and it requires explicit action to
// cause problems.
base::DeleteFile(zip_path);
std::vector<FilePath> log_files;
FileEnumerator enumerator(logs_path, false /* recursive */,
FileEnumerator::FILES);
for (FilePath current = enumerator.Next(); !current.empty();
current = enumerator.Next()) {
if (!current.MatchesExtension(".zip")) {
log_files.push_back(current.BaseName());
}
}
return log_files;
}
void OnZipDone(const scoped_refptr<ZipFileCreator> creator) {
DCHECK(creator);
if (!drive_internals_ || creator->GetResult() != ZipFileCreator::kSuccess) {
CleanUp();
return;
}
download_notifier_ = std::make_unique<download::AllDownloadItemNotifier>(
profile_->GetDownloadManager(), this);
drive_internals_->DownloadLogsZip(zip_path_);
}
void OnDownloadUpdated(content::DownloadManager* manager,
download::DownloadItem* item) override {
if (item->GetState() == download::DownloadItem::IN_PROGRESS ||
item->GetURL() != net::FilePathToFileURL(zip_path_)) {
return;
}
CleanUp();
}
void CleanUp() {
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::GetDeleteFileCallback(zip_path_));
download_notifier_.reset();
if (drive_internals_) {
drive_internals_->OnZipDone();
}
base::SequencedTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE, this);
}
const raw_ptr<Profile> profile_;
const FilePath logs_directory_;
const FilePath zip_path_;
const base::WeakPtr<DriveInternalsWebUIHandler> drive_internals_;
std::unique_ptr<download::AllDownloadItemNotifier> download_notifier_;
};
constexpr char LogsZipper::kLogsZipName[];
void ZipLogs(Profile* profile,
base::WeakPtr<DriveInternalsWebUIHandler> drive_internals) {
auto* logs_zipper = new LogsZipper(profile, std::move(drive_internals));
logs_zipper->Start();
}
} // namespace
DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui)
: WebUIController(web_ui) {
web_ui->AddMessageHandler(std::make_unique<DriveInternalsWebUIHandler>());
content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
Profile::FromWebUI(web_ui), chrome::kChromeUIDriveInternalsHost);
source->AddResourcePath("drive_internals.css", IDR_DRIVE_INTERNALS_CSS);
source->AddResourcePath("drive_internals.js", IDR_DRIVE_INTERNALS_JS);
source->SetDefaultResource(IDR_DRIVE_INTERNALS_HTML);
}
} // namespace ash
|