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

Side by Side 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: Fix test Created 6 years, 9 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 unified diff | Download patch
OLDNEW
(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_(
50 new ScopedObserver<InstallTracker, InstallObserver>(this)) {
51 install_observer_->Add(InstallTrackerFactory::GetForProfile(
52 Profile::FromBrowserContext(browser_context)));
53 }
54
55 WebstoreAPI::~WebstoreAPI() {}
56
57 // static
58 WebstoreAPI* WebstoreAPI::Get(content::BrowserContext* browser_context) {
59 return ProfileKeyedAPIFactory<WebstoreAPI>::GetForProfile(browser_context);
60 }
61
62 void WebstoreAPI::OnInlineInstallStart(int routing_id,
63 IPC::Sender* ipc_sender,
64 const std::string& extension_id,
65 int listeners_mask) {
66 if (listeners_mask & api::webstore::INSTALL_STAGE_LISTENER) {
67 install_stage_listeners_.push_back(
68 ObservedInstallInfo(routing_id, extension_id, ipc_sender));
69 }
70
71 if (listeners_mask & api::webstore::DOWNLOAD_PROGRESS_LISTENER) {
72 download_progress_listeners_.push_back(
73 ObservedInstallInfo(routing_id, extension_id, ipc_sender));
74 }
75 }
76
77 void WebstoreAPI::OnInlineInstallFinished(int routing_id,
78 const std::string& extension_id) {
79 RemoveListeners(routing_id, extension_id, &download_progress_listeners_);
80 RemoveListeners(routing_id, extension_id, &install_stage_listeners_);
81 }
82
83 void WebstoreAPI::OnBeginExtensionDownload(const std::string& extension_id) {
84 SendInstallMessageIfObserved(extension_id,
85 api::webstore::kInstallStageDownloading);
86 }
87
88 void WebstoreAPI::OnDownloadProgress(const std::string& extension_id,
89 int percent_downloaded) {
90 for (ObservedInstallInfoList::const_iterator iter =
91 download_progress_listeners_.begin();
92 iter != download_progress_listeners_.end();
93 ++iter) {
94 if (iter->extension_id == extension_id) {
95 iter->ipc_sender->Send(new ExtensionMsg_InlineInstallDownloadProgress(
96 iter->routing_id, percent_downloaded));
97 }
98 }
99 }
100
101 void WebstoreAPI::OnBeginCrxInstall(const std::string& extension_id) {
102 SendInstallMessageIfObserved(extension_id,
103 api::webstore::kInstallStageInstalling);
104 }
105
106 void WebstoreAPI::OnShutdown() {
107 install_observer_.reset();
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698