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

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

Issue 10913243: extensions: Add ExtensionView interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: instantiate android and mac too Created 8 years, 3 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/singleton.h" 10 #include "base/memory/singleton.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "chrome/browser/browser_shutdown.h" 16 #include "chrome/browser/browser_shutdown.h"
17 #include "chrome/browser/extensions/event_router.h" 17 #include "chrome/browser/extensions/event_router.h"
18 #include "chrome/browser/extensions/extension_process_manager.h" 18 #include "chrome/browser/extensions/extension_process_manager.h"
19 #include "chrome/browser/extensions/extension_service.h" 19 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/extensions/extension_system.h" 20 #include "chrome/browser/extensions/extension_system.h"
21 #include "chrome/browser/extensions/extension_tab_util.h" 21 #include "chrome/browser/extensions/extension_tab_util.h"
22 #include "chrome/browser/extensions/extension_view.h"
22 #include "chrome/browser/extensions/window_controller.h" 23 #include "chrome/browser/extensions/window_controller.h"
23 #include "chrome/browser/file_select_helper.h" 24 #include "chrome/browser/file_select_helper.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.h" 26 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.h"
26 #include "chrome/browser/ui/browser.h" 27 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_finder.h" 28 #include "chrome/browser/ui/browser_finder.h"
28 #include "chrome/browser/ui/browser_list.h" 29 #include "chrome/browser/ui/browser_list.h"
29 #include "chrome/browser/ui/browser_window.h" 30 #include "chrome/browser/ui/browser_window.h"
30 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" 31 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
31 #include "chrome/browser/ui/tab_contents/tab_contents.h" 32 #include "chrome/browser/ui/tab_contents/tab_contents.h"
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 since_created_.Elapsed()); 164 since_created_.Elapsed());
164 } 165 }
165 content::NotificationService::current()->Notify( 166 content::NotificationService::current()->Notify(
166 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, 167 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
167 content::Source<Profile>(profile_), 168 content::Source<Profile>(profile_),
168 content::Details<ExtensionHost>(this)); 169 content::Details<ExtensionHost>(this));
169 ProcessCreationQueue::GetInstance()->Remove(this); 170 ProcessCreationQueue::GetInstance()->Remove(this);
170 } 171 }
171 172
172 void ExtensionHost::CreateView(Browser* browser) { 173 void ExtensionHost::CreateView(Browser* browser) {
173 #if defined(TOOLKIT_VIEWS) 174 extension_view_.reset(ExtensionView::Create(this, browser));
174 view_.reset(new ExtensionViewViews(this, browser));
175 // We own |view_|, so don't auto delete when it's removed from the view
176 // hierarchy.
177 view_->set_owned_by_client();
178 #elif defined(OS_MACOSX)
179 view_.reset(new ExtensionViewMac(this, browser));
180 view_->Init();
181 #elif defined(TOOLKIT_GTK)
182 view_.reset(new ExtensionViewGtk(this, browser));
183 view_->Init();
184 #else
185 // TODO(port)
186 NOTREACHED();
187 #endif
188 } 175 }
189 176
190 WebContents* ExtensionHost::GetAssociatedWebContents() const { 177 WebContents* ExtensionHost::GetAssociatedWebContents() const {
191 return associated_web_contents_; 178 return associated_web_contents_;
192 } 179 }
193 180
194 void ExtensionHost::SetAssociatedWebContents( 181 void ExtensionHost::SetAssociatedWebContents(
195 content::WebContents* web_contents) { 182 content::WebContents* web_contents) {
196 associated_web_contents_ = web_contents; 183 associated_web_contents_ = web_contents;
197 if (web_contents) { 184 if (web_contents) {
(...skipping 29 matching lines...) Expand all
227 void ExtensionHost::CreateRenderViewNow() { 214 void ExtensionHost::CreateRenderViewNow() {
228 LoadInitialURL(); 215 LoadInitialURL();
229 if (is_background_page()) { 216 if (is_background_page()) {
230 DCHECK(IsRenderViewLive()); 217 DCHECK(IsRenderViewLive());
231 profile_->GetExtensionService()->DidCreateRenderViewForBackgroundPage(this); 218 profile_->GetExtensionService()->DidCreateRenderViewForBackgroundPage(this);
232 } 219 }
233 } 220 }
234 221
235 extensions::WindowController* 222 extensions::WindowController*
236 ExtensionHost::GetExtensionWindowController() const { 223 ExtensionHost::GetExtensionWindowController() const {
237 return view() && view()->browser() ? 224 return extension_view() && extension_view()->GetBrowser() ?
238 view()->browser()->extension_window_controller() : NULL; 225 extension_view()->GetBrowser()->extension_window_controller() : NULL;
239 } 226 }
240 227
241 const GURL& ExtensionHost::GetURL() const { 228 const GURL& ExtensionHost::GetURL() const {
242 return host_contents()->GetURL(); 229 return host_contents()->GetURL();
243 } 230 }
244 231
245 void ExtensionHost::LoadInitialURL() { 232 void ExtensionHost::LoadInitialURL() {
246 if (!is_background_page() && 233 if (!is_background_page() &&
247 !profile_->GetExtensionService()->IsBackgroundPageReady(extension_)) { 234 !profile_->GetExtensionService()->IsBackgroundPageReady(extension_)) {
248 // Make sure the background page loads before any others. 235 // Make sure the background page loads before any others.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 } 276 }
290 break; 277 break;
291 default: 278 default:
292 NOTREACHED() << "Unexpected notification sent."; 279 NOTREACHED() << "Unexpected notification sent.";
293 break; 280 break;
294 } 281 }
295 } 282 }
296 283
297 void ExtensionHost::ResizeDueToAutoResize(WebContents* source, 284 void ExtensionHost::ResizeDueToAutoResize(WebContents* source,
298 const gfx::Size& new_size) { 285 const gfx::Size& new_size) {
299 if (view()) 286 if (extension_view())
300 view()->ResizeDueToAutoResize(new_size); 287 extension_view()->ResizeDueToAutoResize(new_size);
301 } 288 }
302 289
303 void ExtensionHost::RenderViewGone(base::TerminationStatus status) { 290 void ExtensionHost::RenderViewGone(base::TerminationStatus status) {
304 // During browser shutdown, we may use sudden termination on an extension 291 // During browser shutdown, we may use sudden termination on an extension
305 // process, so it is expected to lose our connection to the render view. 292 // process, so it is expected to lose our connection to the render view.
306 // Do nothing. 293 // Do nothing.
307 if (browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID) 294 if (browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID)
308 return; 295 return;
309 296
310 // In certain cases, multiple ExtensionHost objects may have pointed to 297 // In certain cases, multiple ExtensionHost objects may have pointed to
(...skipping 25 matching lines...) Expand all
336 } 323 }
337 324
338 void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) { 325 void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) {
339 bool notify = !did_stop_loading_; 326 bool notify = !did_stop_loading_;
340 did_stop_loading_ = true; 327 did_stop_loading_ = true;
341 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP || 328 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP ||
342 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_DIALOG || 329 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_DIALOG ||
343 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR || 330 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR ||
344 extension_host_type_ == chrome::VIEW_TYPE_PANEL) { 331 extension_host_type_ == chrome::VIEW_TYPE_PANEL) {
345 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) 332 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX)
346 if (view()) 333 if (extension_view())
347 view()->DidStopLoading(); 334 extension_view()->DidStopLoading();
348 #endif 335 #endif
349 } 336 }
350 if (notify) { 337 if (notify) {
351 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { 338 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
352 if (extension_ && extension_->has_lazy_background_page()) { 339 if (extension_ && extension_->has_lazy_background_page()) {
353 UMA_HISTOGRAM_TIMES("Extensions.EventPageLoadTime", 340 UMA_HISTOGRAM_TIMES("Extensions.EventPageLoadTime",
354 since_created_.Elapsed()); 341 since_created_.Elapsed());
355 } else { 342 } else {
356 UMA_HISTOGRAM_TIMES("Extensions.BackgroundPageLoadTime", 343 UMA_HISTOGRAM_TIMES("Extensions.BackgroundPageLoadTime",
357 since_created_.Elapsed()); 344 since_created_.Elapsed());
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR || 393 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR ||
407 extension_host_type_ == chrome::VIEW_TYPE_PANEL) { 394 extension_host_type_ == chrome::VIEW_TYPE_PANEL) {
408 Close(); 395 Close();
409 } 396 }
410 } 397 }
411 398
412 void ExtensionHost::OnStartDownload( 399 void ExtensionHost::OnStartDownload(
413 content::WebContents* source, content::DownloadItem* download) { 400 content::WebContents* source, content::DownloadItem* download) {
414 // If |source| is in the context of a Browser, show the DownloadShelf on that 401 // If |source| is in the context of a Browser, show the DownloadShelf on that
415 // Browser. 402 // Browser.
416 if (!view() || !view()->browser()) 403 if (!extension_view() || !extension_view()->GetBrowser())
417 return; 404 return;
418 static_cast<content::WebContentsDelegate*>(view()->browser())-> 405 static_cast<content::WebContentsDelegate*>(extension_view()->GetBrowser())->
419 OnStartDownload(source, download); 406 OnStartDownload(source, download);
420 } 407 }
421 408
422 void ExtensionHost::WebIntentDispatch( 409 void ExtensionHost::WebIntentDispatch(
423 content::WebContents* web_contents, 410 content::WebContents* web_contents,
424 content::WebIntentsDispatcher* intents_dispatcher) { 411 content::WebIntentsDispatcher* intents_dispatcher) {
425 #if !defined(OS_ANDROID) 412 #if !defined(OS_ANDROID)
426 scoped_ptr<content::WebIntentsDispatcher> dispatcher(intents_dispatcher); 413 scoped_ptr<content::WebIntentsDispatcher> dispatcher(intents_dispatcher);
427 414
428 Browser* browser = view() ? view()->browser() 415 Browser* browser = extension_view() ? extension_view()->GetBrowser()
429 : browser::FindBrowserWithWebContents(web_contents); 416 : browser::FindBrowserWithWebContents(web_contents);
430 417
431 // For background scripts/pages, there will be no view(). In this case, we 418 // For background scripts/pages, there will be no extension_view(). In this
432 // want to treat the intent as a browser-initiated one and deliver it into the 419 // case, we want to treat the intent as a browser-initiated one and deliver it
433 // current browser. It probably came from a context menu click or similar. 420 // into the current browser. It probably came from a context menu click or
421 // similar.
434 if (!browser) 422 if (!browser)
435 browser = web_intents::GetBrowserForBackgroundWebIntentDelivery(profile()); 423 browser = web_intents::GetBrowserForBackgroundWebIntentDelivery(profile());
436 424
437 if (browser) { 425 if (browser) {
438 static_cast<WebContentsDelegate*>(browser)-> 426 static_cast<WebContentsDelegate*>(browser)->
439 WebIntentDispatch(NULL, dispatcher.release()); 427 WebIntentDispatch(NULL, dispatcher.release());
440 } 428 }
441 #endif 429 #endif
442 } 430 }
443 431
(...skipping 17 matching lines...) Expand all
461 switch (params.disposition) { 449 switch (params.disposition) {
462 case SINGLETON_TAB: 450 case SINGLETON_TAB:
463 case NEW_FOREGROUND_TAB: 451 case NEW_FOREGROUND_TAB:
464 case NEW_BACKGROUND_TAB: 452 case NEW_BACKGROUND_TAB:
465 case NEW_POPUP: 453 case NEW_POPUP:
466 case NEW_WINDOW: 454 case NEW_WINDOW:
467 case SAVE_TO_DISK: 455 case SAVE_TO_DISK:
468 case OFF_THE_RECORD: { 456 case OFF_THE_RECORD: {
469 // Only allow these from hosts that are bound to a browser (e.g. popups). 457 // Only allow these from hosts that are bound to a browser (e.g. popups).
470 // Otherwise they are not driven by a user gesture. 458 // Otherwise they are not driven by a user gesture.
471 Browser* browser = view() ? view()->browser() : NULL; 459 Browser* browser = extension_view() ?
460 extension_view()->GetBrowser() : NULL;
472 return browser ? browser->OpenURL(params) : NULL; 461 return browser ? browser->OpenURL(params) : NULL;
473 } 462 }
474 default: 463 default:
475 return NULL; 464 return NULL;
476 } 465 }
477 } 466 }
478 467
479 bool ExtensionHost::PreHandleKeyboardEvent(WebContents* source, 468 bool ExtensionHost::PreHandleKeyboardEvent(WebContents* source,
480 const NativeWebKeyboardEvent& event, 469 const NativeWebKeyboardEvent& event,
481 bool* is_keyboard_shortcut) { 470 bool* is_keyboard_shortcut) {
482 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP && 471 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP &&
483 event.type == NativeWebKeyboardEvent::RawKeyDown && 472 event.type == NativeWebKeyboardEvent::RawKeyDown &&
484 event.windowsKeyCode == ui::VKEY_ESCAPE) { 473 event.windowsKeyCode == ui::VKEY_ESCAPE) {
485 DCHECK(is_keyboard_shortcut != NULL); 474 DCHECK(is_keyboard_shortcut != NULL);
486 *is_keyboard_shortcut = true; 475 *is_keyboard_shortcut = true;
487 return false; 476 return false;
488 } 477 }
489 478
490 // Handle higher priority browser shortcuts such as Ctrl-w. 479 // Handle higher priority browser shortcuts such as Ctrl-w.
491 Browser* browser = view() ? view()->browser() : NULL; 480 Browser* browser = extension_view() ? extension_view()->GetBrowser() : NULL;
492 if (browser) 481 if (browser)
493 return browser->PreHandleKeyboardEvent(source, event, is_keyboard_shortcut); 482 return browser->PreHandleKeyboardEvent(source, event, is_keyboard_shortcut);
494 483
495 *is_keyboard_shortcut = false; 484 *is_keyboard_shortcut = false;
496 return false; 485 return false;
497 } 486 }
498 487
499 void ExtensionHost::HandleKeyboardEvent(WebContents* source, 488 void ExtensionHost::HandleKeyboardEvent(WebContents* source,
500 const NativeWebKeyboardEvent& event) { 489 const NativeWebKeyboardEvent& event) {
501 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP) { 490 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 ExtensionProcessManager* pm = 533 ExtensionProcessManager* pm =
545 ExtensionSystem::Get(profile_)->process_manager(); 534 ExtensionSystem::Get(profile_)->process_manager();
546 if (pm) 535 if (pm)
547 pm->DecrementLazyKeepaliveCount(extension()); 536 pm->DecrementLazyKeepaliveCount(extension());
548 } 537 }
549 538
550 void ExtensionHost::UnhandledKeyboardEvent( 539 void ExtensionHost::UnhandledKeyboardEvent(
551 WebContents* source, 540 WebContents* source,
552 const content::NativeWebKeyboardEvent& event) { 541 const content::NativeWebKeyboardEvent& event) {
553 // Handle lower priority browser shortcuts such as Ctrl-f. 542 // Handle lower priority browser shortcuts such as Ctrl-f.
554 Browser* browser = view() ? view()->browser() : NULL; 543 Browser* browser = extension_view() ? extension_view()->GetBrowser() : NULL;
555 if (browser) 544 if (browser)
556 return browser->HandleKeyboardEvent(source, event); 545 return browser->HandleKeyboardEvent(source, event);
557 } 546 }
558 547
559 void ExtensionHost::RenderViewCreated(RenderViewHost* render_view_host) { 548 void ExtensionHost::RenderViewCreated(RenderViewHost* render_view_host) {
560 render_view_host_ = render_view_host; 549 render_view_host_ = render_view_host;
561 550
562 if (view()) 551 if (extension_view())
563 view()->RenderViewCreated(); 552 extension_view()->RenderViewCreated();
564 553
565 // If the host is bound to a window, then extract its id. Extensions hosted 554 // If the host is bound to a window, then extract its id. Extensions hosted
566 // in ExternalTabContainer objects may not have an associated window. 555 // in ExternalTabContainer objects may not have an associated window.
567 extensions::WindowController* window = GetExtensionWindowController(); 556 extensions::WindowController* window = GetExtensionWindowController();
568 if (window) { 557 if (window) {
569 render_view_host->Send(new ExtensionMsg_UpdateBrowserWindowId( 558 render_view_host->Send(new ExtensionMsg_UpdateBrowserWindowId(
570 render_view_host->GetRoutingID(), window->GetWindowId())); 559 render_view_host->GetRoutingID(), window->GetWindowId()));
571 } 560 }
572 } 561 }
573 562
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
624 } 613 }
625 614
626 void ExtensionHost::RenderViewReady() { 615 void ExtensionHost::RenderViewReady() {
627 content::NotificationService::current()->Notify( 616 content::NotificationService::current()->Notify(
628 chrome::NOTIFICATION_EXTENSION_HOST_CREATED, 617 chrome::NOTIFICATION_EXTENSION_HOST_CREATED,
629 content::Source<Profile>(profile_), 618 content::Source<Profile>(profile_),
630 content::Details<ExtensionHost>(this)); 619 content::Details<ExtensionHost>(this));
631 } 620 }
632 621
633 } // namespace extensions 622 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698