Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1254)

Unified Diff: chrome/browser/extensions/api/webstore/webstore_api.cc

Issue 175263003: Add chrome.webstore API methods to allow sites to see progress of installation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..68a9bc9a9811b7a4c56e8874a845891cd93ab75c
--- /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_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;
+ }
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698