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/search/hotword_service.h" | 5 #include "chrome/browser/search/hotword_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); | 254 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); |
255 } | 255 } |
256 } | 256 } |
257 | 257 |
258 void HotwordService::OnExtensionUninstalled( | 258 void HotwordService::OnExtensionUninstalled( |
259 content::BrowserContext* browser_context, | 259 content::BrowserContext* browser_context, |
260 const extensions::Extension* extension, | 260 const extensions::Extension* extension, |
261 extensions::UninstallReason reason) { | 261 extensions::UninstallReason reason) { |
262 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 262 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
263 | 263 |
264 if (extension->id() != extension_misc::kHotwordExtensionId || | 264 if ((extension->id() != extension_misc::kHotwordExtensionId && |
265 profile_ != Profile::FromBrowserContext(browser_context) || | 265 extension->id() != extension_misc::kHotwordSharedModuleId) || |
266 profile_ != Profile::FromBrowserContext(browser_context) || | |
266 !GetExtensionService(profile_)) | 267 !GetExtensionService(profile_)) |
267 return; | 268 return; |
268 | 269 |
269 // If the extension wasn't uninstalled due to language change, don't try to | 270 // If the extension wasn't uninstalled due to language change, don't try to |
270 // reinstall it. | 271 // reinstall it. |
271 if (!reinstall_pending_) | 272 if (!reinstall_pending_) |
272 return; | 273 return; |
273 | 274 |
274 InstallHotwordExtensionFromWebstore(); | 275 InstallHotwordExtensionFromWebstore(); |
275 SetPreviousLanguagePref(); | 276 SetPreviousLanguagePref(); |
276 } | 277 } |
277 | 278 |
278 void HotwordService::InstallHotwordExtensionFromWebstore() { | 279 void HotwordService::InstallHotwordExtensionFromWebstore() { |
280 std::string extension_id; | |
281 if (IsExperimentalHotwordingEnabled()) | |
282 extension_id = extension_misc::kHotwordSharedModuleId; | |
283 else | |
284 extension_id = extension_misc::kHotwordExtensionId; | |
rpetterson
2014/10/20 06:31:21
You're repeating this code a lot in here. Helper f
Anand Mistry (off Chromium)
2014/10/20 10:04:49
Done.
| |
279 installer_ = new extensions::WebstoreStartupInstaller( | 285 installer_ = new extensions::WebstoreStartupInstaller( |
280 extension_misc::kHotwordExtensionId, | 286 extension_id, |
281 profile_, | 287 profile_, |
282 false, | 288 false, |
283 extensions::WebstoreStandaloneInstaller::Callback()); | 289 extensions::WebstoreStandaloneInstaller::Callback()); |
284 installer_->BeginInstall(); | 290 installer_->BeginInstall(); |
285 } | 291 } |
286 | 292 |
287 void HotwordService::OnExtensionInstalled( | 293 void HotwordService::OnExtensionInstalled( |
288 content::BrowserContext* browser_context, | 294 content::BrowserContext* browser_context, |
289 const extensions::Extension* extension, | 295 const extensions::Extension* extension, |
290 bool is_update) { | 296 bool is_update) { |
291 | 297 |
292 if (extension->id() != extension_misc::kHotwordExtensionId || | 298 if ((extension->id() != extension_misc::kHotwordExtensionId && |
293 profile_ != Profile::FromBrowserContext(browser_context)) | 299 extension->id() != extension_misc::kHotwordSharedModuleId) || |
300 profile_ != Profile::FromBrowserContext(browser_context)) | |
294 return; | 301 return; |
295 | 302 |
296 // If the previous locale pref has never been set, set it now since | 303 // If the previous locale pref has never been set, set it now since |
297 // the extension has been installed. | 304 // the extension has been installed. |
298 if (!profile_->GetPrefs()->HasPrefPath(prefs::kHotwordPreviousLanguage)) | 305 if (!profile_->GetPrefs()->HasPrefPath(prefs::kHotwordPreviousLanguage)) |
299 SetPreviousLanguagePref(); | 306 SetPreviousLanguagePref(); |
300 | 307 |
301 // If MaybeReinstallHotwordExtension already triggered an uninstall, we | 308 // If MaybeReinstallHotwordExtension already triggered an uninstall, we |
302 // don't want to loop and trigger another uninstall-install cycle. | 309 // don't want to loop and trigger another uninstall-install cycle. |
303 // However, if we arrived here via an uninstall-triggered-install (and in | 310 // However, if we arrived here via an uninstall-triggered-install (and in |
(...skipping 18 matching lines...) Expand all Loading... | |
322 } | 329 } |
323 } | 330 } |
324 | 331 |
325 bool HotwordService::MaybeReinstallHotwordExtension() { | 332 bool HotwordService::MaybeReinstallHotwordExtension() { |
326 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 333 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
327 | 334 |
328 ExtensionService* extension_service = GetExtensionService(profile_); | 335 ExtensionService* extension_service = GetExtensionService(profile_); |
329 if (!extension_service) | 336 if (!extension_service) |
330 return false; | 337 return false; |
331 | 338 |
339 std::string extension_id; | |
340 if (IsExperimentalHotwordingEnabled()) | |
341 extension_id = extension_misc::kHotwordSharedModuleId; | |
342 else | |
343 extension_id = extension_misc::kHotwordExtensionId; | |
332 const extensions::Extension* extension = extension_service->GetExtensionById( | 344 const extensions::Extension* extension = extension_service->GetExtensionById( |
333 extension_misc::kHotwordExtensionId, true); | 345 extension_id, true); |
334 if (!extension) | 346 if (!extension) |
335 return false; | 347 return false; |
336 | 348 |
337 // If the extension is currently pending, return and we'll check again | 349 // If the extension is currently pending, return and we'll check again |
338 // after the install is finished. | 350 // after the install is finished. |
339 extensions::PendingExtensionManager* pending_manager = | 351 extensions::PendingExtensionManager* pending_manager = |
340 extension_service->pending_extension_manager(); | 352 extension_service->pending_extension_manager(); |
341 if (pending_manager->IsIdPending(extension->id())) | 353 if (pending_manager->IsIdPending(extension->id())) |
342 return false; | 354 return false; |
343 | 355 |
(...skipping 10 matching lines...) Expand all Loading... | |
354 // Ensure the call to OnExtensionUninstalled was triggered by a language | 366 // Ensure the call to OnExtensionUninstalled was triggered by a language |
355 // change so it's okay to reinstall. | 367 // change so it's okay to reinstall. |
356 reinstall_pending_ = true; | 368 reinstall_pending_ = true; |
357 | 369 |
358 return UninstallHotwordExtension(extension_service); | 370 return UninstallHotwordExtension(extension_service); |
359 } | 371 } |
360 | 372 |
361 bool HotwordService::UninstallHotwordExtension( | 373 bool HotwordService::UninstallHotwordExtension( |
362 ExtensionService* extension_service) { | 374 ExtensionService* extension_service) { |
363 base::string16 error; | 375 base::string16 error; |
376 std::string extension_id; | |
377 if (IsExperimentalHotwordingEnabled()) | |
378 extension_id = extension_misc::kHotwordSharedModuleId; | |
379 else | |
380 extension_id = extension_misc::kHotwordExtensionId; | |
364 if (!extension_service->UninstallExtension( | 381 if (!extension_service->UninstallExtension( |
365 extension_misc::kHotwordExtensionId, | 382 extension_id, |
366 extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT, | 383 extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT, |
367 base::Bind(&base::DoNothing), | 384 base::Bind(&base::DoNothing), |
368 &error)) { | 385 &error)) { |
369 LOG(WARNING) << "Cannot uninstall extension with id " | 386 LOG(WARNING) << "Cannot uninstall extension with id " |
370 << extension_misc::kHotwordExtensionId | 387 << extension_id |
371 << ": " << error; | 388 << ": " << error; |
372 reinstall_pending_ = false; | 389 reinstall_pending_ = false; |
373 return false; | 390 return false; |
374 } | 391 } |
375 return true; | 392 return true; |
376 } | 393 } |
377 | 394 |
378 bool HotwordService::IsServiceAvailable() { | 395 bool HotwordService::IsServiceAvailable() { |
379 error_message_ = 0; | 396 error_message_ = 0; |
380 | 397 |
381 // Determine if the extension is available. | 398 // Determine if the extension is available. |
382 extensions::ExtensionSystem* system = | 399 extensions::ExtensionSystem* system = |
383 extensions::ExtensionSystem::Get(profile_); | 400 extensions::ExtensionSystem::Get(profile_); |
384 ExtensionService* service = system->extension_service(); | 401 ExtensionService* service = system->extension_service(); |
385 // Include disabled extensions (true parameter) since it may not be enabled | 402 // Include disabled extensions (true parameter) since it may not be enabled |
386 // if the user opted out. | 403 // if the user opted out. |
387 std::string extensionId; | 404 std::string extensionId; |
388 if (IsExperimentalHotwordingEnabled()) { | 405 if (IsExperimentalHotwordingEnabled()) { |
389 // TODO(amistry): Handle reloading on language change as the old extension | |
390 // does. | |
391 extensionId = extension_misc::kHotwordSharedModuleId; | 406 extensionId = extension_misc::kHotwordSharedModuleId; |
392 } else { | 407 } else { |
393 extensionId = extension_misc::kHotwordExtensionId; | 408 extensionId = extension_misc::kHotwordExtensionId; |
394 } | 409 } |
395 const extensions::Extension* extension = | 410 const extensions::Extension* extension = |
396 service->GetExtensionById(extensionId, true); | 411 service->GetExtensionById(extensionId, true); |
397 if (!extension) | 412 if (!extension) |
398 error_message_ = IDS_HOTWORD_GENERIC_ERROR_MESSAGE; | 413 error_message_ = IDS_HOTWORD_GENERIC_ERROR_MESSAGE; |
399 | 414 |
400 RecordExtensionAvailabilityMetrics(service, extension); | 415 RecordExtensionAvailabilityMetrics(service, extension); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 return false; | 546 return false; |
532 | 547 |
533 std::string previous_locale = | 548 std::string previous_locale = |
534 profile_->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage); | 549 profile_->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage); |
535 std::string locale = GetCurrentLocale(profile_); | 550 std::string locale = GetCurrentLocale(profile_); |
536 | 551 |
537 // If it's a new locale, then the old extension should be uninstalled. | 552 // If it's a new locale, then the old extension should be uninstalled. |
538 return locale != previous_locale && | 553 return locale != previous_locale && |
539 HotwordService::DoesHotwordSupportLanguage(profile_); | 554 HotwordService::DoesHotwordSupportLanguage(profile_); |
540 } | 555 } |
OLD | NEW |