OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_contents_service.h" | 5 #include "chrome/browser/background_contents_service.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const char kNotificationPrefix[] = "app.background.crashed."; | 37 const char kNotificationPrefix[] = "app.background.crashed."; |
38 | 38 |
39 void CloseBalloon(const std::string id) { | 39 void CloseBalloon(const std::string id) { |
40 g_browser_process->notification_ui_manager()->CancelById(id); | 40 g_browser_process->notification_ui_manager()->CancelById(id); |
41 } | 41 } |
42 | 42 |
43 void ScheduleCloseBalloon(const std::string& extension_id) { | 43 void ScheduleCloseBalloon(const std::string& extension_id) { |
| 44 if (!MessageLoop::current()) // For unit_tests |
| 45 return; |
44 MessageLoop::current()->PostTask(FROM_HERE, | 46 MessageLoop::current()->PostTask(FROM_HERE, |
45 NewRunnableFunction(&CloseBalloon, | 47 NewRunnableFunction(&CloseBalloon, |
46 kNotificationPrefix + extension_id)); | 48 kNotificationPrefix + extension_id)); |
47 } | 49 } |
48 | 50 |
49 class CrashNotificationDelegate : public NotificationDelegate { | 51 class CrashNotificationDelegate : public NotificationDelegate { |
50 public: | 52 public: |
51 CrashNotificationDelegate(Profile* profile, const Extension* extension) | 53 CrashNotificationDelegate(Profile* profile, const Extension* extension) |
52 : profile_(profile), | 54 : profile_(profile), |
53 is_hosted_app_(extension->is_hosted_app()), | 55 is_hosted_app_(extension->is_hosted_app()), |
54 extension_id_(extension->id()) { | 56 extension_id_(extension->id()) { |
55 } | 57 } |
56 | 58 |
57 ~CrashNotificationDelegate() { | 59 ~CrashNotificationDelegate() { |
58 } | 60 } |
59 | 61 |
60 void Display() {} | 62 void Display() {} |
61 | 63 |
62 void Error() {} | 64 void Error() {} |
63 | 65 |
64 void Close(bool by_user) {} | 66 void Close(bool by_user) {} |
65 | 67 |
66 void Click() { | 68 void Click() { |
67 if (is_hosted_app_) { | 69 if (is_hosted_app_) { |
68 BackgroundContentsServiceFactory::GetForProfile(profile_)-> | 70 // There can be a race here: user clicks the balloon, and simultaneously |
69 LoadBackgroundContentsForExtension(profile_, extension_id_); | 71 // reloads the sad tab for the app. So we check here to be safe before |
| 72 // loading the background page. |
| 73 BackgroundContentsService* service = |
| 74 BackgroundContentsServiceFactory::GetForProfile(profile_); |
| 75 if (!service->GetAppBackgroundContents(ASCIIToUTF16(extension_id_))) |
| 76 service->LoadBackgroundContentsForExtension(profile_, extension_id_); |
70 } else { | 77 } else { |
71 profile_->GetExtensionService()->ReloadExtension(extension_id_); | 78 profile_->GetExtensionService()->ReloadExtension(extension_id_); |
72 } | 79 } |
73 | 80 |
74 // Closing the balloon here should be OK, but it causes a crash on Mac | 81 // Closing the balloon here should be OK, but it causes a crash on Mac |
75 // http://crbug.com/78167 | 82 // http://crbug.com/78167 |
76 ScheduleCloseBalloon(extension_id_); | 83 ScheduleCloseBalloon(extension_id_); |
77 } | 84 } |
78 | 85 |
79 std::string id() const { | 86 std::string id() const { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 Profile* profile = Source<Profile>(source).ptr(); | 229 Profile* profile = Source<Profile>(source).ptr(); |
223 const string16& appid = GetParentApplicationId(bgcontents); | 230 const string16& appid = GetParentApplicationId(bgcontents); |
224 ExtensionService* extension_service = profile->GetExtensionService(); | 231 ExtensionService* extension_service = profile->GetExtensionService(); |
225 // extension_service can be NULL when running tests. | 232 // extension_service can be NULL when running tests. |
226 if (extension_service) { | 233 if (extension_service) { |
227 const Extension* extension = | 234 const Extension* extension = |
228 extension_service->GetExtensionById(UTF16ToUTF8(appid), false); | 235 extension_service->GetExtensionById(UTF16ToUTF8(appid), false); |
229 if (extension && extension->background_url().is_valid()) | 236 if (extension && extension->background_url().is_valid()) |
230 break; | 237 break; |
231 } | 238 } |
232 RegisterBackgroundContents(Details<BackgroundContents>(details).ptr()); | 239 RegisterBackgroundContents(bgcontents); |
233 break; | 240 break; |
234 } | 241 } |
235 case NotificationType::EXTENSION_LOADED: { | 242 case NotificationType::EXTENSION_LOADED: { |
236 const Extension* extension = Details<const Extension>(details).ptr(); | 243 const Extension* extension = Details<const Extension>(details).ptr(); |
237 Profile* profile = Source<Profile>(source).ptr(); | 244 Profile* profile = Source<Profile>(source).ptr(); |
238 if (extension->is_hosted_app() && | 245 if (extension->is_hosted_app() && |
239 extension->background_url().is_valid()) { | 246 extension->background_url().is_valid()) { |
240 // If there is a background page specified in the manifest for a hosted | 247 // If there is a background page specified in the manifest for a hosted |
241 // app, then blow away registered urls in the pref. | 248 // app, then blow away registered urls in the pref. |
242 ShutdownAssociatedBackgroundContents(ASCIIToUTF16(extension->id())); | 249 ShutdownAssociatedBackgroundContents(ASCIIToUTF16(extension->id())); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 } | 387 } |
381 | 388 |
382 void BackgroundContentsService::LoadBackgroundContentsFromDictionary( | 389 void BackgroundContentsService::LoadBackgroundContentsFromDictionary( |
383 Profile* profile, | 390 Profile* profile, |
384 const std::string& extension_id, | 391 const std::string& extension_id, |
385 const DictionaryValue* contents) { | 392 const DictionaryValue* contents) { |
386 ExtensionService* extensions_service = profile->GetExtensionService(); | 393 ExtensionService* extensions_service = profile->GetExtensionService(); |
387 DCHECK(extensions_service); | 394 DCHECK(extensions_service); |
388 | 395 |
389 DictionaryValue* dict; | 396 DictionaryValue* dict; |
390 contents->GetDictionaryWithoutPathExpansion(extension_id, &dict); | 397 if (!contents->GetDictionaryWithoutPathExpansion(extension_id, &dict) || |
391 if (dict == NULL) | 398 dict == NULL) |
392 return; | 399 return; |
| 400 |
393 string16 frame_name; | 401 string16 frame_name; |
394 std::string url; | 402 std::string url; |
395 dict->GetString(kUrlKey, &url); | 403 dict->GetString(kUrlKey, &url); |
396 dict->GetString(kFrameNameKey, &frame_name); | 404 dict->GetString(kFrameNameKey, &frame_name); |
397 LoadBackgroundContents(profile, | 405 LoadBackgroundContents(profile, |
398 GURL(url), | 406 GURL(url), |
399 frame_name, | 407 frame_name, |
400 UTF8ToUTF16(extension_id)); | 408 UTF8ToUTF16(extension_id)); |
401 } | 409 } |
402 | 410 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 } | 519 } |
512 } | 520 } |
513 | 521 |
514 void BackgroundContentsService::BackgroundContentsOpened( | 522 void BackgroundContentsService::BackgroundContentsOpened( |
515 BackgroundContentsOpenedDetails* details) { | 523 BackgroundContentsOpenedDetails* details) { |
516 // Add the passed object to our list. Should not already be tracked. | 524 // Add the passed object to our list. Should not already be tracked. |
517 DCHECK(!IsTracked(details->contents)); | 525 DCHECK(!IsTracked(details->contents)); |
518 DCHECK(!details->application_id.empty()); | 526 DCHECK(!details->application_id.empty()); |
519 contents_map_[details->application_id].contents = details->contents; | 527 contents_map_[details->application_id].contents = details->contents; |
520 contents_map_[details->application_id].frame_name = details->frame_name; | 528 contents_map_[details->application_id].frame_name = details->frame_name; |
| 529 |
| 530 ScheduleCloseBalloon(UTF16ToASCII(details->application_id)); |
521 } | 531 } |
522 | 532 |
523 // Used by test code and debug checks to verify whether a given | 533 // Used by test code and debug checks to verify whether a given |
524 // BackgroundContents is being tracked by this instance. | 534 // BackgroundContents is being tracked by this instance. |
525 bool BackgroundContentsService::IsTracked( | 535 bool BackgroundContentsService::IsTracked( |
526 BackgroundContents* background_contents) const { | 536 BackgroundContents* background_contents) const { |
527 return !GetParentApplicationId(background_contents).empty(); | 537 return !GetParentApplicationId(background_contents).empty(); |
528 } | 538 } |
529 | 539 |
530 void BackgroundContentsService::BackgroundContentsShutdown( | 540 void BackgroundContentsService::BackgroundContentsShutdown( |
(...skipping 30 matching lines...) Expand all Loading... |
561 TabContents* new_contents, | 571 TabContents* new_contents, |
562 WindowOpenDisposition disposition, | 572 WindowOpenDisposition disposition, |
563 const gfx::Rect& initial_pos, | 573 const gfx::Rect& initial_pos, |
564 bool user_gesture) { | 574 bool user_gesture) { |
565 Browser* browser = BrowserList::GetLastActiveWithProfile( | 575 Browser* browser = BrowserList::GetLastActiveWithProfile( |
566 new_contents->profile()); | 576 new_contents->profile()); |
567 if (!browser) | 577 if (!browser) |
568 return; | 578 return; |
569 browser->AddTabContents(new_contents, disposition, initial_pos, user_gesture); | 579 browser->AddTabContents(new_contents, disposition, initial_pos, user_gesture); |
570 } | 580 } |
OLD | NEW |