Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(543)

Side by Side Diff: base/file_util_posix.cc

Issue 200473002: Move all callers of GetHomeDir() to PathService::Get(base::DIR_HOME). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <libgen.h> 10 #include <libgen.h>
(...skipping 13 matching lines...) Expand all
24 #if defined(OS_MACOSX) 24 #if defined(OS_MACOSX)
25 #include <AvailabilityMacros.h> 25 #include <AvailabilityMacros.h>
26 #include "base/mac/foundation_util.h" 26 #include "base/mac/foundation_util.h"
27 #elif !defined(OS_CHROMEOS) && defined(USE_GLIB) 27 #elif !defined(OS_CHROMEOS) && defined(USE_GLIB)
28 #include <glib.h> // for g_get_home_dir() 28 #include <glib.h> // for g_get_home_dir()
29 #endif 29 #endif
30 30
31 #include <fstream> 31 #include <fstream>
32 32
33 #include "base/basictypes.h" 33 #include "base/basictypes.h"
34 #include "base/command_line.h"
34 #include "base/files/file_enumerator.h" 35 #include "base/files/file_enumerator.h"
35 #include "base/files/file_path.h" 36 #include "base/files/file_path.h"
36 #include "base/logging.h" 37 #include "base/logging.h"
37 #include "base/memory/scoped_ptr.h" 38 #include "base/memory/scoped_ptr.h"
38 #include "base/memory/singleton.h" 39 #include "base/memory/singleton.h"
39 #include "base/path_service.h" 40 #include "base/path_service.h"
40 #include "base/posix/eintr_wrapper.h" 41 #include "base/posix/eintr_wrapper.h"
41 #include "base/stl_util.h" 42 #include "base/stl_util.h"
42 #include "base/strings/string_util.h" 43 #include "base/strings/string_util.h"
43 #include "base/strings/stringprintf.h" 44 #include "base/strings/stringprintf.h"
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 if (mapping != MAP_FAILED) { 185 if (mapping != MAP_FAILED) {
185 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0) 186 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
186 result = true; 187 result = true;
187 munmap(mapping, pagesize); 188 munmap(mapping, pagesize);
188 } 189 }
189 } 190 }
190 return result; 191 return result;
191 } 192 }
192 #endif // defined(OS_LINUX) 193 #endif // defined(OS_LINUX)
193 194
195 #if defined(OS_CHROMEOS)
196 // On Chrome OS Indicates that a user has logged in and that
197 // GetHomeDir() calls are now valid.
198 static bool is_user_logged_in = false;
199
200 // Primary logged in user id hash which is used on ChromeOS to identify
201 // that user home dir.
202 static std::string primary_user_id_hash;
203
204 bool IsUserLoggedIn() {
205 return is_user_logged_in;
206 }
207 #endif
208
194 } // namespace 209 } // namespace
195 210
196 FilePath MakeAbsoluteFilePath(const FilePath& input) { 211 FilePath MakeAbsoluteFilePath(const FilePath& input) {
197 ThreadRestrictions::AssertIOAllowed(); 212 ThreadRestrictions::AssertIOAllowed();
198 char full_path[PATH_MAX]; 213 char full_path[PATH_MAX];
199 if (realpath(input.value().c_str(), full_path) == NULL) 214 if (realpath(input.value().c_str(), full_path) == NULL)
200 return FilePath(); 215 return FilePath();
201 return FilePath(full_path); 216 return FilePath(full_path);
202 } 217 }
203 218
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 return true; 492 return true;
478 } 493 }
479 #endif 494 #endif
480 return GetTempDir(path); 495 return GetTempDir(path);
481 } 496 }
482 #endif // !defined(OS_MACOSX) && !defined(OS_ANDROID) 497 #endif // !defined(OS_MACOSX) && !defined(OS_ANDROID)
483 498
484 #if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm. 499 #if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm.
485 FilePath GetHomeDir() { 500 FilePath GetHomeDir() {
486 #if defined(OS_CHROMEOS) 501 #if defined(OS_CHROMEOS)
487 if (SysInfo::IsRunningOnChromeOS()) 502 if (SysInfo::IsRunningOnChromeOS()) {
488 return FilePath("/home/chronos/user"); 503 if (IsUserLoggedIn()) {
504 return FilePath("/home/chronos/u-" + primary_user_id_hash);
505 } else {
506 NOTREACHED() << "Called GetHomeDir() when not logged in.";
507 return FilePath("/home/chronos/user");
508 }
509 }
489 #endif 510 #endif
490 511
491 const char* home_dir = getenv("HOME"); 512 const char* home_dir = getenv("HOME");
492 if (home_dir && home_dir[0]) 513 if (home_dir && home_dir[0])
493 return FilePath(home_dir); 514 return FilePath(home_dir);
494 515
495 #if defined(OS_ANDROID) 516 #if defined(OS_ANDROID)
496 DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented."; 517 DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
497 #elif defined(USE_GLIB) && !defined(OS_CHROMEOS) 518 #elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
498 // g_get_home_dir calls getpwent, which can fall through to LDAP calls so 519 // g_get_home_dir calls getpwent, which can fall through to LDAP calls so
499 // this may do I/O. However, it should be rare that $HOME is not defined and 520 // this may do I/O. However, it should be rare that $HOME is not defined and
500 // this is typically called from the path service which has no threading 521 // this is typically called from the path service which has no threading
501 // restrictions. The path service will cache the result which limits the 522 // restrictions. The path service will cache the result which limits the
502 // badness of blocking on I/O. As a result, we don't have a thread 523 // badness of blocking on I/O. As a result, we don't have a thread
503 // restriction here. 524 // restriction here.
504 home_dir = g_get_home_dir(); 525 home_dir = g_get_home_dir();
505 if (home_dir && home_dir[0]) 526 if (home_dir && home_dir[0])
506 return FilePath(home_dir); 527 return FilePath(home_dir);
507 #endif 528 #endif
508 529
509 FilePath rv; 530 FilePath rv;
510 if (GetTempDir(&rv)) 531 if (GetTempDir(&rv))
511 return rv; 532 return rv;
512 533
513 // Last resort. 534 // Last resort.
514 return FilePath("/tmp"); 535 return FilePath("/tmp");
515 } 536 }
516 #endif // !defined(OS_MACOSX) 537 #endif // !defined(OS_MACOSX)
517 538
539 #if defined(OS_CHROMEOS)
540 void SetUserLoggedIn(bool logged_in) {
541 is_user_logged_in = logged_in;
542 }
543
544 void SetPrimaryUserIdHash(std::string user_id_hash) {
545 primary_user_id_hash = user_id_hash;
546 }
547 #endif
548
518 bool CreateTemporaryFile(FilePath* path) { 549 bool CreateTemporaryFile(FilePath* path) {
519 ThreadRestrictions::AssertIOAllowed(); // For call to close(). 550 ThreadRestrictions::AssertIOAllowed(); // For call to close().
520 FilePath directory; 551 FilePath directory;
521 if (!GetTempDir(&directory)) 552 if (!GetTempDir(&directory))
522 return false; 553 return false;
523 int fd = CreateAndOpenFdForTemporaryFile(directory, path); 554 int fd = CreateAndOpenFdForTemporaryFile(directory, path);
524 if (fd < 0) 555 if (fd < 0)
525 return false; 556 return false;
526 close(fd); 557 close(fd);
527 return true; 558 return true;
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
921 result = false; 952 result = false;
922 if (IGNORE_EINTR(close(outfile)) < 0) 953 if (IGNORE_EINTR(close(outfile)) < 0)
923 result = false; 954 result = false;
924 955
925 return result; 956 return result;
926 } 957 }
927 #endif // !defined(OS_MACOSX) 958 #endif // !defined(OS_MACOSX)
928 959
929 } // namespace internal 960 } // namespace internal
930 } // namespace base 961 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698