Home | History | Annotate | Download | only in webui
      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 #ifndef CHROME_BROWSER_UI_WEBUI_FILEICON_SOURCE_H_
      6 #define CHROME_BROWSER_UI_WEBUI_FILEICON_SOURCE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/files/file_path.h"
     11 #include "chrome/browser/icon_manager.h"
     12 #include "chrome/common/cancelable_task_tracker.h"
     13 #include "content/public/browser/url_data_source.h"
     14 #include "ui/base/layout.h"
     15 
     16 namespace gfx {
     17 class Image;
     18 }
     19 
     20 // FileIconSource is the gateway between network-level chrome:
     21 // requests for favicons and the history backend that serves these.
     22 class FileIconSource : public content::URLDataSource {
     23  public:
     24   explicit FileIconSource();
     25 
     26   // content::URLDataSource implementation.
     27   virtual std::string GetSource() const OVERRIDE;
     28   virtual void StartDataRequest(
     29       const std::string& path,
     30       int render_process_id,
     31       int render_view_id,
     32       const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
     33   virtual std::string GetMimeType(const std::string&) const OVERRIDE;
     34 
     35  protected:
     36   virtual ~FileIconSource();
     37 
     38   // Once the |path| and |icon_size| has been determined from the request, this
     39   // function is called to perform the actual fetch. Declared as virtual for
     40   // testing.
     41   virtual void FetchFileIcon(
     42       const base::FilePath& path,
     43       ui::ScaleFactor scale_factor,
     44       IconLoader::IconSize icon_size,
     45       const content::URLDataSource::GotDataCallback& callback);
     46 
     47  private:
     48   // Contains the necessary information for completing an icon fetch request.
     49   struct IconRequestDetails {
     50     IconRequestDetails();
     51     ~IconRequestDetails();
     52 
     53     // The callback to run with the response.
     54     content::URLDataSource::GotDataCallback callback;
     55 
     56     // The requested scale factor to respond with.
     57     ui::ScaleFactor scale_factor;
     58   };
     59 
     60   // Called when favicon data is available from the history backend.
     61   void OnFileIconDataAvailable(const IconRequestDetails& details,
     62                                gfx::Image* icon);
     63 
     64   // Tracks tasks requesting file icons.
     65   CancelableTaskTracker cancelable_task_tracker_;
     66 
     67   DISALLOW_COPY_AND_ASSIGN(FileIconSource);
     68 };
     69 #endif  // CHROME_BROWSER_UI_WEBUI_FILEICON_SOURCE_H_
     70