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

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: Latest master 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/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
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
44 WebstoreAPI::ObservedInstallInfo::~ObservedInstallInfo() {}
45
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
54 WebstoreAPI::~WebstoreAPI() {}
55
56 // static
57 WebstoreAPI* WebstoreAPI::Get(content::BrowserContext* browser_context) {
58 return BrowserContextKeyedAPIFactory<WebstoreAPI>::Get(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::INSTALL_STAGE_DOWNLOADING);
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::INSTALL_STAGE_INSTALLING);
103 }
104
105 void WebstoreAPI::OnShutdown() {
106 install_observer_.reset();
107 }
108
109 void WebstoreAPI::Shutdown() {}
110
111 // static
112 BrowserContextKeyedAPIFactory<WebstoreAPI>* WebstoreAPI::GetFactoryInstance() {
113 return g_factory.Pointer();
114 }
115
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698