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

Side by Side Diff: chrome/browser/extensions/api/apps_debugger_private/apps_debugger_private_api.cc

Issue 11428116: First few API implementation of AppsDebuggerPrivate. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: . 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/apps_debugger_private/apps_debugger_priv ate_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/apps_debugger_private/apps_debugger_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/apps_debugger_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::AppsDebuggerAPI;
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 AppsDebuggerAPI* AppsDebuggerAPI::Get(Profile* profile) {
41 return AppsDebuggerAPIFactory::GetForProfile(profile);
42 }
43
44 AppsDebuggerAPI::AppsDebuggerAPI(Profile* profile)
45 : profile_(profile),
46 deleting_rvh_(NULL) {
47 RegisterNotifications();
48 }
49
50 scoped_ptr<apps_debugger::ItemInfo> AppsDebuggerAPI::CreateItemInfo(
51 const Extension& item,
52 ExtensionSystem* system,
53 bool item_is_enabled) {
54 scoped_ptr<apps_debugger::ItemInfo> info(new apps_debugger::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.location() == Extension::LOAD) {
65 info->path.reset(new std::string(item.path().value()));
66 }
67
68 info->enabled_incognito = service->IsIncognitoEnabled(item.id());
69 info->wants_file_access = item.wants_file_access();
70 info->allow_file_access = service->AllowFileAccess(&item);
71 info->allow_reload = (item.location() == Extension::LOAD);
72 info->options_url = item.options_url().spec();
73 info->is_unpacked = (item.location() == Extension::LOAD);
74
75 GURL icon =
76 ExtensionIconSource::GetIconURL(&item,
77 extension_misc::EXTENSION_ICON_MEDIUM,
78 ExtensionIconSet::MATCH_BIGGER,
79 !info->enabled,
80 NULL);
81 info->icon = icon.spec();
82
83 info->homepage_url.reset(new std::string(item.GetHomepageURL().spec()));
84 info->may_disable = system->management_policy()->
85 UserMayModifySettings(&item, NULL);
86 info->is_app = item.is_app();
87 info->views = GetInspectablePagesForExtension(&item, item_is_enabled);
88
89 return info.Pass();
90 }
91
92 void AppsDebuggerAPI::AddItemsInfo(const ExtensionSet& items,
93 ExtensionSystem* system,
94 ItemInfoList* item_list) {
95
96 for (ExtensionSet::const_iterator iter = items.begin();
97 iter != items.end(); ++iter) {
98 const Extension& item = **iter;
99 if (item.location() == Extension::COMPONENT)
100 continue; // Skip built-in extensions / apps;
101 item_list->push_back(make_linked_ptr<apps_debugger::ItemInfo>(
102 CreateItemInfo(item, system, false).release()));
103 }
104 }
105
106 void AppsDebuggerAPI::GetInspectablePagesForExtensionProcess(
107 const std::set<content::RenderViewHost*>& views,
108 ItemInspectViewList* result) {
109 for (std::set<content::RenderViewHost*>::const_iterator iter = views.begin();
110 iter != views.end(); ++iter) {
111 content::RenderViewHost* host = *iter;
112 content::WebContents* web_contents =
113 content::WebContents::FromRenderViewHost(host);
114 chrome::ViewType host_type = chrome::GetViewType(web_contents);
115 if (host == deleting_rvh_ ||
116 chrome::VIEW_TYPE_EXTENSION_POPUP == host_type ||
117 chrome::VIEW_TYPE_EXTENSION_DIALOG == host_type)
118 continue;
119
120 GURL url = web_contents->GetURL();
121 content::RenderProcessHost* process = host->GetProcess();
122 linked_ptr<apps_debugger::ItemInspectView>
123 view(new apps_debugger::ItemInspectView());
124 view->path = url.path().substr(1);
125 view->render_process_id = process->GetID();
126 view->render_view_id = host->GetRoutingID();
127 view->incognito = process->GetBrowserContext()->IsOffTheRecord();
128
129 result->push_back(view);
130 }
131 }
132
133 ItemInspectViewList AppsDebuggerAPI::GetInspectablePagesForExtension(
134 const extensions::Extension* extension,
135 bool extension_is_enabled) {
136
137 ItemInspectViewList result;
138 // Get the extension process's active views.
139 ExtensionProcessManager* process_manager =
140 extensions::ExtensionSystem::Get(profile_)->process_manager();
141 GetInspectablePagesForExtensionProcess(
142 process_manager->GetRenderViewHostsForExtension(extension->id()),
143 &result);
144 return result;
145 }
146
147 void AppsDebuggerAPI::Observe(
148 int type,
149 const content::NotificationSource& source,
150 const content::NotificationDetails& details) {
151 Profile* source_profile = NULL;
152 switch (type) {
153 // TODO(grv): Listen to other notifications.
154 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED:
155 case chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED:
156 deleting_rvh_ = content::Source<RenderViewHost>(source).ptr();
157 source_profile = content::Source<Profile>(source).ptr();
158 break;
159 default:
160 NOTREACHED();
161 }
162 }
163
164 void AppsDebuggerAPI::RegisterNotifications() {
165 registrar_.Add(this,
166 content::NOTIFICATION_RENDER_VIEW_HOST_DELETED,
167 content::NotificationService::AllBrowserContextsAndSources());
168 registrar_.Add(this,
169 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
170 content::NotificationService::AllBrowserContextsAndSources());
171
172 }
173
174 AppsDebuggerAPI::~AppsDebuggerAPI() {
175 }
176
177 void AppsDebuggerAPI::Shutdown() {
178 }
179
180 namespace api {
181
182 bool AppsDebuggerPrivateAutoUpdateFunction::RunImpl() {
183 extensions::ExtensionUpdater* updater = GetExtensionUpdater(profile());
184 if (updater)
185 updater->CheckNow(extensions::ExtensionUpdater::CheckParams());
186 SetResult(Value::CreateBooleanValue(true));
187 return true;
188 }
189
190 bool AppsDebuggerPrivateGetItemsInfoFunction::RunImpl() {
191 ItemInfoList items;
192 ExtensionSystem* system = ExtensionSystem::Get(profile());
193
194 AppsDebuggerAPI::Get(profile())->AddItemInfo(
195 *profile()->GetExtensionService()->extensions(), system, &items);
196
197 results_ = apps_debugger::GetItemsInfo::Results::Create(items);
198 return true;
199 }
200
201 bool AppsDebuggerPrivateInspectFunction::RunImpl() {
202 scoped_ptr<apps_debugger::Inspect::Params> params(
203 apps_debugger::Inspect::Params::Create(*args_));
204 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
205 const apps_debugger::InspectOptions& options = params->options;
206 std::string extension_id;
207 int render_process_id;
208 int render_view_id;
209 bool incognito = false;
210
211 extension_id = options.extension_id;
212 base::StringToInt(options.render_process_id, &render_process_id);
213 base::StringToInt(options.render_view_id, &render_view_id);
214 incognito = options.incognito;
215
216 if (render_process_id == -1) {
217 // This message is for a lazy background page. Start the page if necessary.
218 ExtensionService* service = profile()->GetExtensionService();
219 const Extension* extension = service->extensions()->GetByID(extension_id);
220 DCHECK(extension);
221
222 if (incognito)
223 service = extensions::ExtensionSystem::Get(
224 service->profile()->GetOffTheRecordProfile())->extension_service();
225 service->InspectBackgroundPage(extension);
226 return false;
227 }
228
229 content::RenderViewHost* host = content::RenderViewHost::FromID(
230 render_process_id, render_view_id);
231
232 if (!host) {
233 // This can happen if the host has gone away since the page was dispalyed.
234 return false;
235 }
236
237 DevToolsWindow::OpenDevToolsWindow(host);
238 return true;
239 }
240
241 } // namespace api
242 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698