OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/extensions/api/webstore/webstore_api.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/install_tracker.h" |
| 10 #include "chrome/browser/extensions/install_tracker_factory.h" |
| 11 #include "chrome/common/extensions/api/webstore_api_constants.h" |
| 12 #include "chrome/common/extensions/extension_messages.h" |
| 13 #include "extensions/browser/extension_system.h" |
| 14 #include "ipc/ipc_sender.h" |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 namespace { |
| 19 |
| 20 base::LazyInstance<ProfileKeyedAPIFactory<WebstoreAPI> > g_factory = |
| 21 LAZY_INSTANCE_INITIALIZER; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 WebstoreAPI::ObservedInstallInfo::ObservedInstallInfo( |
| 26 int routing_id, |
| 27 const std::string& extension_id, |
| 28 IPC::Sender* ipc_sender) |
| 29 : routing_id(routing_id), |
| 30 extension_id(extension_id), |
| 31 ipc_sender(ipc_sender) {} |
| 32 |
| 33 WebstoreAPI::ObservedInstallInfo::~ObservedInstallInfo() {} |
| 34 |
| 35 WebstoreAPI::WebstoreAPI(Profile* profile) : profile_(profile) { |
| 36 InstallTrackerFactory::GetForProfile(profile)->AddObserver(this); |
| 37 } |
| 38 |
| 39 WebstoreAPI::~WebstoreAPI() {} |
| 40 |
| 41 // static |
| 42 WebstoreAPI* WebstoreAPI::Get(Profile* profile) { |
| 43 return ProfileKeyedAPIFactory<WebstoreAPI>::GetForProfile(profile); |
| 44 } |
| 45 |
| 46 void WebstoreAPI::OnInlineInstallStart(int routing_id, |
| 47 IPC::Sender* ipc_sender, |
| 48 const std::string& extension_id, |
| 49 int listeners_mask) { |
| 50 if (listeners_mask & api::webstore::INSTALL_STAGE_LISTENER) { |
| 51 install_stage_listeners_.push_back( |
| 52 ObservedInstallInfo(routing_id, extension_id, ipc_sender)); |
| 53 } |
| 54 |
| 55 if (listeners_mask & api::webstore::DOWNLOAD_PROGRESS_LISTENER) { |
| 56 download_progress_listeners_.push_back( |
| 57 ObservedInstallInfo(routing_id, extension_id, ipc_sender)); |
| 58 } |
| 59 } |
| 60 |
| 61 void WebstoreAPI::OnInlineInstallFinished(int routing_id, |
| 62 const std::string& extension_id) { |
| 63 RemoveListeners(routing_id, extension_id, &download_progress_listeners_); |
| 64 RemoveListeners(routing_id, extension_id, &install_stage_listeners_); |
| 65 } |
| 66 |
| 67 void WebstoreAPI::OnBeginExtensionDownload(const std::string& extension_id) { |
| 68 SendInstallMessageIfObserved(extension_id, |
| 69 api::webstore::kInstallStageDownloading); |
| 70 } |
| 71 |
| 72 void WebstoreAPI::OnDownloadProgress(const std::string& extension_id, |
| 73 int percent_downloaded) { |
| 74 for (ObservedInstallInfoList::const_iterator iter = |
| 75 download_progress_listeners_.begin(); |
| 76 iter != download_progress_listeners_.end(); |
| 77 ++iter) { |
| 78 if (iter->extension_id == extension_id) { |
| 79 iter->ipc_sender->Send(new ExtensionMsg_InlineInstallDownloadProgress( |
| 80 iter->routing_id, percent_downloaded)); |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 85 void WebstoreAPI::OnBeginCrxInstall(const std::string& extension_id) { |
| 86 SendInstallMessageIfObserved(extension_id, |
| 87 api::webstore::kInstallStageInstalling); |
| 88 } |
| 89 |
| 90 void WebstoreAPI::Shutdown() { |
| 91 InstallTrackerFactory::GetForProfile(profile_)->RemoveObserver(this); |
| 92 } |
| 93 |
| 94 // static |
| 95 ProfileKeyedAPIFactory<WebstoreAPI>* WebstoreAPI::GetFactoryInstance() { |
| 96 return g_factory.Pointer(); |
| 97 } |
| 98 |
| 99 void WebstoreAPI::SendInstallMessageIfObserved( |
| 100 const std::string& extension_id, |
| 101 const std::string& install_stage) { |
| 102 for (ObservedInstallInfoList::const_iterator iter = |
| 103 install_stage_listeners_.begin(); |
| 104 iter != install_stage_listeners_.end(); |
| 105 ++iter) { |
| 106 if (iter->extension_id == extension_id) { |
| 107 iter->ipc_sender->Send(new ExtensionMsg_InlineInstallStageChanged( |
| 108 iter->routing_id, install_stage)); |
| 109 } |
| 110 } |
| 111 } |
| 112 |
| 113 void WebstoreAPI::RemoveListeners(int routing_id, |
| 114 const std::string& extension_id, |
| 115 ObservedInstallInfoList* listeners) { |
| 116 for (ObservedInstallInfoList::iterator iter = listeners->begin(); |
| 117 iter != listeners->end();) { |
| 118 if (iter->extension_id == extension_id && iter->routing_id == routing_id) |
| 119 iter = listeners->erase(iter); |
| 120 else |
| 121 ++iter; |
| 122 } |
| 123 } |
| 124 |
| 125 } // namespace extensions |
OLD | NEW |