Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_extension_web_contents_observer.cc
blobb692dfbb0e01fcb1cc9de515061cfef4a3cda176
1 // Copyright 2014 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.
5 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
7 #include "chrome/browser/extensions/error_console/error_console.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/window_controller.h"
10 #include "chrome/common/extensions/chrome_extension_messages.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extension_system.h"
17 #include "extensions/common/extension_messages.h"
18 #include "extensions/common/extension_urls.h"
20 using content::BrowserContext;
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
23 extensions::ChromeExtensionWebContentsObserver);
25 namespace extensions {
27 ChromeExtensionWebContentsObserver::ChromeExtensionWebContentsObserver(
28 content::WebContents* web_contents)
29 : ExtensionWebContentsObserver(web_contents) {}
31 ChromeExtensionWebContentsObserver::~ChromeExtensionWebContentsObserver() {}
33 void ChromeExtensionWebContentsObserver::RenderViewCreated(
34 content::RenderViewHost* render_view_host) {
35 ReloadIfTerminated(render_view_host);
36 ExtensionWebContentsObserver::RenderViewCreated(render_view_host);
39 bool ChromeExtensionWebContentsObserver::OnMessageReceived(
40 const IPC::Message& message,
41 content::RenderFrameHost* render_frame_host) {
42 if (ExtensionWebContentsObserver::OnMessageReceived(message,
43 render_frame_host)) {
44 return true;
47 bool handled = true;
48 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(ChromeExtensionWebContentsObserver, message,
49 render_frame_host)
50 IPC_MESSAGE_HANDLER(ExtensionHostMsg_DetailedConsoleMessageAdded,
51 OnDetailedConsoleMessageAdded)
52 IPC_MESSAGE_UNHANDLED(handled = false)
53 IPC_END_MESSAGE_MAP()
54 return handled;
57 void ChromeExtensionWebContentsObserver::OnDetailedConsoleMessageAdded(
58 content::RenderFrameHost* render_frame_host,
59 const base::string16& message,
60 const base::string16& source,
61 const StackTrace& stack_trace,
62 int32 severity_level) {
63 if (!IsSourceFromAnExtension(source))
64 return;
66 std::string extension_id = GetExtensionIdFromFrame(render_frame_host);
67 if (extension_id.empty())
68 extension_id = GURL(source).host();
70 ErrorConsole::Get(browser_context())
71 ->ReportError(scoped_ptr<ExtensionError>(new RuntimeError(
72 extension_id, browser_context()->IsOffTheRecord(), source, message,
73 stack_trace, web_contents()->GetLastCommittedURL(),
74 static_cast<logging::LogSeverity>(severity_level),
75 render_frame_host->GetRoutingID(),
76 render_frame_host->GetProcess()->GetID())));
79 void ChromeExtensionWebContentsObserver::InitializeRenderFrame(
80 content::RenderFrameHost* render_frame_host) {
81 ExtensionWebContentsObserver::InitializeRenderFrame(render_frame_host);
82 WindowController* controller = dispatcher()->GetExtensionWindowController();
83 if (controller) {
84 render_frame_host->Send(new ExtensionMsg_UpdateBrowserWindowId(
85 render_frame_host->GetRoutingID(), controller->GetWindowId()));
89 void ChromeExtensionWebContentsObserver::ReloadIfTerminated(
90 content::RenderViewHost* render_view_host) {
91 std::string extension_id = GetExtensionId(render_view_host);
92 if (extension_id.empty())
93 return;
95 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
97 // Reload the extension if it has crashed.
98 // TODO(yoz): This reload doesn't happen synchronously for unpacked
99 // extensions. It seems to be fast enough, but there is a race.
100 // We should delay loading until the extension has reloaded.
101 if (registry->GetExtensionById(extension_id, ExtensionRegistry::TERMINATED)) {
102 ExtensionSystem::Get(browser_context())->
103 extension_service()->ReloadExtension(extension_id);
107 } // namespace extensions