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

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: Use Event structure 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 WebstoreAPI::ObservedInstallInfo::ObservedInstallInfo(
27 int routing_id,
28 const std::string& extension_id,
29 IPC::Sender* ipc_sender)
30 : routing_id(routing_id),
31 extension_id(extension_id),
32 ipc_sender(ipc_sender) {}
33
34 WebstoreAPI::ObservedInstallInfo::~ObservedInstallInfo() {}
35
36 WebstoreAPI::WebstoreAPI(content::BrowserContext* browser_context)
37 : browser_context_(browser_context) {
38 InstallTrackerFactory::GetForProfile(
39 Profile::FromBrowserContext(browser_context))->AddObserver(this);
asargent_no_longer_on_chrome 2014/03/03 18:49:08 Should you be removing yourself as an observer in
Devlin 2014/03/03 21:15:13 Done, but see related comment.
40 }
41
42 WebstoreAPI::~WebstoreAPI() {}
43
44 // static
45 WebstoreAPI* WebstoreAPI::Get(content::BrowserContext* browser_context) {
46 return ProfileKeyedAPIFactory<WebstoreAPI>::GetForProfile(browser_context);
47 }
48
49 void WebstoreAPI::OnInlineInstallStart(int routing_id,
50 IPC::Sender* ipc_sender,
51 const std::string& extension_id,
52 int listeners_mask) {
53 if (listeners_mask & api::webstore::INSTALL_STAGE_LISTENER) {
54 install_stage_listeners_.push_back(
55 ObservedInstallInfo(routing_id, extension_id, ipc_sender));
56 }
57
58 if (listeners_mask & api::webstore::DOWNLOAD_PROGRESS_LISTENER) {
59 download_progress_listeners_.push_back(
60 ObservedInstallInfo(routing_id, extension_id, ipc_sender));
61 }
62 }
63
64 void WebstoreAPI::OnInlineInstallFinished(int routing_id,
65 const std::string& extension_id) {
66 RemoveListeners(routing_id, extension_id, &download_progress_listeners_);
67 RemoveListeners(routing_id, extension_id, &install_stage_listeners_);
68 }
69
70 void WebstoreAPI::OnBeginExtensionDownload(const std::string& extension_id) {
71 SendInstallMessageIfObserved(extension_id,
72 api::webstore::kInstallStageDownloading);
73 }
74
75 void WebstoreAPI::OnDownloadProgress(const std::string& extension_id,
76 int percent_downloaded) {
77 for (ObservedInstallInfoList::const_iterator iter =
78 download_progress_listeners_.begin();
79 iter != download_progress_listeners_.end();
80 ++iter) {
81 if (iter->extension_id == extension_id) {
82 iter->ipc_sender->Send(new ExtensionMsg_InlineInstallDownloadProgress(
83 iter->routing_id, percent_downloaded));
not at google - send to devlin 2014/03/01 01:42:07 you could actually save yourself the trouble of ad
Devlin 2014/03/03 17:44:59 Yes and no. We can't just do ExtensionMsg_Message
Devlin 2014/03/03 21:15:13 (Talked about this offline and decided that going
84 }
85 }
86 }
87
88 void WebstoreAPI::OnBeginCrxInstall(const std::string& extension_id) {
89 SendInstallMessageIfObserved(extension_id,
90 api::webstore::kInstallStageInstalling);
91 }
92
93 void WebstoreAPI::Shutdown() {
94 InstallTrackerFactory::GetForProfile(
95 Profile::FromBrowserContext(browser_context_))->RemoveObserver(this);
96 }
97
98 // static
99 ProfileKeyedAPIFactory<WebstoreAPI>* WebstoreAPI::GetFactoryInstance() {
100 return g_factory.Pointer();
101 }
102
103 void WebstoreAPI::SendInstallMessageIfObserved(
104 const std::string& extension_id,
105 const std::string& install_stage) {
asargent_no_longer_on_chrome 2014/03/03 18:49:08 Looks like this only gets called with const char*,
Devlin 2014/03/03 21:15:13 Done.
106 for (ObservedInstallInfoList::const_iterator iter =
107 install_stage_listeners_.begin();
108 iter != install_stage_listeners_.end();
109 ++iter) {
110 if (iter->extension_id == extension_id) {
111 iter->ipc_sender->Send(new ExtensionMsg_InlineInstallStageChanged(
112 iter->routing_id, install_stage));
113 }
114 }
115 }
116
117 void WebstoreAPI::RemoveListeners(int routing_id,
118 const std::string& extension_id,
119 ObservedInstallInfoList* listeners) {
120 for (ObservedInstallInfoList::iterator iter = listeners->begin();
121 iter != listeners->end();) {
122 if (iter->extension_id == extension_id && iter->routing_id == routing_id)
123 iter = listeners->erase(iter);
124 else
125 ++iter;
126 }
127 }
128
129 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698