1 // Copyright 2020 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 5 #include "chrome/browser/web_applications/chrome_pwa_launcher/last_browser_file_util.h" 6 7 #include <string> 8 9 #include "base/base_paths.h" 10 #include "base/files/file_util.h" 11 #include "base/path_service.h" 12 #include "base/strings/string_util.h" 13 14 namespace web_app { 15 16 const base::FilePath::StringPieceType kLastBrowserFilename = 17 FILE_PATH_LITERAL("Last Browser"); 18 ReadChromePathFromLastBrowserFile(const base::FilePath & last_browser_file)19base::FilePath ReadChromePathFromLastBrowserFile( 20 const base::FilePath& last_browser_file) { 21 std::string last_browser_file_data; 22 if (!base::ReadFileToStringWithMaxSize( 23 last_browser_file, &last_browser_file_data, 24 MAX_PATH * sizeof(base::FilePath::CharType))) { 25 return base::FilePath(); 26 } 27 28 base::FilePath::StringType chrome_path( 29 reinterpret_cast<const base::FilePath::CharType*>( 30 last_browser_file_data.data()), 31 last_browser_file_data.size() / sizeof(base::FilePath::CharType)); 32 const base::FilePath::StringPieceType chrome_path_trimmed = 33 base::TrimString(chrome_path, FILE_PATH_LITERAL(" \n"), base::TRIM_ALL); 34 return base::FilePath(chrome_path_trimmed); 35 } 36 WriteChromePathToLastBrowserFile(const base::FilePath & user_data_dir)37void WriteChromePathToLastBrowserFile(const base::FilePath& user_data_dir) { 38 DCHECK(!user_data_dir.empty()); 39 base::FilePath chrome_path; 40 if (!base::PathService::Get(base::FILE_EXE, &chrome_path)) 41 return; 42 43 const base::FilePath::StringType& chrome_path_str = chrome_path.value(); 44 DCHECK(!chrome_path_str.empty()); 45 base::WriteFile(user_data_dir.Append(kLastBrowserFilename), 46 reinterpret_cast<const char*>(chrome_path_str.data()), 47 chrome_path_str.size() * sizeof(base::FilePath::CharType)); 48 } 49 GetLastBrowserFileFromWebAppDir(const base::FilePath & web_app_dir)50base::FilePath GetLastBrowserFileFromWebAppDir( 51 const base::FilePath& web_app_dir) { 52 return web_app_dir.DirName().DirName().DirName().Append(kLastBrowserFilename); 53 } 54 55 } // namespace web_app 56