| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/webui/settings/profile_info_handler.h" | 5 #include "chrome/browser/ui/webui/settings/profile_info_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 24 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 25 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 namespace settings { | 28 namespace settings { |
| 29 | 29 |
| 30 // static | 30 // static |
| 31 const char ProfileInfoHandler::kProfileInfoChangedEventName[] = | 31 const char ProfileInfoHandler::kProfileInfoChangedEventName[] = |
| 32 "profile-info-changed"; | 32 "profile-info-changed"; |
| 33 | 33 |
| 34 ProfileInfoHandler::ProfileInfoHandler(Profile* profile) | 34 ProfileInfoHandler::ProfileInfoHandler(Profile* profile) : profile_(profile) {} |
| 35 : profile_(profile), observers_registered_(false) {} | |
| 36 | 35 |
| 37 void ProfileInfoHandler::RegisterMessages() { | 36 void ProfileInfoHandler::RegisterMessages() { |
| 38 web_ui()->RegisterMessageCallback( | 37 web_ui()->RegisterMessageCallback( |
| 39 "getProfileInfo", base::Bind(&ProfileInfoHandler::HandleGetProfileInfo, | 38 "getProfileInfo", base::Bind(&ProfileInfoHandler::HandleGetProfileInfo, |
| 40 base::Unretained(this))); | 39 base::Unretained(this))); |
| 41 } | 40 } |
| 42 | 41 |
| 43 void ProfileInfoHandler::RenderViewReused() { | 42 void ProfileInfoHandler::OnJavascriptAllowed() { |
| 44 if (!observers_registered_) | 43 g_browser_process->profile_manager() |
| 45 return; | 44 ->GetProfileAttributesStorage() |
| 45 .AddObserver(this); |
| 46 | 46 |
| 47 #if defined(OS_CHROMEOS) |
| 48 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, |
| 49 content::NotificationService::AllSources()); |
| 50 #endif |
| 51 } |
| 52 |
| 53 void ProfileInfoHandler::OnJavascriptDisallowed() { |
| 47 g_browser_process->profile_manager() | 54 g_browser_process->profile_manager() |
| 48 ->GetProfileAttributesStorage() | 55 ->GetProfileAttributesStorage() |
| 49 .RemoveObserver(this); | 56 .RemoveObserver(this); |
| 50 | 57 |
| 51 #if defined(OS_CHROMEOS) | 58 #if defined(OS_CHROMEOS) |
| 52 registrar_.RemoveAll(); | 59 registrar_.RemoveAll(); |
| 53 #endif | 60 #endif |
| 54 | |
| 55 observers_registered_ = false; | |
| 56 } | 61 } |
| 57 | 62 |
| 58 #if defined(OS_CHROMEOS) | 63 #if defined(OS_CHROMEOS) |
| 59 void ProfileInfoHandler::Observe(int type, | 64 void ProfileInfoHandler::Observe(int type, |
| 60 const content::NotificationSource& source, | 65 const content::NotificationSource& source, |
| 61 const content::NotificationDetails& details) { | 66 const content::NotificationDetails& details) { |
| 62 switch (type) { | 67 switch (type) { |
| 63 case chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED: | 68 case chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED: |
| 64 PushProfileInfo(); | 69 PushProfileInfo(); |
| 65 break; | 70 break; |
| 66 default: | 71 default: |
| 67 NOTREACHED(); | 72 NOTREACHED(); |
| 68 } | 73 } |
| 69 } | 74 } |
| 70 #endif | 75 #endif |
| 71 | 76 |
| 72 void ProfileInfoHandler::OnProfileNameChanged( | 77 void ProfileInfoHandler::OnProfileNameChanged( |
| 73 const base::FilePath& /* profile_path */, | 78 const base::FilePath& /* profile_path */, |
| 74 const base::string16& /* old_profile_name */) { | 79 const base::string16& /* old_profile_name */) { |
| 75 PushProfileInfo(); | 80 PushProfileInfo(); |
| 76 } | 81 } |
| 77 | 82 |
| 78 void ProfileInfoHandler::OnProfileAvatarChanged( | 83 void ProfileInfoHandler::OnProfileAvatarChanged( |
| 79 const base::FilePath& /* profile_path */) { | 84 const base::FilePath& /* profile_path */) { |
| 80 PushProfileInfo(); | 85 PushProfileInfo(); |
| 81 } | 86 } |
| 82 | 87 |
| 83 void ProfileInfoHandler::HandleGetProfileInfo(const base::ListValue* args) { | 88 void ProfileInfoHandler::HandleGetProfileInfo(const base::ListValue* args) { |
| 84 if (!observers_registered_) { | 89 AllowJavascript(); |
| 85 observers_registered_ = true; | |
| 86 | |
| 87 g_browser_process->profile_manager() | |
| 88 ->GetProfileAttributesStorage() | |
| 89 .AddObserver(this); | |
| 90 | |
| 91 #if defined(OS_CHROMEOS) | |
| 92 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, | |
| 93 content::NotificationService::AllSources()); | |
| 94 #endif | |
| 95 } | |
| 96 | 90 |
| 97 CHECK_EQ(1U, args->GetSize()); | 91 CHECK_EQ(1U, args->GetSize()); |
| 98 const base::Value* callback_id; | 92 const base::Value* callback_id; |
| 99 CHECK(args->Get(0, &callback_id)); | 93 CHECK(args->Get(0, &callback_id)); |
| 100 | 94 |
| 101 ResolveJavascriptCallback(*callback_id, *GetAccountNameAndIcon()); | 95 ResolveJavascriptCallback(*callback_id, *GetAccountNameAndIcon()); |
| 102 } | 96 } |
| 103 | 97 |
| 104 void ProfileInfoHandler::PushProfileInfo() { | 98 void ProfileInfoHandler::PushProfileInfo() { |
| 105 web_ui()->CallJavascriptFunction( | 99 web_ui()->CallJavascriptFunction( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } | 141 } |
| 148 #endif // defined(OS_CHROMEOS) | 142 #endif // defined(OS_CHROMEOS) |
| 149 | 143 |
| 150 base::DictionaryValue* response = new base::DictionaryValue(); | 144 base::DictionaryValue* response = new base::DictionaryValue(); |
| 151 response->SetString("name", name); | 145 response->SetString("name", name); |
| 152 response->SetString("iconUrl", icon_url); | 146 response->SetString("iconUrl", icon_url); |
| 153 return base::WrapUnique(response); | 147 return base::WrapUnique(response); |
| 154 } | 148 } |
| 155 | 149 |
| 156 } // namespace settings | 150 } // namespace settings |
| OLD | NEW |