Index: chrome/browser/extensions/api/webstore/webstore_api.cc |
diff --git a/chrome/browser/extensions/api/webstore/webstore_api.cc b/chrome/browser/extensions/api/webstore/webstore_api.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..851616069fe7e759b41ba8a9267efbeb76f6ecc6 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/webstore/webstore_api.cc |
@@ -0,0 +1,125 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/api/webstore/webstore_api.h" |
+ |
+#include "base/lazy_instance.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/install_tracker.h" |
+#include "chrome/browser/extensions/install_tracker_factory.h" |
+#include "chrome/common/extensions/api/webstore/webstore_api_constants.h" |
+#include "chrome/common/extensions/extension_messages.h" |
+#include "extensions/browser/extension_system.h" |
+#include "ipc/ipc_sender.h" |
+ |
+namespace extensions { |
+ |
+namespace { |
+ |
+base::LazyInstance<ProfileKeyedAPIFactory<WebstoreAPI> > g_factory = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+} // namespace |
+ |
+WebstoreAPI::ObservedInstallInfo::ObservedInstallInfo( |
+ int routing_id, |
+ const std::string& extension_id, |
+ IPC::Sender* ipc_sender) |
+ : routing_id(routing_id), |
+ extension_id(extension_id), |
+ ipc_sender(ipc_sender) {} |
+ |
+WebstoreAPI::ObservedInstallInfo::~ObservedInstallInfo() {} |
+ |
+WebstoreAPI::WebstoreAPI(Profile* profile) : profile_(profile) { |
+ InstallTrackerFactory::GetForProfile(profile)->AddObserver(this); |
+} |
+ |
+WebstoreAPI::~WebstoreAPI() {} |
+ |
+// static |
+WebstoreAPI* WebstoreAPI::Get(Profile* profile) { |
+ return ProfileKeyedAPIFactory<WebstoreAPI>::GetForProfile(profile); |
+} |
+ |
+void WebstoreAPI::OnInlineInstallStart(int routing_id, |
+ IPC::Sender* ipc_sender, |
+ const std::string& extension_id, |
+ int listeners_mask) { |
+ if (listeners_mask & api::webstore::INSTALL_STAGE_LISTENER) { |
+ install_stage_listeners_.push_back( |
+ ObservedInstallInfo(routing_id, extension_id, ipc_sender)); |
+ } |
+ |
+ if (listeners_mask & api::webstore::DOWNLOAD_PROGRESS_LISTENER) { |
+ download_progress_listeners_.push_back( |
+ ObservedInstallInfo(routing_id, extension_id, ipc_sender)); |
+ } |
+} |
+ |
+void WebstoreAPI::OnInlineInstallFinished(int routing_id, |
+ const std::string& extension_id) { |
+ RemoveListeners(routing_id, extension_id, &download_progress_listeners_); |
+ RemoveListeners(routing_id, extension_id, &install_stage_listeners_); |
+} |
+ |
+void WebstoreAPI::OnBeginExtensionDownload(const std::string& extension_id) { |
+ SendInstallMessageIfObserved(extension_id, |
+ api::webstore::kInstallStageDownloading); |
+} |
+ |
+void WebstoreAPI::OnDownloadProgress(const std::string& extension_id, |
+ int percent_downloaded) { |
+ for (ObservedInstallInfoList::const_iterator iter = |
+ download_progress_listeners_.begin(); |
+ iter != download_progress_listeners_.end(); |
+ ++iter) { |
+ if (iter->extension_id == extension_id) { |
+ iter->ipc_sender->Send(new ExtensionMsg_InlineInstallDownloadProgress( |
+ iter->routing_id, percent_downloaded)); |
+ } |
+ } |
+} |
+ |
+void WebstoreAPI::OnBeginCrxInstall(const std::string& extension_id) { |
+ SendInstallMessageIfObserved(extension_id, |
+ api::webstore::kInstallStageInstalling); |
+} |
+ |
+void WebstoreAPI::Shutdown() { |
+ InstallTrackerFactory::GetForProfile(profile_)->RemoveObserver(this); |
+} |
+ |
+// static |
+ProfileKeyedAPIFactory<WebstoreAPI>* WebstoreAPI::GetFactoryInstance() { |
+ return g_factory.Pointer(); |
+} |
+ |
+void WebstoreAPI::SendInstallMessageIfObserved( |
+ const std::string& extension_id, |
+ const std::string& install_stage) { |
+ for (ObservedInstallInfoList::const_iterator iter = |
+ install_stage_listeners_.begin(); |
+ iter != install_stage_listeners_.end(); |
+ ++iter) { |
+ if (iter->extension_id == extension_id) { |
+ iter->ipc_sender->Send(new ExtensionMsg_InlineInstallStageChanged( |
+ iter->routing_id, install_stage)); |
+ } |
+ } |
+} |
+ |
+void WebstoreAPI::RemoveListeners(int routing_id, |
+ const std::string& extension_id, |
+ ObservedInstallInfoList* listeners) { |
+ for (ObservedInstallInfoList::iterator iter = listeners->begin(); |
+ iter != listeners->end();) { |
+ if (iter->extension_id == extension_id && iter->routing_id == routing_id) |
+ iter = listeners->erase(iter); |
+ else |
+ ++iter; |
not at google - send to devlin
2014/02/27 21:49:56
is this why you're using a std::list rather than a
Devlin
2014/02/28 18:04:51
Yeah, I was using a list for the removal speed. I
|
+ } |
+} |
+ |
+} // namespace extensions |