agent_util.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2019 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.
  4. #include "components/ui_devtools/agent_util.h"
  5. #include "base/command_line.h"
  6. #include "base/files/file_path.h"
  7. #include "base/files/file_util.h"
  8. #include "base/logging.h"
  9. #include "base/path_service.h"
  10. #include "base/run_loop.h"
  11. #include "base/task/task_traits.h"
  12. #include "base/task/thread_pool.h"
  13. namespace ui_devtools {
  14. namespace {
  15. void OnSourceFile(base::OnceClosure quit_closure,
  16. bool* return_value,
  17. bool read_file_result) {
  18. *return_value = read_file_result;
  19. std::move(quit_closure).Run();
  20. }
  21. } // namespace
  22. const char kChromiumCodeSearchURL[] = "https://cs.chromium.org/";
  23. const char kChromiumCodeSearchSrcURL[] =
  24. "https://cs.chromium.org/chromium/src/";
  25. bool GetSourceCode(std::string path, std::string* source_code) {
  26. base::FilePath src_dir;
  27. base::PathService::Get(base::DIR_SOURCE_ROOT, &src_dir);
  28. src_dir = src_dir.AppendASCII(path);
  29. base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
  30. bool return_value;
  31. base::ThreadPool::PostTaskAndReplyWithResult(
  32. FROM_HERE,
  33. {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
  34. base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
  35. base::BindOnce(&base::ReadFileToString, src_dir, source_code),
  36. base::BindOnce(&OnSourceFile, run_loop.QuitClosure(), &return_value));
  37. run_loop.Run();
  38. if (!return_value)
  39. DLOG(ERROR) << "Could not get source file of " << src_dir.value() << ".";
  40. return return_value;
  41. }
  42. } // namespace ui_devtools