OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/app_list/start_page_handler.h" | 5 #include "chrome/browser/ui/webui/app_list/start_page_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "base/version.h" |
13 #include "chrome/browser/extensions/extension_service.h" | |
14 #include "chrome/browser/omaha_query_params/omaha_query_params.h" | 13 #include "chrome/browser/omaha_query_params/omaha_query_params.h" |
15 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search/hotword_service.h" | 15 #include "chrome/browser/search/hotword_service.h" |
17 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" | 16 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" |
18 #include "chrome/browser/ui/app_list/app_list_service.h" | 17 #include "chrome/browser/ui/app_list/app_list_service.h" |
19 #include "chrome/browser/ui/app_list/recommended_apps.h" | 18 #include "chrome/browser/ui/app_list/recommended_apps.h" |
20 #include "chrome/browser/ui/app_list/start_page_service.h" | 19 #include "chrome/browser/ui/app_list/start_page_service.h" |
21 #include "chrome/browser/ui/host_desktop.h" | 20 #include "chrome/browser/ui/host_desktop.h" |
22 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 21 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
23 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
24 #include "content/public/browser/notification_details.h" | |
25 #include "content/public/browser/notification_source.h" | |
26 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
| 24 #include "extensions/browser/extension_registry.h" |
27 #include "extensions/browser/extension_system.h" | 25 #include "extensions/browser/extension_system.h" |
| 26 #include "extensions/common/constants.h" |
28 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
29 #include "extensions/common/extension_icon_set.h" | 28 #include "extensions/common/extension_icon_set.h" |
30 #include "ui/app_list/app_list_switches.h" | 29 #include "ui/app_list/app_list_switches.h" |
31 #include "ui/app_list/speech_ui_model_observer.h" | 30 #include "ui/app_list/speech_ui_model_observer.h" |
32 #include "ui/events/event_constants.h" | 31 #include "ui/events/event_constants.h" |
33 | 32 |
34 namespace app_list { | 33 namespace app_list { |
35 | 34 |
36 namespace { | 35 namespace { |
37 | 36 |
(...skipping 16 matching lines...) Expand all Loading... |
54 ExtensionIconSet::MATCH_BIGGER, | 53 ExtensionIconSet::MATCH_BIGGER, |
55 grayscale, | 54 grayscale, |
56 &icon_exists); | 55 &icon_exists); |
57 dict->SetString("iconUrl", icon_url.spec()); | 56 dict->SetString("iconUrl", icon_url.spec()); |
58 | 57 |
59 return dict.Pass(); | 58 return dict.Pass(); |
60 } | 59 } |
61 | 60 |
62 } // namespace | 61 } // namespace |
63 | 62 |
64 StartPageHandler::StartPageHandler() : recommended_apps_(NULL) {} | 63 StartPageHandler::StartPageHandler() |
| 64 : recommended_apps_(NULL), |
| 65 extension_registry_observer_(this) { |
| 66 } |
65 | 67 |
66 StartPageHandler::~StartPageHandler() { | 68 StartPageHandler::~StartPageHandler() { |
67 if (recommended_apps_) | 69 if (recommended_apps_) |
68 recommended_apps_->RemoveObserver(this); | 70 recommended_apps_->RemoveObserver(this); |
69 } | 71 } |
70 | 72 |
71 void StartPageHandler::RegisterMessages() { | 73 void StartPageHandler::RegisterMessages() { |
72 web_ui()->RegisterMessageCallback( | 74 web_ui()->RegisterMessageCallback( |
73 "initialize", | 75 "initialize", |
74 base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this))); | 76 base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this))); |
75 web_ui()->RegisterMessageCallback( | 77 web_ui()->RegisterMessageCallback( |
76 "launchApp", | 78 "launchApp", |
77 base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this))); | 79 base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this))); |
78 web_ui()->RegisterMessageCallback( | 80 web_ui()->RegisterMessageCallback( |
79 "speechResult", | 81 "speechResult", |
80 base::Bind(&StartPageHandler::HandleSpeechResult, | 82 base::Bind(&StartPageHandler::HandleSpeechResult, |
81 base::Unretained(this))); | 83 base::Unretained(this))); |
82 web_ui()->RegisterMessageCallback( | 84 web_ui()->RegisterMessageCallback( |
83 "speechSoundLevel", | 85 "speechSoundLevel", |
84 base::Bind(&StartPageHandler::HandleSpeechSoundLevel, | 86 base::Bind(&StartPageHandler::HandleSpeechSoundLevel, |
85 base::Unretained(this))); | 87 base::Unretained(this))); |
86 web_ui()->RegisterMessageCallback( | 88 web_ui()->RegisterMessageCallback( |
87 "setSpeechRecognitionState", | 89 "setSpeechRecognitionState", |
88 base::Bind(&StartPageHandler::HandleSpeechRecognition, | 90 base::Bind(&StartPageHandler::HandleSpeechRecognition, |
89 base::Unretained(this))); | 91 base::Unretained(this))); |
90 } | 92 } |
91 | 93 |
92 void StartPageHandler::Observe(int type, | 94 void StartPageHandler::OnExtensionLoaded( |
93 const content::NotificationSource& source, | 95 content::BrowserContext* browser_context, |
94 const content::NotificationDetails& details) { | 96 const extensions::Extension* extension) { |
95 #if defined(OS_CHROMEOS) | 97 #if defined(OS_CHROMEOS) |
96 DCHECK_EQ(Profile::FromWebUI(web_ui()), | 98 DCHECK_EQ(Profile::FromWebUI(web_ui()), |
97 content::Source<Profile>(source).ptr()); | 99 Profile::FromBrowserContext(browser_context)); |
98 switch (type) { | 100 if (extension->id() == extension_misc::kHotwordExtensionId) |
99 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | 101 OnHotwordEnabledChanged(); |
100 extensions::Extension* extension = | |
101 content::Details<extensions::Extension>(details).ptr(); | |
102 if (extension->id() == extension_misc::kHotwordExtensionId) | |
103 OnHotwordEnabledChanged(); | |
104 break; | |
105 } | |
106 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | |
107 extensions::UnloadedExtensionInfo* info = | |
108 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); | |
109 if (info->extension->id() == extension_misc::kHotwordExtensionId) | |
110 OnHotwordEnabledChanged(); | |
111 break; | |
112 } | |
113 default: | |
114 NOTREACHED(); | |
115 break; | |
116 } | |
117 #endif | 102 #endif |
118 } | 103 } |
119 | 104 |
| 105 void StartPageHandler::OnExtensionUnloaded( |
| 106 content::BrowserContext* browser_context, |
| 107 const extensions::Extension* extension, |
| 108 extensions::UnloadedExtensionInfo::Reason reason) { |
| 109 #if defined(OS_CHROMEOS) |
| 110 DCHECK_EQ(Profile::FromWebUI(web_ui()), |
| 111 Profile::FromBrowserContext(browser_context)); |
| 112 if (extension->id() == extension_misc::kHotwordExtensionId) |
| 113 OnHotwordEnabledChanged(); |
| 114 #endif |
| 115 } |
| 116 |
120 void StartPageHandler::OnRecommendedAppsChanged() { | 117 void StartPageHandler::OnRecommendedAppsChanged() { |
121 SendRecommendedApps(); | 118 SendRecommendedApps(); |
122 } | 119 } |
123 | 120 |
124 void StartPageHandler::SendRecommendedApps() { | 121 void StartPageHandler::SendRecommendedApps() { |
125 const RecommendedApps::Apps& recommends = recommended_apps_->apps(); | 122 const RecommendedApps::Apps& recommends = recommended_apps_->apps(); |
126 | 123 |
127 base::ListValue recommended_list; | 124 base::ListValue recommended_list; |
128 for (size_t i = 0; i < recommends.size(); ++i) { | 125 for (size_t i = 0; i < recommends.size(); ++i) { |
129 recommended_list.Append(CreateAppInfo(recommends[i].get()).release()); | 126 recommended_list.Append(CreateAppInfo(recommends[i].get()).release()); |
130 } | 127 } |
131 | 128 |
132 web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps", | 129 web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps", |
133 recommended_list); | 130 recommended_list); |
134 } | 131 } |
135 | 132 |
136 #if defined(OS_CHROMEOS) | 133 #if defined(OS_CHROMEOS) |
137 void StartPageHandler::OnHotwordEnabledChanged() { | 134 void StartPageHandler::OnHotwordEnabledChanged() { |
138 // If the hotword extension is new enough, we should use the new | 135 // If the hotword extension is new enough, we should use the new |
139 // hotwordPrivate API to provide the feature. | 136 // hotwordPrivate API to provide the feature. |
140 // TODO(mukai): remove this after everything gets stable. | 137 // TODO(mukai): remove this after everything gets stable. |
141 Profile* profile = Profile::FromWebUI(web_ui()); | 138 Profile* profile = Profile::FromWebUI(web_ui()); |
142 ExtensionService* extension_service = | |
143 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
144 if (!extension_service) | |
145 return; | |
146 | 139 |
| 140 extensions::ExtensionRegistry* registry = |
| 141 extensions::ExtensionRegistry::Get(profile); |
147 const extensions::Extension* hotword_extension = | 142 const extensions::Extension* hotword_extension = |
148 extension_service->GetExtensionById( | 143 registry->GetExtensionById(extension_misc::kHotwordExtensionId, |
149 extension_misc::kHotwordExtensionId, false /* include_disabled */); | 144 extensions::ExtensionRegistry::ENABLED); |
150 if (hotword_extension && hotword_extension->version()->CompareTo( | 145 if (hotword_extension && |
| 146 hotword_extension->version()->CompareTo( |
151 base::Version(kOldHotwordExtensionVersionString)) <= 0) { | 147 base::Version(kOldHotwordExtensionVersionString)) <= 0) { |
152 StartPageService* service = StartPageService::Get(profile); | 148 StartPageService* service = StartPageService::Get(profile); |
153 web_ui()->CallJavascriptFunction( | 149 web_ui()->CallJavascriptFunction( |
154 "appList.startPage.setHotwordEnabled", | 150 "appList.startPage.setHotwordEnabled", |
155 base::FundamentalValue(service->HotwordEnabled())); | 151 base::FundamentalValue(service->HotwordEnabled())); |
156 } | 152 } |
157 } | 153 } |
158 #endif | 154 #endif |
159 | 155 |
160 void StartPageHandler::HandleInitialize(const base::ListValue* args) { | 156 void StartPageHandler::HandleInitialize(const base::ListValue* args) { |
161 Profile* profile = Profile::FromWebUI(web_ui()); | 157 Profile* profile = Profile::FromWebUI(web_ui()); |
162 StartPageService* service = StartPageService::Get(profile); | 158 StartPageService* service = StartPageService::Get(profile); |
163 if (!service) | 159 if (!service) |
164 return; | 160 return; |
165 | 161 |
166 recommended_apps_ = service->recommended_apps(); | 162 recommended_apps_ = service->recommended_apps(); |
167 recommended_apps_->AddObserver(this); | 163 recommended_apps_->AddObserver(this); |
168 | 164 |
169 SendRecommendedApps(); | 165 SendRecommendedApps(); |
170 | 166 |
171 #if defined(OS_CHROMEOS) | 167 #if defined(OS_CHROMEOS) |
172 if (app_list::switches::IsVoiceSearchEnabled() && | 168 if (app_list::switches::IsVoiceSearchEnabled() && |
173 HotwordService::DoesHotwordSupportLanguage(profile)) { | 169 HotwordService::DoesHotwordSupportLanguage(profile)) { |
174 OnHotwordEnabledChanged(); | 170 OnHotwordEnabledChanged(); |
175 pref_change_registrar_.Init(profile->GetPrefs()); | 171 pref_change_registrar_.Init(profile->GetPrefs()); |
176 pref_change_registrar_.Add( | 172 pref_change_registrar_.Add( |
177 prefs::kHotwordSearchEnabled, | 173 prefs::kHotwordSearchEnabled, |
178 base::Bind(&StartPageHandler::OnHotwordEnabledChanged, | 174 base::Bind(&StartPageHandler::OnHotwordEnabledChanged, |
179 base::Unretained(this))); | 175 base::Unretained(this))); |
180 registrar_.Add(this, | 176 |
181 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 177 extension_registry_observer_.Add( |
182 content::Source<Profile>(profile)); | 178 extensions::ExtensionRegistry::Get(profile)); |
183 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
184 content::Source<Profile>(profile)); | |
185 } | 179 } |
186 #endif | 180 #endif |
187 | 181 |
188 web_ui()->CallJavascriptFunction( | 182 web_ui()->CallJavascriptFunction( |
189 "appList.startPage.setNaclArch", | 183 "appList.startPage.setNaclArch", |
190 base::StringValue(chrome::OmahaQueryParams::GetNaclArch())); | 184 base::StringValue(chrome::OmahaQueryParams::GetNaclArch())); |
191 | 185 |
192 if (!app_list::switches::IsExperimentalAppListEnabled()) { | 186 if (!app_list::switches::IsExperimentalAppListEnabled()) { |
193 web_ui()->CallJavascriptFunction( | 187 web_ui()->CallJavascriptFunction( |
194 "appList.startPage.onAppListShown", | 188 "appList.startPage.onAppListShown", |
195 base::FundamentalValue(service->HotwordEnabled())); | 189 base::FundamentalValue(service->HotwordEnabled())); |
196 } | 190 } |
197 } | 191 } |
198 | 192 |
199 void StartPageHandler::HandleLaunchApp(const base::ListValue* args) { | 193 void StartPageHandler::HandleLaunchApp(const base::ListValue* args) { |
200 std::string app_id; | 194 std::string app_id; |
201 CHECK(args->GetString(0, &app_id)); | 195 CHECK(args->GetString(0, &app_id)); |
202 | 196 |
203 Profile* profile = Profile::FromWebUI(web_ui()); | 197 Profile* profile = Profile::FromWebUI(web_ui()); |
204 ExtensionService* service = | 198 const extensions::Extension* app = |
205 extensions::ExtensionSystem::Get(profile)->extension_service(); | 199 extensions::ExtensionRegistry::Get(profile) |
206 const extensions::Extension* app = service->GetInstalledExtension(app_id); | 200 ->GetExtensionById(app_id, extensions::ExtensionRegistry::EVERYTHING); |
207 if (!app) { | 201 if (!app) { |
208 NOTREACHED(); | 202 NOTREACHED(); |
209 return; | 203 return; |
210 } | 204 } |
211 | 205 |
212 AppListControllerDelegate* controller = AppListService::Get( | 206 AppListControllerDelegate* controller = AppListService::Get( |
213 chrome::GetHostDesktopTypeForNativeView( | 207 chrome::GetHostDesktopTypeForNativeView( |
214 web_ui()->GetWebContents()->GetNativeView()))-> | 208 web_ui()->GetWebContents()->GetNativeView()))-> |
215 GetControllerDelegate(); | 209 GetControllerDelegate(); |
216 controller->ActivateApp(profile, | 210 controller->ActivateApp(profile, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 else if (state_string == "STOPPING") | 249 else if (state_string == "STOPPING") |
256 new_state = SPEECH_RECOGNITION_STOPPING; | 250 new_state = SPEECH_RECOGNITION_STOPPING; |
257 | 251 |
258 StartPageService* service = | 252 StartPageService* service = |
259 StartPageService::Get(Profile::FromWebUI(web_ui())); | 253 StartPageService::Get(Profile::FromWebUI(web_ui())); |
260 if (service) | 254 if (service) |
261 service->OnSpeechRecognitionStateChanged(new_state); | 255 service->OnSpeechRecognitionStateChanged(new_state); |
262 } | 256 } |
263 | 257 |
264 } // namespace app_list | 258 } // namespace app_list |
OLD | NEW |