Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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/chromeos/ui/screensaver_extension_dialog.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "chrome/browser/extensions/extension_service.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/profiles/profile_manager.h" | |
| 13 #include "chrome/browser/sessions/restore_tab_helper.h" | |
| 14 #include "chrome/browser/ui/views/extensions/extension_dialog.h" | |
| 15 #include "chrome/common/chrome_switches.h" | |
| 16 #include "chrome/common/extensions/extension.h" | |
| 17 #include "chrome/common/extensions/extension_file_util.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 ScreensaverExtensionDialog* g_instance = NULL; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 namespace browser { | |
| 28 | |
| 29 void ShowScreensaverDialog() { | |
| 30 ScreensaverExtensionDialog::ShowScreensaverDialog(); | |
| 31 } | |
| 32 | |
| 33 void CloseScreensaverDialog() { | |
| 34 ScreensaverExtensionDialog::CloseScreensaverDialog(); | |
| 35 } | |
| 36 | |
| 37 } // namespace browser | |
| 38 | |
| 39 // static | |
| 40 void ScreensaverExtensionDialog::ShowScreensaverDialog() { | |
| 41 if (!g_instance) | |
| 42 g_instance = new ScreensaverExtensionDialog(); | |
| 43 g_instance->Show(); | |
| 44 } | |
| 45 | |
| 46 // static | |
| 47 void ScreensaverExtensionDialog::CloseScreensaverDialog() { | |
| 48 if (g_instance) | |
| 49 g_instance->Close(); | |
| 50 } | |
| 51 | |
| 52 ScreensaverExtensionDialog::ScreensaverExtensionDialog() | |
| 53 : screensaver_extension_(NULL), loading_extension_(false) { | |
|
sky
2012/02/27 15:52:43
nit: when you wrap, each param on its own line.
rkc
2012/02/27 22:24:43
Done.
| |
| 54 } | |
| 55 | |
| 56 void ScreensaverExtensionDialog::LoadExtension() { | |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 58 std::string error; | |
| 59 std::string extension_path = CommandLine::ForCurrentProcess()-> | |
| 60 GetSwitchValueASCII(switches::kKioskModeScreensaverPath); | |
| 61 | |
| 62 scoped_refptr<Extension> screensaver_extension = | |
| 63 extension_file_util::LoadExtension( | |
| 64 FilePath(extension_path), | |
| 65 Extension::COMPONENT, | |
| 66 Extension::NO_FLAGS, | |
| 67 &error); | |
| 68 | |
| 69 if (!screensaver_extension) { | |
| 70 LOG(ERROR) << "Could not load screensaver extension from: " << | |
| 71 extension_path; | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 BrowserThread::PostTask(BrowserThread::UI, | |
| 76 FROM_HERE, | |
| 77 base::Bind( | |
| 78 &ScreensaverExtensionDialog::SetExtensionAndShow, | |
| 79 base::Unretained(this), | |
| 80 screensaver_extension)); | |
| 81 } | |
| 82 | |
| 83 void ScreensaverExtensionDialog::SetExtensionAndShow( | |
| 84 scoped_refptr<Extension> extension) { | |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 86 screensaver_extension_ = extension; | |
| 87 loading_extension_ = false; | |
| 88 Show(); | |
| 89 } | |
| 90 | |
| 91 void ScreensaverExtensionDialog::Show() { | |
| 92 // Whenever we're loading the extension, Show() will | |
| 93 // be called after the load finishes, so return. | |
| 94 if (loading_extension_) return; | |
|
sky
2012/02/27 15:52:43
Don't use single line ifs.
rkc
2012/02/27 22:24:43
Done.
| |
| 95 | |
| 96 if (!screensaver_extension_) { | |
| 97 loading_extension_ = true; | |
| 98 BrowserThread::PostTask(BrowserThread::FILE, | |
| 99 FROM_HERE, | |
| 100 base::Bind( | |
| 101 &ScreensaverExtensionDialog::LoadExtension, | |
| 102 base::Unretained(this))); | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 107 Profile* default_profile = ProfileManager::GetDefaultProfile(); | |
| 108 if (default_profile->GetExtensionService()->AddExtension( | |
| 109 screensaver_extension_)) { | |
| 110 extension_dialog_ = ExtensionDialog::ShowFullscreen( | |
| 111 screensaver_extension_->GetFullLaunchURL(), | |
| 112 default_profile, | |
| 113 string16(), | |
| 114 this); | |
| 115 } else { | |
| 116 LOG(ERROR) << "Couldn't add screensaver extension to profile."; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void ScreensaverExtensionDialog::Close() { | |
| 121 if (extension_dialog_) { | |
|
sky
2012/02/27 15:52:43
Is it possible for close to be invoked while you'r
rkc
2012/02/27 22:24:43
Yep, but in that case extension_dialog will be nul
| |
| 122 extension_dialog_->Close(); | |
| 123 extension_dialog_ = NULL; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 ScreensaverExtensionDialog::~ScreensaverExtensionDialog() { | |
| 128 if (extension_dialog_) | |
| 129 extension_dialog_->ObserverDestroyed(); | |
| 130 } | |
| 131 | |
| 132 void ScreensaverExtensionDialog::ExtensionDialogClosing( | |
| 133 ExtensionDialog* dialog) { | |
| 134 // Release our reference to the dialog to allow it to close. | |
| 135 extension_dialog_ = NULL; | |
| 136 } | |
| 137 | |
| 138 void ScreensaverExtensionDialog::ExtensionTerminated( | |
| 139 ExtensionDialog* dialog) { | |
| 140 MessageLoop::current()->PostTask(FROM_HERE, | |
|
sky
2012/02/27 15:52:43
Add comment as to why you need to delay this.
rkc
2012/02/27 22:24:43
Done.
| |
| 141 base::Bind(&ScreensaverExtensionDialog::ReloadAndShow, | |
| 142 base::Unretained(this))); | |
| 143 dialog->Close(); | |
| 144 } | |
| 145 | |
| 146 void ScreensaverExtensionDialog::ReloadAndShow() { | |
| 147 ProfileManager::GetDefaultProfile()->GetExtensionService()->ReloadExtension( | |
| 148 screensaver_extension_->id()); | |
| 149 | |
| 150 Show(); | |
| 151 } | |
| OLD | NEW |