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

Side by Side Diff: chrome/browser/ui/app_list/drive/drive_app_converter.cc

Issue 292523004: app_list: DriveAppConverter to install local URL app from DriveAppInfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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
(Empty)
1 // Copyright 2014 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/ui/app_list/drive/drive_app_converter.h"
6
7 #include <algorithm>
8 #include <set>
9
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/extensions/crx_installer.h"
14 #include "chrome/browser/extensions/install_tracker.h"
15 #include "chrome/browser/image_decoder.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/common/constants.h"
20 #include "net/base/load_flags.h"
21 #include "net/url_request/url_fetcher.h"
22 #include "net/url_request/url_fetcher_delegate.h"
23 #include "net/url_request/url_request_status.h"
24 #include "third_party/skia/include/core/SkBitmap.h"
25
26 using content::BrowserThread;
27
28 class DriveAppConverter::IconFetcher : public net::URLFetcherDelegate,
calamity 2014/05/19 04:01:08 Class comment. It would good to mention somewhere
xiyuan 2014/05/19 06:08:14 Done.
29 public ImageDecoder::Delegate {
30 public:
31 IconFetcher(DriveAppConverter* converter,
32 const GURL& icon_url,
33 int expected_size)
34 : converter_(converter),
35 icon_url_(icon_url),
36 expected_size_(expected_size) {}
37 virtual ~IconFetcher() {
38 if (image_decoder_.get())
39 image_decoder_->set_delegate(NULL);
40 }
41
42 void Start() {
43 fetcher_.reset(
44 net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this));
45 fetcher_->SetRequestContext(converter_->profile_->GetRequestContext());
46 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
47 fetcher_->Start();
48 }
49
50 const GURL& icon_url() const { return icon_url_; }
51 const SkBitmap& icon() const { return icon_; }
52
53 private:
54 // net::URLFetcherDelegate overrides:
55 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE {
56 CHECK_EQ(fetcher_.get(), source);
57 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass());
58
59 if (!fetcher->GetStatus().is_success() ||
60 fetcher->GetResponseCode() != 200) {
calamity 2014/05/19 04:01:08 s/200/net::HTTP_OK/
xiyuan 2014/05/19 06:08:14 Done.
61 converter_->OnIconFetchComplete(this);
62 return;
63 }
64
65 std::string unsafe_icon_data;
66 fetcher->GetResponseAsString(&unsafe_icon_data);
67
68 image_decoder_ =
69 new ImageDecoder(this, unsafe_icon_data, ImageDecoder::DEFAULT_CODEC);
70 image_decoder_->Start(
71 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI));
72 }
73
74 // ImageDecoder::Delegate overrides:
75 virtual void OnImageDecoded(const ImageDecoder* decoder,
76 const SkBitmap& decoded_image) OVERRIDE {
77 if (decoded_image.width() == expected_size_)
78 icon_ = decoded_image;
79 converter_->OnIconFetchComplete(this);
80 }
81
82 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE {
83 converter_->OnIconFetchComplete(this);
84 }
85
86 DriveAppConverter* converter_;
87 const GURL icon_url_;
88 const int expected_size_;
89
90 scoped_ptr<net::URLFetcher> fetcher_;
91 scoped_refptr<ImageDecoder> image_decoder_;
92 SkBitmap icon_;
93
94 DISALLOW_COPY_AND_ASSIGN(IconFetcher);
95 };
96
97 DriveAppConverter::DriveAppConverter(Profile* profile,
98 const drive::DriveAppInfo& app_info,
99 const FinishedCallback& finished_callback)
100 : profile_(profile),
101 app_info_(app_info),
102 app_(NULL),
103 finished_callback_(finished_callback) {
104 DCHECK(profile_);
105 }
106
107 DriveAppConverter::~DriveAppConverter() {
108 PostInstallCleanUp();
109 }
110
111 void DriveAppConverter::Start() {
112 if (app_info_.app_name.empty() ||
113 !app_info_.create_url.is_valid()) {
114 finished_callback_.Run(this, false);
115 return;
116 }
117
118 web_app_.title = base::UTF8ToUTF16(app_info_.app_name);
119 web_app_.app_url = app_info_.create_url;
120
121 const std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes,
122 extension_misc::kExtensionIconSizes +
123 extension_misc::kNumExtensionIconSizes);
124 for (size_t i = 0; i < app_info_.app_icons.size(); ++i) {
125 const int icon_size = app_info_.app_icons[i].first;
126 if (allowed_sizes.find(icon_size) == allowed_sizes.end())
127 continue;
128
129 const GURL& icon_url = app_info_.app_icons[i].second;
130 IconFetcher* fetcher = new IconFetcher(this, icon_url, icon_size);
131 fetchers_.push_back(fetcher); // Pass ownership to |fetchers|.
132 fetcher->Start();
133 }
134
135 if (fetchers_.empty())
136 StartInstall();
137 }
138
139 void DriveAppConverter::OnIconFetchComplete(IconFetcher* fetcher) {
140 const SkBitmap& icon = fetcher->icon();
141 if (!icon.empty() && icon.width() != 0) {
142 WebApplicationInfo::IconInfo icon_info;
143 icon_info.url = fetcher->icon_url();
144 icon_info.data = icon;
145 icon_info.width = icon.width();
146 icon_info.height = icon.height();
147 web_app_.icons.push_back(icon_info);
148 }
149
150 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher));
151
152 if (fetchers_.empty())
153 StartInstall();
154 }
155
156 void DriveAppConverter::StartInstall() {
157 DCHECK(!crx_installer_);
158 crx_installer_ = extensions::CrxInstaller::CreateSilent(
159 extensions::ExtensionSystem::Get(profile_)->extension_service());
160 extensions::InstallTracker::Get(profile_)->AddObserver(this);
161 crx_installer_->InstallWebApp(web_app_);
162 }
163
164 void DriveAppConverter::PostInstallCleanUp() {
165 if (!crx_installer_)
166 return;
167
168 extensions::InstallTracker::Get(profile_)->RemoveObserver(this);
169 crx_installer_ = NULL;
170 }
171
172 void DriveAppConverter::OnFinishCrxInstall(const std::string& extension_id,
173 bool success) {
174 if (!crx_installer_->extension() ||
175 crx_installer_->extension()->id() != extension_id) {
176 return;
177 }
178
179 app_ = crx_installer_->extension();
180 finished_callback_.Run(this, success);
181
182 PostInstallCleanUp();
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698