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

Side by Side Diff: chrome/browser/background/background_contents_service.cc

Issue 23427003: Reload force-installed extensions on crash/force-close (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Responding to Bartosz' review (take 2) Created 7 years, 3 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/background/background_contents_service.h" 5 #include "chrome/browser/background/background_contents_service.h"
6 6
7 #include "apps/app_load_service.h" 7 #include "apps/app_load_service.h"
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_service.h" 12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "chrome/browser/background/background_contents_service_factory.h" 16 #include "chrome/browser/background/background_contents_service_factory.h"
17 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/chrome_notification_types.h" 18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/extensions/extension_host.h" 19 #include "chrome/browser/extensions/extension_host.h"
20 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_system.h" 21 #include "chrome/browser/extensions/extension_system.h"
22 #include "chrome/browser/extensions/image_loader.h" 22 #include "chrome/browser/extensions/image_loader.h"
23 #include "chrome/browser/notifications/desktop_notification_service.h" 23 #include "chrome/browser/notifications/desktop_notification_service.h"
24 #include "chrome/browser/notifications/notification.h" 24 #include "chrome/browser/notifications/notification.h"
25 #include "chrome/browser/notifications/notification_delegate.h"
25 #include "chrome/browser/notifications/notification_ui_manager.h" 26 #include "chrome/browser/notifications/notification_ui_manager.h"
26 #include "chrome/browser/prefs/scoped_user_pref_update.h" 27 #include "chrome/browser/prefs/scoped_user_pref_update.h"
27 #include "chrome/browser/profiles/profile.h" 28 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/ui/browser.h" 29 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_finder.h" 30 #include "chrome/browser/ui/browser_finder.h"
30 #include "chrome/browser/ui/browser_tabstrip.h" 31 #include "chrome/browser/ui/browser_tabstrip.h"
31 #include "chrome/browser/ui/host_desktop.h" 32 #include "chrome/browser/ui/host_desktop.h"
32 #include "chrome/common/chrome_switches.h" 33 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/extensions/background_info.h" 34 #include "chrome/common/extensions/background_info.h"
34 #include "chrome/common/extensions/extension.h" 35 #include "chrome/common/extensions/extension.h"
(...skipping 12 matching lines...) Expand all
47 #include "ui/gfx/image/image.h" 48 #include "ui/gfx/image/image.h"
48 49
49 using content::SiteInstance; 50 using content::SiteInstance;
50 using content::WebContents; 51 using content::WebContents;
51 using extensions::BackgroundInfo; 52 using extensions::BackgroundInfo;
52 using extensions::Extension; 53 using extensions::Extension;
53 using extensions::UnloadedExtensionInfo; 54 using extensions::UnloadedExtensionInfo;
54 55
55 namespace { 56 namespace {
56 57
57 const char kNotificationPrefix[] = "app.background.crashed."; 58 const char kCrashNotificationPrefix[] = "app.background.crashed.";
59 const char kMisbehaveNotificationPrefix[] = "app.background.misbehaved.";
58 60
59 void CloseBalloon(const std::string& id) { 61 // Number of recent crashes required to trigger an extension-misbehave popup
anitawoodruff 2013/08/27 09:05:36 * app/extension - will fix
anitawoodruff 2013/08/27 09:13:03 Done.
60 g_browser_process->notification_ui_manager()->CancelById(id); 62 // for force-installed extensions/apps.
63 const unsigned int kForceInstalledExtensionCrashNotificationThreshold = 5;
bartfab (slow) 2013/08/27 09:21:29 I know I have been asking for more verbosity but f
anitawoodruff 2013/08/27 11:57:39 Done - renamed to kMisbehaveCrashCountThreshold.
64
65 void CloseBalloon(const std::string& prefixed_extension_id) {
66 g_browser_process->notification_ui_manager()->
67 CancelById(prefixed_extension_id);
61 } 68 }
62 69
63 void ScheduleCloseBalloon(const std::string& extension_id) { 70 // Closes the balloon created with the specified id, which should be of the form
bartfab (slow) 2013/08/27 09:21:29 If you rename |prefixed_extension_id|, this commen
anitawoodruff 2013/08/27 11:57:39 Done.
71 // notification prefix + extension/app-id.
72 void ScheduleCloseBalloon(const std::string& prefixed_extension_id) {
64 if (!base::MessageLoop::current()) // For unit_tests 73 if (!base::MessageLoop::current()) // For unit_tests
65 return; 74 return;
66 base::MessageLoop::current()->PostTask( 75 base::MessageLoop::current()->PostTask(
67 FROM_HERE, base::Bind(&CloseBalloon, kNotificationPrefix + extension_id)); 76 FROM_HERE, base::Bind(&CloseBalloon, prefixed_extension_id));
68 } 77 }
69 78
79 // Closes the crash notification balloon for the app/extension with this id.
80 void ScheduleCloseCrashBalloon(const std::string& extension_id) {
81 ScheduleCloseBalloon(kCrashNotificationPrefix + extension_id);
82 }
83
84 // Closes all notification balloons relating to the app/extension with this id.
85 void ScheduleCloseBalloons(const std::string& extension_id) {
86 ScheduleCloseBalloon(kMisbehaveNotificationPrefix + extension_id);
87 ScheduleCloseBalloon(kCrashNotificationPrefix + extension_id);
88 }
89
90 // Delegate for the 'Extension/app has crashed' popup balloon. Restarts the
91 // extension/app when the balloon is clicked.
70 class CrashNotificationDelegate : public NotificationDelegate { 92 class CrashNotificationDelegate : public NotificationDelegate {
71 public: 93 public:
72 CrashNotificationDelegate(Profile* profile, 94 CrashNotificationDelegate(Profile* profile,
73 const Extension* extension) 95 const Extension* extension)
74 : profile_(profile), 96 : profile_(profile),
75 is_hosted_app_(extension->is_hosted_app()), 97 is_hosted_app_(extension->is_hosted_app()),
76 is_platform_app_(extension->is_platform_app()), 98 is_platform_app_(extension->is_platform_app()),
77 extension_id_(extension->id()) { 99 extension_id_(extension->id()) {
78 } 100 }
79 101
(...skipping 20 matching lines...) Expand all
100 service->LoadBackgroundContentsForExtension(profile_, 122 service->LoadBackgroundContentsForExtension(profile_,
101 copied_extension_id); 123 copied_extension_id);
102 } else if (is_platform_app_) { 124 } else if (is_platform_app_) {
103 apps::AppLoadService::Get(profile_)-> 125 apps::AppLoadService::Get(profile_)->
104 RestartApplication(copied_extension_id); 126 RestartApplication(copied_extension_id);
105 } else { 127 } else {
106 extensions::ExtensionSystem::Get(profile_)->extension_service()-> 128 extensions::ExtensionSystem::Get(profile_)->extension_service()->
107 ReloadExtension(copied_extension_id); 129 ReloadExtension(copied_extension_id);
108 } 130 }
109 131
110 // Closing the balloon here should be OK, but it causes a crash on Mac 132 // Closing the 'Extension has crashed' balloon here should be OK, but it
anitawoodruff 2013/08/27 09:05:36 *Extension/App
anitawoodruff 2013/08/27 09:13:03 Done.
111 // http://crbug.com/78167 133 // causes a crash on Mac (http://crbug.com/78167).
112 ScheduleCloseBalloon(copied_extension_id); 134 ScheduleCloseCrashBalloon(copied_extension_id);
113 } 135 }
114 136
115 virtual bool HasClickedListener() OVERRIDE { return true; } 137 virtual bool HasClickedListener() OVERRIDE { return true; }
116 138
117 virtual std::string id() const OVERRIDE { 139 virtual std::string id() const OVERRIDE {
118 return kNotificationPrefix + extension_id_; 140 return kCrashNotificationPrefix + extension_id_;
119 } 141 }
120 142
121 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { 143 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
122 return NULL; 144 return NULL;
123 } 145 }
124 146
125 private: 147 private:
126 virtual ~CrashNotificationDelegate() {} 148 virtual ~CrashNotificationDelegate() {}
127 149
128 Profile* profile_; 150 Profile* profile_;
129 bool is_hosted_app_; 151 bool is_hosted_app_;
130 bool is_platform_app_; 152 bool is_platform_app_;
131 std::string extension_id_; 153 std::string extension_id_;
132 154
133 DISALLOW_COPY_AND_ASSIGN(CrashNotificationDelegate); 155 DISALLOW_COPY_AND_ASSIGN(CrashNotificationDelegate);
134 }; 156 };
135 157
158 // Empty delegate for the 'Extension/app is misbehaving' popup balloon. which is
159 // triggered if a force-installed app/extension gets stuck in a crash/reload
160 // cycle. Doesn't do anything on click because force-installed apps/extensions
161 // get restarted automatically.
162 class MisbehaveNotificationDelegate : public NotificationDelegate {
163 public:
164 explicit MisbehaveNotificationDelegate(const Extension* extension)
165 : extension_id_(extension->id()) {
166 }
167
168 virtual void Display() OVERRIDE {}
169
170 virtual void Error() OVERRIDE {}
171
172 virtual void Close(bool by_user) OVERRIDE {}
173
174 virtual void Click() OVERRIDE {}
175
176 virtual bool HasClickedListener() OVERRIDE { return true; }
177
178 virtual std::string id() const OVERRIDE {
179 return kMisbehaveNotificationPrefix + extension_id_;
180 }
181
182 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
183 return NULL;
184 }
185
186 private:
187 virtual ~MisbehaveNotificationDelegate() {}
188
189 std::string extension_id_;
190
191 DISALLOW_COPY_AND_ASSIGN(MisbehaveNotificationDelegate);
192 };
193
136 #if defined(ENABLE_NOTIFICATIONS) 194 #if defined(ENABLE_NOTIFICATIONS)
137 void NotificationImageReady( 195 void NotificationImageReady(
138 const std::string extension_name, 196 const std::string extension_name,
139 const string16 message, 197 const string16 message,
140 const GURL extension_url, 198 const GURL extension_url,
141 scoped_refptr<CrashNotificationDelegate> delegate, 199 scoped_refptr<NotificationDelegate> delegate,
142 Profile* profile, 200 Profile* profile,
143 const gfx::Image& icon) { 201 const gfx::Image& icon) {
144 gfx::Image notification_icon(icon); 202 gfx::Image notification_icon(icon);
145 if (icon.IsEmpty()) { 203 if (icon.IsEmpty()) {
146 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 204 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
147 notification_icon = rb.GetImageNamed(IDR_EXTENSION_DEFAULT_ICON); 205 notification_icon = rb.GetImageNamed(IDR_EXTENSION_DEFAULT_ICON);
148 } 206 }
149 string16 title; // no notification title 207 string16 title; // no notification title
150 DesktopNotificationService::AddIconNotification(extension_url, 208 DesktopNotificationService::AddIconNotification(extension_url,
151 title, 209 title,
152 message, 210 message,
153 notification_icon, 211 notification_icon,
154 string16(), 212 string16(),
155 delegate.get(), 213 delegate.get(),
156 profile); 214 profile);
157 } 215 }
158 #endif 216 #endif
159 217
160 void ShowBalloon(const Extension* extension, Profile* profile) { 218 void ReloadExtension(const std::string extension_id, Profile* profile) {
219 extensions::ExtensionSystem* extensionSystem =
220 extensions::ExtensionSystem::Get(profile);
221 if (extensionSystem && extensionSystem->extension_service())
222 extensionSystem->extension_service()->ReloadExtension(extension_id);
223 }
224
225 // Show a popup notification balloon with a crash message for a given app/
226 // extension. |isForced| is true if the app/extension was force-installed, in
227 // which case we show an 'App/extension misbehaving' message instead of a
228 // crash message (since it will get restarted automatically).
229 void ShowBalloon(const Extension* extension, Profile* profile, bool isForced) {
bartfab (slow) 2013/08/27 09:21:29 Nit: You used |forceInstalled| in a similar contex
anitawoodruff 2013/08/27 11:57:39 Done - just realised I shouldn't be using camelCas
bartfab (slow) 2013/08/27 15:37:44 Aye. Something about that variable name looked fis
anitawoodruff 2013/08/27 17:34:29 Done. So don't use 'const' for args which aren't p
bartfab (slow) 2013/08/27 19:21:30 Our usage is not entirely consistent but your summ
161 #if defined(ENABLE_NOTIFICATIONS) 230 #if defined(ENABLE_NOTIFICATIONS)
162 string16 message = l10n_util::GetStringFUTF16( 231 string16 message;
163 extension->is_app() ? IDS_BACKGROUND_CRASHED_APP_BALLOON_MESSAGE : 232 scoped_refptr<NotificationDelegate> delegate;
164 IDS_BACKGROUND_CRASHED_EXTENSION_BALLOON_MESSAGE, 233 if (isForced) {
165 UTF8ToUTF16(extension->name())); 234 message = l10n_util::GetStringFUTF16(
166 235 extension->is_app() ?
236 IDS_BACKGROUND_MISBEHAVING_APP_BALLOON_MESSAGE :
237 IDS_BACKGROUND_MISBEHAVING_EXTENSION_BALLOON_MESSAGE,
238 UTF8ToUTF16(extension->name()));
239 delegate = new MisbehaveNotificationDelegate(extension);
240 } else {
241 message = l10n_util::GetStringFUTF16(
242 extension->is_app() ? IDS_BACKGROUND_CRASHED_APP_BALLOON_MESSAGE :
243 IDS_BACKGROUND_CRASHED_EXTENSION_BALLOON_MESSAGE,
244 UTF8ToUTF16(extension->name()));
245 delegate = new CrashNotificationDelegate(profile, extension);
246 }
167 extension_misc::ExtensionIcons size(extension_misc::EXTENSION_ICON_MEDIUM); 247 extension_misc::ExtensionIcons size(extension_misc::EXTENSION_ICON_MEDIUM);
168 extensions::ExtensionResource resource = 248 extensions::ExtensionResource resource =
169 extensions::IconsInfo::GetIconResource( 249 extensions::IconsInfo::GetIconResource(
170 extension, size, ExtensionIconSet::MATCH_SMALLER); 250 extension, size, ExtensionIconSet::MATCH_SMALLER);
171 scoped_refptr<CrashNotificationDelegate> delegate =
172 new CrashNotificationDelegate(profile, extension);
173 // We can't just load the image in the Observe method below because, despite 251 // We can't just load the image in the Observe method below because, despite
174 // what this method is called, it may call the callback synchronously. 252 // what this method is called, it may call the callback synchronously.
175 // However, it's possible that the extension went away during the interim, 253 // However, it's possible that the extension went away during the interim,
176 // so we'll bind all the pertinent data here. 254 // so we'll bind all the pertinent data here.
177 extensions::ImageLoader::Get(profile)->LoadImageAsync( 255 extensions::ImageLoader::Get(profile)->LoadImageAsync(
178 extension, 256 extension,
179 resource, 257 resource,
180 gfx::Size(size, size), 258 gfx::Size(size, size),
181 base::Bind( 259 base::Bind(
182 &NotificationImageReady, 260 &NotificationImageReady,
(...skipping 15 matching lines...) Expand all
198 // 276 //
199 // kRegisteredBackgroundContents: 277 // kRegisteredBackgroundContents:
200 // DictionaryValue { 278 // DictionaryValue {
201 // <appid_1>: { "url": <url1>, "name": <frame_name> }, 279 // <appid_1>: { "url": <url1>, "name": <frame_name> },
202 // <appid_2>: { "url": <url2>, "name": <frame_name> }, 280 // <appid_2>: { "url": <url2>, "name": <frame_name> },
203 // ... etc ... 281 // ... etc ...
204 // } 282 // }
205 const char kUrlKey[] = "url"; 283 const char kUrlKey[] = "url";
206 const char kFrameNameKey[] = "name"; 284 const char kFrameNameKey[] = "name";
207 285
286 int BackgroundContentsService::restart_delay_millis_ = 3000;
bartfab (slow) 2013/08/27 09:21:29 Nit: It is customary to follow this with " // 3 s
anitawoodruff 2013/08/27 11:57:39 Done.
287 int BackgroundContentsService::crash_window_millis_ = 1000;
bartfab (slow) 2013/08/27 09:21:29 Nit: It is customary to follow this with " // 1 s
anitawoodruff 2013/08/27 11:57:39 Done.
288
208 BackgroundContentsService::BackgroundContentsService( 289 BackgroundContentsService::BackgroundContentsService(
209 Profile* profile, const CommandLine* command_line) 290 Profile* profile, const CommandLine* command_line)
210 : prefs_(NULL) { 291 : prefs_(NULL) {
211 // Don't load/store preferences if the proper switch is not enabled, or if 292 // Don't load/store preferences if the proper switch is not enabled, or if
212 // the parent profile is incognito. 293 // the parent profile is incognito.
213 if (!profile->IsOffTheRecord() && 294 if (!profile->IsOffTheRecord() &&
214 !command_line->HasSwitch(switches::kDisableRestoreBackgroundContents)) 295 !command_line->HasSwitch(switches::kDisableRestoreBackgroundContents))
215 prefs_ = profile->GetPrefs(); 296 prefs_ = profile->GetPrefs();
216 297
217 // Listen for events to tell us when to load/unload persisted background 298 // Listen for events to tell us when to load/unload persisted background
218 // contents. 299 // contents.
219 StartObserving(profile); 300 StartObserving(profile);
220 } 301 }
221 302
222 BackgroundContentsService::~BackgroundContentsService() { 303 BackgroundContentsService::~BackgroundContentsService() {
223 // BackgroundContents should be shutdown before we go away, as otherwise 304 // BackgroundContents should be shutdown before we go away, as otherwise
224 // our browser process refcount will be off. 305 // our browser process refcount will be off.
225 DCHECK(contents_map_.empty()); 306 DCHECK(contents_map_.empty());
226 } 307 }
227 308
309 // static
310 void BackgroundContentsService::
311 SetCrashDelaysForForceInstalledAppsAndExtensionsForTesting(
312 const int restart_delay_millis,
313 const int crash_window_millis) {
314 restart_delay_millis_ = restart_delay_millis;
315 crash_window_millis_ = crash_window_millis;
316 }
317
228 std::vector<BackgroundContents*> 318 std::vector<BackgroundContents*>
229 BackgroundContentsService::GetBackgroundContents() const 319 BackgroundContentsService::GetBackgroundContents() const
230 { 320 {
231 std::vector<BackgroundContents*> contents; 321 std::vector<BackgroundContents*> contents;
232 for (BackgroundContentsMap::const_iterator it = contents_map_.begin(); 322 for (BackgroundContentsMap::const_iterator it = contents_map_.begin();
233 it != contents_map_.end(); ++it) 323 it != contents_map_.end(); ++it)
234 contents.push_back(it->second.contents); 324 contents.push_back(it->second.contents);
235 return contents; 325 return contents;
236 } 326 }
237 327
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 if (service && service->is_ready()) { 432 if (service && service->is_ready()) {
343 // Now load the manifest-specified background page. If service isn't 433 // Now load the manifest-specified background page. If service isn't
344 // ready, then the background page will be loaded from the 434 // ready, then the background page will be loaded from the
345 // EXTENSIONS_READY callback. 435 // EXTENSIONS_READY callback.
346 LoadBackgroundContents(profile, 436 LoadBackgroundContents(profile,
347 BackgroundInfo::GetBackgroundURL(extension), 437 BackgroundInfo::GetBackgroundURL(extension),
348 ASCIIToUTF16("background"), 438 ASCIIToUTF16("background"),
349 UTF8ToUTF16(extension->id())); 439 UTF8ToUTF16(extension->id()));
350 } 440 }
351 } 441 }
352
353 // Remove any "This extension has crashed" balloons. 442 // Remove any "This extension has crashed" balloons.
354 ScheduleCloseBalloon(extension->id()); 443 ScheduleCloseCrashBalloon(extension->id());
355 SendChangeNotification(profile); 444 SendChangeNotification(profile);
356 break; 445 break;
357 } 446 }
358 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: 447 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED:
359 case chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED: { 448 case chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED: {
360 Profile* profile = content::Source<Profile>(source).ptr(); 449 Profile* profile = content::Source<Profile>(source).ptr();
361 const Extension* extension = NULL; 450 const Extension* extension = NULL;
362 if (type == chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED) { 451 if (type == chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED) {
363 BackgroundContents* bg = 452 BackgroundContents* bg =
364 content::Details<BackgroundContents>(details).ptr(); 453 content::Details<BackgroundContents>(details).ptr();
365 std::string extension_id = UTF16ToASCII( 454 std::string extension_id = UTF16ToASCII(
366 BackgroundContentsServiceFactory::GetForProfile(profile)-> 455 BackgroundContentsServiceFactory::GetForProfile(profile)->
367 GetParentApplicationId(bg)); 456 GetParentApplicationId(bg));
368 extension = 457 extension =
369 extensions::ExtensionSystem::Get(profile)->extension_service()-> 458 extensions::ExtensionSystem::Get(profile)->extension_service()->
370 GetExtensionById(extension_id, false); 459 GetExtensionById(extension_id, false);
371 } else { 460 } else {
372 extensions::ExtensionHost* extension_host = 461 extensions::ExtensionHost* extension_host =
373 content::Details<extensions::ExtensionHost>(details).ptr(); 462 content::Details<extensions::ExtensionHost>(details).ptr();
374 extension = extension_host->extension(); 463 extension = extension_host->extension();
375 } 464 }
376 if (!extension) 465 if (!extension)
377 break; 466 break;
378 467
379 // When an extension crashes, EXTENSION_PROCESS_TERMINATED is followed by 468 // When an extension crashes, EXTENSION_PROCESS_TERMINATED is followed by
380 // an EXTENSION_UNLOADED notification. This UNLOADED signal causes all the 469 // an EXTENSION_UNLOADED notification. This UNLOADED signal causes all the
381 // notifications for this extension to be cancelled by 470 // notifications for this extension to be cancelled by
382 // DesktopNotificationService. For this reason, instead of showing the 471 // DesktopNotificationService. For this reason, we post the crash handling
383 // balloon right now, we schedule it to show a little later. 472 // code as a task here so that it is not executed before this event.
384 base::MessageLoop::current()->PostTask( 473 bool forceInstalled =
bartfab (slow) 2013/08/27 09:21:29 Nit: const
anitawoodruff 2013/08/27 11:57:39 Done.
385 FROM_HERE, base::Bind(&ShowBalloon, extension, profile)); 474 extension->location() ==
475 extensions::Manifest::EXTERNAL_POLICY_DOWNLOAD;
476 if (!forceInstalled) {
477 // Notify user extension has crashed.
478 base::MessageLoop::current()->PostTask(
479 FROM_HERE, base::Bind(&ShowBalloon, extension, profile, false));
480 } else {
481 // Restart the extension; notify user if crash recurs frequently.
482 RestartForceInstalledExtensionOnCrash(extension, profile);
483 }
386 break; 484 break;
387 } 485 }
388 case chrome::NOTIFICATION_EXTENSION_UNLOADED: 486 case chrome::NOTIFICATION_EXTENSION_UNLOADED:
389 switch (content::Details<UnloadedExtensionInfo>(details)->reason) { 487 switch (content::Details<UnloadedExtensionInfo>(details)->reason) {
390 case extension_misc::UNLOAD_REASON_DISABLE: // Fall through. 488 case extension_misc::UNLOAD_REASON_DISABLE: // Fall through.
391 case extension_misc::UNLOAD_REASON_TERMINATE: // Fall through. 489 case extension_misc::UNLOAD_REASON_TERMINATE: // Fall through.
392 case extension_misc::UNLOAD_REASON_UNINSTALL: // Fall through. 490 case extension_misc::UNLOAD_REASON_UNINSTALL: // Fall through.
393 case extension_misc::UNLOAD_REASON_BLACKLIST: 491 case extension_misc::UNLOAD_REASON_BLACKLIST:
394 ShutdownAssociatedBackgroundContents( 492 ShutdownAssociatedBackgroundContents(
395 ASCIIToUTF16(content::Details<UnloadedExtensionInfo>(details)-> 493 ASCIIToUTF16(content::Details<UnloadedExtensionInfo>(details)->
(...skipping 16 matching lines...) Expand all
412 default: 510 default:
413 NOTREACHED(); 511 NOTREACHED();
414 ShutdownAssociatedBackgroundContents( 512 ShutdownAssociatedBackgroundContents(
415 ASCIIToUTF16(content::Details<UnloadedExtensionInfo>(details)-> 513 ASCIIToUTF16(content::Details<UnloadedExtensionInfo>(details)->
416 extension->id())); 514 extension->id()));
417 break; 515 break;
418 } 516 }
419 break; 517 break;
420 518
421 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { 519 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
520 const std::string& extension_id =
521 content::Details<const Extension>(details).ptr()->id();
422 // Remove any "This extension has crashed" balloons. 522 // Remove any "This extension has crashed" balloons.
423 ScheduleCloseBalloon( 523 ScheduleCloseBalloons(extension_id);
424 content::Details<const Extension>(details).ptr()->id()); 524 misbehaving_extensions_.erase(extension_id);
525 extension_crashlog_map_.erase(extension_id);
425 break; 526 break;
426 } 527 }
427 528
428 default: 529 default:
429 NOTREACHED(); 530 NOTREACHED();
430 break; 531 break;
431 } 532 }
432 } 533 }
433 534
535 void BackgroundContentsService::RestartForceInstalledExtensionOnCrash(
536 const Extension* extension, Profile* profile) {
anitawoodruff 2013/08/27 09:05:36 Is this the right place to put the extracted metho
bartfab (slow) 2013/08/27 15:37:44 It will work here and you can leave it here. If yo
anitawoodruff 2013/08/27 17:34:29 But, Observe (which calls this) is the first metho
537 const std::string& extension_id = extension->id();
538 const bool alreadyNotified = misbehaving_extensions_.find(extension_id) !=
539 misbehaving_extensions_.end();
540 std::queue<base::TimeTicks>& crashes = extension_crashlog_map_[extension_id];
541 const base::TimeDelta duration = base::TimeDelta::FromMilliseconds(
542 kForceInstalledExtensionCrashNotificationThreshold *
543 (restart_delay_millis_ + crash_window_millis_));
544 // Show a notification if the threshold number of crashes has occurred within
545 // a given duration.
546 const bool shouldNotify =
bartfab (slow) 2013/08/27 09:21:29 How about this kind of structure instead: if (!al
anitawoodruff 2013/08/27 11:57:39 Done.
547 !alreadyNotified &&
548 crashes.size() ==
549 kForceInstalledExtensionCrashNotificationThreshold - 1 &&
550 base::TimeTicks::Now() - crashes.front() < duration;
551 if (shouldNotify) {
552 base::MessageLoop::current()->PostTask(FROM_HERE,
553 base::Bind(&ShowBalloon, extension, profile, true));
554 misbehaving_extensions_.insert(extension_id);
555 extension_crashlog_map_.erase(extension_id);
556 } else if (!alreadyNotified) {
557 while (!crashes.empty() &&
558 base::TimeTicks::Now() - crashes.front() > duration) {
559 crashes.pop(); // Remove old timestamps.
560 }
561 crashes.push(base::TimeTicks::Now());
562 if (crashes.size() == kForceInstalledExtensionCrashNotificationThreshold)
563 crashes.pop();
564 }
565 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
566 base::Bind(&ReloadExtension, extension_id, profile),
567 base::TimeDelta::FromMilliseconds(restart_delay_millis_));
568 }
569
434 // Loads all background contents whose urls have been stored in prefs. 570 // Loads all background contents whose urls have been stored in prefs.
435 void BackgroundContentsService::LoadBackgroundContentsFromPrefs( 571 void BackgroundContentsService::LoadBackgroundContentsFromPrefs(
436 Profile* profile) { 572 Profile* profile) {
437 if (!prefs_) 573 if (!prefs_)
438 return; 574 return;
439 const DictionaryValue* contents = 575 const DictionaryValue* contents =
440 prefs_->GetDictionary(prefs::kRegisteredBackgroundContents); 576 prefs_->GetDictionary(prefs::kRegisteredBackgroundContents);
441 if (!contents) 577 if (!contents)
442 return; 578 return;
443 ExtensionService* extensions_service = 579 ExtensionService* extensions_service =
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 } 782 }
647 783
648 void BackgroundContentsService::BackgroundContentsOpened( 784 void BackgroundContentsService::BackgroundContentsOpened(
649 BackgroundContentsOpenedDetails* details) { 785 BackgroundContentsOpenedDetails* details) {
650 // Add the passed object to our list. Should not already be tracked. 786 // Add the passed object to our list. Should not already be tracked.
651 DCHECK(!IsTracked(details->contents)); 787 DCHECK(!IsTracked(details->contents));
652 DCHECK(!details->application_id.empty()); 788 DCHECK(!details->application_id.empty());
653 contents_map_[details->application_id].contents = details->contents; 789 contents_map_[details->application_id].contents = details->contents;
654 contents_map_[details->application_id].frame_name = details->frame_name; 790 contents_map_[details->application_id].frame_name = details->frame_name;
655 791
656 ScheduleCloseBalloon(UTF16ToASCII(details->application_id)); 792 ScheduleCloseCrashBalloon(UTF16ToASCII(details->application_id));
657 } 793 }
658 794
659 // Used by test code and debug checks to verify whether a given 795 // Used by test code and debug checks to verify whether a given
660 // BackgroundContents is being tracked by this instance. 796 // BackgroundContents is being tracked by this instance.
661 bool BackgroundContentsService::IsTracked( 797 bool BackgroundContentsService::IsTracked(
662 BackgroundContents* background_contents) const { 798 BackgroundContents* background_contents) const {
663 return !GetParentApplicationId(background_contents).empty(); 799 return !GetParentApplicationId(background_contents).empty();
664 } 800 }
665 801
666 void BackgroundContentsService::BackgroundContentsShutdown( 802 void BackgroundContentsService::BackgroundContentsShutdown(
(...skipping 27 matching lines...) Expand all
694 bool user_gesture, 830 bool user_gesture,
695 bool* was_blocked) { 831 bool* was_blocked) {
696 Browser* browser = chrome::FindLastActiveWithProfile( 832 Browser* browser = chrome::FindLastActiveWithProfile(
697 Profile::FromBrowserContext(new_contents->GetBrowserContext()), 833 Profile::FromBrowserContext(new_contents->GetBrowserContext()),
698 chrome::GetActiveDesktop()); 834 chrome::GetActiveDesktop());
699 if (browser) { 835 if (browser) {
700 chrome::AddWebContents(browser, NULL, new_contents, disposition, 836 chrome::AddWebContents(browser, NULL, new_contents, disposition,
701 initial_pos, user_gesture, was_blocked); 837 initial_pos, user_gesture, was_blocked);
702 } 838 }
703 } 839 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698