Chromium Code Reviews| 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/browser/profiles/profile.h" | |
| 12 #include "chrome/common/extensions/api/webstore/webstore_api_constants.h" | |
| 13 #include "chrome/common/extensions/extension_messages.h" | |
| 14 #include "extensions/browser/extension_system.h" | |
| 15 #include "ipc/ipc_sender.h" | |
| 16 | |
| 17 namespace extensions { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 base::LazyInstance<ProfileKeyedAPIFactory<WebstoreAPI> > g_factory = | |
| 22 LAZY_INSTANCE_INITIALIZER; | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 struct WebstoreAPI::ObservedInstallInfo { | |
| 27 ObservedInstallInfo(int routing_id, | |
| 28 const std::string& extension_id, | |
| 29 IPC::Sender* ipc_sender); | |
| 30 ~ObservedInstallInfo(); | |
| 31 | |
| 32 int routing_id; | |
| 33 std::string extension_id; | |
| 34 IPC::Sender* ipc_sender; | |
| 35 }; | |
| 36 | |
| 37 WebstoreAPI::ObservedInstallInfo::ObservedInstallInfo( | |
| 38 int routing_id, | |
| 39 const std::string& extension_id, | |
| 40 IPC::Sender* ipc_sender) | |
| 41 : routing_id(routing_id), | |
| 42 extension_id(extension_id), | |
| 43 ipc_sender(ipc_sender) {} | |
| 44 | |
| 45 WebstoreAPI::ObservedInstallInfo::~ObservedInstallInfo() {} | |
| 46 | |
| 47 WebstoreAPI::WebstoreAPI(content::BrowserContext* browser_context) | |
| 48 : browser_context_(browser_context), | |
| 49 install_observer_(this) { | |
| 50 install_observer_.Add(InstallTrackerFactory::GetForProfile( | |
| 51 Profile::FromBrowserContext(browser_context))); | |
| 52 } | |
| 53 | |
| 54 WebstoreAPI::~WebstoreAPI() {} | |
| 55 | |
| 56 // static | |
| 57 WebstoreAPI* WebstoreAPI::Get(content::BrowserContext* browser_context) { | |
| 58 return ProfileKeyedAPIFactory<WebstoreAPI>::GetForProfile(browser_context); | |
| 59 } | |
| 60 | |
| 61 void WebstoreAPI::OnInlineInstallStart(int routing_id, | |
| 62 IPC::Sender* ipc_sender, | |
| 63 const std::string& extension_id, | |
| 64 int listeners_mask) { | |
| 65 if (listeners_mask & api::webstore::INSTALL_STAGE_LISTENER) { | |
| 66 install_stage_listeners_.push_back( | |
| 67 ObservedInstallInfo(routing_id, extension_id, ipc_sender)); | |
| 68 } | |
| 69 | |
| 70 if (listeners_mask & api::webstore::DOWNLOAD_PROGRESS_LISTENER) { | |
| 71 download_progress_listeners_.push_back( | |
| 72 ObservedInstallInfo(routing_id, extension_id, ipc_sender)); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void WebstoreAPI::OnInlineInstallFinished(int routing_id, | |
| 77 const std::string& extension_id) { | |
| 78 RemoveListeners(routing_id, extension_id, &download_progress_listeners_); | |
| 79 RemoveListeners(routing_id, extension_id, &install_stage_listeners_); | |
| 80 } | |
| 81 | |
| 82 void WebstoreAPI::OnBeginExtensionDownload(const std::string& extension_id) { | |
| 83 SendInstallMessageIfObserved(extension_id, | |
| 84 api::webstore::kInstallStageDownloading); | |
| 85 } | |
| 86 | |
| 87 void WebstoreAPI::OnDownloadProgress(const std::string& extension_id, | |
| 88 int percent_downloaded) { | |
| 89 for (ObservedInstallInfoList::const_iterator iter = | |
| 90 download_progress_listeners_.begin(); | |
| 91 iter != download_progress_listeners_.end(); | |
| 92 ++iter) { | |
| 93 if (iter->extension_id == extension_id) { | |
| 94 iter->ipc_sender->Send(new ExtensionMsg_InlineInstallDownloadProgress( | |
| 95 iter->routing_id, percent_downloaded)); | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void WebstoreAPI::OnBeginCrxInstall(const std::string& extension_id) { | |
| 101 SendInstallMessageIfObserved(extension_id, | |
| 102 api::webstore::kInstallStageInstalling); | |
| 103 } | |
| 104 | |
| 105 void WebstoreAPI::OnShutdown() { | |
| 106 install_observer_.Remove(InstallTrackerFactory::GetForProfile( | |
|
Devlin
2014/03/03 21:15:13
Is it okay to fetch a BCKS while it's being Shutdo
asargent_no_longer_on_chrome
2014/03/03 22:25:33
I think so. From components/browser_context_keyed_
Devlin
2014/03/03 22:49:16
Yep, that was my contingency plan :) And actually
| |
| 107 Profile::FromBrowserContext(browser_context_))); | |
| 108 } | |
| 109 | |
| 110 void WebstoreAPI::Shutdown() {} | |
| 111 | |
| 112 // static | |
| 113 ProfileKeyedAPIFactory<WebstoreAPI>* WebstoreAPI::GetFactoryInstance() { | |
| 114 return g_factory.Pointer(); | |
| 115 } | |
| 116 | |
| 117 void WebstoreAPI::SendInstallMessageIfObserved( | |
| 118 const std::string& extension_id, | |
| 119 const char* install_stage) { | |
| 120 for (ObservedInstallInfoList::const_iterator iter = | |
| 121 install_stage_listeners_.begin(); | |
| 122 iter != install_stage_listeners_.end(); | |
| 123 ++iter) { | |
| 124 if (iter->extension_id == extension_id) { | |
| 125 iter->ipc_sender->Send(new ExtensionMsg_InlineInstallStageChanged( | |
| 126 iter->routing_id, install_stage)); | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 void WebstoreAPI::RemoveListeners(int routing_id, | |
| 132 const std::string& extension_id, | |
| 133 ObservedInstallInfoList* listeners) { | |
| 134 for (ObservedInstallInfoList::iterator iter = listeners->begin(); | |
| 135 iter != listeners->end();) { | |
| 136 if (iter->extension_id == extension_id && iter->routing_id == routing_id) | |
| 137 iter = listeners->erase(iter); | |
| 138 else | |
| 139 ++iter; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 } // namespace extensions | |
| OLD | NEW |