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

Side by Side Diff: chrome/browser/extensions/extension_host.cc

Issue 6794035: Move dispatching and sending of the last extension specific messages out of TabContents and Rende... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 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 | Annotate | Revision Log
OLDNEW
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/extensions/extension_host.h" 5 #include "chrome/browser/extensions/extension_host.h"
6 6
7 #include <list> 7 #include <list>
8 8
9 #include "base/memory/singleton.h" 9 #include "base/memory/singleton.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "chrome/browser/browser_list.h" 13 #include "chrome/browser/browser_list.h"
14 #include "chrome/browser/browser_shutdown.h" 14 #include "chrome/browser/browser_shutdown.h"
15 #include "chrome/browser/browser_window.h" 15 #include "chrome/browser/browser_window.h"
16 #include "chrome/browser/debugger/devtools_manager.h" 16 #include "chrome/browser/debugger/devtools_manager.h"
17 #include "chrome/browser/debugger/devtools_handler.h" 17 #include "chrome/browser/debugger/devtools_handler.h"
18 #include "chrome/browser/desktop_notification_handler.h" 18 #include "chrome/browser/desktop_notification_handler.h"
19 #include "chrome/browser/extensions/extension_message_service.h" 19 #include "chrome/browser/extensions/extension_message_handler.h"
20 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_tabs_module.h" 21 #include "chrome/browser/extensions/extension_tabs_module.h"
22 #include "chrome/browser/file_select_helper.h" 22 #include "chrome/browser/file_select_helper.h"
23 #include "chrome/browser/platform_util.h" 23 #include "chrome/browser/platform_util.h"
24 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/renderer_host/browser_render_process_host.h" 25 #include "chrome/browser/renderer_host/browser_render_process_host.h"
26 #include "chrome/browser/renderer_preferences_util.h" 26 #include "chrome/browser/renderer_preferences_util.h"
27 #include "chrome/browser/tab_contents/popup_menu_helper_mac.h" 27 #include "chrome/browser/tab_contents/popup_menu_helper_mac.h"
28 #include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h" 28 #include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
29 #include "chrome/browser/ui/browser.h" 29 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/webui/chrome_web_ui_factory.h" 30 #include "chrome/browser/ui/webui/chrome_web_ui_factory.h"
31 #include "chrome/common/chrome_constants.h" 31 #include "chrome/common/chrome_constants.h"
32 #include "chrome/common/bindings_policy.h" 32 #include "chrome/common/bindings_policy.h"
33 #include "chrome/common/extensions/extension.h" 33 #include "chrome/common/extensions/extension.h"
34 #include "chrome/common/extensions/extension_constants.h" 34 #include "chrome/common/extensions/extension_constants.h"
35 #include "chrome/common/extensions/extension_messages.h"
36 #include "chrome/common/render_messages.h" 35 #include "chrome/common/render_messages.h"
37 #include "chrome/common/url_constants.h" 36 #include "chrome/common/url_constants.h"
38 #include "chrome/common/view_types.h" 37 #include "chrome/common/view_types.h"
39 #include "content/browser/browsing_instance.h" 38 #include "content/browser/browsing_instance.h"
40 #include "content/browser/renderer_host/render_process_host.h" 39 #include "content/browser/renderer_host/render_process_host.h"
41 #include "content/browser/renderer_host/render_view_host.h" 40 #include "content/browser/renderer_host/render_view_host.h"
42 #include "content/browser/renderer_host/render_widget_host.h" 41 #include "content/browser/renderer_host/render_widget_host.h"
43 #include "content/browser/renderer_host/render_widget_host_view.h" 42 #include "content/browser/renderer_host/render_widget_host_view.h"
44 #include "content/browser/site_instance.h" 43 #include "content/browser/site_instance.h"
45 #include "content/browser/tab_contents/tab_contents.h" 44 #include "content/browser/tab_contents/tab_contents.h"
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED, 152 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED,
154 Source<RenderProcessHost>(render_process_host())); 153 Source<RenderProcessHost>(render_process_host()));
155 // Listen for when an extension is unloaded from the same profile, as it may 154 // Listen for when an extension is unloaded from the same profile, as it may
156 // be the same extension that this points to. 155 // be the same extension that this points to.
157 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, 156 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
158 Source<Profile>(profile_)); 157 Source<Profile>(profile_));
159 158
160 desktop_notification_handler_.reset( 159 desktop_notification_handler_.reset(
161 new DesktopNotificationHandler(NULL, render_process_host())); 160 new DesktopNotificationHandler(NULL, render_process_host()));
162 dev_tools_handler_.reset(new DevToolsHandler(NULL, render_view_host_)); 161 dev_tools_handler_.reset(new DevToolsHandler(NULL, render_view_host_));
162 extension_message_handler_.reset(new ExtensionMessageHandler(
163 render_process_host()->id(), render_view_host_, profile_));
163 } 164 }
164 165
165 ExtensionHost::~ExtensionHost() { 166 ExtensionHost::~ExtensionHost() {
166 NotificationService::current()->Notify( 167 NotificationService::current()->Notify(
167 NotificationType::EXTENSION_HOST_DESTROYED, 168 NotificationType::EXTENSION_HOST_DESTROYED,
168 Source<Profile>(profile_), 169 Source<Profile>(profile_),
169 Details<ExtensionHost>(this)); 170 Details<ExtensionHost>(this));
170 ProcessCreationQueue::GetInstance()->Remove(this); 171 ProcessCreationQueue::GetInstance()->Remove(this);
171 render_view_host_->Shutdown(); // deletes render_view_host 172 render_view_host_->Shutdown(); // deletes render_view_host
172 } 173 }
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 Details<ExtensionHost>(this)); 330 Details<ExtensionHost>(this));
330 } 331 }
331 332
332 void ExtensionHost::DidNavigate(RenderViewHost* render_view_host, 333 void ExtensionHost::DidNavigate(RenderViewHost* render_view_host,
333 const ViewHostMsg_FrameNavigate_Params& params) { 334 const ViewHostMsg_FrameNavigate_Params& params) {
334 // We only care when the outer frame changes. 335 // We only care when the outer frame changes.
335 if (!PageTransition::IsMainFrame(params.transition)) 336 if (!PageTransition::IsMainFrame(params.transition))
336 return; 337 return;
337 338
338 if (!params.url.SchemeIs(chrome::kExtensionScheme)) { 339 if (!params.url.SchemeIs(chrome::kExtensionScheme)) {
339 extension_function_dispatcher_.reset(NULL); 340 SetExtensionFunctionDispatcher(NULL);
340 url_ = params.url; 341 url_ = params.url;
341 return; 342 return;
342 } 343 }
343 344
344 // This catches two bogus use cases: 345 // This catches two bogus use cases:
345 // (1) URLs that look like chrome-extension://somethingbogus or 346 // (1) URLs that look like chrome-extension://somethingbogus or
346 // chrome-extension://nosuchid/, in other words, no Extension would 347 // chrome-extension://nosuchid/, in other words, no Extension would
347 // be found. 348 // be found.
348 // (2) URLs that refer to a different extension than this one. 349 // (2) URLs that refer to a different extension than this one.
349 // In both cases, we preserve the old URL and reset the EFD to NULL. This 350 // In both cases, we preserve the old URL and reset the EFD to NULL. This
350 // will leave the host in kind of a bad state with poor UI and errors, but 351 // will leave the host in kind of a bad state with poor UI and errors, but
351 // it's better than the alternative. 352 // it's better than the alternative.
352 // TODO(erikkay) Perhaps we should display errors in developer mode. 353 // TODO(erikkay) Perhaps we should display errors in developer mode.
353 if (params.url.host() != extension_->id()) { 354 if (params.url.host() != extension_->id()) {
354 extension_function_dispatcher_.reset(NULL); 355 SetExtensionFunctionDispatcher(NULL);
355 return; 356 return;
356 } 357 }
357 358
358 url_ = params.url; 359 url_ = params.url;
359 extension_function_dispatcher_.reset( 360 SetExtensionFunctionDispatcher(
360 ExtensionFunctionDispatcher::Create(render_view_host_, this, url_)); 361 ExtensionFunctionDispatcher::Create(render_view_host_, this, url_));
361 } 362 }
362 363
363 void ExtensionHost::InsertInfobarCSS() { 364 void ExtensionHost::InsertInfobarCSS() {
364 DCHECK(!is_background_page()); 365 DCHECK(!is_background_page());
365 366
366 static const base::StringPiece css( 367 static const base::StringPiece css(
367 ResourceBundle::GetSharedInstance().GetRawDataResource( 368 ResourceBundle::GetSharedInstance().GetRawDataResource(
368 IDR_EXTENSIONS_INFOBAR_CSS)); 369 IDR_EXTENSIONS_INFOBAR_CSS));
369 370
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 550
550 // TODO(dcheng): incorporate this setting into kClipboardPermission check. 551 // TODO(dcheng): incorporate this setting into kClipboardPermission check.
551 webkit_prefs.javascript_can_access_clipboard = true; 552 webkit_prefs.javascript_can_access_clipboard = true;
552 553
553 // TODO(dcheng): check kClipboardPermission instead once it's implemented. 554 // TODO(dcheng): check kClipboardPermission instead once it's implemented.
554 if (extension_->HasApiPermission(Extension::kExperimentalPermission)) 555 if (extension_->HasApiPermission(Extension::kExperimentalPermission))
555 webkit_prefs.dom_paste_enabled = true; 556 webkit_prefs.dom_paste_enabled = true;
556 return webkit_prefs; 557 return webkit_prefs;
557 } 558 }
558 559
559 void ExtensionHost::ProcessWebUIMessage(
560 const ExtensionHostMsg_DomMessage_Params& params) {
561 if (extension_function_dispatcher_.get()) {
562 extension_function_dispatcher_->HandleRequest(params);
563 }
564 }
565
566 RenderViewHostDelegate::View* ExtensionHost::GetViewDelegate() { 560 RenderViewHostDelegate::View* ExtensionHost::GetViewDelegate() {
567 return this; 561 return this;
568 } 562 }
569 563
570 void ExtensionHost::CreateNewWindow( 564 void ExtensionHost::CreateNewWindow(
571 int route_id, 565 int route_id,
572 const ViewHostMsg_CreateWindow_Params& params) { 566 const ViewHostMsg_CreateWindow_Params& params) {
573 // TODO(aa): Use the browser's profile if the extension is split mode 567 // TODO(aa): Use the browser's profile if the extension is split mode
574 // incognito. 568 // incognito.
575 TabContents* new_contents = delegate_view_helper_.CreateNewWindow( 569 TabContents* new_contents = delegate_view_helper_.CreateNewWindow(
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 } 781 }
788 782
789 ViewType::Type ExtensionHost::GetRenderViewType() const { 783 ViewType::Type ExtensionHost::GetRenderViewType() const {
790 return extension_host_type_; 784 return extension_host_type_;
791 } 785 }
792 786
793 bool ExtensionHost::OnMessageReceived(const IPC::Message& message) { 787 bool ExtensionHost::OnMessageReceived(const IPC::Message& message) {
794 bool handled = true; 788 bool handled = true;
795 IPC_BEGIN_MESSAGE_MAP(ExtensionHost, message) 789 IPC_BEGIN_MESSAGE_MAP(ExtensionHost, message)
796 IPC_MESSAGE_HANDLER(ViewHostMsg_RunFileChooser, OnRunFileChooser) 790 IPC_MESSAGE_HANDLER(ViewHostMsg_RunFileChooser, OnRunFileChooser)
797 IPC_MESSAGE_HANDLER(ExtensionHostMsg_PostMessage, OnPostMessage)
798 IPC_MESSAGE_UNHANDLED(handled = false) 791 IPC_MESSAGE_UNHANDLED(handled = false)
799 IPC_END_MESSAGE_MAP() 792 IPC_END_MESSAGE_MAP()
800 793
801 if (!handled) 794 if (!handled)
802 handled = desktop_notification_handler_->OnMessageReceived(message); 795 handled = desktop_notification_handler_->OnMessageReceived(message);
803 if (!handled) 796 if (!handled)
804 handled = dev_tools_handler_->OnMessageReceived(message); 797 handled = dev_tools_handler_->OnMessageReceived(message);
798 if (!handled)
799 handled = extension_message_handler_->OnMessageReceived(message);
805 return handled; 800 return handled;
806 } 801 }
807 802
808 const GURL& ExtensionHost::GetURL() const { 803 const GURL& ExtensionHost::GetURL() const {
809 return url_; 804 return url_;
810 } 805 }
811 806
812 void ExtensionHost::RenderViewCreated(RenderViewHost* render_view_host) { 807 void ExtensionHost::RenderViewCreated(RenderViewHost* render_view_host) {
813 if (view_.get()) 808 if (view_.get())
814 view_->RenderViewCreated(); 809 view_->RenderViewCreated();
815 810
816 // TODO(mpcomplete): This is duplicated in DidNavigate, which means that 811 // TODO(mpcomplete): This is duplicated in DidNavigate, which means that
817 // we'll create 2 EFDs for the first navigation. We should try to find a 812 // we'll create 2 EFDs for the first navigation. We should try to find a
818 // better way to unify them. 813 // better way to unify them.
819 // See http://code.google.com/p/chromium/issues/detail?id=18240 814 // See http://code.google.com/p/chromium/issues/detail?id=18240
820 extension_function_dispatcher_.reset( 815 SetExtensionFunctionDispatcher(
821 ExtensionFunctionDispatcher::Create(render_view_host, this, url_)); 816 ExtensionFunctionDispatcher::Create(render_view_host, this, url_));
822 817
823 if (extension_host_type_ == ViewType::EXTENSION_POPUP || 818 if (extension_host_type_ == ViewType::EXTENSION_POPUP ||
824 extension_host_type_ == ViewType::EXTENSION_INFOBAR) { 819 extension_host_type_ == ViewType::EXTENSION_INFOBAR) {
825 render_view_host->EnablePreferredSizeChangedMode( 820 render_view_host->EnablePreferredSizeChangedMode(
826 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow); 821 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
827 } 822 }
828 } 823 }
829 824
830 int ExtensionHost::GetBrowserWindowID() const { 825 int ExtensionHost::GetBrowserWindowID() const {
(...skipping 14 matching lines...) Expand all
845 return window_id; 840 return window_id;
846 } 841 }
847 842
848 void ExtensionHost::OnRunFileChooser( 843 void ExtensionHost::OnRunFileChooser(
849 const ViewHostMsg_RunFileChooser_Params& params) { 844 const ViewHostMsg_RunFileChooser_Params& params) {
850 if (file_select_helper_.get() == NULL) 845 if (file_select_helper_.get() == NULL)
851 file_select_helper_.reset(new FileSelectHelper(profile())); 846 file_select_helper_.reset(new FileSelectHelper(profile()));
852 file_select_helper_->RunFileChooser(render_view_host_, params); 847 file_select_helper_->RunFileChooser(render_view_host_, params);
853 } 848 }
854 849
855 void ExtensionHost::OnPostMessage(int port_id, const std::string& message) { 850 void ExtensionHost::SetExtensionFunctionDispatcher(
856 if (profile()->GetExtensionMessageService()) { 851 ExtensionFunctionDispatcher* efd) {
857 profile()->GetExtensionMessageService()->PostMessageFromRenderer( 852 extension_function_dispatcher_.reset(efd);
858 port_id, message); 853 extension_message_handler_->set_extension_function_dispatcher(efd);
859 }
860 } 854 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698