Revert 285173 "Removed InProcessBrowserTest::CleanUpOnMainThread()"
[chromium-blink-merge.git] / chrome / browser / extensions / api / webstore / webstore_api.cc
bloba0def2af014f82be959e2cf764359189e5b3fa21
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.
5 #include "chrome/browser/extensions/api/webstore/webstore_api.h"
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/chrome_extension_messages.h"
13 #include "extensions/browser/extension_system.h"
14 #include "ipc/ipc_sender.h"
16 namespace extensions {
18 namespace {
20 base::LazyInstance<BrowserContextKeyedAPIFactory<WebstoreAPI> > g_factory =
21 LAZY_INSTANCE_INITIALIZER;
23 } // namespace
25 struct WebstoreAPI::ObservedInstallInfo {
26 ObservedInstallInfo(int routing_id,
27 const std::string& extension_id,
28 IPC::Sender* ipc_sender);
29 ~ObservedInstallInfo();
31 int routing_id;
32 std::string extension_id;
33 IPC::Sender* ipc_sender;
36 WebstoreAPI::ObservedInstallInfo::ObservedInstallInfo(
37 int routing_id,
38 const std::string& extension_id,
39 IPC::Sender* ipc_sender)
40 : routing_id(routing_id),
41 extension_id(extension_id),
42 ipc_sender(ipc_sender) {}
44 WebstoreAPI::ObservedInstallInfo::~ObservedInstallInfo() {}
46 WebstoreAPI::WebstoreAPI(content::BrowserContext* browser_context)
47 : browser_context_(browser_context),
48 install_observer_(
49 new ScopedObserver<InstallTracker, InstallObserver>(this)) {
50 install_observer_->Add(InstallTrackerFactory::GetForProfile(
51 Profile::FromBrowserContext(browser_context)));
54 WebstoreAPI::~WebstoreAPI() {}
56 // static
57 WebstoreAPI* WebstoreAPI::Get(content::BrowserContext* browser_context) {
58 return BrowserContextKeyedAPIFactory<WebstoreAPI>::Get(browser_context);
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));
70 if (listeners_mask & api::webstore::DOWNLOAD_PROGRESS_LISTENER) {
71 download_progress_listeners_.push_back(
72 ObservedInstallInfo(routing_id, extension_id, ipc_sender));
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_);
82 void WebstoreAPI::OnBeginExtensionDownload(const std::string& extension_id) {
83 SendInstallMessageIfObserved(extension_id,
84 api::webstore::INSTALL_STAGE_DOWNLOADING);
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));
100 void WebstoreAPI::OnBeginCrxInstall(const std::string& extension_id) {
101 SendInstallMessageIfObserved(extension_id,
102 api::webstore::INSTALL_STAGE_INSTALLING);
105 void WebstoreAPI::OnShutdown() {
106 install_observer_.reset();
109 void WebstoreAPI::Shutdown() {}
111 // static
112 BrowserContextKeyedAPIFactory<WebstoreAPI>* WebstoreAPI::GetFactoryInstance() {
113 return g_factory.Pointer();
116 void WebstoreAPI::SendInstallMessageIfObserved(
117 const std::string& extension_id,
118 api::webstore::InstallStage install_stage) {
119 for (ObservedInstallInfoList::const_iterator iter =
120 install_stage_listeners_.begin();
121 iter != install_stage_listeners_.end();
122 ++iter) {
123 if (iter->extension_id == extension_id) {
124 iter->ipc_sender->Send(new ExtensionMsg_InlineInstallStageChanged(
125 iter->routing_id, install_stage));
130 void WebstoreAPI::RemoveListeners(int routing_id,
131 const std::string& extension_id,
132 ObservedInstallInfoList* listeners) {
133 for (ObservedInstallInfoList::iterator iter = listeners->begin();
134 iter != listeners->end();) {
135 if (iter->extension_id == extension_id && iter->routing_id == routing_id)
136 iter = listeners->erase(iter);
137 else
138 ++iter;
142 } // namespace extensions