| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/extensions/bundle_installer.h" | 5 #include "chrome/browser/extensions/bundle_installer.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/crx_installer.h" | 13 #include "chrome/browser/extensions/crx_installer.h" |
| 14 #include "chrome/browser/extensions/extension_install_dialog.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/common/extensions/extension.h" |
| 16 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/navigation_controller.h" | 20 #include "content/public/browser/navigation_controller.h" |
| 18 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 #include "grit/generated_resources.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 19 | 24 |
| 20 using content::NavigationController; | 25 using content::NavigationController; |
| 21 | 26 |
| 22 namespace extensions { | 27 namespace extensions { |
| 23 | 28 |
| 24 namespace { | 29 namespace { |
| 25 | 30 |
| 26 enum AutoApproveForTest { | 31 enum AutoApproveForTest { |
| 27 DO_NOT_SKIP = 0, | 32 DO_NOT_SKIP = 0, |
| 28 PROCEED, | 33 PROCEED, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 43 | 48 |
| 44 std::string error; | 49 std::string error; |
| 45 return Extension::Create(FilePath(), | 50 return Extension::Create(FilePath(), |
| 46 Extension::INTERNAL, | 51 Extension::INTERNAL, |
| 47 *manifest, | 52 *manifest, |
| 48 Extension::NO_FLAGS, | 53 Extension::NO_FLAGS, |
| 49 item.id, | 54 item.id, |
| 50 &error); | 55 &error); |
| 51 } | 56 } |
| 52 | 57 |
| 58 bool IsAppPredicate(scoped_refptr<const Extension> extension) { |
| 59 return extension->is_app(); |
| 60 } |
| 61 |
| 62 struct MatchIdFunctor { |
| 63 explicit MatchIdFunctor(const std::string& id) : id(id) {} |
| 64 bool operator()(scoped_refptr<const Extension> extension) { |
| 65 return extension->id() == id; |
| 66 } |
| 67 std::string id; |
| 68 }; |
| 69 |
| 70 // Holds the message IDs for BundleInstaller::GetHeadingTextFor. |
| 71 const int kHeadingIds[3][4] = { |
| 72 { |
| 73 0, |
| 74 IDS_EXTENSION_BUNDLE_INSTALL_PROMPT_HEADING_EXTENSIONS, |
| 75 IDS_EXTENSION_BUNDLE_INSTALL_PROMPT_HEADING_APPS, |
| 76 IDS_EXTENSION_BUNDLE_INSTALL_PROMPT_HEADING_EXTENSION_APPS |
| 77 }, |
| 78 { |
| 79 0, |
| 80 IDS_EXTENSION_BUNDLE_INSTALLED_HEADING_EXTENSIONS, |
| 81 IDS_EXTENSION_BUNDLE_INSTALLED_HEADING_APPS, |
| 82 IDS_EXTENSION_BUNDLE_INSTALLED_HEADING_EXTENSION_APPS |
| 83 }, |
| 84 { IDS_EXTENSION_BUNDLE_ERROR_HEADING, 0, 0, 0 } |
| 85 }; |
| 86 |
| 53 } // namespace | 87 } // namespace |
| 54 | 88 |
| 55 // static | 89 // static |
| 56 void BundleInstaller::SetAutoApproveForTesting(bool auto_approve) { | 90 void BundleInstaller::SetAutoApproveForTesting(bool auto_approve) { |
| 57 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)); | 91 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)); |
| 58 g_auto_approve_for_test = auto_approve ? PROCEED : ABORT; | 92 g_auto_approve_for_test = auto_approve ? PROCEED : ABORT; |
| 59 } | 93 } |
| 60 | 94 |
| 61 BundleInstaller::Item::Item() : state(STATE_PENDING) {} | 95 BundleInstaller::Item::Item() : state(STATE_PENDING) {} |
| 62 | 96 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( | 154 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( |
| 121 profile_, | 155 profile_, |
| 122 this, | 156 this, |
| 123 controller, | 157 controller, |
| 124 i->first, | 158 i->first, |
| 125 WebstoreInstaller::FLAG_NONE); | 159 WebstoreInstaller::FLAG_NONE); |
| 126 installer->Start(); | 160 installer->Start(); |
| 127 } | 161 } |
| 128 } | 162 } |
| 129 | 163 |
| 164 string16 BundleInstaller::GetHeadingTextFor(Item::State state) const { |
| 165 size_t total = 0; |
| 166 size_t apps = 0; |
| 167 |
| 168 // For STATE_FAILED, we can't tell if the items were apps or extensions |
| 169 // so we always show the same message. |
| 170 if (state == Item::STATE_INSTALLED || state == Item::STATE_PENDING) { |
| 171 total = GetItemsWithState(state).size(); |
| 172 apps = std::count_if( |
| 173 dummy_extensions_.begin(), dummy_extensions_.end(), &IsAppPredicate); |
| 174 } |
| 175 |
| 176 bool has_apps = apps > 0; |
| 177 bool has_extensions = apps < total; |
| 178 size_t index = (has_extensions << 0) + (has_apps << 1); |
| 179 |
| 180 CHECK_LT(static_cast<size_t>(state), arraysize(kHeadingIds)); |
| 181 CHECK_LT(index, arraysize(kHeadingIds[state])); |
| 182 |
| 183 int msg_id = kHeadingIds[state][index]; |
| 184 if (!msg_id) |
| 185 return string16(); |
| 186 |
| 187 return l10n_util::GetStringUTF16(msg_id); |
| 188 } |
| 189 |
| 190 #if !defined(TOOLKIT_GTK) |
| 130 // static | 191 // static |
| 131 void BundleInstaller::ShowInstalledBubble( | 192 void BundleInstaller::ShowInstalledBubble( |
| 132 const BundleInstaller* bundle, Browser* browser) { | 193 const BundleInstaller* bundle, Browser* browser) { |
| 133 // TODO(jstritar): provide platform specific implementations. | 194 // TODO(jstritar): provide platform specific implementations. |
| 134 } | 195 } |
| 196 #endif |
| 135 | 197 |
| 136 void BundleInstaller::ParseManifests() { | 198 void BundleInstaller::ParseManifests() { |
| 137 if (items_.empty()) { | 199 if (items_.empty()) { |
| 138 ReportCanceled(false); | 200 ReportCanceled(false); |
| 139 return; | 201 return; |
| 140 } | 202 } |
| 141 | 203 |
| 142 for (ItemMap::iterator i = items_.begin(); i != items_.end(); ++i) { | 204 for (ItemMap::iterator i = items_.begin(); i != items_.end(); ++i) { |
| 143 scoped_refptr<WebstoreInstallHelper> helper = new WebstoreInstallHelper( | 205 scoped_refptr<WebstoreInstallHelper> helper = new WebstoreInstallHelper( |
| 144 this, i->first, i->second.manifest, "", GURL(), NULL); | 206 this, i->first, i->second.manifest, "", GURL(), NULL); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 ReportCanceled(false); | 244 ReportCanceled(false); |
| 183 return; | 245 return; |
| 184 } | 246 } |
| 185 | 247 |
| 186 scoped_refptr<ExtensionPermissionSet> permissions; | 248 scoped_refptr<ExtensionPermissionSet> permissions; |
| 187 for (size_t i = 0; i < dummy_extensions_.size(); ++i) { | 249 for (size_t i = 0; i < dummy_extensions_.size(); ++i) { |
| 188 permissions = ExtensionPermissionSet::CreateUnion( | 250 permissions = ExtensionPermissionSet::CreateUnion( |
| 189 permissions, dummy_extensions_[i]->required_permission_set()); | 251 permissions, dummy_extensions_[i]->required_permission_set()); |
| 190 } | 252 } |
| 191 | 253 |
| 192 // TODO(jstritar): show the actual prompt. | 254 if (g_auto_approve_for_test == PROCEED) { |
| 193 if (g_auto_approve_for_test == PROCEED) | |
| 194 InstallUIProceed(); | 255 InstallUIProceed(); |
| 195 else if (g_auto_approve_for_test == ABORT) | 256 } else if (g_auto_approve_for_test == ABORT) { |
| 196 InstallUIAbort(true); | 257 InstallUIAbort(true); |
| 197 else | 258 } else { |
| 198 InstallUIAbort(false); | 259 ExtensionInstallUI::Prompt prompt( |
| 260 ExtensionInstallUI::BUNDLE_INSTALL_PROMPT); |
| 261 prompt.SetPermissions(permissions->GetWarningMessages()); |
| 262 prompt.set_bundle(this); |
| 263 |
| 264 ShowExtensionInstallDialog(profile_, this, prompt); |
| 265 } |
| 199 } | 266 } |
| 200 | 267 |
| 201 void BundleInstaller::ShowInstalledBubbleIfDone() { | 268 void BundleInstaller::ShowInstalledBubbleIfDone() { |
| 202 // We're ready to show the installed bubble when no items are pending. | 269 // We're ready to show the installed bubble when no items are pending. |
| 203 if (!GetItemsWithState(Item::STATE_PENDING).empty()) | 270 if (!GetItemsWithState(Item::STATE_PENDING).empty()) |
| 204 return; | 271 return; |
| 205 | 272 |
| 206 if (browser_) | 273 if (browser_) |
| 207 ShowInstalledBubble(this, browser_); | 274 ShowInstalledBubble(this, browser_); |
| 208 | 275 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 void BundleInstaller::OnExtensionInstallSuccess(const std::string& id) { | 322 void BundleInstaller::OnExtensionInstallSuccess(const std::string& id) { |
| 256 items_[id].state = Item::STATE_INSTALLED; | 323 items_[id].state = Item::STATE_INSTALLED; |
| 257 | 324 |
| 258 ShowInstalledBubbleIfDone(); | 325 ShowInstalledBubbleIfDone(); |
| 259 } | 326 } |
| 260 | 327 |
| 261 void BundleInstaller::OnExtensionInstallFailure(const std::string& id, | 328 void BundleInstaller::OnExtensionInstallFailure(const std::string& id, |
| 262 const std::string& error) { | 329 const std::string& error) { |
| 263 items_[id].state = Item::STATE_FAILED; | 330 items_[id].state = Item::STATE_FAILED; |
| 264 | 331 |
| 332 std::remove_if(dummy_extensions_.begin(), dummy_extensions_.end(), |
| 333 MatchIdFunctor(id)); |
| 334 |
| 265 ShowInstalledBubbleIfDone(); | 335 ShowInstalledBubbleIfDone(); |
| 266 } | 336 } |
| 267 | 337 |
| 268 void BundleInstaller::OnBrowserAdded(const Browser* browser) { | 338 void BundleInstaller::OnBrowserAdded(const Browser* browser) { |
| 269 } | 339 } |
| 270 | 340 |
| 271 void BundleInstaller::OnBrowserRemoved(const Browser* browser) { | 341 void BundleInstaller::OnBrowserRemoved(const Browser* browser) { |
| 272 if (browser_ == browser) | 342 if (browser_ == browser) |
| 273 browser_ = NULL; | 343 browser_ = NULL; |
| 274 } | 344 } |
| 275 | 345 |
| 276 void BundleInstaller::OnBrowserSetLastActive(const Browser* browser) { | 346 void BundleInstaller::OnBrowserSetLastActive(const Browser* browser) { |
| 277 } | 347 } |
| 278 | 348 |
| 279 } // namespace extensions | 349 } // namespace extensions |
| OLD | NEW |