| 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/chromeos/extensions/file_browser_resource_throttle.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/chromeos/extensions/file_browser_handler.h" | |
| 12 #include "chrome/browser/extensions/event_router.h" | |
| 13 #include "chrome/browser/extensions/extension_info_map.h" | |
| 14 #include "chrome/browser/extensions/extension_system.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/common/extensions/extension.h" | |
| 17 #include "chrome/common/extensions/extension_constants.h" | |
| 18 #include "chrome/common/extensions/extension_set.h" | |
| 19 #include "content/public/browser/browser_context.h" | |
| 20 #include "content/public/browser/browser_thread.h" | |
| 21 #include "content/public/browser/render_view_host.h" | |
| 22 #include "content/public/browser/resource_controller.h" | |
| 23 #include "content/public/browser/web_contents.h" | |
| 24 #include "net/url_request/url_request.h" | |
| 25 | |
| 26 using extensions::Event; | |
| 27 using extensions::Extension; | |
| 28 using extensions::ExtensionSystem; | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 const char* const kOnExecuteContentHandlerEvent = | |
| 33 "fileBrowserHandler.onExecuteContentHandler"; | |
| 34 | |
| 35 // Goes through the extension's file browser handlers and checks it there is one | |
| 36 // that can handle the |mime_type|. | |
| 37 // |extension| must not be NULL. | |
| 38 bool CanHandleMimeType(const Extension* extension, | |
| 39 const std::string& mime_type) { | |
| 40 FileBrowserHandler::List* handlers = | |
| 41 FileBrowserHandler::GetHandlers(extension); | |
| 42 if (!handlers) | |
| 43 return false; | |
| 44 | |
| 45 for (FileBrowserHandler::List::const_iterator handler = handlers->begin(); | |
| 46 handler != handlers->end(); | |
| 47 ++handler) { | |
| 48 if ((*handler)->CanHandleMIMEType(mime_type)) { | |
| 49 return true; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 // Retrieves Profile for a render view host specified by |render_process_id| and | |
| 57 // |render_view_id|. | |
| 58 Profile* GetProfile(int render_process_id, int render_view_id) { | |
| 59 content::RenderViewHost* render_view_host = | |
| 60 content::RenderViewHost::FromID(render_process_id, render_view_id); | |
| 61 if (!render_view_host) | |
| 62 return NULL; | |
| 63 | |
| 64 content::WebContents* web_contents = | |
| 65 content::WebContents::FromRenderViewHost(render_view_host); | |
| 66 if (!web_contents) | |
| 67 return NULL; | |
| 68 | |
| 69 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); | |
| 70 if (!browser_context) | |
| 71 return NULL; | |
| 72 | |
| 73 return Profile::FromBrowserContext(browser_context); | |
| 74 } | |
| 75 | |
| 76 void DispatchEventOnUIThread(const std::string& mime_type, | |
| 77 const GURL& request_url, | |
| 78 int render_process_id, | |
| 79 int render_view_id, | |
| 80 const std::string& extension_id) { | |
| 81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 82 | |
| 83 Profile* profile = GetProfile(render_process_id, render_view_id); | |
| 84 if (!profile) | |
| 85 return; | |
| 86 | |
| 87 // Create the event's arguments value. | |
| 88 scoped_ptr<ListValue> event_args(new ListValue()); | |
| 89 event_args->Append(new base::StringValue(mime_type)); | |
| 90 event_args->Append(new base::StringValue(request_url.spec())); | |
| 91 | |
| 92 scoped_ptr<Event> event(new Event(kOnExecuteContentHandlerEvent, | |
| 93 event_args.Pass())); | |
| 94 | |
| 95 ExtensionSystem::Get(profile)->event_router()->DispatchEventToExtension( | |
| 96 extension_id, event.Pass()); | |
| 97 } | |
| 98 | |
| 99 // Default implementation of FileBrowserHandlerEventRouter. | |
| 100 class FileBrowserHandlerEventRouterImpl | |
| 101 : public FileBrowserResourceThrottle::FileBrowserHandlerEventRouter { | |
| 102 public: | |
| 103 FileBrowserHandlerEventRouterImpl() {} | |
| 104 virtual ~FileBrowserHandlerEventRouterImpl() {} | |
| 105 | |
| 106 virtual void DispatchMimeTypeHandlerEvent( | |
| 107 int render_process_id, | |
| 108 int render_view_id, | |
| 109 const std::string& mime_type, | |
| 110 const GURL& request_url, | |
| 111 const std::string& extension_id) OVERRIDE { | |
| 112 // The event must be dispatched on the UI thread. | |
| 113 content::BrowserThread::PostTask( | |
| 114 content::BrowserThread::UI, FROM_HERE, | |
| 115 base::Bind(&DispatchEventOnUIThread, mime_type, request_url, | |
| 116 render_process_id, render_view_id, extension_id)); | |
| 117 } | |
| 118 }; | |
| 119 | |
| 120 } // namespace | |
| 121 | |
| 122 // static | |
| 123 FileBrowserResourceThrottle* FileBrowserResourceThrottle::Create( | |
| 124 int render_process_id, | |
| 125 int render_view_id, | |
| 126 net::URLRequest* request, | |
| 127 bool profile_is_incognito, | |
| 128 const ExtensionInfoMap* extension_info_map) { | |
| 129 std::string mime_type; | |
| 130 request->GetMimeType(&mime_type); | |
| 131 scoped_ptr<FileBrowserHandlerEventRouter> event_router( | |
| 132 new FileBrowserHandlerEventRouterImpl()); | |
| 133 return new FileBrowserResourceThrottle(render_process_id, render_view_id, | |
| 134 mime_type, request->url(), profile_is_incognito, extension_info_map, | |
| 135 event_router.Pass()); | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 FileBrowserResourceThrottle* FileBrowserResourceThrottle::CreateForTest( | |
| 140 int render_process_id, | |
| 141 int render_view_id, | |
| 142 const std::string& mime_type, | |
| 143 const GURL& request_url, | |
| 144 bool profile_is_incognito, | |
| 145 const ExtensionInfoMap* extension_info_map, | |
| 146 scoped_ptr<FileBrowserHandlerEventRouter> event_router_in) { | |
| 147 scoped_ptr<FileBrowserHandlerEventRouter> event_router = | |
| 148 event_router_in.Pass(); | |
| 149 if (!event_router) | |
| 150 event_router.reset(new FileBrowserHandlerEventRouterImpl()); | |
| 151 return new FileBrowserResourceThrottle(render_process_id, render_view_id, | |
| 152 mime_type, request_url, profile_is_incognito, extension_info_map, | |
| 153 event_router.Pass()); | |
| 154 } | |
| 155 | |
| 156 FileBrowserResourceThrottle::FileBrowserResourceThrottle( | |
| 157 int render_process_id, | |
| 158 int render_view_id, | |
| 159 const std::string& mime_type, | |
| 160 const GURL& request_url, | |
| 161 bool profile_is_incognito, | |
| 162 const ExtensionInfoMap* extension_info_map, | |
| 163 scoped_ptr<FileBrowserHandlerEventRouter> event_router) | |
| 164 : render_process_id_(render_process_id), | |
| 165 render_view_id_(render_view_id), | |
| 166 mime_type_(mime_type), | |
| 167 request_url_(request_url), | |
| 168 profile_is_incognito_(profile_is_incognito), | |
| 169 extension_info_map_(extension_info_map), | |
| 170 event_router_(event_router.Pass()) { | |
| 171 } | |
| 172 | |
| 173 FileBrowserResourceThrottle::~FileBrowserResourceThrottle() {} | |
| 174 | |
| 175 void FileBrowserResourceThrottle::WillProcessResponse(bool* defer) { | |
| 176 std::vector<std::string> whitelist = | |
| 177 FileBrowserHandler::GetMIMETypeWhitelist(); | |
| 178 // Go through the white-listed extensions and try to use them to intercept | |
| 179 // the URL request. | |
| 180 for (size_t i = 0; i < whitelist.size(); ++i) { | |
| 181 if (MaybeInterceptWithExtension(whitelist[i])) | |
| 182 return; | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 bool FileBrowserResourceThrottle::MaybeInterceptWithExtension( | |
| 187 const std::string& extension_id) { | |
| 188 const Extension* extension = | |
| 189 extension_info_map_->extensions().GetByID(extension_id); | |
| 190 // The white-listed extension may not be installed, so we have to NULL check | |
| 191 // |extension|. | |
| 192 if (!extension) | |
| 193 return false; | |
| 194 | |
| 195 // If in incognito mode, skip the extensions that are not incognito enabled. | |
| 196 if (profile_is_incognito_ && | |
| 197 !extension_info_map_->IsIncognitoEnabled(extension_id)) { | |
| 198 return false; | |
| 199 } | |
| 200 | |
| 201 if (CanHandleMimeType(extension, mime_type_)) { | |
| 202 event_router_->DispatchMimeTypeHandlerEvent(render_process_id_, | |
| 203 render_view_id_, mime_type_, request_url_, extension->id()); | |
| 204 controller()->CancelAndIgnore(); | |
| 205 return true; | |
| 206 } | |
| 207 return false; | |
| 208 } | |
| OLD | NEW |