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#ifndef CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_LOADER_HANDLER_H_
6#define CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_LOADER_HANDLER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/files/file_path.h"
13#include "base/macros.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/memory/weak_ptr.h"
16#include "base/scoped_observer.h"
17#include "base/values.h"
18#include "chrome/browser/extensions/extension_error_reporter.h"
19#include "content/public/browser/navigation_controller.h"
20#include "content/public/browser/web_contents_observer.h"
21#include "content/public/browser/web_ui_message_handler.h"
22
23namespace content {
24class WebUIDataSource;
25}
26
27class Profile;
28
29namespace extensions {
30
31class Extension;
32
33// The handler page for the Extension Commands UI overlay.
34class ExtensionLoaderHandler : public content::WebUIMessageHandler,
35                               public ExtensionErrorReporter::Observer,
36                               public content::WebContentsObserver {
37 public:
38  explicit ExtensionLoaderHandler(Profile* profile);
39  virtual ~ExtensionLoaderHandler();
40
41  // Fetches the localized values for the page and deposits them into |source|.
42  void GetLocalizedValues(content::WebUIDataSource* source);
43
44  // WebUIMessageHandler implementation.
45  virtual void RegisterMessages() OVERRIDE;
46
47 private:
48  class FileHelper;
49
50  // Handle the 'extensionLoaderLoadUnpacked' message.
51  void HandleLoadUnpacked(const base::ListValue* args);
52
53  // Handle the 'extensionLoaderRetry' message.
54  void HandleRetry(const base::ListValue* args);
55
56  // Handle the 'extensionLoaderIgnoreFailure' message.
57  void HandleIgnoreFailure(const base::ListValue* args);
58
59  // Handle the 'extensionLoaderDisplayFailures' message.
60  void HandleDisplayFailures(const base::ListValue* args);
61
62  // Try to load an unpacked extension from the given |file_path|.
63  void LoadUnpackedExtensionImpl(const base::FilePath& file_path);
64
65  // ExtensionErrorReporter::Observer:
66  virtual void OnLoadFailure(content::BrowserContext* browser_context,
67                             const base::FilePath& file_path,
68                             const std::string& error) OVERRIDE;
69
70  // content::WebContentsObserver:
71  virtual void DidStartNavigationToPendingEntry(
72      const GURL& url,
73      content::NavigationController::ReloadType reload_type) OVERRIDE;
74
75  // Add a failure to |failures_|. If it was a manifest error, |manifest| will
76  // hold the manifest contents, and |line_number| will point to the line at
77  // which the error was found.
78  void AddFailure(const base::FilePath& file_path,
79                  const std::string& error,
80                  size_t line_number,
81                  const std::string& manifest);
82
83  // Notify the frontend of all failures.
84  void NotifyFrontendOfFailure();
85
86  // The profile with which this Handler is associated.
87  Profile* profile_;
88
89  // A helper to manage file picking.
90  scoped_ptr<FileHelper> file_helper_;
91
92  // Holds information about all unpacked extension install failures that
93  // were reported while the extensions page was loading.
94  base::ListValue failures_;
95
96  // Holds failed paths for load retries.
97  std::vector<base::FilePath> failed_paths_;
98
99  ScopedObserver<ExtensionErrorReporter, ExtensionErrorReporter::Observer>
100      extension_error_reporter_observer_;
101
102  // Set when the chrome://extensions page is fully loaded and the frontend is
103  // ready to receive failure notifications. We need this because the page
104  // fails to display failures if they are sent before the Javascript is loaded.
105  bool ui_ready_;
106
107  // Weak pointer factory for posting background tasks.
108  base::WeakPtrFactory<ExtensionLoaderHandler> weak_ptr_factory_;
109
110  DISALLOW_COPY_AND_ASSIGN(ExtensionLoaderHandler);
111};
112
113}  // namespace extensions
114
115#endif  // CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_LOADER_HANDLER_H_
116