File: shortcut_helper.h

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (163 lines) | stat: -rw-r--r-- 6,088 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2013 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.

#ifndef CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_
#define CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_

#include "base/android/jni_android.h"
#include "base/android/jni_weak_ref.h"
#include "base/basictypes.h"
#include "base/strings/string16.h"
#include "base/task/cancelable_task_tracker.h"
#include "chrome/common/web_application_info.h"
#include "components/favicon_base/favicon_types.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/manifest.h"
#include "third_party/WebKit/public/platform/WebScreenOrientationLockType.h"

namespace content {
class WebContents;
}  // namespace content

namespace IPC {
class Message;
}

class GURL;

// ShortcutHelper is the C++ counterpart of org.chromium.chrome.browser's
// ShortcutHelper in Java. The object is owned by the Java object. It is created
// from there via a JNI (Initialize) call and can be destroyed from Java too
// using TearDown. When the Java implementations calls AddShortcut, it gives up
// the ownership of the object. The instance will then destroy itself when done.
class ShortcutHelper : public content::WebContentsObserver {
 public:
  ShortcutHelper(JNIEnv* env,
                 jobject obj,
                 content::WebContents* web_contents);

  // Initialize the helper by requesting the information about the page to the
  // renderer process. The initialization is asynchronous and
  // OnDidRetrieveWebappInformation is expected to be called when finished.
  void Initialize();

  // Called by the Java counter part to let the object knows that it can destroy
  // itself.
  void TearDown(JNIEnv* env, jobject obj);

  // IPC message received when the initialization is finished.
  void OnDidGetWebApplicationInfo(const WebApplicationInfo& web_app_info);

  // Callback run when the Manifest is ready to be used.
  void OnDidGetManifest(const content::Manifest& manifest);

  // Adds a shortcut to the current URL to the Android home screen.
  void AddShortcut(JNIEnv* env,
                   jobject obj,
                   jstring title,
                   jint launcher_large_icon_size);

  // Callback run when the requested Manifest icon is ready to be used.
  void OnDidDownloadIcon(int id,
                         int http_status_code,
                         const GURL& url,
                         const std::vector<SkBitmap>& bitmaps,
                         const std::vector<gfx::Size>& sizes);

  // Called after AddShortcut() and OnDidDownloadIcon() are run if
  // OnDidDownloadIcon has a valid icon.
  void AddShortcutUsingManifestIcon();

  // Use FaviconService to get the best available favicon and create the
  // shortcut using it. This is used when no Manifest icons are available or
  // appropriate.
  void AddShortcutUsingFavicon();

  // Callback run when a favicon is received from GetFavicon() call.
  void OnDidGetFavicon(
      const favicon_base::FaviconRawBitmapResult& bitmap_result);

  // WebContentsObserver
  virtual bool OnMessageReceived(const IPC::Message& message) override;
  virtual void WebContentsDestroyed() override;

  // Adds a shortcut to the launcher using a FaviconRawBitmapResult.
  // Must be called from a WorkerPool task.
  static void AddShortcutInBackgroundWithRawBitmap(
      const GURL& url,
      const base::string16& title,
      content::Manifest::DisplayMode display,
      const favicon_base::FaviconRawBitmapResult& bitmap_result,
      blink::WebScreenOrientationLockType orientation);

  // Adds a shortcut to the launcher using a SkBitmap.
  // Must be called from a WorkerPool task.
  static void AddShortcutInBackgroundWithSkBitmap(
      const GURL& url,
      const base::string16& title,
      content::Manifest::DisplayMode display,
      const SkBitmap& icon_bitmap,
      blink::WebScreenOrientationLockType orientation);

  // Registers JNI hooks.
  static bool RegisterShortcutHelper(JNIEnv* env);

 private:
  enum ManifestIconStatus {
    MANIFEST_ICON_STATUS_NONE,
    MANIFEST_ICON_STATUS_FETCHING,
    MANIFEST_ICON_STATUS_DONE
  };

  virtual ~ShortcutHelper();

  void Destroy();

  // Runs the algorithm to find the best matching icon in the icons listed in
  // the Manifest.
  // Returns the icon url if a suitable icon is found. An empty URL otherwise.
  GURL FindBestMatchingIcon(
      const std::vector<content::Manifest::Icon>& icons) const;

  // Runs an algorithm only based on icon declared sizes. It will try to find
  // size that is the closest to preferred_icon_size_in_px_ but bigger than
  // preferred_icon_size_in_px_ if possible.
  // Returns the icon url if a suitable icon is found. An empty URL otherwise.
  GURL FindBestMatchingIcon(const std::vector<content::Manifest::Icon>& icons,
                            float density) const;

  // Returns an array containing the items in |icons| without the unsupported
  // image MIME types.
  static std::vector<content::Manifest::Icon> FilterIconsByType(
      const std::vector<content::Manifest::Icon>& icons);

  // Returns whether the preferred_icon_size_in_px_ is in the given |sizes|.
  bool IconSizesContainsPreferredSize(
      const std::vector<gfx::Size>& sizes) const;

  // Returns whether the 'any' (ie. gfx::Size(0,0)) is in the given |sizes|.
  bool IconSizesContainsAny(const std::vector<gfx::Size>& sizes) const;

  JavaObjectWeakGlobalRef java_ref_;

  GURL url_;
  base::string16 title_;
  content::Manifest::DisplayMode display_;
  SkBitmap manifest_icon_;
  base::CancelableTaskTracker cancelable_task_tracker_;
  blink::WebScreenOrientationLockType orientation_;

  bool add_shortcut_requested_;

  ManifestIconStatus manifest_icon_status_;
  const int preferred_icon_size_in_px_;
  static const int kPreferredIconSizeInDp;

  base::WeakPtrFactory<ShortcutHelper> weak_ptr_factory_;

  friend class ShortcutHelperTest;
  DISALLOW_COPY_AND_ASSIGN(ShortcutHelper);
};

#endif  // CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_