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

Unified Diff: chrome/browser/apps/drive/drive_app_provider.cc

Issue 308003005: app_list: Drive app integration. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move to c/b/apps, add OWNERS Created 6 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/apps/drive/drive_app_provider.cc
diff --git a/chrome/browser/apps/drive/drive_app_provider.cc b/chrome/browser/apps/drive/drive_app_provider.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f6cdaec479dc722eb69bfa8bf821ccbdff484332
--- /dev/null
+++ b/chrome/browser/apps/drive/drive_app_provider.cc
@@ -0,0 +1,276 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/apps/drive/drive_app_provider.h"
+
+#include <vector>
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/logging.h"
+#include "base/message_loop/message_loop.h"
+#include "base/stl_util.h"
+#include "chrome/browser/apps/drive/drive_app_converter.h"
+#include "chrome/browser/apps/drive/drive_app_mapping.h"
+#include "chrome/browser/apps/drive/drive_service_bridge.h"
+#include "chrome/browser/drive/drive_app_registry.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/extensions/install_tracker.h"
+#include "chrome/browser/extensions/install_tracker_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
+#include "extensions/browser/extension_registry.h"
+#include "extensions/browser/extension_system.h"
+#include "extensions/common/extension.h"
+#include "ui/app_list/app_list_item.h"
+#include "ui/app_list/app_list_model.h"
+
+namespace {
+
+void IgnoreUninstallResult(google_apis::GDataErrorCode) {
+}
+
+} // namespace
+
+namespace extensions {
+
+DriveAppProvider::DriveAppProvider(Profile* profile,
+ app_list::AppListModel* model)
+ : profile_(profile),
+ model_(model),
+ service_bridge_(DriveServiceBridge::Create(profile).Pass()),
+ mapping_(new DriveAppMapping(profile->GetPrefs())),
+ weak_ptr_factory_(this) {
+ service_bridge_->GetAppRegistry()->AddObserver(this);
+ InstallTracker::Get(profile_)->AddObserver(this);
+}
+
+DriveAppProvider::~DriveAppProvider() {
+ InstallTracker::Get(profile_)->RemoveObserver(this);
+ service_bridge_->GetAppRegistry()->RemoveObserver(this);
+}
+
+// static
+void DriveAppProvider::AppendDependsOnFactories(
+ std::set<BrowserContextKeyedServiceFactory*>* factories) {
+ factories->insert(InstallTrackerFactory::GetInstance());
+ DriveServiceBridge::AppendDependsOnFactories(factories);
+}
+
+void DriveAppProvider::SetDriveServiceBridgeForTest(
+ scoped_ptr<DriveServiceBridge> test_bridge) {
+ service_bridge_->GetAppRegistry()->RemoveObserver(this);
+ service_bridge_ = test_bridge.Pass();
+ service_bridge_->GetAppRegistry()->AddObserver(this);
+}
+
+void DriveAppProvider::MigrateModelSettings(
+ const std::string& drive_app_id,
+ const std::string& old_chrome_app_id,
+ const std::string& new_chrome_app_id) {
+ if (!model_ || old_chrome_app_id == new_chrome_app_id)
+ return;
+
+ app_list::AppListItem* old_app_item = model_->FindItem(old_chrome_app_id);
+ if (!old_app_item) {
+ NOTREACHED();
+ return;
+ }
+
+ app_list::AppListItem* new_app_item = model_->FindItem(new_chrome_app_id);
+ if (!new_app_item) {
+ NOTREACHED();
+ return;
+ }
+
+ model_->MoveItemToFolderAt(
+ new_app_item, old_app_item->folder_id(), old_app_item->position());
+}
+
+void DriveAppProvider::UpdateMappingAndExtensionSystem(
+ const std::string& drive_app_id,
+ const Extension* new_app) {
+ const std::string& new_chrome_app_id = new_app->id();
+
+ const std::string existing_chrome_app_id =
+ mapping_->GetChromeApp(drive_app_id);
+ if (existing_chrome_app_id.empty()) {
+ mapping_->Add(drive_app_id, new_chrome_app_id);
+ return;
+ }
+
+ if (existing_chrome_app_id == new_chrome_app_id)
+ return;
+
+ const Extension* existing_app =
+ ExtensionRegistry::Get(profile_)->GetExtensionById(
+ existing_chrome_app_id, ExtensionRegistry::EVERYTHING);
+ if (!existing_app) {
+ mapping_->Add(drive_app_id, new_chrome_app_id);
+ return;
+ }
+
+ if (existing_app->from_bookmark()) {
+ MigrateModelSettings(
+ drive_app_id, existing_chrome_app_id, new_chrome_app_id);
+ mapping_->Add(drive_app_id, new_chrome_app_id);
+ ExtensionSystem::Get(profile_)->extension_service()->UninstallExtension(
+ existing_chrome_app_id, false, NULL);
+ return;
+ }
+
+ if (new_app->from_bookmark()) {
+ // New app is an URL app. Existing app is not URL app. Keep existing.
+ ExtensionSystem::Get(profile_)->extension_service()->UninstallExtension(
+ new_chrome_app_id, false, NULL);
+ } else {
+ // Both existing and new chrome apps are not URL apps, keep existing app
+ // but update mapping to use the new one.
+ mapping_->Add(drive_app_id, new_chrome_app_id);
+ }
+}
+
+void DriveAppProvider::ProcessDeferredOnExtensionInstalled(
+ const std::string drive_app_id,
+ const std::string chrome_app_id) {
+ const Extension* app = ExtensionRegistry::Get(profile_)->GetExtensionById(
+ chrome_app_id, ExtensionRegistry::EVERYTHING);
+ if (!app)
+ return;
+
+ UpdateMappingAndExtensionSystem(drive_app_id, app);
+}
+
+void DriveAppProvider::SchedulePendingConverters() {
+ if (pending_converters_.empty())
+ return;
+
+ if (!pending_converters_.front()->IsStarted())
+ pending_converters_.front()->Start();
+}
+
+void DriveAppProvider::OnLocalAppConverted(const DriveAppConverter* converter,
+ bool success) {
+ DCHECK_EQ(pending_converters_.front(), converter);
+
+ if (success) {
+ UpdateMappingAndExtensionSystem(converter->app_info().app_id,
+ converter->app());
+ } else {
+ LOG(WARNING) << "Failed to convert drive app to web app, "
+ << "drive app id= " << converter->app_info().app_id
+ << ", name=" << converter->app_info().app_name;
+ }
+
+ pending_converters_.erase(pending_converters_.begin());
+ SchedulePendingConverters();
+}
+
+bool DriveAppProvider::IsDriveAppUpToDate(
+ const drive::DriveAppInfo& drive_app) const {
+ const std::string& url_app_id = mapping_->GetChromeApp(drive_app.app_id);
+ if (url_app_id.empty())
+ return false;
+
+ const Extension* url_app = ExtensionRegistry::Get(profile_)->GetExtensionById(
+ url_app_id, ExtensionRegistry::EVERYTHING);
+ if (!url_app)
+ return false;
+
+ return drive_app.app_name == url_app->name() &&
+ drive_app.create_url == AppLaunchInfo::GetLaunchWebURL(url_app);
+}
+
+void DriveAppProvider::AddOrUpdateDriveApp(
+ const drive::DriveAppInfo& drive_app) {
+ const Extension* chrome_app =
+ ExtensionRegistry::Get(profile_)->GetExtensionById(
+ drive_app.product_id, ExtensionRegistry::EVERYTHING);
+ if (chrome_app) {
+ UpdateMappingAndExtensionSystem(drive_app.app_id, chrome_app);
+ return;
+ }
+
+ if (IsDriveAppUpToDate(drive_app))
+ return;
+
+ ScopedVector<DriveAppConverter>::iterator it = pending_converters_.begin();
+ while (it != pending_converters_.end()) {
+ if (!(*it)->IsStarted() && (*it)->app_info().app_id == drive_app.app_id) {
+ it = pending_converters_.erase(it);
+ } else {
+ ++it;
+ }
+ }
+
+ pending_converters_.push_back(
+ new DriveAppConverter(profile_,
+ drive_app,
+ base::Bind(&DriveAppProvider::OnLocalAppConverted,
+ base::Unretained(this))));
+}
+
+void DriveAppProvider::RemoveDriveApp(const std::string& drive_app_id) {
+ const std::string chrome_app_id = mapping_->GetChromeApp(drive_app_id);
+ mapping_->Remove(drive_app_id);
+
+ if (chrome_app_id.empty())
+ return;
+
+ const Extension* existing_app =
+ ExtensionRegistry::Get(profile_)
+ ->GetExtensionById(chrome_app_id, ExtensionRegistry::EVERYTHING);
+ if (!existing_app || !existing_app->from_bookmark())
+ return;
+
+ ExtensionSystem::Get(profile_)->extension_service()->UninstallExtension(
+ chrome_app_id, false, NULL);
+}
+
+void DriveAppProvider::OnDriveAppRegistryUpdated() {
+ service_bridge_->GetAppRegistry()->GetAppList(&drive_apps_);
+
+ IdSet current_ids;
+ for (size_t i = 0; i < drive_apps_.size(); ++i)
+ current_ids.insert(drive_apps_[i].app_id);
+
+ const IdSet existing_ids = mapping_->GetDriveAppIds();
+ const IdSet ids_to_remove =
+ base::STLSetDifference<IdSet>(existing_ids, current_ids);
+ for (IdSet::const_iterator it = ids_to_remove.begin();
+ it != ids_to_remove.end();
+ ++it) {
+ RemoveDriveApp(*it);
+ }
+
+ for (size_t i = 0; i < drive_apps_.size(); ++i) {
+ AddOrUpdateDriveApp(drive_apps_[i]);
+ }
+ SchedulePendingConverters();
+}
+
+void DriveAppProvider::OnExtensionInstalled(const Extension* extension) {
+ for (size_t i = 0; i < drive_apps_.size(); ++i) {
+ if (drive_apps_[i].product_id == extension->id()) {
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&DriveAppProvider::ProcessDeferredOnExtensionInstalled,
+ weak_ptr_factory_.GetWeakPtr(),
+ drive_apps_[i].app_id,
+ extension->id()));
+ return;
+ }
+ }
+}
+
+void DriveAppProvider::OnExtensionUninstalled(const Extension* extension) {
+ std::string drive_app_id = mapping_->GetDriveApp(extension->id());
+ if (drive_app_id.empty())
+ return;
+
+ service_bridge_->GetAppRegistry()->UninstallApp(
+ drive_app_id, base::Bind(&IgnoreUninstallResult));
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698