| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/platform_app_host.h" |
| 6 |
| 7 #include <list> |
| 8 |
| 9 // TODO(benwells): review include list and forward decls |
| 10 #include "base/bind.h" |
| 11 #include "base/memory/singleton.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop.h" |
| 14 #include "base/metrics/histogram.h" |
| 15 #include "base/string_util.h" |
| 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/browser_shutdown.h" |
| 18 #include "chrome/browser/extensions/extension_process_manager.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/extensions/extension_system.h" |
| 21 #include "chrome/browser/extensions/extension_tab_util.h" |
| 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h" |
| 24 #include "chrome/browser/ui/browser.h" |
| 25 #include "chrome/browser/ui/browser_list.h" |
| 26 #include "chrome/browser/ui/browser_window.h" |
| 27 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
| 28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 29 #include "chrome/common/chrome_constants.h" |
| 30 #include "chrome/common/chrome_notification_types.h" |
| 31 #include "chrome/common/chrome_view_type.h" |
| 32 #include "chrome/common/extensions/extension.h" |
| 33 #include "chrome/common/extensions/extension_constants.h" |
| 34 #include "chrome/common/extensions/extension_messages.h" |
| 35 #include "chrome/common/render_messages.h" |
| 36 #include "chrome/common/url_constants.h" |
| 37 #include "content/public/browser/content_browser_client.h" |
| 38 #include "content/public/browser/native_web_keyboard_event.h" |
| 39 #include "content/public/browser/notification_service.h" |
| 40 #include "content/public/browser/render_process_host.h" |
| 41 #include "content/public/browser/render_view_host.h" |
| 42 #include "content/public/browser/site_instance.h" |
| 43 #include "content/public/browser/web_contents.h" |
| 44 #include "content/public/browser/web_contents_view.h" |
| 45 #include "content/public/common/renderer_preferences.h" |
| 46 #include "grit/browser_resources.h" |
| 47 #include "grit/chromium_strings.h" |
| 48 #include "grit/generated_resources.h" |
| 49 #include "ui/base/keycodes/keyboard_codes.h" |
| 50 #include "ui/base/l10n/l10n_util.h" |
| 51 #include "ui/base/resource/resource_bundle.h" |
| 52 |
| 53 #if defined(TOOLKIT_VIEWS) |
| 54 #include "ui/views/widget/widget.h" |
| 55 #endif |
| 56 |
| 57 using WebKit::WebDragOperation; |
| 58 using WebKit::WebDragOperationsMask; |
| 59 using content::OpenURLParams; |
| 60 using content::RenderViewHost; |
| 61 using content::WebContents; |
| 62 |
| 63 ////////////////// |
| 64 // PlatformAppHost |
| 65 |
| 66 // TODO(benwells): SiteInstance should be created per platform app, not per |
| 67 // platform app host (which means per shell window). This probably means we |
| 68 // need some sort of PlatformAppSiteManager or something :( |
| 69 PlatformAppHost::PlatformAppHost(Profile* profile, |
| 70 const GURL& url) |
| 71 : profile_(profile), |
| 72 render_view_host_(NULL), |
| 73 did_stop_loading_(false), |
| 74 initial_url_(url), |
| 75 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 76 extension_function_dispatcher_(profile_, this)), |
| 77 associated_web_contents_(NULL), |
| 78 site_instance_(content::SiteInstance::Create(profile)) { |
| 79 host_contents_.reset(WebContents::Create( |
| 80 profile_, site_instance_.get(), MSG_ROUTING_NONE, NULL, NULL)); |
| 81 content::WebContentsObserver::Observe(host_contents_.get()); |
| 82 host_contents_->SetDelegate(this); |
| 83 host_contents_->SetViewType(chrome::VIEW_TYPE_APP_SHELL); |
| 84 host_contents_->GetMutableRendererPrefs()->browser_handles_all_requests = |
| 85 true; |
| 86 host_contents_->GetRenderViewHost()->SyncRendererPrefs(); |
| 87 |
| 88 prefs_tab_helper_.reset(new PrefsTabHelper(host_contents())); |
| 89 |
| 90 render_view_host_ = host_contents_->GetRenderViewHost(); |
| 91 |
| 92 CreateRenderViewSoon(); |
| 93 } |
| 94 |
| 95 PlatformAppHost::~PlatformAppHost() {} |
| 96 |
| 97 WebContents* PlatformAppHost::GetAssociatedWebContents() const { |
| 98 return associated_web_contents_; |
| 99 } |
| 100 |
| 101 void PlatformAppHost::SetAssociatedWebContents( |
| 102 content::WebContents* web_contents) { |
| 103 associated_web_contents_ = web_contents; |
| 104 if (web_contents) { |
| 105 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 106 content::Source<WebContents>(associated_web_contents_)); |
| 107 } |
| 108 } |
| 109 |
| 110 content::RenderProcessHost* PlatformAppHost::render_process_host() const { |
| 111 return render_view_host()->GetProcess(); |
| 112 } |
| 113 |
| 114 void PlatformAppHost::CreateRenderViewSoon() { |
| 115 host_contents_->GetController().LoadURL( |
| 116 initial_url_, content::Referrer(), content::PAGE_TRANSITION_LINK, |
| 117 std::string()); |
| 118 } |
| 119 |
| 120 Browser* PlatformAppHost::GetBrowser() { |
| 121 return NULL; |
| 122 } |
| 123 |
| 124 void PlatformAppHost::Close() { |
| 125 // TODO(benwells):create NOTIFICATION_PLATFORM_APP_VIEW_SHOULD_CLOSE. |
| 126 // content::NotificationService::current()->Notify( |
| 127 // chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
| 128 // content::Source<Profile>(profile_), |
| 129 // content::Details<PlatformAppHost>(this)); |
| 130 } |
| 131 |
| 132 void PlatformAppHost::Observe(int type, |
| 133 const content::NotificationSource& source, |
| 134 const content::NotificationDetails& details) { |
| 135 switch (type) { |
| 136 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: |
| 137 if (content::Source<WebContents>(source).ptr() == |
| 138 associated_web_contents_) { |
| 139 associated_web_contents_ = NULL; |
| 140 } |
| 141 break; |
| 142 default: |
| 143 NOTREACHED() << "Unexpected notification sent."; |
| 144 break; |
| 145 } |
| 146 } |
| 147 |
| 148 void PlatformAppHost::DidStopLoading() { |
| 149 bool notify = !did_stop_loading_; |
| 150 did_stop_loading_ = true; |
| 151 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) |
| 152 if (view_.get()) |
| 153 view_->DidStopLoading(); |
| 154 #endif |
| 155 if (notify) |
| 156 UMA_HISTOGRAM_TIMES("Extensions.ShellLoadTime", since_created_.Elapsed()); |
| 157 } |
| 158 |
| 159 void PlatformAppHost::CloseContents(WebContents* contents) { |
| 160 Close(); |
| 161 } |
| 162 |
| 163 bool PlatformAppHost::ShouldSuppressDialogs() { |
| 164 return true; |
| 165 } |
| 166 |
| 167 bool PlatformAppHost::OnMessageReceived(const IPC::Message& message) { |
| 168 bool handled = true; |
| 169 IPC_BEGIN_MESSAGE_MAP(PlatformAppHost, message) |
| 170 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 171 IPC_MESSAGE_UNHANDLED(handled = false) |
| 172 IPC_END_MESSAGE_MAP() |
| 173 return handled; |
| 174 } |
| 175 |
| 176 void PlatformAppHost::OnRequest(const ExtensionHostMsg_Request_Params& params) { |
| 177 extension_function_dispatcher_.Dispatch(params, render_view_host()); |
| 178 } |
| 179 |
| 180 void PlatformAppHost::RenderViewDeleted(RenderViewHost* render_view_host) { |
| 181 // If our RenderViewHost is deleted, fall back to the host_contents' current |
| 182 // RVH. There is sometimes a small gap between the pending RVH being deleted |
| 183 // and RenderViewCreated being called, so we update it here. |
| 184 if (render_view_host == render_view_host_) |
| 185 render_view_host_ = host_contents_->GetRenderViewHost(); |
| 186 } |
| OLD | NEW |