 Chromium Code Reviews
 Chromium Code Reviews Issue 308003005:
  app_list: Drive app integration.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 308003005:
  app_list: Drive app integration.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| OLD | NEW | 
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/ui/app_list/drive/drive_app_converter.h" | 5 #include "chrome/browser/apps/drive/drive_app_converter.h" | 
| 6 | 6 | 
| 7 #include <algorithm> | 7 #include <algorithm> | 
| 8 #include <set> | 8 #include <set> | 
| 9 | 9 | 
| 10 #include "base/logging.h" | 10 #include "base/logging.h" | 
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" | 
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" | 
| 13 #include "chrome/browser/extensions/crx_installer.h" | 13 #include "chrome/browser/extensions/crx_installer.h" | 
| 14 #include "chrome/browser/extensions/install_tracker.h" | 14 #include "chrome/browser/extensions/install_tracker.h" | 
| 15 #include "chrome/browser/image_decoder.h" | 15 #include "chrome/browser/image_decoder.h" | 
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" | 
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" | 
| 18 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" | 
| 19 #include "extensions/common/constants.h" | 19 #include "extensions/common/constants.h" | 
| 20 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" | 
| 21 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" | 
| 22 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" | 
| 23 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" | 
| 24 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" | 
| 25 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" | 
| 26 | 26 | 
| 27 using content::BrowserThread; | 27 using content::BrowserThread; | 
| 28 | 28 | 
| 29 namespace extensions { | |
| 30 | |
| 29 // IconFetcher downloads |icon_url| using |converter|'s profile. The icon | 31 // IconFetcher downloads |icon_url| using |converter|'s profile. The icon | 
| 30 // url is passed from a DriveAppInfo and should follow icon url definition | 32 // url is passed from a DriveAppInfo and should follow icon url definition | 
| 31 // in Drive API: | 33 // in Drive API: | 
| 32 // https://developers.google.com/drive/v2/reference/apps#resource | 34 // https://developers.google.com/drive/v2/reference/apps#resource | 
| 33 // Each icon url represents a single image associated with a certain size. | 35 // Each icon url represents a single image associated with a certain size. | 
| 34 class DriveAppConverter::IconFetcher : public net::URLFetcherDelegate, | 36 class DriveAppConverter::IconFetcher : public net::URLFetcherDelegate, | 
| 35 public ImageDecoder::Delegate { | 37 public ImageDecoder::Delegate { | 
| 36 public: | 38 public: | 
| 37 IconFetcher(DriveAppConverter* converter, | 39 IconFetcher(DriveAppConverter* converter, | 
| 38 const GURL& icon_url, | 40 const GURL& icon_url, | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 app_(NULL), | 110 app_(NULL), | 
| 109 finished_callback_(finished_callback) { | 111 finished_callback_(finished_callback) { | 
| 110 DCHECK(profile_); | 112 DCHECK(profile_); | 
| 111 } | 113 } | 
| 112 | 114 | 
| 113 DriveAppConverter::~DriveAppConverter() { | 115 DriveAppConverter::~DriveAppConverter() { | 
| 114 PostInstallCleanUp(); | 116 PostInstallCleanUp(); | 
| 115 } | 117 } | 
| 116 | 118 | 
| 117 void DriveAppConverter::Start() { | 119 void DriveAppConverter::Start() { | 
| 118 if (app_info_.app_name.empty() || | 120 DCHECK(!IsStarted()); | 
| 119 !app_info_.create_url.is_valid()) { | 121 | 
| 122 if (app_info_.app_name.empty() || !app_info_.create_url.is_valid()) { | |
| 120 finished_callback_.Run(this, false); | 123 finished_callback_.Run(this, false); | 
| 121 return; | 124 return; | 
| 122 } | 125 } | 
| 123 | 126 | 
| 124 web_app_.title = base::UTF8ToUTF16(app_info_.app_name); | 127 web_app_.title = base::UTF8ToUTF16(app_info_.app_name); | 
| 125 web_app_.app_url = app_info_.create_url; | 128 web_app_.app_url = app_info_.create_url; | 
| 126 | 129 | 
| 127 const std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes, | 130 const std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes, | 
| 128 extension_misc::kExtensionIconSizes + | 131 extension_misc::kExtensionIconSizes + | 
| 129 extension_misc::kNumExtensionIconSizes); | 132 extension_misc::kNumExtensionIconSizes); | 
| 130 std::set<int> pending_sizes; | 133 std::set<int> pending_sizes; | 
| 131 for (size_t i = 0; i < app_info_.app_icons.size(); ++i) { | 134 for (size_t i = 0; i < app_info_.app_icons.size(); ++i) { | 
| 132 const int icon_size = app_info_.app_icons[i].first; | 135 const int icon_size = app_info_.app_icons[i].first; | 
| 133 if (allowed_sizes.find(icon_size) == allowed_sizes.end() || | 136 if (allowed_sizes.find(icon_size) == allowed_sizes.end() || | 
| 134 pending_sizes.find(icon_size) != pending_sizes.end()) { | 137 pending_sizes.find(icon_size) != pending_sizes.end()) { | 
| 135 continue; | 138 continue; | 
| 136 } | 139 } | 
| 137 | 140 | 
| 138 pending_sizes.insert(icon_size); | 141 pending_sizes.insert(icon_size); | 
| 139 const GURL& icon_url = app_info_.app_icons[i].second; | 142 const GURL& icon_url = app_info_.app_icons[i].second; | 
| 140 IconFetcher* fetcher = new IconFetcher(this, icon_url, icon_size); | 143 IconFetcher* fetcher = new IconFetcher(this, icon_url, icon_size); | 
| 141 fetchers_.push_back(fetcher); // Pass ownership to |fetchers|. | 144 fetchers_.push_back(fetcher); // Pass ownership to |fetchers|. | 
| 142 fetcher->Start(); | 145 fetcher->Start(); | 
| 143 } | 146 } | 
| 144 | 147 | 
| 145 if (fetchers_.empty()) | 148 if (fetchers_.empty()) | 
| 146 StartInstall(); | 149 StartInstall(); | 
| 147 } | 150 } | 
| 148 | 151 | 
| 152 bool DriveAppConverter::IsStarted() const { | |
| 153 return !fetchers_.empty() || crx_installer_; | |
| 154 } | |
| 155 | |
| 149 void DriveAppConverter::OnIconFetchComplete(const IconFetcher* fetcher) { | 156 void DriveAppConverter::OnIconFetchComplete(const IconFetcher* fetcher) { | 
| 150 const SkBitmap& icon = fetcher->icon(); | 157 const SkBitmap& icon = fetcher->icon(); | 
| 151 if (!icon.empty() && icon.width() != 0) { | 158 if (!icon.empty() && icon.width() != 0) { | 
| 152 WebApplicationInfo::IconInfo icon_info; | 159 WebApplicationInfo::IconInfo icon_info; | 
| 153 icon_info.url = fetcher->icon_url(); | 160 icon_info.url = fetcher->icon_url(); | 
| 154 icon_info.data = icon; | 161 icon_info.data = icon; | 
| 155 icon_info.width = icon.width(); | 162 icon_info.width = icon.width(); | 
| 156 icon_info.height = icon.height(); | 163 icon_info.height = icon.height(); | 
| 157 web_app_.icons.push_back(icon_info); | 164 web_app_.icons.push_back(icon_info); | 
| 158 } | 165 } | 
| 159 | 166 | 
| 160 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher)); | 167 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher)); | 
| 161 | 168 | 
| 162 if (fetchers_.empty()) | 169 if (fetchers_.empty()) | 
| 163 StartInstall(); | 170 StartInstall(); | 
| 164 } | 171 } | 
| 165 | 172 | 
| 166 void DriveAppConverter::StartInstall() { | 173 void DriveAppConverter::StartInstall() { | 
| 167 DCHECK(!crx_installer_); | 174 DCHECK(!crx_installer_); | 
| 168 crx_installer_ = extensions::CrxInstaller::CreateSilent( | 175 crx_installer_ = CrxInstaller::CreateSilent( | 
| 169 extensions::ExtensionSystem::Get(profile_)->extension_service()); | 176 ExtensionSystem::Get(profile_)->extension_service()); | 
| 170 extensions::InstallTracker::Get(profile_)->AddObserver(this); | 177 | 
| 178 // Sets WAS_INSTALLED_BY_DEFAULT to make the converted url app not syncable. | |
| 179 // Drive apps goes with the user's account and url apps will be created when | |
| 
benwells
2014/06/05 01:24:39
Nit: goes -> go
 
xiyuan
2014/06/05 17:48:32
Done.
 | |
| 180 // needed. Syncing those apps could hit an edge case where the synced url | |
| 181 // apps become orphans when the user has corresponding chrome apps. | |
| 182 crx_installer_->set_creation_flags(Extension::FROM_BOOKMARK | | |
| 183 Extension::WAS_INSTALLED_BY_DEFAULT); | |
| 184 | |
| 185 InstallTracker::Get(profile_)->AddObserver(this); | |
| 171 crx_installer_->InstallWebApp(web_app_); | 186 crx_installer_->InstallWebApp(web_app_); | 
| 172 } | 187 } | 
| 173 | 188 | 
| 174 void DriveAppConverter::PostInstallCleanUp() { | 189 void DriveAppConverter::PostInstallCleanUp() { | 
| 175 if (!crx_installer_) | 190 if (!crx_installer_) | 
| 176 return; | 191 return; | 
| 177 | 192 | 
| 178 extensions::InstallTracker::Get(profile_)->RemoveObserver(this); | 193 InstallTracker::Get(profile_)->RemoveObserver(this); | 
| 179 crx_installer_ = NULL; | 194 crx_installer_ = NULL; | 
| 180 } | 195 } | 
| 181 | 196 | 
| 182 void DriveAppConverter::OnFinishCrxInstall(const std::string& extension_id, | 197 void DriveAppConverter::OnFinishCrxInstall(const std::string& extension_id, | 
| 183 bool success) { | 198 bool success) { | 
| 184 if (!crx_installer_->extension() || | 199 if (!crx_installer_->extension() || | 
| 185 crx_installer_->extension()->id() != extension_id) { | 200 crx_installer_->extension()->id() != extension_id) { | 
| 186 return; | 201 return; | 
| 187 } | 202 } | 
| 188 | 203 | 
| 189 app_ = crx_installer_->extension(); | 204 app_ = crx_installer_->extension(); | 
| 205 PostInstallCleanUp(); | |
| 206 | |
| 190 finished_callback_.Run(this, success); | 207 finished_callback_.Run(this, success); | 
| 208 // |finished_callback_| could delete this. | |
| 209 } | |
| 191 | 210 | 
| 192 PostInstallCleanUp(); | 211 } // namespace extensions | 
| 193 } | |
| OLD | NEW |