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

Side by Side Diff: chrome/browser/extensions/api/developer_private/developer_private_api.cc

Issue 11428116: First few API implementation of AppsDebuggerPrivate. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressed Mike's comments Created 8 years 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 (c) 2012 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/developer_private/developer_private_api. h"
6
7 #include "base/string_number_conversions.h"
8 #include "base/values.h"
9 #include "chrome/browser/debugger/devtools_window.h"
10 #include "chrome/browser/extensions/api/developer_private/developer_private_api_ factory.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_system.h"
13 #include "chrome/browser/extensions/management_policy.h"
14 #include "chrome/browser/extensions/updater/extension_updater.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
17 #include "chrome/browser/view_type_utils.h"
18 #include "chrome/common/extensions/api/developer_private.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "grit/generated_resources.h"
24
25 using content::RenderViewHost;
26 using extensions::DeveloperPrivateAPI;
27 using extensions::Extension;
28 using extensions::ExtensionSystem;
29
30 namespace {
31
32 extensions::ExtensionUpdater* GetExtensionUpdater(Profile* profile) {
33 return profile->GetExtensionService()->updater();
34 }
35
36 } // namespace
37
38 namespace extensions {
39
40 DeveloperPrivateAPI* DeveloperPrivateAPI::Get(Profile* profile) {
41 return DeveloperPrivateAPIFactory::GetForProfile(profile);
42 }
43
44 DeveloperPrivateAPI::DeveloperPrivateAPI(Profile* profile)
45 : profile_(profile),
46 deleting_render_view_host_(NULL) {
47 RegisterNotifications();
48 }
49
50 scoped_ptr<developer::ItemInfo> DeveloperPrivateAPI::CreateItemInfo(
51 const Extension& item,
52 ExtensionSystem* system,
53 bool item_is_enabled) {
54 scoped_ptr<developer::ItemInfo> info(new developer::ItemInfo());
55 ExtensionService* service = system->extension_service();
56
57 info->id = item.id();
58 info->name = item.name();
59 info->enabled = service->IsExtensionEnabled(info->id);
60 info->offline_enabled = item.offline_enabled();
61 info->version = item.VersionString();
62 info->description = item.description();
63
64 if (item.is_app()) {
65 if (item.is_legacy_packaged_app())
66 info->type = developer::DEVELOPER_PRIVATE_ITEM_TYPE_LEGACY_PACKAGED_APP;
67 else if (item.is_hosted_app())
68 info->type = developer::DEVELOPER_PRIVATE_ITEM_TYPE_HOSTED_APP;
69 else if (item.is_platform_app())
70 info->type = developer::DEVELOPER_PRIVATE_ITEM_TYPE_PACKAGED_APP;
71 else
72 NOTREACHED();
73 } else if (item.is_theme()) {
74 info->type = developer::DEVELOPER_PRIVATE_ITEM_TYPE_THEME;
75 } else if (item.is_extension()) {
76 info->type = developer::DEVELOPER_PRIVATE_ITEM_TYPE_EXTENSION;
77 } else {
78 NOTREACHED();
79 }
80
81 if (item.location() == Extension::LOAD) {
82 info->path.reset(new std::string(item.path().value()));
83 }
84
85 info->enabled_incognito = service->IsIncognitoEnabled(item.id());
86 info->wants_file_access = item.wants_file_access();
87 info->allow_file_access = service->AllowFileAccess(&item);
88 info->allow_reload = (item.location() == Extension::LOAD);
89 info->is_unpacked = (item.location() == Extension::LOAD);
90
91 GURL icon =
92 ExtensionIconSource::GetIconURL(&item,
93 extension_misc::EXTENSION_ICON_MEDIUM,
94 ExtensionIconSet::MATCH_BIGGER,
95 !info->enabled,
96 NULL);
97 info->icon = icon.spec();
98
99 info->homepage_url.reset(new std::string(item.GetHomepageURL().spec()));
100 if (!item.options_url().is_empty()) {
101 info->options_url.reset(new std::string(item.options_url().spec()));
102 }
103
104 if (!item.update_url().is_empty()) {
105 info->update_url.reset(new std::string(
106 item.update_url().spec()));
107 }
108
109 if (item.is_app()) {
110 info->app_launch_url.reset(new std::string(
111 item.GetFullLaunchURL().spec()));
112 }
113
114 info->may_disable = system->management_policy()->
115 UserMayModifySettings(&item, NULL);
116 info->is_app = item.is_app();
117 info->views = GetInspectablePagesForExtension(&item, item_is_enabled);
118
119 return info.Pass();
120 }
121
122 void DeveloperPrivateAPI::AddItemsInfo(const ExtensionSet& items,
123 ExtensionSystem* system,
124 ItemInfoList* item_list) {
125
126 for (ExtensionSet::const_iterator iter = items.begin();
127 iter != items.end(); ++iter) {
128 const Extension& item = **iter;
129 if (item.location() == Extension::COMPONENT)
130 continue; // Skip built-in extensions / apps;
131 item_list->push_back(make_linked_ptr<developer::ItemInfo>(
132 CreateItemInfo(item, system, false).release()));
133 }
134 }
135
136 void DeveloperPrivateAPI::GetInspectablePagesForExtensionProcess(
137 const std::set<content::RenderViewHost*>& views,
138 ItemInspectViewList* result) {
139 for (std::set<content::RenderViewHost*>::const_iterator iter = views.begin();
140 iter != views.end(); ++iter) {
141 content::RenderViewHost* host = *iter;
142 content::WebContents* web_contents =
143 content::WebContents::FromRenderViewHost(host);
144 chrome::ViewType host_type = chrome::GetViewType(web_contents);
145 if (host == deleting_render_view_host_ ||
146 chrome::VIEW_TYPE_EXTENSION_POPUP == host_type ||
147 chrome::VIEW_TYPE_EXTENSION_DIALOG == host_type)
148 continue;
149
150 GURL url = web_contents->GetURL();
151 content::RenderProcessHost* process = host->GetProcess();
152 linked_ptr<developer::ItemInspectView>
153 view(new developer::ItemInspectView());
154 view->path = url.path().substr(1);
155 view->render_process_id = process->GetID();
156 view->render_view_id = host->GetRoutingID();
157 view->incognito = process->GetBrowserContext()->IsOffTheRecord();
158
159 result->push_back(view);
160 }
161 }
162
163 ItemInspectViewList DeveloperPrivateAPI::GetInspectablePagesForExtension(
164 const extensions::Extension* extension,
165 bool extension_is_enabled) {
166
167 ItemInspectViewList result;
168 // Get the extension process's active views.
169 ExtensionProcessManager* process_manager =
170 extensions::ExtensionSystem::Get(profile_)->process_manager();
171 GetInspectablePagesForExtensionProcess(
172 process_manager->GetRenderViewHostsForExtension(extension->id()),
173 &result);
174 return result;
175 }
176
177 void DeveloperPrivateAPI::Observe(
178 int type,
179 const content::NotificationSource& source,
180 const content::NotificationDetails& details) {
181 Profile* source_profile = NULL;
182 switch (type) {
183 // TODO(grv): Listen to other notifications.
184 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED:
185 case chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED:
186 deleting_render_view_host_
187 = content::Source<RenderViewHost>(source).ptr();
188 source_profile = content::Source<Profile>(source).ptr();
189 break;
190 default:
191 NOTREACHED();
192 }
193 }
194
195 void DeveloperPrivateAPI::RegisterNotifications() {
196 registrar_.Add(this,
197 content::NOTIFICATION_RENDER_VIEW_HOST_DELETED,
198 content::NotificationService::AllBrowserContextsAndSources());
199 registrar_.Add(this,
200 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
201 content::NotificationService::AllBrowserContextsAndSources());
202 }
203
204 DeveloperPrivateAPI::~DeveloperPrivateAPI() {}
205
206 void DeveloperPrivateAPI::Shutdown() {}
207
208 namespace api {
209
210 bool DeveloperPrivateAutoUpdateFunction::RunImpl() {
211 extensions::ExtensionUpdater* updater = GetExtensionUpdater(profile());
212 if (updater)
213 updater->CheckNow(extensions::ExtensionUpdater::CheckParams());
214 SetResult(Value::CreateBooleanValue(true));
215 return true;
216 }
217
218 DeveloperPrivateAutoUpdateFunction::~DeveloperPrivateAutoUpdateFunction() {}
219
220 bool DeveloperPrivateGetItemsInfoFunction::RunImpl() {
221 ItemInfoList items;
222 ExtensionSystem* system = ExtensionSystem::Get(profile());
223 scoped_ptr<developer::GetItemsInfo::Params> params(
224 developer::GetItemsInfo::Params::Create(*args_));
225 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
226
227 bool include_disabled = params->include_disabled;
228 bool include_terminated = params->include_terminated;
229 ExtensionSet extension_set;
230 extension_set.InsertAll(
231 *profile()->GetExtensionService()->extensions());
232
233 if (include_disabled) {
234 extension_set.InsertAll(
235 *profile()->GetExtensionService()->disabled_extensions());
236 }
237
238 if (include_terminated) {
239 extension_set.InsertAll(
240 *profile()->GetExtensionService()->disabled_extensions());
241 }
242
243 include_terminated = include_disabled;
244
245 DeveloperPrivateAPI::Get(profile())->AddItemsInfo(
246 extension_set, system, &items);
247
248 results_ = developer::GetItemsInfo::Results::Create(items);
249 return true;
250 }
251
252 DeveloperPrivateGetItemsInfoFunction::~DeveloperPrivateGetItemsInfoFunction() {}
253
254 bool DeveloperPrivateInspectFunction::RunImpl() {
255 scoped_ptr<developer::Inspect::Params> params(
256 developer::Inspect::Params::Create(*args_));
257 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
258 const developer::InspectOptions& options = params->options;
259
260 int render_process_id;
261 base::StringToInt(options.render_process_id, &render_process_id);
262
263 if (render_process_id == -1) {
264 // This is a lazy background page. Identify if it is a normal
265 // or incognito background page.
266 ExtensionService* service = profile()->GetExtensionService();
267 if (options.incognito)
268 service = extensions::ExtensionSystem::Get(
269 service->profile()->GetOffTheRecordProfile())->extension_service();
270 const Extension* extension = service->extensions()->GetByID(
271 options.extension_id);
272 DCHECK(extension);
273 // Wakes up the background page and opens the inspect window.
274 service->InspectBackgroundPage(extension);
275 return false;
276 }
277
278 int render_view_id;
279 base::StringToInt(options.render_view_id, &render_view_id);
280 content::RenderViewHost* host = content::RenderViewHost::FromID(
281 render_process_id, render_view_id);
282
283 if (!host) {
284 // This can happen if the host has gone away since the page was displayed.
285 return false;
286 }
287
288 DevToolsWindow::OpenDevToolsWindow(host);
289 return true;
290 }
291
292 DeveloperPrivateInspectFunction::~DeveloperPrivateInspectFunction() {}
293
294 } // namespace api
295
296 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698