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/chrome_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<BrowserContextKeyedAPIFactory<WebstoreAPI> > g_factory =
21 LAZY_INSTANCE_INITIALIZER;
22
23 } // namespace
24
25 struct WebstoreAPI::ObservedInstallInfo {
26 ObservedInstallInfo(int routing_id,
27 const std::string& extension_id,
28 IPC::Sender* ipc_sender);
29 ~ObservedInstallInfo();
30
31 int routing_id;
32 std::string extension_id;
33 IPC::Sender* ipc_sender;
34 };
35
ObservedInstallInfo(int routing_id,const std::string & extension_id,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) {}
43
~ObservedInstallInfo()44 WebstoreAPI::ObservedInstallInfo::~ObservedInstallInfo() {}
45
WebstoreAPI(content::BrowserContext * browser_context)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)));
52 }
53
~WebstoreAPI()54 WebstoreAPI::~WebstoreAPI() {}
55
56 // static
Get(content::BrowserContext * browser_context)57 WebstoreAPI* WebstoreAPI::Get(content::BrowserContext* browser_context) {
58 return BrowserContextKeyedAPIFactory<WebstoreAPI>::Get(browser_context);
59 }
60
OnInlineInstallStart(int routing_id,IPC::Sender * ipc_sender,const std::string & extension_id,int listeners_mask)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
OnInlineInstallFinished(int routing_id,const std::string & extension_id)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
OnBeginExtensionDownload(const std::string & extension_id)82 void WebstoreAPI::OnBeginExtensionDownload(const std::string& extension_id) {
83 SendInstallMessageIfObserved(extension_id,
84 api::webstore::INSTALL_STAGE_DOWNLOADING);
85 }
86
OnDownloadProgress(const std::string & extension_id,int percent_downloaded)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
OnBeginCrxInstall(const std::string & extension_id)100 void WebstoreAPI::OnBeginCrxInstall(const std::string& extension_id) {
101 SendInstallMessageIfObserved(extension_id,
102 api::webstore::INSTALL_STAGE_INSTALLING);
103 }
104
OnShutdown()105 void WebstoreAPI::OnShutdown() {
106 install_observer_.reset();
107 }
108
Shutdown()109 void WebstoreAPI::Shutdown() {}
110
111 // static
GetFactoryInstance()112 BrowserContextKeyedAPIFactory<WebstoreAPI>* WebstoreAPI::GetFactoryInstance() {
113 return g_factory.Pointer();
114 }
115
SendInstallMessageIfObserved(const std::string & extension_id,api::webstore::InstallStage install_stage)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));
126 }
127 }
128 }
129
RemoveListeners(int routing_id,const std::string & extension_id,ObservedInstallInfoList * listeners)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;
139 }
140 }
141
142 } // namespace extensions
143