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

Side by Side Diff: chrome/browser/ui/webui/settings/profile_info_handler.cc

Issue 1900913002: Settings People Revamp: Split Profile Info out into its own handler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2016 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/ui/webui/settings/profile_info_handler.h"
6
7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_attributes_entry.h"
12 #include "chrome/browser/ui/user_manager.h"
13 #include "ui/base/webui/web_ui_util.h"
14
15 #if defined(OS_CHROMEOS)
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/chromeos/profiles/profile_helper.h"
18 #include "chrome/browser/ui/webui/options/chromeos/user_image_source.h"
19 #include "components/signin/core/account_id/account_id.h"
20 #include "components/user_manager/user_manager.h"
21 #include "content/public/browser/notification_service.h"
22 #else
23 #include "base/strings/utf_string_conversions.h"
24 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26 #endif
27
28 namespace settings {
29
30 ProfileInfoHandler::ProfileInfoHandler(Profile* profile)
31 : profile_(profile), observers_registered_(false) {}
32
33 void ProfileInfoHandler::RegisterMessages() {
34 web_ui()->RegisterMessageCallback(
35 "getProfileInfo", base::Bind(&ProfileInfoHandler::HandleGetProfileInfo,
36 base::Unretained(this)));
37 }
38
39 void ProfileInfoHandler::RenderViewReused() {
40 if (!observers_registered_)
41 return;
42
43 g_browser_process->profile_manager()
44 ->GetProfileAttributesStorage()
45 .RemoveObserver(this);
46
47 #if defined(OS_CHROMEOS)
48 registrar_.RemoveAll();
49 #endif
50
51 observers_registered_ = false;
52 }
53
54 #if defined(OS_CHROMEOS)
55 void ProfileInfoHandler::Observe(int type,
56 const content::NotificationSource& source,
57 const content::NotificationDetails& details) {
58 switch (type) {
59 case chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED:
60 PushProfileInfo();
61 break;
62 default:
63 NOTREACHED();
64 }
65 }
66 #endif
67
68 void ProfileInfoHandler::OnProfileNameChanged(
69 const base::FilePath& /* profile_path */,
70 const base::string16& /* old_profile_name */) {
71 PushProfileInfo();
72 }
73
74 void ProfileInfoHandler::OnProfileAvatarChanged(
75 const base::FilePath& /* profile_path */) {
76 PushProfileInfo();
77 }
78
79 void ProfileInfoHandler::HandleGetProfileInfo(const base::ListValue* args) {
80 if (!observers_registered_) {
81 observers_registered_ = true;
82
83 g_browser_process->profile_manager()
84 ->GetProfileAttributesStorage()
85 .AddObserver(this);
86
87 #if defined(OS_CHROMEOS)
88 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED,
89 content::NotificationService::AllSources());
90 #endif
91 }
92
93 CHECK_EQ(1U, args->GetSize());
94 const base::Value* callback_id;
95 CHECK(args->Get(0, &callback_id));
96
97 ResolveJavascriptCallback(*callback_id, *GetAccountNameAndIcon());
98 }
99
100 void ProfileInfoHandler::PushProfileInfo() {
101 web_ui()->CallJavascriptFunction("cr.webUIListenerCallback",
102 base::StringValue("profile-info-changed"),
103 *GetAccountNameAndIcon());
104 }
105
106 std::unique_ptr<base::DictionaryValue>
107 ProfileInfoHandler::GetAccountNameAndIcon() const {
108 std::string name;
109 std::string icon_url;
110
111 #if defined(OS_CHROMEOS)
112 name = profile_->GetProfileUserName();
113 if (name.empty()) {
114 const user_manager::User* user =
115 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
116 if (user && (user->GetType() != user_manager::USER_TYPE_GUEST))
117 name = user->email();
118 }
119 if (!name.empty())
120 name = gaia::SanitizeEmail(gaia::CanonicalizeEmail(name));
121
122 // Get image as data URL instead of using chrome://userimage source to avoid
123 // issues with caching.
124 const AccountId account_id(AccountId::FromUserEmail(name));
125 scoped_refptr<base::RefCountedMemory> image =
126 chromeos::options::UserImageSource::GetUserImage(account_id);
127 icon_url = webui::GetPngDataUrl(image->front(), image->size());
128 #else // !defined(OS_CHROMEOS)
129 ProfileAttributesEntry* entry;
130 if (g_browser_process->profile_manager()
131 ->GetProfileAttributesStorage()
132 .GetProfileAttributesWithPath(profile_->GetPath(), &entry)) {
133 name = base::UTF16ToUTF8(entry->GetName());
134
135 if (entry->IsUsingGAIAPicture() && entry->GetGAIAPicture()) {
136 gfx::Image icon =
137 profiles::GetAvatarIconForWebUI(entry->GetAvatarIcon(), true);
138 icon_url = webui::GetBitmapDataUrl(icon.AsBitmap());
139 } else {
140 icon_url = profiles::GetDefaultAvatarIconUrl(entry->GetAvatarIconIndex());
141 }
142 }
143 #endif // defined(OS_CHROMEOS)
144
145 base::DictionaryValue* response = new base::DictionaryValue();
146 response->SetString("name", name);
147 response->SetString("iconUrl", icon_url);
148 return base::WrapUnique(response);
149 }
150
151 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698