OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/chrome_notification_observer.h" | 5 #include "chrome/browser/extensions/chrome_notification_observer.h" |
6 | 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser.h" |
7 #include "chrome/common/extensions/features/feature_channel.h" | 11 #include "chrome/common/extensions/features/feature_channel.h" |
8 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
9 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
10 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 15 #include "extensions/browser/extension_system.h" |
| 16 #include "extensions/browser/process_manager.h" |
11 #include "extensions/common/extension_messages.h" | 17 #include "extensions/common/extension_messages.h" |
12 | 18 |
13 namespace extensions { | 19 namespace extensions { |
14 | 20 |
15 ChromeNotificationObserver::ChromeNotificationObserver() { | 21 ChromeNotificationObserver::ChromeNotificationObserver() { |
16 registrar_.Add(this, | 22 registrar_.Add(this, |
| 23 chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
| 24 content::NotificationService::AllSources()); |
| 25 registrar_.Add(this, |
17 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 26 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
18 content::NotificationService::AllBrowserContextsAndSources()); | 27 content::NotificationService::AllBrowserContextsAndSources()); |
19 } | 28 } |
20 | 29 |
21 ChromeNotificationObserver::~ChromeNotificationObserver() {} | 30 ChromeNotificationObserver::~ChromeNotificationObserver() {} |
22 | 31 |
| 32 void ChromeNotificationObserver::OnBrowserWindowReady(Browser* browser) { |
| 33 Profile* profile = browser->profile(); |
| 34 DCHECK(profile); |
| 35 |
| 36 // Inform the process manager for this profile that the window is ready. |
| 37 // We continue to observe the notification in case browser windows open for |
| 38 // a related incognito profile or other regular profiles. |
| 39 extensions::ProcessManager* manager = |
| 40 ExtensionSystem::Get(profile)->process_manager(); |
| 41 if (!manager) // Tests may not have a process manager. |
| 42 return; |
| 43 DCHECK_EQ(profile, manager->GetBrowserContext()); |
| 44 manager->OnBrowserWindowReady(); |
| 45 |
| 46 // For incognito profiles also inform the original profile's process manager |
| 47 // that the window is ready. This will usually be a no-op because the |
| 48 // original profile's process manager should have been informed when the |
| 49 // non-incognito window opened. |
| 50 if (profile->IsOffTheRecord()) { |
| 51 Profile* original_profile = profile->GetOriginalProfile(); |
| 52 extensions::ProcessManager* original_manager = |
| 53 ExtensionSystem::Get(original_profile)->process_manager(); |
| 54 DCHECK(original_manager); |
| 55 DCHECK_EQ(original_profile, original_manager->GetBrowserContext()); |
| 56 original_manager->OnBrowserWindowReady(); |
| 57 } |
| 58 } |
| 59 |
23 void ChromeNotificationObserver::OnRendererProcessCreated( | 60 void ChromeNotificationObserver::OnRendererProcessCreated( |
24 content::RenderProcessHost* process) { | 61 content::RenderProcessHost* process) { |
25 // Extensions need to know the channel for API restrictions. Send the channel | 62 // Extensions need to know the channel for API restrictions. Send the channel |
26 // to all renderers, as the non-extension renderers may have content scripts. | 63 // to all renderers, as the non-extension renderers may have content scripts. |
27 process->Send(new ExtensionMsg_SetChannel(GetCurrentChannel())); | 64 process->Send(new ExtensionMsg_SetChannel(GetCurrentChannel())); |
28 } | 65 } |
29 | 66 |
30 void ChromeNotificationObserver::Observe(int type, | 67 void ChromeNotificationObserver::Observe(int type, |
31 const content::NotificationSource& source, | 68 const content::NotificationSource& source, |
32 const content::NotificationDetails& details) { | 69 const content::NotificationDetails& details) { |
33 switch (type) { | 70 switch (type) { |
| 71 case chrome::NOTIFICATION_BROWSER_WINDOW_READY: { |
| 72 Browser* browser = content::Source<Browser>(source).ptr(); |
| 73 OnBrowserWindowReady(browser); |
| 74 break; |
| 75 } |
| 76 |
34 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { | 77 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { |
35 content::RenderProcessHost* process = | 78 content::RenderProcessHost* process = |
36 content::Source<content::RenderProcessHost>(source).ptr(); | 79 content::Source<content::RenderProcessHost>(source).ptr(); |
37 OnRendererProcessCreated(process); | 80 OnRendererProcessCreated(process); |
38 break; | 81 break; |
39 } | 82 } |
40 | 83 |
41 default: | 84 default: |
42 NOTREACHED(); | 85 NOTREACHED(); |
43 } | 86 } |
44 } | 87 } |
45 | 88 |
46 } // namespace extensions | 89 } // namespace extensions |
OLD | NEW |