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/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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "chrome/browser/ui/browser_window.h" | 25 #include "chrome/browser/ui/browser_window.h" |
26 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 26 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
27 #include "chrome/browser/ui/webui/chrome_web_ui_factory.h" | 27 #include "chrome/browser/ui/webui/chrome_web_ui_factory.h" |
28 #include "chrome/common/chrome_constants.h" | 28 #include "chrome/common/chrome_constants.h" |
29 #include "chrome/common/extensions/extension.h" | 29 #include "chrome/common/extensions/extension.h" |
30 #include "chrome/common/extensions/extension_constants.h" | 30 #include "chrome/common/extensions/extension_constants.h" |
31 #include "chrome/common/extensions/extension_messages.h" | 31 #include "chrome/common/extensions/extension_messages.h" |
32 #include "chrome/common/render_messages.h" | 32 #include "chrome/common/render_messages.h" |
33 #include "chrome/common/url_constants.h" | 33 #include "chrome/common/url_constants.h" |
34 #include "content/browser/browsing_instance.h" | 34 #include "content/browser/browsing_instance.h" |
| 35 #include "content/browser/content_browser_client.h" |
35 #include "content/browser/renderer_host/browser_render_process_host.h" | 36 #include "content/browser/renderer_host/browser_render_process_host.h" |
36 #include "content/browser/renderer_host/render_process_host.h" | 37 #include "content/browser/renderer_host/render_process_host.h" |
37 #include "content/browser/renderer_host/render_view_host.h" | 38 #include "content/browser/renderer_host/render_view_host.h" |
38 #include "content/browser/renderer_host/render_widget_host.h" | 39 #include "content/browser/renderer_host/render_widget_host.h" |
39 #include "content/browser/renderer_host/render_widget_host_view.h" | 40 #include "content/browser/renderer_host/render_widget_host_view.h" |
40 #include "content/browser/site_instance.h" | 41 #include "content/browser/site_instance.h" |
41 #include "content/browser/tab_contents/tab_contents.h" | 42 #include "content/browser/tab_contents/tab_contents.h" |
42 #include "content/browser/tab_contents/tab_contents_view.h" | 43 #include "content/browser/tab_contents/tab_contents_view.h" |
43 #include "content/common/bindings_policy.h" | 44 #include "content/common/bindings_policy.h" |
| 45 #include "content/common/content_client.h" |
44 #include "content/common/native_web_keyboard_event.h" | 46 #include "content/common/native_web_keyboard_event.h" |
45 #include "content/common/notification_service.h" | 47 #include "content/common/notification_service.h" |
46 #include "content/common/view_messages.h" | 48 #include "content/common/view_messages.h" |
47 #include "grit/browser_resources.h" | 49 #include "grit/browser_resources.h" |
48 #include "grit/generated_resources.h" | 50 #include "grit/generated_resources.h" |
49 #include "ui/base/keycodes/keyboard_codes.h" | 51 #include "ui/base/keycodes/keyboard_codes.h" |
50 #include "ui/base/l10n/l10n_util.h" | 52 #include "ui/base/l10n/l10n_util.h" |
51 #include "ui/base/resource/resource_bundle.h" | 53 #include "ui/base/resource/resource_bundle.h" |
52 #include "webkit/glue/context_menu.h" | 54 #include "webkit/glue/context_menu.h" |
53 | 55 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 ViewType::Type host_type) | 129 ViewType::Type host_type) |
128 : extension_(extension), | 130 : extension_(extension), |
129 extension_id_(extension->id()), | 131 extension_id_(extension->id()), |
130 profile_(site_instance->browsing_instance()->profile()), | 132 profile_(site_instance->browsing_instance()->profile()), |
131 did_stop_loading_(false), | 133 did_stop_loading_(false), |
132 document_element_available_(false), | 134 document_element_available_(false), |
133 url_(url), | 135 url_(url), |
134 ALLOW_THIS_IN_INITIALIZER_LIST( | 136 ALLOW_THIS_IN_INITIALIZER_LIST( |
135 extension_function_dispatcher_(profile_, this)), | 137 extension_function_dispatcher_(profile_, this)), |
136 extension_host_type_(host_type), | 138 extension_host_type_(host_type), |
137 associated_tab_contents_(NULL), | 139 associated_tab_contents_(NULL) { |
138 suppress_javascript_messages_(false) { | |
139 render_view_host_ = new RenderViewHost(site_instance, this, MSG_ROUTING_NONE, | 140 render_view_host_ = new RenderViewHost(site_instance, this, MSG_ROUTING_NONE, |
140 NULL); | 141 NULL); |
141 if (enable_dom_automation_) | 142 if (enable_dom_automation_) |
142 render_view_host_->AllowBindings(BindingsPolicy::DOM_AUTOMATION); | 143 render_view_host_->AllowBindings(BindingsPolicy::DOM_AUTOMATION); |
143 | 144 |
144 // Listen for when the render process' handle is available so we can add it | 145 // Listen for when the render process' handle is available so we can add it |
145 // to the task manager then. | 146 // to the task manager then. |
146 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED, | 147 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED, |
147 Source<RenderProcessHost>(render_process_host())); | 148 Source<RenderProcessHost>(render_process_host())); |
148 // Listen for when an extension is unloaded from the same profile, as it may | 149 // Listen for when an extension is unloaded from the same profile, as it may |
149 // be the same extension that this points to. | 150 // be the same extension that this points to. |
150 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 151 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
151 Source<Profile>(profile_)); | 152 Source<Profile>(profile_)); |
152 } | 153 } |
153 | 154 |
154 ExtensionHost::~ExtensionHost() { | 155 ExtensionHost::~ExtensionHost() { |
155 NotificationService::current()->Notify( | 156 NotificationService::current()->Notify( |
156 NotificationType::EXTENSION_HOST_DESTROYED, | 157 NotificationType::EXTENSION_HOST_DESTROYED, |
157 Source<Profile>(profile_), | 158 Source<Profile>(profile_), |
158 Details<ExtensionHost>(this)); | 159 Details<ExtensionHost>(this)); |
159 ProcessCreationQueue::GetInstance()->Remove(this); | 160 ProcessCreationQueue::GetInstance()->Remove(this); |
| 161 GetJavaScriptDialogCreatorInstance()->ResetJavaScriptState(this); |
160 render_view_host_->Shutdown(); // deletes render_view_host | 162 render_view_host_->Shutdown(); // deletes render_view_host |
161 } | 163 } |
162 | 164 |
163 void ExtensionHost::CreateView(Browser* browser) { | 165 void ExtensionHost::CreateView(Browser* browser) { |
164 #if defined(TOOLKIT_VIEWS) | 166 #if defined(TOOLKIT_VIEWS) |
165 view_.reset(new ExtensionView(this, browser)); | 167 view_.reset(new ExtensionView(this, browser)); |
166 // We own |view_|, so don't auto delete when it's removed from the view | 168 // We own |view_|, so don't auto delete when it's removed from the view |
167 // hierarchy. | 169 // hierarchy. |
168 view_->set_parent_owned(false); | 170 view_->set_parent_owned(false); |
169 #elif defined(OS_MACOSX) | 171 #elif defined(OS_MACOSX) |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 } | 411 } |
410 } | 412 } |
411 | 413 |
412 void ExtensionHost::RunJavaScriptMessage(const RenderViewHost* rvh, | 414 void ExtensionHost::RunJavaScriptMessage(const RenderViewHost* rvh, |
413 const string16& message, | 415 const string16& message, |
414 const string16& default_prompt, | 416 const string16& default_prompt, |
415 const GURL& frame_url, | 417 const GURL& frame_url, |
416 const int flags, | 418 const int flags, |
417 IPC::Message* reply_msg, | 419 IPC::Message* reply_msg, |
418 bool* did_suppress_message) { | 420 bool* did_suppress_message) { |
419 base::TimeDelta time_since_last_message( | 421 bool suppress_this_message = false; |
420 base::TimeTicks::Now() - last_javascript_message_dismissal_); | 422 GetJavaScriptDialogCreatorInstance()->RunJavaScriptDialog( |
| 423 this, |
| 424 frame_url, |
| 425 flags, |
| 426 message, |
| 427 default_prompt, |
| 428 reply_msg, |
| 429 &suppress_this_message, |
| 430 profile()); |
421 | 431 |
422 *did_suppress_message = suppress_javascript_messages_; | 432 if (suppress_this_message) { |
423 if (!suppress_javascript_messages_) { | 433 // If we are suppressing messages, just reply as if the user immediately |
424 bool show_suppress_checkbox = false; | 434 // pressed "Cancel". |
425 // Show a checkbox offering to suppress further messages if this message is | 435 OnDialogClosed(reply_msg, false, string16()); |
426 // being displayed within kJavascriptMessageExpectedDelay of the last one. | 436 } |
427 if (time_since_last_message < | |
428 base::TimeDelta::FromMilliseconds( | |
429 chrome::kJavascriptMessageExpectedDelay)) | |
430 show_suppress_checkbox = true; | |
431 | 437 |
432 // Unlike for page alerts, navigations aren't a good signal for when to | 438 *did_suppress_message = suppress_this_message; |
433 // resume showing alerts, so we can't reasonably stop showing them even if | |
434 // the extension is spammy. | |
435 RunJavascriptMessageBox(profile_, | |
436 this, | |
437 frame_url, | |
438 flags, | |
439 UTF16ToWideHack(message), | |
440 UTF16ToWideHack(default_prompt), | |
441 show_suppress_checkbox, | |
442 reply_msg); | |
443 } else { | |
444 // If we are suppressing messages, just reply as is if the user immediately | |
445 // pressed "Cancel". | |
446 OnMessageBoxClosed(reply_msg, false, std::wstring()); | |
447 } | |
448 } | 439 } |
449 | 440 |
450 gfx::NativeWindow ExtensionHost::GetMessageBoxRootWindow() { | 441 gfx::NativeWindow ExtensionHost::GetDialogRootWindow() { |
451 // If we have a view, use that. | 442 // If we have a view, use that. |
452 gfx::NativeView native_view = GetNativeViewOfHost(); | 443 gfx::NativeView native_view = GetNativeViewOfHost(); |
453 if (native_view) | 444 if (native_view) |
454 return platform_util::GetTopLevel(native_view); | 445 return platform_util::GetTopLevel(native_view); |
455 | 446 |
456 // Otherwise, try the active tab's view. | 447 // Otherwise, try the active tab's view. |
457 Browser* browser = extension_function_dispatcher_.GetCurrentBrowser( | 448 Browser* browser = extension_function_dispatcher_.GetCurrentBrowser( |
458 render_view_host_, true); | 449 render_view_host_, true); |
459 if (browser) { | 450 if (browser) { |
460 TabContents* active_tab = browser->GetSelectedTabContents(); | 451 TabContents* active_tab = browser->GetSelectedTabContents(); |
461 if (active_tab) | 452 if (active_tab) |
462 return active_tab->view()->GetTopLevelNativeWindow(); | 453 return active_tab->view()->GetTopLevelNativeWindow(); |
463 } | 454 } |
464 | 455 |
465 return NULL; | 456 return NULL; |
466 } | 457 } |
467 | 458 |
468 TabContents* ExtensionHost::AsTabContents() { | 459 TabContents* ExtensionHost::AsTabContents() { |
469 return NULL; | 460 return NULL; |
470 } | 461 } |
471 | 462 |
472 ExtensionHost* ExtensionHost::AsExtensionHost() { | 463 ExtensionHost* ExtensionHost::AsExtensionHost() { |
473 return this; | 464 return this; |
474 } | 465 } |
475 | 466 |
476 void ExtensionHost::OnMessageBoxClosed(IPC::Message* reply_msg, | 467 void ExtensionHost::OnDialogClosed(IPC::Message* reply_msg, |
477 bool success, | 468 bool success, |
478 const std::wstring& user_input) { | 469 const string16& user_input) { |
479 last_javascript_message_dismissal_ = base::TimeTicks::Now(); | |
480 render_view_host()->JavaScriptDialogClosed(reply_msg, | 470 render_view_host()->JavaScriptDialogClosed(reply_msg, |
481 success, | 471 success, |
482 WideToUTF16Hack(user_input)); | 472 user_input); |
483 } | |
484 | |
485 void ExtensionHost::SetSuppressMessageBoxes(bool suppress_message_boxes) { | |
486 suppress_javascript_messages_ = suppress_message_boxes; | |
487 } | 473 } |
488 | 474 |
489 void ExtensionHost::Close(RenderViewHost* render_view_host) { | 475 void ExtensionHost::Close(RenderViewHost* render_view_host) { |
490 if (extension_host_type_ == ViewType::EXTENSION_POPUP || | 476 if (extension_host_type_ == ViewType::EXTENSION_POPUP || |
491 extension_host_type_ == ViewType::EXTENSION_DIALOG || | 477 extension_host_type_ == ViewType::EXTENSION_DIALOG || |
492 extension_host_type_ == ViewType::EXTENSION_INFOBAR) { | 478 extension_host_type_ == ViewType::EXTENSION_INFOBAR) { |
493 NotificationService::current()->Notify( | 479 NotificationService::current()->Notify( |
494 NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 480 NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
495 Source<Profile>(profile_), | 481 Source<Profile>(profile_), |
496 Details<ExtensionHost>(this)); | 482 Details<ExtensionHost>(this)); |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 return window_id; | 808 return window_id; |
823 } | 809 } |
824 | 810 |
825 void ExtensionHost::OnRunFileChooser( | 811 void ExtensionHost::OnRunFileChooser( |
826 const ViewHostMsg_RunFileChooser_Params& params) { | 812 const ViewHostMsg_RunFileChooser_Params& params) { |
827 if (file_select_helper_.get() == NULL) | 813 if (file_select_helper_.get() == NULL) |
828 file_select_helper_.reset(new FileSelectHelper(profile())); | 814 file_select_helper_.reset(new FileSelectHelper(profile())); |
829 file_select_helper_->RunFileChooser(render_view_host_, | 815 file_select_helper_->RunFileChooser(render_view_host_, |
830 GetAssociatedTabContents(), params); | 816 GetAssociatedTabContents(), params); |
831 } | 817 } |
OLD | NEW |