[ChromeOS] getting path to user's cryptohome, failing gracefully if not logged in?

85 views
Skip to first unread message

Miriam Zimmerman

unread,
Apr 16, 2021, 7:24:42 PM4/16/21
to chromi...@chromium.org
Hi there,

I'd like to write a file to either /home/chronos/crash/some-file if no one is logged in, or otherwise to /home/chronos/u-hash/crash/some-file, where u-hash is the active user's home directory.

Right now, I'm attempting to use ProfileManager::GetActiveUserProfile:

  base::FilePath path("/home/chronos");
  Profile* profile = ProfileManager::GetActiveUserProfile(); 
  if (profile) {
    path = profile->GetPath();
  }
  path = path.Append(kCrashFilePath);                                               
However, ProfileManager::GetActiveUserProfile check-fails if there is not an active user, so this crashes when no user is logged in. Further, ProfileManager doesn't seem to give a way to check if there is an active user profile.

How can I check if there is a user logged in/if there is an active profile?

Alternatively, is there a better way to implement "get /home/chronos or /home/chronos/u-whatever, depending on whether there's a logged in user"?

Thanks!

Miriam

Harry Cutts

unread,
Apr 16, 2021, 9:26:59 PM4/16/21
to Chromium-dev, Miriam Zimmerman, Chromium OS Development
+chromium-os-dev

Ryo Hashimoto

unread,
Apr 21, 2021, 11:12:52 PM4/21/21
to Harry Cutts, Chromium-dev, Miriam Zimmerman, Chromium OS Development
How about doing the same thing as IsLoggedIn() in profile_manager.cc?
It uses UserManager to check if it's after user login.

bool IsLoggedIn() {
  return user_manager::UserManager::IsInitialized() &&
         user_manager::UserManager::Get()->IsUserLoggedIn();
}

--
--
Chromium OS Developers mailing list: chromiu...@chromium.org
View archives, change email options, or unsubscribe:
https://groups.google.com/a/chromium.org/group/chromium-os-dev

Miriam Zimmerman

unread,
Apr 29, 2021, 4:14:34 PM4/29/21
to Xiyuan Xia, chromi...@chromium.org, chromiu...@chromium.org, James Cook, Harry Cutts
+chrom...@chromium.org +chromium-os-dev again.

Thanks, Xiyuan -- this was exactly what I needed to accomplish this from components/!

Miriam


On Tue, Apr 20, 2021 at 1:06 PM Xiyuan Xia <xiy...@google.com> wrote:
We probably could not use ProfileHelper from within //components since it is in //chrome/browser, which is supposed to be a higer layer of //components.

//components/variation should probably be in a pretty low level layer and could only depend on //base. In this case, base::DIR_HOME is probably the best we could use. It would be overridden upon the primary user sign-in in UserSessionManager::OverrideHomedir. Not sure how to get  "/home/chronos" though since that is chrome::DIR_USER_DADTA but it is a chrome path.

So maybe something like:
  base::FilePath path = base::CheckedGet(base::DIR_HOME);
  if (path == base::FilePath("/")) {
    // Fallback to /home/chronos if DIR_HOME is not overridden and
    // no user has signed in.
    path = base::FilePath("/home/chronos");
  }
  path = path.Append(kCrashFilePath);
  


On Tue, Apr 20, 2021 at 12:48 PM Miriam Zimmerman <mute...@google.com> wrote:
Unfortunately, it looks like I'm having a circular dependency issue, because I'm doing this in //components/variations:variations, which is already indirectly depended on by the targets that include profile_helper.h (AFAICT). 

Is there any way around this, or am I out of luck with using this API?

Miriam


On Tue, Apr 20, 2021 at 12:14 PM Miriam Zimmerman <mute...@google.com> wrote:

Miriam


On Tue, Apr 20, 2021 at 12:07 PM Xiyuan Xia <xiy...@google.com> wrote:
> ProfileManager::GetActiveUserProfile check-fails

Do you have a crash stack? Think it should fallback to the sign-in profile when there is no active user (code).
I didn't; that was based on reading the code-- but now that I read it again I think you're right, and the crash was actually elsewhere.
 
But personally, I would avoid using ProfileManager::GetActiveUserProfile() because I don't know exactly what is returned. If the code is in chrome browser, the way James suggested should work:
e.g.
  base::FilePath path;
  if (user_manager::UserManager::Get()->IsUserLoggedIn()) {
    path = ProfileHelper::Get()->GetActiveUserProfileDir();
  } else {
    path = ProfileHelper::GetSigninProfileDir();
  }
  path = path.Append(kCrashFilePath);
Thanks! I'll give this a shot now :)
 

On Tue, Apr 20, 2021 at 11:25 AM Miriam Zimmerman <mute...@google.com> wrote:
Thanks, James!

+Xiyuan Xia do you have any tips here? :)

Miriam


On Mon, Apr 19, 2021 at 1:15 PM James Cook <jame...@google.com> wrote:
-lists because I'm not so sure this is the right answer....

Maybe some combination of ProfileHelper::GetSigninProfileDir vs. GetActiveUserProfileDir vs. user_manager::UserManager to check signin state?


If that's not right, xiyuan@ might know.

--
Reply all
Reply to author
Forward
0 new messages